forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.go
More file actions
384 lines (324 loc) · 12.6 KB
/
Copy pathfs.go
File metadata and controls
384 lines (324 loc) · 12.6 KB
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
package local
import (
"cmp"
"context"
"crypto/sha256"
"errors"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/samber/lo"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/cache"
"github.com/aquasecurity/trivy/pkg/digest"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"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"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/semaphore"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
"github.com/aquasecurity/trivy/pkg/uuid"
)
const artifactVersion = 0
var _ Walker = (*walker.FS)(nil)
type Walker interface {
Walk(root string, opt walker.Option, fn walker.WalkFunc) error
}
type Artifact struct {
rootPath string
logger *log.Logger
cache cache.ArtifactCache
walker Walker
analyzer analyzer.AnalyzerGroup
handlerManager handler.Manager
artifactOption artifact.Option
isClean bool // whether git repository is clean (for caching)
repoMetadata artifact.RepoMetadata // git repository metadata
}
func NewArtifact(rootPath string, c cache.ArtifactCache, w Walker, opt artifact.Option) (artifact.Artifact, error) {
handlerManager, err := handler.NewManager(opt)
if err != nil {
return nil, xerrors.Errorf("handler initialize error: %w", err)
}
a, err := analyzer.NewAnalyzerGroup(opt.AnalyzerOptions())
if err != nil {
return nil, xerrors.Errorf("analyzer group error: %w", err)
}
opt.Type = cmp.Or(opt.Type, types.TypeFilesystem)
prefix := lo.Ternary(opt.Type == types.TypeRepository, "repo", "fs")
art := Artifact{
rootPath: filepath.ToSlash(filepath.Clean(rootPath)),
logger: log.WithPrefix(prefix),
cache: c,
walker: w,
analyzer: a,
handlerManager: handlerManager,
artifactOption: opt,
}
art.logger.Debug("Analyzing...", log.String("root", art.rootPath),
lo.Ternary(opt.Original != "", log.String("original", opt.Original), log.Nil))
// Check if the directory is a git repository and extract metadata
if art.isClean, art.repoMetadata, err = extractGitInfo(art.rootPath); err == nil {
// If git info is detected, change artifact type to repository
art.artifactOption.Type = types.TypeRepository
if art.isClean {
art.logger.Debug("Using the latest commit hash for calculating cache key",
log.String("commit_hash", art.repoMetadata.Commit))
} else {
art.logger.Debug("Repository is dirty, random cache key will be used")
}
} else if !errors.Is(err, git.ErrRepositoryNotExists) {
// Only log if the file path is a git repository
art.logger.Debug("Random cache key will be used", log.Err(err))
}
return art, nil
}
// extractGitInfo extracts git repository information including clean status and metadata
// Returns clean status (for caching), metadata, and error
func extractGitInfo(dir string) (bool, artifact.RepoMetadata, error) {
var metadata artifact.RepoMetadata
repo, err := git.PlainOpen(dir)
if err != nil {
return false, metadata, xerrors.Errorf("failed to open git repository: %w", err)
}
// Get HEAD commit
head, err := repo.Head()
if err != nil {
return false, metadata, xerrors.Errorf("failed to get HEAD: %w", err)
}
commit, err := repo.CommitObject(head.Hash())
if err != nil {
return false, metadata, xerrors.Errorf("failed to get commit object: %w", err)
}
// Extract basic commit metadata
metadata.Commit = head.Hash().String()
metadata.CommitMsg = strings.TrimSpace(commit.Message)
metadata.Author = commit.Author.String()
metadata.Committer = commit.Committer.String()
// Get branch name
if head.Name().IsBranch() {
metadata.Branch = head.Name().Short()
}
// Get all tag names that point to HEAD
if tags, err := repo.Tags(); err == nil {
var headTags []string
_ = tags.ForEach(func(tag *plumbing.Reference) error {
if tag.Hash() == head.Hash() {
headTags = append(headTags, tag.Name().Short())
}
return nil
})
metadata.Tags = headTags
}
// Get repository URL - prefer upstream, fallback to origin
remoteConfig, err := repo.Remote("upstream")
if err != nil {
remoteConfig, err = repo.Remote("origin")
}
if err == nil && len(remoteConfig.Config().URLs) > 0 {
metadata.RepoURL = sanitizeRemoteURL(remoteConfig.Config().URLs[0])
}
// Check if repository is clean for caching purposes
worktree, err := repo.Worktree()
if err != nil {
return false, metadata, xerrors.Errorf("failed to get worktree: %w", err)
}
status, err := worktree.Status()
if err != nil {
return false, metadata, xerrors.Errorf("failed to get status: %w", err)
}
// Return clean status and metadata
return status.IsClean(), metadata, nil
}
func (a Artifact) Inspect(ctx context.Context) (artifact.Reference, error) {
// Calculate cache key
cacheKey, err := a.calcCacheKey()
if err != nil {
return artifact.Reference{}, xerrors.Errorf("failed to calculate a cache key: %w", err)
}
// Check if the cache exists only when it's a clean git repository
if a.isClean && a.repoMetadata.Commit != "" {
_, missingBlobs, err := a.cache.MissingBlobs(ctx, cacheKey, []string{cacheKey})
if err != nil {
return artifact.Reference{}, xerrors.Errorf("unable to get missing blob: %w", err)
}
if len(missingBlobs) == 0 {
// Cache hit
a.logger.DebugContext(ctx, "Cache hit", log.String("key", cacheKey))
return artifact.Reference{
Name: cmp.Or(a.artifactOption.Original, a.rootPath),
Type: a.artifactOption.Type,
ID: cacheKey,
BlobIDs: []string{cacheKey},
RepoMetadata: a.repoMetadata,
}, nil
}
}
// `errgroup` cancels the context after Wait returns, so it can’t be use later.
// We need a separate context specifically for Analyze.
eg, egCtx := errgroup.WithContext(ctx)
result := analyzer.NewAnalysisResult()
limit := semaphore.New(a.artifactOption.Parallel)
opts := analyzer.AnalysisOptions{
Offline: a.artifactOption.Offline,
FileChecksum: a.artifactOption.FileChecksum,
}
// Prepare filesystem for post analysis
composite, err := a.analyzer.PostAnalyzerFS()
if err != nil {
return artifact.Reference{}, xerrors.Errorf("failed to prepare filesystem for post analysis: %w", err)
}
defer composite.Cleanup()
// Use static paths instead of traversing the filesystem when all analyzers implement StaticPathAnalyzer
// so that we can analyze files faster
var analyzeErr error
if paths, canUseStaticPaths := a.analyzer.StaticPaths(a.artifactOption.DisabledAnalyzers); canUseStaticPaths {
// Analyze files in static paths
a.logger.Debug("Analyzing files in static paths")
if err = a.analyzeWithStaticPaths(egCtx, eg, limit, result, composite, opts, paths); err != nil {
analyzeErr = xerrors.Errorf("analyze with static paths: %w", err)
}
} else {
// Analyze files by traversing the root directory
if err = a.analyzeWithRootDir(egCtx, eg, limit, result, composite, opts); err != nil {
analyzeErr = xerrors.Errorf("analyze with traversal: %w", err)
}
}
// errgroup cancels egCtx when an analysis goroutine fails, so the walk above can
// fail with context.Canceled and mask the real cause (e.g. a remote 429).
// Surface eg.Wait()'s error first; fall back to the walk error only when the group is clean.
if err = eg.Wait(); err != nil {
return artifact.Reference{}, xerrors.Errorf("analyze error: %w", err)
}
if analyzeErr != nil {
return artifact.Reference{}, analyzeErr
}
// Post-analysis
if err = a.analyzer.PostAnalyze(ctx, composite, result, opts); err != nil {
return artifact.Reference{}, xerrors.Errorf("post analysis error: %w", err)
}
// Sort the analysis result for consistent results
result.Sort()
blobInfo := types.BlobInfo{
SchemaVersion: types.BlobJSONSchemaVersion,
OS: result.OS,
Repository: result.Repository,
PackageInfos: result.PackageInfos,
Applications: result.Applications,
Misconfigurations: result.Misconfigurations,
Secrets: result.Secrets,
Licenses: result.Licenses,
CustomResources: result.CustomResources,
// For Red Hat
BuildInfo: result.BuildInfo,
}
if err = a.handlerManager.PostHandle(ctx, result, &blobInfo); err != nil {
return artifact.Reference{}, xerrors.Errorf("failed to call hooks: %w", err)
}
if err = a.cache.PutBlob(ctx, cacheKey, blobInfo); err != nil {
return artifact.Reference{}, xerrors.Errorf("failed to store blob (%s) in cache: %w", cacheKey, err)
}
// get hostname
var hostName string
b, err := os.ReadFile(filepath.Join(a.rootPath, "etc", "hostname"))
if err == nil && len(b) != 0 {
hostName = strings.TrimSpace(string(b))
} else {
target := cmp.Or(a.artifactOption.Original, a.rootPath)
hostName = filepath.ToSlash(target) // To slash for Windows
}
return artifact.Reference{
Name: hostName,
Type: a.artifactOption.Type,
ID: cacheKey, // use a cache key as pseudo artifact ID
BlobIDs: []string{cacheKey},
RepoMetadata: a.repoMetadata,
}, nil
}
func (a Artifact) analyzeWithRootDir(ctx context.Context, eg *errgroup.Group, limit *semaphore.Weighted,
result *analyzer.AnalysisResult, composite *analyzer.CompositeFS, opts analyzer.AnalysisOptions) error {
root := a.rootPath
relativePath := ""
// When the root path is a file, rewrite the root path and relative path
if fsutils.FileExists(a.rootPath) {
root, relativePath = path.Split(a.rootPath)
}
return a.analyzeWithTraversal(ctx, root, relativePath, eg, limit, result, composite, opts)
}
// analyzeWithStaticPaths analyzes files using static paths from analyzers
func (a Artifact) analyzeWithStaticPaths(ctx context.Context, eg *errgroup.Group, limit *semaphore.Weighted,
result *analyzer.AnalysisResult, composite *analyzer.CompositeFS, opts analyzer.AnalysisOptions,
staticPaths []string) error {
// Process each static path
for _, relativePath := range staticPaths {
if err := a.analyzeWithTraversal(ctx, a.rootPath, relativePath, eg, limit, result, composite, opts); errors.Is(err, fs.ErrNotExist) {
continue
} else if err != nil {
return xerrors.Errorf("analyze with traversal: %w", err)
}
}
return nil
}
// analyzeWithTraversal analyzes files by traversing the entire filesystem
func (a Artifact) analyzeWithTraversal(ctx context.Context, root, relativePath string, eg *errgroup.Group, limit *semaphore.Weighted,
result *analyzer.AnalysisResult, composite *analyzer.CompositeFS, opts analyzer.AnalysisOptions) error {
return a.walker.Walk(filepath.Join(root, relativePath), a.artifactOption.WalkerOption, func(filePath string, info os.FileInfo, opener analyzer.Opener) error {
filePath = path.Join(relativePath, filePath)
if err := a.analyzer.AnalyzeFile(ctx, eg, limit, result, root, filePath, info, opener, nil, opts); err != nil {
return xerrors.Errorf("analyze file (%s): %w", filePath, err)
}
// Skip post analysis if the file is not required
analyzerTypes := a.analyzer.RequiredPostAnalyzers(filePath, info)
if len(analyzerTypes) == 0 {
return nil
}
// Build filesystem for post analysis
if err := composite.CreateLink(analyzerTypes, root, filePath, filepath.Join(root, filePath)); err != nil {
return xerrors.Errorf("failed to create link: %w", err)
}
return nil
})
}
func (a Artifact) Clean(reference artifact.Reference) error {
// Don't delete cache if it's a clean git repository
if a.isClean && a.repoMetadata.Commit != "" {
return nil
}
return a.cache.DeleteBlobs(context.TODO(), reference.BlobIDs)
}
func (a Artifact) calcCacheKey() (string, error) {
// If this is a clean git repository, use the commit hash as cache key
if a.isClean && a.repoMetadata.Commit != "" {
return cache.CalcKey(a.repoMetadata.Commit, artifactVersion, a.analyzer.AnalyzerVersions(), a.handlerManager.Versions(), a.artifactOption)
}
// For non-git repositories or dirty git repositories, use UUID as cache key
h := sha256.New()
if _, err := h.Write([]byte(uuid.New().String())); err != nil {
return "", xerrors.Errorf("sha256 calculation error: %w", err)
}
// Format as sha256 digest
d := digest.NewDigest(digest.SHA256, h)
return d.String(), nil
}
// sanitizeRemoteURL removes credentials (userinfo) from URLs.
func sanitizeRemoteURL(gitUrl string) string {
// Only attempt sanitization for URLs with an explicit scheme.
if !strings.Contains(gitUrl, "://") {
return gitUrl
}
// Try URL parsing first.
if u, err := url.Parse(gitUrl); err == nil {
// Clear userinfo (username:password)
u.User = nil
gitUrl = u.String()
}
return gitUrl
}