forked from juju/charmrepo
/
charmstore.go
247 lines (221 loc) · 7.18 KB
/
charmstore.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmrepo
import (
"crypto/sha512"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/juju/utils"
"gopkg.in/errgo.v1"
"gopkg.in/juju/charm.v6-unstable"
"gopkg.in/juju/charmrepo.v0/csclient"
"gopkg.in/juju/charmrepo.v0/csclient/params"
)
// CacheDir stores the charm cache directory path.
var CacheDir string
// CharmStore is a repository Interface that provides access to the public Juju
// charm store.
type CharmStore struct {
client *csclient.Client
}
var _ Interface = (*CharmStore)(nil)
// NewCharmStoreParams holds parameters for instantiating a new CharmStore.
type NewCharmStoreParams struct {
// URL holds the root endpoint URL of the charm store,
// with no trailing slash, not including the version.
// For example https://api.jujucharms.com/charmstore
// If empty, the default charm store client location is used.
URL string
// HTTPClient holds the HTTP client to use when making
// requests to the store. If nil, httpbakery.NewHTTPClient will
// be used.
HTTPClient *http.Client
// VisitWebPage is called when authorization requires that
// the user visits a web page to authenticate themselves.
// If nil, a default function that returns an error will be used.
VisitWebPage func(url *url.URL) error
}
// NewCharmStore creates and returns a charm store repository.
// The given parameters are used to instantiate the charm store.
//
// The errors returned from the interface methods will
// preserve the causes returned from the underlying csclient
// methods.
func NewCharmStore(p NewCharmStoreParams) Interface {
return &CharmStore{
client: csclient.New(csclient.Params{
URL: p.URL,
HTTPClient: p.HTTPClient,
VisitWebPage: p.VisitWebPage,
}),
}
}
// Get implements Interface.Get.
func (s *CharmStore) Get(curl *charm.URL) (charm.Charm, error) {
// The cache location must have been previously set.
if CacheDir == "" {
panic("charm cache directory path is empty")
}
if curl.Series == "bundle" {
return nil, errgo.Newf("expected a charm URL, got bundle URL %q", curl)
}
// Prepare the cache directory and retrieve the charm.
if err := os.MkdirAll(CacheDir, 0755); err != nil {
return nil, errgo.Notef(err, "cannot create the cache directory")
}
r, id, expectHash, expectSize, err := s.client.GetArchive(curl.Reference())
if err != nil {
if errgo.Cause(err) == params.ErrNotFound {
// Make a prettier error message for the user.
return nil, errgo.WithCausef(nil, params.ErrNotFound, "cannot retrieve charm %q: charm not found", curl)
}
return nil, errgo.NoteMask(err, fmt.Sprintf("cannot retrieve charm %q", curl), errgo.Any)
}
defer r.Close()
// Check if the archive already exists in the cache.
path := filepath.Join(CacheDir, charm.Quote(id.String())+".charm")
if verifyHash384AndSize(path, expectHash, expectSize) == nil {
return charm.ReadCharmArchive(path)
}
// Verify and save the new archive.
f, err := ioutil.TempFile(CacheDir, "charm-download")
if err != nil {
return nil, errgo.Notef(err, "cannot make temporary file")
}
defer f.Close()
hash := sha512.New384()
size, err := io.Copy(io.MultiWriter(hash, f), r)
if err != nil {
return nil, errgo.Notef(err, "cannot read charm archive")
}
if size != expectSize {
return nil, errgo.Newf("size mismatch; network corruption?")
}
if fmt.Sprintf("%x", hash.Sum(nil)) != expectHash {
return nil, errgo.Newf("hash mismatch; network corruption?")
}
// Move the archive to the expected place, and return the charm.
if err := utils.ReplaceFile(f.Name(), path); err != nil {
return nil, errgo.Notef(err, "cannot move the charm archive")
}
return charm.ReadCharmArchive(path)
}
func verifyHash384AndSize(path, expectHash string, expectSize int64) error {
f, err := os.Open(path)
if err != nil {
return errgo.Mask(err)
}
defer f.Close()
hash := sha512.New384()
size, err := io.Copy(hash, f)
if err != nil {
return errgo.Mask(err)
}
if size != expectSize {
logger.Debugf("size mismatch for %q", path)
return errgo.Newf("size mismatch for %q", path)
}
if fmt.Sprintf("%x", hash.Sum(nil)) != expectHash {
logger.Debugf("hash mismatch for %q", path)
return errgo.Newf("hash mismatch for %q", path)
}
return nil
}
// Latest implements Interface.Latest.
func (s *CharmStore) Latest(curls ...*charm.URL) ([]CharmRevision, error) {
if len(curls) == 0 {
return nil, nil
}
// Prepare the request to the charm store.
urls := make([]string, len(curls))
values := url.Values{}
// Include the ignore-auth flag so that non-public results do not generate
// an error for the whole request.
values.Add("ignore-auth", "1")
values.Add("include", "id-revision")
values.Add("include", "hash256")
for i, curl := range curls {
url := curl.WithRevision(-1).String()
urls[i] = url
values.Add("id", url)
}
u := url.URL{
Path: "/meta/any",
RawQuery: values.Encode(),
}
// Execute the request and retrieve results.
var results map[string]struct {
Meta struct {
IdRevision params.IdRevisionResponse `json:"id-revision"`
Hash256 params.HashResponse `json:"hash256"`
}
}
if err := s.client.Get(u.String(), &results); err != nil {
return nil, errgo.NoteMask(err, "cannot get metadata from the charm store", errgo.Any)
}
// Build the response.
responses := make([]CharmRevision, len(curls))
for i, url := range urls {
result, found := results[url]
if !found {
responses[i] = CharmRevision{
Err: CharmNotFound(url),
}
continue
}
responses[i] = CharmRevision{
Revision: result.Meta.IdRevision.Revision,
Sha256: result.Meta.Hash256.Sum,
}
}
return responses, nil
}
// Resolve implements Interface.Resolve.
func (s *CharmStore) Resolve(ref *charm.Reference) (*charm.URL, error) {
var result struct {
Id params.IdResponse
}
if _, err := s.client.Meta(ref, &result); err != nil {
if errgo.Cause(err) == params.ErrNotFound {
// Make a prettier error message for the user.
return nil, errgo.WithCausef(nil, params.ErrNotFound, "cannot resolve charm URL %q: charm not found", ref)
}
return nil, errgo.NoteMask(err, fmt.Sprintf("cannot resolve charm URL %q", ref), errgo.Any)
}
url, err := result.Id.Id.URL("")
if err != nil {
return nil, errgo.Notef(err, "cannot make fully resolved entity URL from %s", url)
}
return url, nil
}
// URL returns the root endpoint URL of the charm store.
func (s *CharmStore) URL() string {
return s.client.ServerURL()
}
// WithTestMode returns a repository Interface where test mode is enabled,
// meaning charm store download stats are not increased when charms are
// retrieved.
func (s *CharmStore) WithTestMode() Interface {
newRepo := *s
newRepo.client.DisableStats()
return &newRepo
}
// JujuMetadataHTTPHeader is the HTTP header name used to send Juju metadata
// attributes to the charm store.
const JujuMetadataHTTPHeader = "Juju-Metadata"
// WithJujuAttrs returns a repository Interface with the Juju metadata
// attributes set.
func (s *CharmStore) WithJujuAttrs(attrs map[string]string) Interface {
newRepo := *s
header := make(http.Header)
for k, v := range attrs {
header.Add(JujuMetadataHTTPHeader, k+"="+v)
}
newRepo.client.SetHTTPHeader(header)
return &newRepo
}