-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scan.go
199 lines (170 loc) · 5.34 KB
/
scan.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
package scanner
import (
"context"
"github.com/google/wire"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/cache"
"github.com/aquasecurity/trivy/pkg/clock"
"github.com/aquasecurity/trivy/pkg/fanal/artifact"
aimage "github.com/aquasecurity/trivy/pkg/fanal/artifact/image"
flocal "github.com/aquasecurity/trivy/pkg/fanal/artifact/local"
"github.com/aquasecurity/trivy/pkg/fanal/artifact/repo"
"github.com/aquasecurity/trivy/pkg/fanal/artifact/sbom"
"github.com/aquasecurity/trivy/pkg/fanal/artifact/vm"
"github.com/aquasecurity/trivy/pkg/fanal/image"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/report"
"github.com/aquasecurity/trivy/pkg/rpc/client"
"github.com/aquasecurity/trivy/pkg/scanner/local"
"github.com/aquasecurity/trivy/pkg/types"
)
///////////////
// Standalone
///////////////
// StandaloneSuperSet is used in the standalone mode
var StandaloneSuperSet = wire.NewSet(
// Cache
cache.New,
wire.Bind(new(cache.ArtifactCache), new(cache.Cache)),
wire.Bind(new(cache.LocalArtifactCache), new(cache.Cache)),
local.SuperSet,
wire.Bind(new(Driver), new(local.Scanner)),
NewScanner,
)
// StandaloneDockerSet binds docker dependencies
var StandaloneDockerSet = wire.NewSet(
image.NewContainerImage,
aimage.NewArtifact,
StandaloneSuperSet,
)
// StandaloneArchiveSet binds archive scan dependencies
var StandaloneArchiveSet = wire.NewSet(
image.NewArchiveImage,
aimage.NewArtifact,
StandaloneSuperSet,
)
// StandaloneFilesystemSet binds filesystem dependencies
var StandaloneFilesystemSet = wire.NewSet(
flocal.ArtifactSet,
StandaloneSuperSet,
)
// StandaloneRepositorySet binds repository dependencies
var StandaloneRepositorySet = wire.NewSet(
repo.ArtifactSet,
StandaloneSuperSet,
)
// StandaloneSBOMSet binds sbom dependencies
var StandaloneSBOMSet = wire.NewSet(
sbom.NewArtifact,
StandaloneSuperSet,
)
// StandaloneVMSet binds vm dependencies
var StandaloneVMSet = wire.NewSet(
vm.ArtifactSet,
StandaloneSuperSet,
)
/////////////////
// Client/Server
/////////////////
// RemoteSuperSet is used in the client mode
var RemoteSuperSet = wire.NewSet(
// Cache
cache.NewRemoteCache,
wire.Bind(new(cache.ArtifactCache), new(*cache.RemoteCache)), // No need for LocalArtifactCache
client.NewScanner,
wire.Value([]client.Option(nil)),
wire.Bind(new(Driver), new(client.Scanner)),
NewScanner,
)
// RemoteFilesystemSet binds filesystem dependencies for client/server mode
var RemoteFilesystemSet = wire.NewSet(
flocal.ArtifactSet,
RemoteSuperSet,
)
// RemoteRepositorySet binds repository dependencies for client/server mode
var RemoteRepositorySet = wire.NewSet(
repo.ArtifactSet,
RemoteSuperSet,
)
// RemoteSBOMSet binds sbom dependencies for client/server mode
var RemoteSBOMSet = wire.NewSet(
sbom.NewArtifact,
RemoteSuperSet,
)
// RemoteVMSet binds vm dependencies for client/server mode
var RemoteVMSet = wire.NewSet(
vm.ArtifactSet,
RemoteSuperSet,
)
// RemoteDockerSet binds remote docker dependencies
var RemoteDockerSet = wire.NewSet(
aimage.NewArtifact,
image.NewContainerImage,
RemoteSuperSet,
)
// RemoteArchiveSet binds remote archive dependencies
var RemoteArchiveSet = wire.NewSet(
aimage.NewArtifact,
image.NewArchiveImage,
RemoteSuperSet,
)
// Scanner implements the Artifact and Driver operations
type Scanner struct {
driver Driver
artifact artifact.Artifact
}
// Driver defines operations of scanner
type Driver interface {
Scan(ctx context.Context, target, artifactKey string, blobKeys []string, options types.ScanOptions) (
results types.Results, osFound ftypes.OS, err error)
}
// NewScanner is the factory method of Scanner
func NewScanner(driver Driver, ar artifact.Artifact) Scanner {
return Scanner{
driver: driver,
artifact: ar,
}
}
// ScanArtifact scans the artifacts and returns results
func (s Scanner) ScanArtifact(ctx context.Context, options types.ScanOptions) (types.Report, error) {
artifactInfo, err := s.artifact.Inspect(ctx)
if err != nil {
return types.Report{}, xerrors.Errorf("failed analysis: %w", err)
}
defer func() {
if err := s.artifact.Clean(artifactInfo); err != nil {
log.Warn("Failed to clean the artifact",
log.String("artifact", artifactInfo.Name), log.Err(err))
}
}()
results, osFound, err := s.driver.Scan(ctx, artifactInfo.Name, artifactInfo.ID, artifactInfo.BlobIDs, options)
if err != nil {
return types.Report{}, xerrors.Errorf("scan failed: %w", err)
}
ptros := &osFound
if osFound.Detected() && osFound.Eosl {
log.Warn("This OS version is no longer supported by the distribution",
log.String("family", string(osFound.Family)), log.String("version", osFound.Name))
log.Warn("The vulnerability detection may be insufficient because security updates are not provided")
} else if !osFound.Detected() {
ptros = nil
}
return types.Report{
SchemaVersion: report.SchemaVersion,
CreatedAt: clock.Now(ctx),
ArtifactName: artifactInfo.Name,
ArtifactType: artifactInfo.Type,
Metadata: types.Metadata{
OS: ptros,
// Container image
ImageID: artifactInfo.ImageMetadata.ID,
DiffIDs: artifactInfo.ImageMetadata.DiffIDs,
RepoTags: artifactInfo.ImageMetadata.RepoTags,
RepoDigests: artifactInfo.ImageMetadata.RepoDigests,
ImageConfig: artifactInfo.ImageMetadata.ConfigFile,
},
Results: results,
BOM: artifactInfo.BOM,
}, nil
}