-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact.go
465 lines (396 loc) · 13.4 KB
/
artifact.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
// Trivy
// Copyright 2019-2020 Aqua Security Software Ltd.
// This product includes software developed by Aqua Security (https://aquasec.com).
//
// Adapted from https://github.com/aquasecurity/trivy/blob/main/pkg/fanal/artifact/image/image.go in order to remove some checks and fix race conditions
// while scanning multiple images.
package analyzer
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
"sync"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/all"
"github.com/aquasecurity/trivy/pkg/fanal/artifact"
"github.com/aquasecurity/trivy/pkg/fanal/handler"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/walker"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
_ "github.com/castai/image-analyzer/apk"
_ "github.com/castai/image-analyzer/dpkg"
_ "github.com/castai/image-analyzer/rpm"
)
// Artifact bundles image with the required dependencies to be able to scan it.
type Artifact struct {
log logrus.FieldLogger
image types.Image
cache CacheClient
walker walker.LayerTar
analyzer analyzer.AnalyzerGroup
configAnalyzer analyzer.ConfigAnalyzerGroup
handlerManager handler.Manager
artifactOption artifact.Option
}
// ArtifactReference represents uncompressed image with its layers also uncompressed
type ArtifactReference struct {
// BlobsInfo contains information about image layers
BlobsInfo []types.BlobInfo
ConfigFile *v1.ConfigFile
ArtifactInfo *types.ArtifactInfo
OsInfo *types.OS
}
// ArtifactOption customizes scanning behavior
type ArtifactOption = artifact.Option
// CachedImage does not contain information about layers.
// Identified by ImageID in cache.
type CachedImage = types.ArtifactInfo
// CachedLayers are identified by diffID in cache.
type CachedLayers = map[string]types.BlobInfo
type layerHashes struct {
// required to avoid calculation later
compressed string
// required for layer lookup in the cache
uncompressed string
}
// NewArtifact bundles already pulled image with additional dependencies for scanning.
func NewArtifact(img types.Image, log logrus.FieldLogger, c CacheClient, opt ArtifactOption) (*Artifact, error) {
a, err := analyzer.NewAnalyzerGroup(analyzer.AnalyzerOptions{
Group: opt.AnalyzerGroup,
DisabledAnalyzers: opt.DisabledAnalyzers,
})
if err != nil {
return nil, fmt.Errorf("create analyzer group: %w", err)
}
ca, err := analyzer.NewConfigAnalyzerGroup(analyzer.ConfigAnalyzerOptions{
FilePatterns: opt.FilePatterns,
DisabledAnalyzers: opt.DisabledAnalyzers,
MisconfScannerOption: opt.MisconfScannerOption,
SecretScannerOption: opt.SecretScannerOption,
})
if err != nil {
return nil, fmt.Errorf("create config analyzer group: %w", err)
}
return &Artifact{
log: log,
image: img,
cache: c,
walker: walker.NewLayerTar(walker.Option{
SkipFiles: opt.WalkerOption.SkipFiles,
SkipDirs: opt.WalkerOption.SkipDirs,
}),
analyzer: a,
configAnalyzer: ca,
artifactOption: opt,
}, nil
}
func (a Artifact) Inspect(ctx context.Context) (*ArtifactReference, error) {
imageID, err := a.image.ID()
if err != nil {
return nil, fmt.Errorf("getting image ID: %w", err)
}
a.log.Debugf("image ID: %s", imageID)
layers, err := a.image.Layers()
if err != nil {
return nil, fmt.Errorf("getting image's layers: %w", err)
}
layerIDs, err := getLayerHashes(layers)
if err != nil {
return nil, fmt.Errorf("getting layer's hashes: %w", err)
}
diffIDs := lo.Map(layerIDs, func(pair layerHashes, _ int) string { return pair.uncompressed })
a.log.Debugf("diff IDs: %v", diffIDs)
configFile, err := a.image.ConfigFile()
if err != nil {
return nil, fmt.Errorf("getting image's config file: %w", err)
}
baseDiffIDs := a.guessBaseLayers(diffIDs, configFile)
a.log.Debugf("base layer diff IDs: %v", baseDiffIDs)
cachedLayers, err := a.getCachedLayers(ctx, diffIDs)
if err != nil {
return nil, fmt.Errorf("getting cached layers: %w", err)
}
missingLayerDiffIDs := lo.Filter(diffIDs, func(v string, _ int) bool {
_, ok := cachedLayers[v]
return !ok
})
a.log.Debugf("found %d cached layers, %d layers will be inspected", len(cachedLayers), len(missingLayerDiffIDs))
a.log.Debugf("layers with the following diff IDs will be inspected: %v", missingLayerDiffIDs)
blobsInfo, osInfo, err := a.inspectLayers(ctx, layerIDs, baseDiffIDs)
if err != nil {
return nil, fmt.Errorf("analyzing layers: %w", err)
}
artifactInfo, err := a.inspectImage(ctx, configFile, imageID, *osInfo)
if err != nil {
return nil, fmt.Errorf("analyzing image: %w", err)
}
return &ArtifactReference{
BlobsInfo: append(blobsInfo, lo.Values(cachedLayers)...),
ConfigFile: configFile,
ArtifactInfo: artifactInfo,
OsInfo: osInfo,
}, nil
}
func getLayerHashes(layers []v1.Layer) ([]layerHashes, error) {
layerIDs := make([]layerHashes, 0, len(layers))
for _, layer := range layers {
compressedID, err := layer.Digest()
if err != nil {
return nil, fmt.Errorf("getting layer digest: %w", err)
}
uncompressedID, err := layer.DiffID()
if err != nil {
return nil, fmt.Errorf("getting layer diff ID: %w", err)
}
layerIDs = append(layerIDs, layerHashes{
compressed: compressedID.String(),
uncompressed: uncompressedID.String(),
})
}
return layerIDs, nil
}
func (a Artifact) getCachedImage(ctx context.Context, imageID string) (*CachedImage, error) {
blobBytes, err := a.cache.GetBlob(ctx, imageID)
if err != nil {
return nil, ErrCacheNotFound
}
var res types.ArtifactInfo
if err := json.Unmarshal(blobBytes, &res); err != nil {
return nil, fmt.Errorf("unmarshalling image: %w", err)
}
return &res, nil
}
func (a Artifact) getCachedLayers(ctx context.Context, diffIDs []string) (CachedLayers, error) {
blobs := CachedLayers{}
for _, diffID := range diffIDs {
blobBytes, err := a.cache.GetBlob(ctx, diffID)
if err != nil && !errors.Is(err, ErrCacheNotFound) {
continue
}
if len(blobBytes) > 0 {
var blob types.BlobInfo
if err := json.Unmarshal(blobBytes, &blob); err != nil {
return nil, fmt.Errorf("unmarshalling layer: %w", err)
}
blobs[diffID] = blob
}
}
return blobs, nil
}
func (a Artifact) putImageToCache(ctx context.Context, imageID string, image types.ArtifactInfo) error {
infoBytes, err := json.Marshal(image)
if err != nil {
return fmt.Errorf("marshalling image: %w", err)
}
return a.cache.PutBlob(ctx, imageID, infoBytes)
}
func (a Artifact) putLayerToCache(ctx context.Context, diffID string, layer types.BlobInfo) error {
layerBytes, err := json.Marshal(layer)
if err != nil {
return fmt.Errorf("marshalling layer: %w", err)
}
return a.cache.PutBlob(ctx, diffID, layerBytes)
}
func (Artifact) Clean(_ artifact.Reference) error {
return nil
}
func (a Artifact) inspectLayers(ctx context.Context, layerIDs []layerHashes, baseLayerDiffIDs []string) ([]types.BlobInfo, *types.OS, error) {
blobCh := make(chan types.BlobInfo)
errCh := make(chan error)
limit := semaphore.NewWeighted(int64(a.artifactOption.Parallel))
var osFound types.OS
go func() {
for _, layerIdPair := range layerIDs {
if err := limit.Acquire(ctx, 1); err != nil {
errCh <- fmt.Errorf("acquiring semaphore: %w", err)
return
}
go func(ctx context.Context, blobCh chan<- types.BlobInfo, errCh chan<- error, digest, diffID string) {
defer limit.Release(1)
// If it is a base layer, secret scanning should not be performed.
var disabledAnalyzers []analyzer.Type
if slices.Contains(baseLayerDiffIDs, diffID) {
disabledAnalyzers = append(disabledAnalyzers, analyzer.TypeSecret)
}
layerInfo, err := a.inspectLayer(ctx, digest, diffID, disabledAnalyzers)
if err != nil {
errCh <- fmt.Errorf("analyzing layer with diff ID %s: %w", diffID, err)
return
}
if err := a.putLayerToCache(ctx, diffID, layerInfo); err != nil {
a.log.Warnf("putting layer blob to cache: %v", err)
}
if layerInfo.OS != (types.OS{}) {
osFound = layerInfo.OS
}
blobCh <- layerInfo
}(ctx, blobCh, errCh, layerIdPair.compressed, layerIdPair.uncompressed)
}
}()
blobsInfo := make([]types.BlobInfo, 0, len(layerIDs))
for range layerIDs {
select {
case blob := <-blobCh:
blobsInfo = append(blobsInfo, blob)
case err := <-errCh:
return nil, nil, err
case <-ctx.Done():
return nil, nil, fmt.Errorf("timeout: %w", ctx.Err())
}
}
return blobsInfo, &osFound, nil
}
func (a Artifact) inspectLayer(ctx context.Context, digest, diffID string, disabled []analyzer.Type) (types.BlobInfo, error) {
a.log.Debugf("analyzing layer with digest %q and diff ID %q", digest, diffID)
layerReader, err := a.openUncompressedLayer(diffID)
if err != nil {
return types.BlobInfo{}, fmt.Errorf("unable to get uncompressed layer %s: %w", diffID, err)
}
var wg sync.WaitGroup
opts := analyzer.AnalysisOptions{Offline: a.artifactOption.Offline}
result := analyzer.NewAnalysisResult()
limit := semaphore.NewWeighted(int64(a.artifactOption.Parallel))
opaqueDirs, whiteoutFiles, err := a.walker.Walk(layerReader, func(filePath string, info os.FileInfo, opener analyzer.Opener) error {
return a.analyzer.AnalyzeFile(ctx, &wg, limit, result, "", filePath, info, opener, disabled, opts)
})
if err != nil {
return types.BlobInfo{}, fmt.Errorf("walk error: %w", err)
}
wg.Wait()
// Sort the analysis result for consistent results
result.Sort()
blobInfo := types.BlobInfo{
SchemaVersion: types.BlobJSONSchemaVersion,
Digest: digest,
DiffID: diffID,
OS: result.OS,
Repository: result.Repository,
PackageInfos: result.PackageInfos,
Applications: result.Applications,
Secrets: result.Secrets,
OpaqueDirs: opaqueDirs,
WhiteoutFiles: whiteoutFiles,
CustomResources: result.CustomResources,
// For Red Hat
BuildInfo: result.BuildInfo,
}
// Call post handlers to modify blob info
if err = a.handlerManager.PostHandle(ctx, result, &blobInfo); err != nil {
return types.BlobInfo{}, fmt.Errorf("post handler error: %w", err)
}
return blobInfo, nil
}
func (a Artifact) openUncompressedLayer(diffID string) (io.Reader, error) {
// diffID is a hash of the uncompressed layer
h, err := v1.NewHash(diffID)
if err != nil {
return nil, fmt.Errorf("invalid layer ID (%s): %w", diffID, err)
}
layer, err := a.image.LayerByDiffID(h)
if err != nil {
return nil, fmt.Errorf("failed to get the layer (%s): %w", diffID, err)
}
return layer.Uncompressed()
}
func (a Artifact) inspectImage(ctx context.Context, cfg *v1.ConfigFile, imageID string, osFound types.OS) (*types.ArtifactInfo, error) {
cachedImage, err := a.getCachedImage(ctx, imageID)
if err == nil {
return cachedImage, nil
}
pkgs := a.configAnalyzer.AnalyzeImageConfig(ctx, osFound, cfg)
info := types.ArtifactInfo{
SchemaVersion: types.ArtifactJSONSchemaVersion,
Architecture: cfg.Architecture,
Created: cfg.Created.Time,
DockerVersion: cfg.DockerVersion,
OS: cfg.OS,
HistoryPackages: pkgs.HistoryPackages,
}
if err := a.putImageToCache(ctx, imageID, info); err != nil {
a.log.Warnf("putting image blob to cache: %v", err)
}
return &info, nil
}
// Guess layers in base image (call base layers).
//
// e.g. In the following example, we should detect layers in debian:8.
//
// FROM debian:8
// RUN apt-get update
// COPY mysecret /
// ENTRYPOINT ["entrypoint.sh"]
// CMD ["somecmd"]
//
// debian:8 may be like
//
// ADD file:5d673d25da3a14ce1f6cf66e4c7fd4f4b85a3759a9d93efb3fd9ff852b5b56e4 in /
// CMD ["/bin/sh"]
//
// In total, it would be like:
//
// ADD file:5d673d25da3a14ce1f6cf66e4c7fd4f4b85a3759a9d93efb3fd9ff852b5b56e4 in /
// CMD ["/bin/sh"] # empty layer (detected)
// RUN apt-get update
// COPY mysecret /
// ENTRYPOINT ["entrypoint.sh"] # empty layer (skipped)
// CMD ["somecmd"] # empty layer (skipped)
//
// This method tries to detect CMD in the second line and assume the first line is a base layer.
// 1. Iterate histories from the bottom.
// 2. Skip all the empty layers at the bottom. In the above example, "entrypoint.sh" and "somecmd" will be skipped
// 3. If it finds CMD, it assumes that it is the end of base layers.
// 4. It gets all the layers as base layers above the CMD found in #3.
func (a Artifact) guessBaseLayers(diffIDs []string, configFile *v1.ConfigFile) []string {
if configFile == nil {
return nil
}
var baseImageIndex int
var foundNonEmpty bool
for i := len(configFile.History) - 1; i >= 0; i-- {
h := configFile.History[i]
// Skip the last CMD, ENTRYPOINT, etc.
if !foundNonEmpty {
if h.EmptyLayer {
continue
}
foundNonEmpty = true
}
if !h.EmptyLayer {
continue
}
// Detect CMD instruction in base image
if strings.HasPrefix(h.CreatedBy, "/bin/sh -c #(nop) CMD") ||
strings.HasPrefix(h.CreatedBy, "CMD") { // BuildKit
baseImageIndex = i
break
}
}
// Diff IDs don't include empty layers, so the index is different from histories
var diffIDIndex int
var baseDiffIDs []string
for i, h := range configFile.History {
// It is no longer base layer.
if i > baseImageIndex {
break
}
// Empty layers are not included in diff IDs.
if h.EmptyLayer {
continue
}
if diffIDIndex >= len(diffIDs) {
// something wrong...
return nil
}
baseDiffIDs = append(baseDiffIDs, diffIDs[diffIDIndex])
diffIDIndex++
}
return baseDiffIDs
}