forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
287 lines (253 loc) · 9.64 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore
import (
"io"
"net/http"
"net/url"
"github.com/juju/errors"
"github.com/juju/loggo"
"gopkg.in/juju/charm.v6-unstable"
charmresource "gopkg.in/juju/charm.v6-unstable/resource"
"gopkg.in/juju/charmrepo.v2-unstable/csclient"
csparams "gopkg.in/juju/charmrepo.v2-unstable/csclient/params"
"gopkg.in/macaroon-bakery.v1/httpbakery"
"gopkg.in/macaroon.v1"
)
var logger = loggo.GetLogger("juju.charmstore")
// TODO(natefinch): Ideally, this whole package would live in the
// charmstore-client repo, so as to keep it near the API it wraps (and make it
// more available to tools outside juju-core).
// MacaroonCache represents a value that can store and retrieve macaroons for
// charms. It is used when we are requesting data from the charmstore for
// private charms.
type MacaroonCache interface {
Set(*charm.URL, macaroon.Slice) error
Get(*charm.URL) (macaroon.Slice, error)
}
// NewCachingClient returns a Juju charm store client that stores and retrieves
// macaroons for calls in the given cache. If not nil, the client will use server
// as the charmstore url, otherwise it will default to the standard juju
// charmstore url.
func NewCachingClient(cache MacaroonCache, server *url.URL) (Client, error) {
return newCachingClient(cache, server, makeWrapper)
}
func newCachingClient(
cache MacaroonCache,
server *url.URL,
makeWrapper func(*httpbakery.Client, *url.URL) csWrapper,
) (Client, error) {
bakeryClient := &httpbakery.Client{
Client: httpbakery.NewHTTPClient(),
}
client := makeWrapper(bakeryClient, server)
server, err := url.Parse(client.ServerURL())
if err != nil {
return Client{}, errors.Trace(err)
}
jar, err := newMacaroonJar(cache, server)
if err != nil {
return Client{}, errors.Trace(err)
}
bakeryClient.Jar = jar
return Client{client, jar}, nil
}
// TODO(natefinch): we really shouldn't let something like a bakeryclient
// leak out of our abstraction like this. Instead, pass more salient details.
// NewCustomClient returns a juju charmstore client that relies on the passed-in
// httpbakery.Client to store and retrieve macaroons. If not nil, the client
// will use server as the charmstore url, otherwise it will default to the
// standard juju charmstore url.
func NewCustomClient(bakeryClient *httpbakery.Client, server *url.URL) (Client, error) {
return newCustomClient(bakeryClient, server, makeWrapper)
}
func newCustomClient(
bakeryClient *httpbakery.Client,
server *url.URL,
makeWrapper func(*httpbakery.Client, *url.URL) csWrapper,
) (Client, error) {
client := makeWrapper(bakeryClient, server)
return Client{csWrapper: client}, nil
}
func makeWrapper(bakeryClient *httpbakery.Client, server *url.URL) csWrapper {
p := csclient.Params{
BakeryClient: bakeryClient,
}
if server != nil {
p.URL = server.String()
}
return csclientImpl{csclient.New(p)}
}
// Client wraps charmrepo/csclient (the charm store's API client
// library) in a higher level API.
type Client struct {
csWrapper
jar *macaroonJar
}
// CharmRevision holds the data returned from the charmstore about the latest
// revision of a charm. Notet hat this may be different per channel.
type CharmRevision struct {
// Revision is newest revision for the charm.
Revision int
// Err holds any error that occurred while making the request.
Err error
}
// LatestRevisions returns the latest revisions of the given charms, using the given metadata.
func (c Client) LatestRevisions(charms []CharmID, metadata map[string][]string) ([]CharmRevision, error) {
// Due to the fact that we cannot use multiple macaroons per API call,
// we need to perform one call at a time, rather than making bulk calls.
// We could bulk the calls that use non-private charms, but we'd still need
// to do one bulk call per channel, due to how channels are used by the
// underlying csclient.
results := make([]CharmRevision, len(charms))
for i, cid := range charms {
revisions, err := c.csWrapper.Latest(cid.Channel, []*charm.URL{cid.URL}, metadata)
if err != nil {
return nil, errors.Trace(err)
}
rev := revisions[0]
results[i] = CharmRevision{Revision: rev.Revision, Err: rev.Err}
}
return results, nil
}
// ResourceRequest is the data needed to request a resource from the charmstore.
type ResourceRequest struct {
// Charm is the URL of the charm for which we're requesting a resource.
Charm *charm.URL
// Channel is the channel from which to request the resource info.
Channel csparams.Channel
// Name is the name of the resource we're asking about.
Name string
// Revision is the specific revision of the resource we're asking about.
Revision int
}
// ResourceData represents the response from the charmstore about a request for
// resource bytes.
type ResourceData struct {
// ReadCloser holds the bytes for the resource.
io.ReadCloser
// Resource holds the metadata for the resource.
Resource charmresource.Resource
}
// GetResource returns the data (bytes) and metadata for a resource from the charmstore.
func (c Client) GetResource(req ResourceRequest) (data ResourceData, err error) {
if err := c.jar.Activate(req.Charm); err != nil {
return ResourceData{}, errors.Trace(err)
}
defer c.jar.Deactivate()
meta, err := c.csWrapper.ResourceMeta(req.Channel, req.Charm, req.Name, req.Revision)
if err != nil {
return ResourceData{}, errors.Trace(err)
}
data.Resource, err = csparams.API2Resource(meta)
if err != nil {
return ResourceData{}, errors.Trace(err)
}
resData, err := c.csWrapper.GetResource(req.Channel, req.Charm, req.Name, req.Revision)
if err != nil {
return ResourceData{}, errors.Trace(err)
}
defer func() {
if err != nil {
resData.Close()
}
}()
data.ReadCloser = resData.ReadCloser
fpHash := data.Resource.Fingerprint.String()
if resData.Hash != fpHash {
return ResourceData{},
errors.Errorf("fingerprint for data (%s) does not match fingerprint in metadata (%s)", resData.Hash, fpHash)
}
return data, nil
}
// ResourceInfo returns the metadata for the given resource from the charmstore.
func (c Client) ResourceInfo(req ResourceRequest) (charmresource.Resource, error) {
if err := c.jar.Activate(req.Charm); err != nil {
return charmresource.Resource{}, errors.Trace(err)
}
defer c.jar.Deactivate()
meta, err := c.csWrapper.ResourceMeta(req.Channel, req.Charm, req.Name, req.Revision)
if err != nil {
return charmresource.Resource{}, errors.Trace(err)
}
res, err := csparams.API2Resource(meta)
if err != nil {
return charmresource.Resource{}, errors.Trace(err)
}
return res, nil
}
// ListResources returns a list of resources for each of the given charms.
func (c Client) ListResources(charms []CharmID) ([][]charmresource.Resource, error) {
results := make([][]charmresource.Resource, len(charms))
for i, ch := range charms {
res, err := c.listResources(ch)
if err != nil {
if csclient.IsAuthorizationError(err) || errors.Cause(err) == csparams.ErrNotFound {
// Ignore authorization errors and not-found errors so we get some results
// even if others fail.
continue
}
return nil, errors.Trace(err)
}
results[i] = res
}
return results, nil
}
func (c Client) listResources(ch CharmID) ([]charmresource.Resource, error) {
if err := c.jar.Activate(ch.URL); err != nil {
return nil, errors.Trace(err)
}
defer c.jar.Deactivate()
resources, err := c.csWrapper.ListResources(ch.Channel, ch.URL)
if err != nil {
return nil, errors.Trace(err)
}
return api2resources(resources)
}
// csWrapper is a type that abstracts away the low-level implementation details
// of the charmstore client.
type csWrapper interface {
Latest(channel csparams.Channel, ids []*charm.URL, headers map[string][]string) ([]csparams.CharmRevision, error)
ListResources(channel csparams.Channel, id *charm.URL) ([]csparams.Resource, error)
GetResource(channel csparams.Channel, id *charm.URL, name string, revision int) (csclient.ResourceData, error)
ResourceMeta(channel csparams.Channel, id *charm.URL, name string, revision int) (csparams.Resource, error)
ServerURL() string
}
// csclientImpl is an implementation of csWrapper that uses csclient.Client.
// It exists for testing purposes to hide away the hard-to-mock parts of
// csclient.Client.
type csclientImpl struct {
*csclient.Client
}
// Latest gets the latest CharmRevisions for the charm URLs on the channel.
func (c csclientImpl) Latest(channel csparams.Channel, ids []*charm.URL, metadata map[string][]string) ([]csparams.CharmRevision, error) {
client := c.WithChannel(channel)
client.SetHTTPHeader(http.Header(metadata))
return client.Latest(ids)
}
// ListResources gets the latest resources for the charm URL on the channel.
func (c csclientImpl) ListResources(channel csparams.Channel, id *charm.URL) ([]csparams.Resource, error) {
client := c.WithChannel(channel)
return client.ListResources(id)
}
// Getresource downloads the bytes and some metadata about the bytes for the revisioned resource.
func (c csclientImpl) GetResource(channel csparams.Channel, id *charm.URL, name string, revision int) (csclient.ResourceData, error) {
client := c.WithChannel(channel)
return client.GetResource(id, name, revision)
}
// ResourceInfo gets the full metadata for the revisioned resource.
func (c csclientImpl) ResourceMeta(channel csparams.Channel, id *charm.URL, name string, revision int) (csparams.Resource, error) {
client := c.WithChannel(channel)
return client.ResourceMeta(id, name, revision)
}
func api2resources(res []csparams.Resource) ([]charmresource.Resource, error) {
result := make([]charmresource.Resource, len(res))
for i, r := range res {
var err error
result[i], err = csparams.API2Resource(r)
if err != nil {
return nil, errors.Trace(err)
}
}
return result, nil
}