-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
db.go
269 lines (227 loc) · 7.58 KB
/
db.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
package db
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/samber/lo"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy-db/pkg/metadata"
"github.com/aquasecurity/trivy/pkg/clock"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/oci"
"github.com/aquasecurity/trivy/pkg/version/doc"
)
const (
SchemaVersion = db.SchemaVersion
dbMediaType = "application/vnd.aquasec.trivy.db.layer.v1.tar+gzip"
)
var (
// GitHub Container Registry
DefaultGHCRRepository = fmt.Sprintf("%s:%d", "ghcr.io/aquasecurity/trivy-db", db.SchemaVersion)
defaultGHCRRepository = lo.Must(name.NewTag(DefaultGHCRRepository))
// Amazon Elastic Container Registry
DefaultECRRepository = fmt.Sprintf("%s:%d", "public.ecr.aws/aquasecurity/trivy-db", db.SchemaVersion)
defaultECRRepository = lo.Must(name.NewTag(DefaultECRRepository))
Init = db.Init
Close = db.Close
Path = db.Path
)
type options struct {
artifact *oci.Artifact
dbRepositories []name.Reference
}
// Option is a functional option
type Option func(*options)
// WithOCIArtifact takes an OCI artifact
func WithOCIArtifact(art *oci.Artifact) Option {
return func(opts *options) {
opts.artifact = art
}
}
// WithDBRepository takes a dbRepository
func WithDBRepository(dbRepository []name.Reference) Option {
return func(opts *options) {
opts.dbRepositories = dbRepository
}
}
// Client implements DB operations
type Client struct {
*options
dbDir string
metadata metadata.Client
quiet bool
}
func Dir(cacheDir string) string {
return filepath.Join(cacheDir, "db")
}
// NewClient is the factory method for DB client
func NewClient(dbDir string, quiet bool, opts ...Option) *Client {
o := &options{
dbRepositories: []name.Reference{
defaultGHCRRepository,
defaultECRRepository,
},
}
for _, opt := range opts {
opt(o)
}
return &Client{
options: o,
dbDir: dbDir,
metadata: metadata.NewClient(dbDir),
quiet: quiet,
}
}
// NeedsUpdate check is DB needs update
func (c *Client) NeedsUpdate(ctx context.Context, cliVersion string, skip bool) (bool, error) {
meta, err := c.metadata.Get()
if err != nil {
log.Debug("There is no valid metadata file", log.Err(err))
if skip {
log.Error("The first run cannot skip downloading DB")
return false, xerrors.New("--skip-update cannot be specified on the first run")
}
meta = metadata.Metadata{Version: db.SchemaVersion}
}
if db.SchemaVersion < meta.Version {
log.Error("The Trivy version is old. Update to the latest version.", log.String("version", cliVersion))
return false, xerrors.Errorf("the version of DB schema doesn't match. Local DB: %d, Expected: %d",
meta.Version, db.SchemaVersion)
}
if skip {
log.Debug("Skipping DB update...")
if err = c.validate(meta); err != nil {
return false, xerrors.Errorf("validate error: %w", err)
}
return false, nil
}
if db.SchemaVersion != meta.Version {
log.Debug("The local DB schema version does not match with supported version schema.",
log.Int("local_version", meta.Version), log.Int("supported_version", db.SchemaVersion))
return true, nil
}
return !c.isNewDB(ctx, meta), nil
}
func (c *Client) validate(meta metadata.Metadata) error {
if db.SchemaVersion != meta.Version {
log.Error("The local DB has an old schema version which is not supported by the current version of Trivy CLI. DB needs to be updated.")
return xerrors.Errorf("--skip-update cannot be specified with the old DB schema. Local DB: %d, Expected: %d",
meta.Version, db.SchemaVersion)
}
return nil
}
func (c *Client) isNewDB(ctx context.Context, meta metadata.Metadata) bool {
now := clock.Now(ctx)
if now.Before(meta.NextUpdate) {
log.Debug("DB update was skipped because the local DB is the latest")
return true
}
if now.Before(meta.DownloadedAt.Add(time.Hour)) {
log.Debug("DB update was skipped because the local DB was downloaded during the last hour")
return true
}
return false
}
// Download downloads the DB file
func (c *Client) Download(ctx context.Context, dst string, opt types.RegistryOptions) error {
// Remove the metadata file under the cache directory before downloading DB
if err := c.metadata.Delete(); err != nil {
log.Debug("No metadata file")
}
if err := c.downloadDB(ctx, opt, dst); err != nil {
return xerrors.Errorf("OCI artifact error: %w", err)
}
if err := c.updateDownloadedAt(ctx, dst); err != nil {
return xerrors.Errorf("failed to update downloaded_at: %w", err)
}
return nil
}
func (c *Client) Clear(_ context.Context) error {
if err := os.RemoveAll(c.dbDir); err != nil {
return xerrors.Errorf("failed to remove vulnerability database: %w", err)
}
return nil
}
func (c *Client) updateDownloadedAt(ctx context.Context, dbDir string) error {
log.Debug("Updating database metadata...")
// We have to initialize a metadata client here
// since the destination may be different from the cache directory.
client := metadata.NewClient(dbDir)
meta, err := client.Get()
if err != nil {
return xerrors.Errorf("unable to get metadata: %w", err)
}
meta.DownloadedAt = clock.Now(ctx).UTC()
if err = client.Update(meta); err != nil {
return xerrors.Errorf("failed to update metadata: %w", err)
}
return nil
}
func (c *Client) initOCIArtifact(repository name.Reference, opt types.RegistryOptions) (*oci.Artifact, error) {
art, err := oci.NewArtifact(repository.String(), c.quiet, opt)
// TODO: NewArtifact never returns an error
if err != nil {
var terr *transport.Error
if errors.As(err, &terr) {
for _, diagnostic := range terr.Errors {
// For better user experience
if diagnostic.Code == transport.DeniedErrorCode || diagnostic.Code == transport.UnauthorizedErrorCode {
// e.g. https://aquasecurity.github.io/trivy/latest/docs/references/troubleshooting/#db
log.Warnf("See %s", doc.URL("/docs/references/troubleshooting/", "db"))
break
}
}
}
return nil, xerrors.Errorf("OCI artifact error: %w", err)
}
return art, nil
}
func (c *Client) initArtifacts(opt types.RegistryOptions) ([]*oci.Artifact, error) {
if c.artifact != nil {
return []*oci.Artifact{c.artifact}, nil
}
artifacts := make([]*oci.Artifact, 0, len(c.dbRepositories))
for _, repo := range c.dbRepositories {
a, err := c.initOCIArtifact(repo, opt)
if err != nil {
return nil, err
}
artifacts = append(artifacts, a)
}
return artifacts, nil
}
func (c *Client) downloadDB(ctx context.Context, opt types.RegistryOptions, dst string) error {
arts, err := c.initArtifacts(opt)
if err != nil {
return err
}
for i, art := range arts {
log.Info("Downloading vulnerability DB...", log.String("repo", art.Repository()))
if err := art.Download(ctx, dst, oci.DownloadOption{MediaType: dbMediaType}); err != nil {
log.Error("Failed to download DB", log.String("repo", art.Repository()), log.Err(err))
if i < len(c.dbRepositories)-1 {
log.Info("Trying to download DB from other repository...")
}
continue
}
log.Info("DB successfully downloaded", log.String("repo", art.Repository()))
return nil
}
return xerrors.New("failed to download vulnerability DB from any source")
}
func (c *Client) ShowInfo() error {
meta, err := c.metadata.Get()
if err != nil {
return xerrors.Errorf("something wrong with DB: %w", err)
}
log.Debug("DB info", log.Int("schema", meta.Version), log.Time("updated_at", meta.UpdatedAt),
log.Time("next_update", meta.NextUpdate), log.Time("downloaded_at", meta.DownloadedAt))
return nil
}