Skip to content

Commit 0f4fc19

Browse files
tom1299DmitriyLewen
andcommitted
Fix NeedsUpdate if trivy.db is missing
Co-authored-by: DmitriyLewen <[email protected]>
1 parent 4f2a44e commit 0f4fc19

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

pkg/db/db.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,26 @@ func (c *Client) NeedsUpdate(ctx context.Context, cliVersion string, skip bool)
110110
meta = metadata.Metadata{Version: db.SchemaVersion}
111111
}
112112

113+
// We can't use the DB if either `trivy.db` or `metadata.json` is missing.
114+
// In that case, we need to download the DB.
115+
if noRequiredFiles {
116+
if skip {
117+
log.ErrorContext(ctx, "The first run cannot skip downloading DB")
118+
return false, xerrors.New("--skip-db-update cannot be specified on the first run")
119+
}
120+
return true, nil
121+
}
122+
113123
// There are 3 cases when DownloadAt field is zero:
114124
// - metadata file was not created yet. This is the first run of Trivy.
115125
// - trivy-db was downloaded with `oras`. In this case user can use `--skip-db-update` (like for air-gapped) or re-download trivy-db.
116126
// - trivy-db was corrupted while copying from tmp directory to cache directory. We should update this trivy-db.
117127
// We can't detect these cases, so we will show warning for users who use oras + air-gapped.
118-
if !noRequiredFiles && meta.DownloadedAt.IsZero() && !skip {
128+
if meta.DownloadedAt.IsZero() && !skip {
119129
log.WarnContext(ctx, "Trivy DB may be corrupted and will be re-downloaded. If you manually downloaded DB - use the `--skip-db-update` flag to skip updating DB.")
120130
return true, nil
121131
}
122132

123-
if skip && noRequiredFiles {
124-
log.ErrorContext(ctx, "The first run cannot skip downloading DB")
125-
return false, xerrors.New("--skip-db-update cannot be specified on the first run")
126-
}
127-
128133
if db.SchemaVersion < meta.Version {
129134
log.ErrorContext(ctx, "Trivy version is old. Update to the latest version.", log.String("version", cliVersion))
130135
return false, xerrors.Errorf("the version of DB schema doesn't match. Local DB: %d, Expected: %d",

pkg/db/db_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ func TestClient_NeedsUpdate(t *testing.T) {
219219
"The local DB has an old schema version which is not supported by the current version of Trivy CLI. DB needs to be updated.",
220220
},
221221
},
222+
{
223+
name: "trivy.db is missing but metadata with recent DownloadedAt",
224+
dbFileExists: false,
225+
metadata: metadata.Metadata{
226+
Version: db.SchemaVersion,
227+
NextUpdate: timeNextUpdateDay1,
228+
DownloadedAt: time.Date(2019, 9, 30, 23, 30, 0, 0, time.UTC),
229+
},
230+
want: true,
231+
wantLogs: []string{
232+
"There is no db file",
233+
},
234+
},
222235
}
223236

224237
for _, tt := range tests {

0 commit comments

Comments
 (0)