This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
hub.go
390 lines (362 loc) · 11.6 KB
/
hub.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright 2018 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hub
import (
"bufio"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/gravitational/gravity/lib/constants"
"github.com/gravitational/gravity/lib/defaults"
"github.com/gravitational/gravity/lib/loc"
"github.com/gravitational/gravity/lib/utils"
"github.com/coreos/go-semver/semver"
"github.com/dustin/go-humanize"
"github.com/ghodss/yaml"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/repo"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
// Hub defines an interface for the hub that stores Telekube application installers
//
// The current hub implementation is backed by S3 and all application installers
// are stored in the bucket of the following structure:
//
// hub.gravitational.io
// ∟ gravity
// ∟ oss
// ∟ index.yaml
// ∟ app
// ∟ telekube
// ∟ 5.2.0
// ∟ linux
// ∟ x86_64
// ∟ telekube-5.2.0-linux-x86_64.tar
// ∟ telekube-5.2.0-linux-x86_64.tar.sha256
// ∟ latest: // same as versioned sub-bucket
// ∟ stable: // same as versioned sub-bucket
//
// The index file, index.yaml, provides information about installers stored
// in the bucket and is updated every time a new version is published. The
// index file format is the same as Helm's chart repository index file.
type Hub interface {
// List returns a list of applications in the hub
List(withPrereleases bool) ([]App, error)
// Download downloads the specified application installer into provided file
Download(*os.File, loc.Locator) error
// Get returns application installer tarball of the specified version
Get(loc.Locator) (io.ReadCloser, error)
// GetLatestVersion returns latest version of the specified application
GetLatestVersion(name string) (string, error)
}
// App represents a single application item in the hub
type App struct {
// Name is the application name
Name string `json:"name"`
// Version is the application version
Version string `json:"version"`
// Created is the application creation timestamp
Created time.Time `json:"created"`
// SizeBytes is the application size in bytes
SizeBytes int64 `json:"sizeBytes"`
// Description is the image description
Description string `json:"description"`
// Type is the image type, for example application or cluster
Type string `json:"type"`
}
// S3Hub is the S3-backed hub implementation
type S3Hub struct {
// Config is the hub configuration
config Config
// downloader is the S3 download manager
downloader *s3manager.Downloader
}
// Config is the S3-backed hub configuration
type Config struct {
// Bucket is the S3 bucket name
Bucket string
// Prefix is the S3 path prefix
Prefix string
// Region is the S3 region
Region string
// FieldLogger is used for logging
logrus.FieldLogger
// S3 is optional S3 API client
S3 s3iface.S3API
}
// CheckAndSetDefaults validates config and sets defaults
func (c *Config) CheckAndSetDefaults() error {
if c.Bucket == "" {
c.Bucket = defaults.HubBucket
}
if c.Prefix == "" {
c.Prefix = defaults.HubTelekubePrefix
}
if c.Region == "" {
c.Region = defaults.AWSRegion
}
if c.FieldLogger == nil {
c.FieldLogger = logrus.WithField(trace.Component, "s3hub")
}
if c.S3 == nil {
session, err := session.NewSession(&aws.Config{
Region: aws.String(c.Region),
Credentials: credentials.AnonymousCredentials,
})
if err != nil {
return trace.Wrap(err)
}
c.S3 = s3.New(session)
}
return nil
}
// New returns a new S3-backed hub instance
func New(config Config) (*S3Hub, error) {
err := config.CheckAndSetDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
return &S3Hub{
config: config,
downloader: s3manager.NewDownloaderWithClient(config.S3),
}, nil
}
// List returns a list of applications in the hub
func (h *S3Hub) List(withPrereleases bool) (items []App, err error) {
indexFile, err := h.getIndexFile()
if err != nil {
return nil, trace.Wrap(err)
}
for _, entryVersions := range indexFile.Entries {
for _, entry := range entryVersions {
items = append(items, App{
Name: entry.Name,
Version: entry.Version,
Created: entry.Created,
Description: strings.TrimSpace(entry.Description),
Type: entry.Annotations[constants.AnnotationKind],
})
}
}
return items, nil
}
// Download downloads the specified application installer into provided file
func (h *S3Hub) Download(f *os.File, locator loc.Locator) (err error) {
version := locator.Version
// in case the provided version is a special 'latest' or 'stable' label,
// we need to look into respective bucket to find out the actual version
switch version {
case loc.LatestVersion:
locator.Version, err = h.GetLatestVersion(locator.Name)
case loc.StableVersion:
locator.Version, err = h.getStableVersion(locator.Name)
}
if err != nil {
return trace.Wrap(err)
}
h.config.Infof("Downloading: %v.", h.appPath(locator.Name, locator.Version))
n, err := h.downloader.Download(f, &s3.GetObjectInput{
Bucket: aws.String(h.config.Bucket),
Key: aws.String(h.appPath(locator.Name, locator.Version)),
})
if err != nil {
err := utils.ConvertS3Error(err)
if trace.IsNotFound(err) {
return trace.NotFound("image %v:%v not found in %v, use 'tele ls -a' to see available images",
locator.Name, locator.Version, h.config.Bucket)
}
return trace.Wrap(err)
}
h.config.Infof("Download complete: %v %v.", locator, humanize.Bytes(uint64(n)))
if err := h.verifyChecksum(locator.Name, locator.Version, f.Name()); err != nil {
return trace.Wrap(err, "failed to verify %v:%v checksum", locator.Name, locator.Version)
}
return nil
}
// Get returns application installer tarball of the specified version
func (h *S3Hub) Get(locator loc.Locator) (io.ReadCloser, error) {
tarFile, err := ioutil.TempFile("", locator.Name)
if err != nil {
return nil, trace.Wrap(err)
}
readCloser := &utils.CleanupReadCloser{
ReadCloser: tarFile,
Cleanup: func() {
if err := os.RemoveAll(tarFile.Name()); err != nil {
h.config.Warnf("Failed to remove %v: %v.", tarFile.Name(), err)
}
},
}
err = h.Download(tarFile, locator)
if err != nil {
readCloser.Close()
return nil, trace.Wrap(err)
}
return readCloser, nil
}
// getIndexFile returns the hub's index file.
func (h *S3Hub) getIndexFile() (*repo.IndexFile, error) {
object, err := h.config.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(h.config.Bucket),
Key: aws.String(filepath.Join(h.config.Prefix, indexFileName)),
})
if err != nil {
return nil, trace.Wrap(err)
}
defer object.Body.Close()
bytes, err := ioutil.ReadAll(object.Body)
if err != nil {
return nil, trace.Wrap(err)
}
var indexFile repo.IndexFile
err = yaml.Unmarshal(bytes, &indexFile)
if err != nil {
return nil, trace.Wrap(err)
}
return &indexFile, nil
}
// appsBucket returns sub-bucket where all applications are stored
func (h *S3Hub) appsBucket() string {
return fmt.Sprintf("%v/app", h.config.Prefix)
}
// appBucket returns sub-bucket where the specified application is stored
func (h *S3Hub) appBucket(name, version string) string {
return fmt.Sprintf("%v/%v/%v/linux/x86_64", h.appsBucket(), name, version)
}
// appBucketPath returns path to the specified application in the hub
func (h *S3Hub) appPath(name, version string) string {
return fmt.Sprintf("%v/%v", h.appBucket(name, version), makeFilename(name, version))
}
// shaPath returns path to the checksum file of the specified application in the hub
func (h *S3Hub) shaPath(name, version string) string {
return h.appPath(name, version) + ".sha256"
}
// GetLatestVersion returns the latest version of the specified application in the hub
func (h *S3Hub) GetLatestVersion(name string) (string, error) {
indexFile, err := h.getIndexFile()
if err != nil {
return "", trace.Wrap(err)
}
versions := indexFile.Entries[name]
if len(versions) == 0 {
return "", trace.NotFound("image %q not found", name)
}
latestVersion, err := semver.NewVersion(versions[0].Version)
if err != nil {
return "", trace.Wrap(err)
}
for _, version := range versions {
nextVersion, err := semver.NewVersion(version.Version)
if err != nil {
return "", trace.Wrap(err)
}
if latestVersion.LessThan(*nextVersion) {
latestVersion = nextVersion
}
}
return latestVersion.String(), nil
}
// getStableVersion returns the stable version of the specified application in the hub
func (h *S3Hub) getStableVersion(name string) (string, error) {
indexFile, err := h.getIndexFile()
if err != nil {
return "", trace.Wrap(err)
}
versions, ok := indexFile.Entries[name]
if !ok || len(versions) == 0 {
return "", trace.NotFound("image %q not found", name)
}
var stableVersion *semver.Version
for _, version := range versions {
nextVersion, err := semver.NewVersion(version.Version)
if err != nil {
return "", trace.Wrap(err)
}
if nextVersion.PreRelease != "" {
continue
}
if stableVersion == nil || stableVersion.LessThan(*nextVersion) {
stableVersion = nextVersion
}
}
if stableVersion == nil {
return "", trace.NotFound("no stable version of image %q found", name)
}
return stableVersion.String(), nil
}
// verifyChecksum verifies the checksum of the downloaded installer file
func (h *S3Hub) verifyChecksum(name, version, path string) error {
storedChecksum, err := h.getChecksum(name, version)
if err != nil {
return trace.Wrap(err)
}
file, err := os.Open(path)
if err != nil {
return trace.Wrap(err)
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return trace.Wrap(err)
}
checksum := fmt.Sprintf("%x", hash.Sum(nil))
if storedChecksum != checksum {
return trace.BadParameter("checksum mismatch: stored %q, calculated %q",
storedChecksum, checksum)
}
h.config.Infof("Checksum for %v:%v verified: %v.", name, version, checksum)
return nil
}
// getChecksum fetches the sha256 checksum of the specified application from the hub
func (h *S3Hub) getChecksum(name, version string) (string, error) {
object, err := h.config.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(h.config.Bucket),
Key: aws.String(h.shaPath(name, version)),
})
if err != nil {
return "", trace.Wrap(err)
}
defer object.Body.Close()
// even though the file should only contain the checksum, read in only
// first 64 bytes (checksum block size) to be on a safe side
checksum := make([]byte, sha256.BlockSize)
n, err := bufio.NewReader(object.Body).Read(checksum)
if err != nil {
return "", trace.Wrap(err)
}
if n != sha256.BlockSize {
return "", trace.BadParameter("expected %v bytes but read only %v",
sha256.BlockSize, n)
}
return string(checksum), nil
}
// makeFilename returns the name of the file under which the application
// specified by the provided locator is stored in the hub
func makeFilename(name, version string) string {
return fmt.Sprintf("%v-%v-linux-x86_64.tar", name, version)
}
const (
// indexFileName is the repository index file name.
indexFileName = "index.yaml"
)