-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
493 lines (437 loc) · 13.5 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package gcs
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/getkin/kin-openapi/openapi3"
"github.com/rs/zerolog/log"
"go.uber.org/multierr"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"github.com/snyk/vervet/v7"
vustorage "github.com/snyk/vervet/v7/internal/storage"
)
// StaticKeyCredentials structure google.Credentials for
// GCS storage.NewClient API.
type StaticKeyCredentials struct {
ProjectId string
Filename string
}
// Config holds setting up and targeting proper GCS targets.
type Config struct {
GcsRegion string
GcsEndpoint string
BucketName string
IamRoleEnabled bool
Credentials StaticKeyCredentials
WithoutAuthentication bool
}
// Storage implements storage.Storage.
type Storage struct {
c *storage.Client
config Config
newCollator func() (*vustorage.Collator, error)
}
// New instantiates a gcs.Storage client to handle storing and retrieving
// storage.ContentRevision and Collated Versions.
func New(ctx context.Context, gcsConfig *Config, options ...Option) (vustorage.Storage, error) {
if gcsConfig == nil || gcsConfig.BucketName == "" {
return nil, fmt.Errorf("missing GCS configuration")
}
var clientOptions []option.ClientOption
if !gcsConfig.IamRoleEnabled {
clientOptions = []option.ClientOption{option.WithEndpoint(gcsConfig.GcsEndpoint)}
if gcsConfig.Credentials.Filename != "" {
clientOptions = append(clientOptions, option.WithCredentialsFile(gcsConfig.Credentials.Filename))
} else if gcsConfig.WithoutAuthentication {
clientOptions = append(clientOptions, option.WithoutAuthentication())
}
}
client, err := storage.NewClient(ctx, clientOptions...)
if err != nil {
log.Error().Err(err).Msg("failed to create client")
return nil, err
}
st := &Storage{
c: client,
config: *gcsConfig,
newCollator: func() (*vustorage.Collator, error) { return vustorage.NewCollator() },
}
for _, option := range options {
option(st)
}
err = st.CreateBucket(ctx)
if err != nil {
return nil, err
}
return st, nil
}
// Option defines a Storage constructor option.
type Option func(*Storage)
// NewCollator configures the Storage instance to use the given constructor
// function for creating collator instances.
func NewCollator(newCollator func() (*vustorage.Collator, error)) Option {
return func(s *Storage) {
s.newCollator = newCollator
}
}
// NotifyVersions implements scraper.Storage.
func (s *Storage) NotifyVersions(ctx context.Context, name string, versions []string, scrapeTime time.Time) error {
for _, version := range versions {
// TODO: Add method to fetch contents here
// TODO: implement notify versions; update sunset when versions are removed
err := s.NotifyVersion(ctx, name, version, []byte{}, scrapeTime)
if err != nil {
return err
}
}
return nil
}
// CollateVersions iterates over all possible permutations of Service versions
// to create a unified version spec for each unique vervet.Version.
func (s *Storage) CollateVersions(ctx context.Context, serviceFilter map[string]bool) error {
// create an aggregate to process collated data from storage data
aggregate, err := s.newCollator()
if err != nil {
return err
}
serviceRevisionResults, err := s.ListObjects(ctx, vustorage.ServiceVersionsFolder, "")
if err != nil {
return err
}
// all specs are stored as key: "service-versions/{service_name}/{version}/{digest}.json"
for _, revContent := range serviceRevisionResults {
service, version, digest, err := parseServiceVersionRevisionKey(revContent.Name)
if err != nil {
return err
}
if _, ok := serviceFilter[service]; !ok {
continue
}
rev, obj, err := s.GetObjectWithMetadata(ctx, vustorage.ServiceVersionsFolder+service+"/"+version+"/"+digest+".json")
if err != nil {
return err
}
// Assuming version is valid in path uploads
parsedVersion, err := vervet.ParseVersion(version)
if err != nil {
log.Error().Err(err).Msg("unexpected version path in GCS. Validate Service Revision uploads")
return err
}
blob, err := io.ReadAll(rev)
err = multierr.Append(err, rev.Close())
if err != nil {
log.Error().Err(err).Msg("failed to read Service ContentRevision JSON")
return err
}
revision := vustorage.ContentRevision{
Service: service,
Version: parsedVersion,
Timestamp: obj.Created,
Digest: vustorage.Digest(digest),
Blob: blob,
}
aggregate.Add(service, revision)
}
specs, err := aggregate.Collate()
if err != nil {
return err
}
n, err := s.putCollatedSpecs(ctx, specs)
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("no objects uploaded")
}
return nil
}
// HasVersion implements scraper.Storage.
func (s *Storage) HasVersion(ctx context.Context, name string, version string, digest string) (bool, error) {
key := getServiceVersionRevisionKey(name, version, digest)
revisions, err := s.ListObjects(ctx, key, "")
if err != nil {
return false, err
}
// storage.Digest(digest) for the revision present
return len(revisions) == 1, nil
}
// NotifyVersion updates a Service's storage.ContentRevision if storage.Digest has changed.
func (s *Storage) NotifyVersion(ctx context.Context,
name string,
version string,
contents []byte,
scrapeTime time.Time,
) error {
digest := vustorage.NewDigest(contents)
key := getServiceVersionRevisionKey(name, version, string(digest))
parsedVersion, err := vervet.ParseVersion(version)
if err != nil {
log.Error().Err(err).Msgf("Failed to resolve Vervet version for %s : %s", name, version)
return err
}
currentRevision := vustorage.ContentRevision{
Service: name,
Timestamp: scrapeTime,
Digest: digest,
Blob: contents,
Version: parsedVersion,
}
// will return empty in the event of no keys found without an error
serviceVersionRevision, err := s.GetObject(ctx, key)
if err != nil {
return err
}
// if the digest file exists, it counts as a match, no-op, no change
if len(serviceVersionRevision) > 0 {
return nil
}
// Since the digest doesn't exist, add the whole key path
reader := bytes.NewReader(currentRevision.Blob)
err = s.PutObject(ctx, key, reader)
if err != nil {
return err
}
return nil
}
// Versions lists all available Collated Versions.
func (s *Storage) VersionIndex(ctx context.Context) (vervet.VersionIndex, error) {
versions, err := s.ListCollatedVersions(ctx)
if err != nil {
return vervet.VersionIndex{}, err
}
vs := make(vervet.VersionSlice, len(versions))
for idx, version := range versions {
vs[idx], err = vervet.ParseVersion(version)
if err != nil {
return vervet.VersionIndex{}, err
}
}
return vervet.NewVersionIndex(vs), nil
}
// Version implements scraper.Storage.
func (s *Storage) Version(ctx context.Context, version string) ([]byte, error) {
parsedVersion, err := vervet.ParseVersion(version)
if err != nil {
return nil, err
}
blob, err := s.GetCollatedVersionSpec(ctx, version)
if err != nil {
index, err := s.VersionIndex(ctx)
if err != nil {
return nil, err
}
resolved, err := index.Resolve(parsedVersion)
if err != nil {
return nil, err
}
return s.GetCollatedVersionSpec(ctx, resolved.String())
}
return blob, nil
}
// ListCollatedVersions nice wrapper around the GCS ListObjects request.
// example: key = "collated-versions/"
// result: []string{"2022-02-02~wip", "2022-12-02~beta"}
// Defaults to 1000 results.
func (s *Storage) ListCollatedVersions(ctx context.Context) ([]string, error) {
res, err := s.ListObjects(ctx, vustorage.CollatedVersionsFolder, "/")
if err != nil {
return nil, err
}
prefixes := make([]string, 0)
for _, v := range res {
prefixes = append(prefixes, getCollatedVersionFromKey(v.Prefix))
}
return prefixes, nil
}
// putCollatedSpecs stores the given collated specs and returns the number of
// specs stored to GCS.
func (s *Storage) putCollatedSpecs(ctx context.Context, objects map[vervet.Version]openapi3.T) (int, error) {
var n int
// TODO: Look for alternative to iteratively uploading.
for key, file := range objects {
jsonBlob, err := file.MarshalJSON()
if err != nil {
return 0, fmt.Errorf("failure to marshal json for collation upload: %w", err)
}
reader := bytes.NewReader(jsonBlob)
err = s.PutObject(ctx, vustorage.CollatedVersionsFolder+key.String()+"/spec.json", reader)
if err != nil {
return n, err
}
n++
}
return n, nil
}
// GetCollatedVersionSpecs retrieves a map of vervet.Version strings
// and their corresponding JSON blobs and returns the result.
func (s *Storage) GetCollatedVersionSpecs(ctx context.Context) (map[string][]byte, error) {
versionSpecs := map[string][]byte{}
versions, err := s.ListCollatedVersions(ctx)
if err != nil {
return nil, err
}
for _, key := range versions {
jsonBlob, err := s.GetCollatedVersionSpec(ctx, key)
if err != nil {
return nil, err
}
versionSpecs[getCollatedVersionFromKey(key)] = jsonBlob
}
return versionSpecs, nil
}
// GetCollatedVersionSpec retrieves a single collated vervet.Version
// and returns the JSON blob.
func (s *Storage) GetCollatedVersionSpec(ctx context.Context, version string) ([]byte, error) {
jsonBlob, err := s.GetObject(ctx, vustorage.CollatedVersionsFolder+version+"/spec.json")
if err != nil {
return nil, err
}
return jsonBlob, nil
}
// PutObject nice wrapper around the GCS PutObject request.
func (s *Storage) PutObject(ctx context.Context, key string, reader io.Reader) (putErr error) {
wc := s.c.Bucket(s.config.BucketName).Object(key).NewWriter(ctx)
defer func() {
if err := wc.Close(); err != nil {
putErr = err
}
}()
if _, err := io.Copy(wc, reader); err != nil {
return err
}
return nil
}
// GetObject actually retrieves the json blob form GCS.
func (s *Storage) GetObject(ctx context.Context, key string) ([]byte, error) {
reader, err := s.c.Bucket(s.config.BucketName).Object(key).NewReader(ctx)
if err != nil {
if errors.Is(err, storage.ErrObjectNotExist) {
return nil, nil
}
return nil, err
}
defer reader.Close()
return io.ReadAll(reader)
}
// GetObjectWithMetadata actually retrieves the json blob form GCS
// with metadata around the storage in GCS.
func (s *Storage) GetObjectWithMetadata(ctx context.Context,
key string) (*storage.Reader,
*storage.ObjectAttrs,
error,
) {
handle := s.c.Bucket(s.config.BucketName).Object(key)
attrs, err := handle.Attrs(ctx)
if err != nil {
return nil, nil, err
}
reader, err := handle.NewReader(ctx)
if err != nil {
return nil, nil, err
}
return reader, attrs, err
}
// ListObjects nice wrapper around the GCS storage.BucketHandle Objects request.
// Defaults to 1000 results.
func (s *Storage) ListObjects(ctx context.Context, key string, delimeter string) ([]storage.ObjectAttrs, error) {
query := &storage.Query{
Prefix: key,
}
if delimeter == "/" {
query.Delimiter = "/"
}
if query.Prefix == "" {
query = nil
}
it := s.c.Bucket(s.config.BucketName).Objects(ctx, query)
r := make([]storage.ObjectAttrs, 0)
for {
obj, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return nil, err
}
if obj == nil {
break
}
r = append(r, *obj)
}
log.Trace().Msgf("GCS Objects response: %+v", r)
return r, nil
}
// DeleteObject deletes a file if it exists.
func (s *Storage) DeleteObject(ctx context.Context, key string) error {
return s.c.Bucket(s.config.BucketName).Object(key).Delete(ctx)
}
// CreateBucket idempotently creates an GCS bucket for VU.
func (s *Storage) CreateBucket(ctx context.Context) error {
bucket, err := s.getBucketAttrs(ctx)
if err != nil && !errors.Is(err, storage.ErrBucketNotExist) {
return err
}
// if bucket exists, idempotent return
if bucket != nil {
return nil
}
err = s.c.Bucket(s.config.BucketName).Create(
ctx,
s.config.Credentials.ProjectId,
nil)
if err != nil {
return err
}
return nil
}
// ListBucketContents lists all available files in a GCS bucket.
func (s *Storage) ListBucketContents(ctx context.Context) ([]string, error) {
objects := make([]string, 0)
it := s.c.Bucket(s.config.BucketName).Objects(ctx, &storage.Query{})
for {
attrs, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return nil, err
}
objects = append(objects, attrs.Name)
}
return objects, nil
}
// getBucketAttrs gets the metadata around a bucket if it exists.
func (s *Storage) getBucketAttrs(ctx context.Context) (*storage.BucketAttrs, error) {
return s.c.Bucket(s.config.BucketName).Attrs(ctx)
}
// getCollatedVersionFromKey helper function to clean up GCS keys for
// printing versions. Example: collated-versions/2022-02-02/spec.json --> 2022-02-02.
func getCollatedVersionFromKey(key string) string {
versionPath := strings.Split(strings.TrimPrefix(key, vustorage.CollatedVersionsFolder), "/")
if len(versionPath) != 2 {
return key
}
return versionPath[0]
}
func getServiceVersionRevisionKey(name string, version string, digest string) string {
host := vustorage.GetSantizedHost(name)
return fmt.Sprintf("%v%v/%v/%v.json", vustorage.ServiceVersionsFolder, host, version, digest)
}
func parseServiceVersionRevisionKey(key string) (string, string, string, error) {
// digest can have "/" chars, so only split for service and version
arr := strings.SplitN(strings.TrimPrefix(key, vustorage.ServiceVersionsFolder), "/", 3)
if len(arr) != 3 {
err := fmt.Errorf("service Content Revision not able to be parsed: %v", key)
log.Error().Err(err).Msg("GCS service path malformed")
return "", "", "", err
}
service, version, digestJson := arr[0], arr[1], arr[2]
digest := strings.TrimSuffix(digestJson, ".json")
return service, version, digest, nil
}