Skip to content

Commit bb3cca6

Browse files
authored
fix(db): fix case when 2 trivy-db were copied at the same time (#8452)
1 parent a99498c commit bb3cca6

File tree

9 files changed

+80
-36
lines changed

9 files changed

+80
-36
lines changed

go.mod

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/aquasecurity/testdocker v0.0.0-20240730042311-4642e94c7fc8
2626
github.com/aquasecurity/tml v0.6.1
2727
github.com/aquasecurity/trivy-checks v1.6.1
28-
github.com/aquasecurity/trivy-db v0.0.0-20241209111357-8c398f13db0e
28+
github.com/aquasecurity/trivy-db v0.0.0-20250227071930-8bd8a9b89e2d
2929
github.com/aquasecurity/trivy-java-db v0.0.0-20240109071736-184bd7481d48
3030
github.com/aquasecurity/trivy-kubernetes v0.7.0
3131
github.com/aws/aws-sdk-go-v2 v1.34.0
@@ -80,7 +80,7 @@ require (
8080
github.com/masahiro331/go-disk v0.0.0-20240625071113-56c933208fee
8181
github.com/masahiro331/go-ebs-file v0.0.0-20240917043618-e6d2bea5c32e
8282
github.com/masahiro331/go-ext4-filesystem v0.0.0-20240620024024-ca14e6327bbd
83-
github.com/masahiro331/go-mvn-version v0.0.0-20210429150710-d3157d602a08
83+
github.com/masahiro331/go-mvn-version v0.0.0-20250131095131-f4974fa13b8a
8484
github.com/masahiro331/go-vmdk-parser v0.0.0-20221225061455-612096e4bbbd
8585
github.com/masahiro331/go-xfs-filesystem v0.0.0-20231205045356-1b22259a6c44
8686
github.com/mattn/go-shellwords v1.0.12
@@ -437,4 +437,8 @@ require (
437437
github.com/aws/aws-sdk-go-v2/service/sso v1.24.12 // indirect
438438
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.11 // indirect
439439
github.com/aws/aws-sdk-go-v2/service/sts v1.33.10 // indirect
440+
github.com/oklog/ulid/v2 v2.1.0 // indirect
441+
github.com/samber/oops v1.15.0 // indirect
440442
)
443+
444+
replace github.com/aquasecurity/trivy-db => github.com/DmitriyLewen/trivy-db v0.0.0-20250225102547-67e9b4dfc612

go.sum

+9-4
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl
685685
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
686686
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
687687
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
688+
github.com/DmitriyLewen/trivy-db v0.0.0-20250225102547-67e9b4dfc612 h1:T/H7M/RT9eSesHJdVVDQybAkTYNKI5RjyIeWpa2jK8o=
689+
github.com/DmitriyLewen/trivy-db v0.0.0-20250225102547-67e9b4dfc612/go.mod h1:4bTsQPtMBN8v+UfUlE1aQBN1imftefnDafHBF85+aT8=
688690
github.com/GoogleCloudPlatform/docker-credential-gcr v2.0.5+incompatible h1:juIaKLLVhqzP55d8x4cSVgwyQv76Z55/fRv/UBr2KkQ=
689691
github.com/GoogleCloudPlatform/docker-credential-gcr v2.0.5+incompatible/go.mod h1:BB1eHdMLYEFuFdBlRMb0N7YGVdM5s6Pt0njxgvfbGGs=
690692
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 h1:3c8yed4lgqTt+oTQ+JNMDo+F4xprBf+O/il4ZC0nRLw=
@@ -805,8 +807,6 @@ github.com/aquasecurity/tml v0.6.1 h1:y2ZlGSfrhnn7t4ZJ/0rotuH+v5Jgv6BDDO5jB6A9gw
805807
github.com/aquasecurity/tml v0.6.1/go.mod h1:OnYMWY5lvI9ejU7yH9LCberWaaTBW7hBFsITiIMY2yY=
806808
github.com/aquasecurity/trivy-checks v1.6.1 h1:ANxKl+c9/k3Uk0YNQwpFBx++CG9Goi5T0YeN7Qimmf4=
807809
github.com/aquasecurity/trivy-checks v1.6.1/go.mod h1:xjHg4ivIIIFD7FFNpGrqxi1pRgAW1EXeG4VlkGiymjI=
808-
github.com/aquasecurity/trivy-db v0.0.0-20241209111357-8c398f13db0e h1:O5j5SeCNBrXApgBTOobO06q4LMxJxIhcSGE7H6Y154E=
809-
github.com/aquasecurity/trivy-db v0.0.0-20241209111357-8c398f13db0e/go.mod h1:gS8VhlNxhraiq60BBnJw9kGtjeMspQ9E8pX24jCL4jg=
810810
github.com/aquasecurity/trivy-java-db v0.0.0-20240109071736-184bd7481d48 h1:JVgBIuIYbwG+ekC5lUHUpGJboPYiCcxiz06RCtz8neI=
811811
github.com/aquasecurity/trivy-java-db v0.0.0-20240109071736-184bd7481d48/go.mod h1:Ldya37FLi0e/5Cjq2T5Bty7cFkzUDwTcPeQua+2M8i8=
812812
github.com/aquasecurity/trivy-kubernetes v0.7.0 h1:0pRJFSslUYd9xzQIEw1c0mS7k1Vv489nH/LsxeU6yME=
@@ -1534,8 +1534,8 @@ github.com/masahiro331/go-ebs-file v0.0.0-20240917043618-e6d2bea5c32e h1:nCgF1JE
15341534
github.com/masahiro331/go-ebs-file v0.0.0-20240917043618-e6d2bea5c32e/go.mod h1:XFWPTlAcEL733RUjbr0QBybdt6oK2DH7LZk8id2qtd4=
15351535
github.com/masahiro331/go-ext4-filesystem v0.0.0-20240620024024-ca14e6327bbd h1:JEIW94K3spsvBI5Xb9PGhKSIza9/jxO1lF30tPCAJlA=
15361536
github.com/masahiro331/go-ext4-filesystem v0.0.0-20240620024024-ca14e6327bbd/go.mod h1:3XMMY1M486mWGTD13WPItg6FsgflQR72ZMAkd+gsyoQ=
1537-
github.com/masahiro331/go-mvn-version v0.0.0-20210429150710-d3157d602a08 h1:AevUBW4cc99rAF8q8vmddIP8qd/0J5s/UyltGbp66dg=
1538-
github.com/masahiro331/go-mvn-version v0.0.0-20210429150710-d3157d602a08/go.mod h1:JOkBRrE1HvgTyjk6diFtNGgr8XJMtIfiBzkL5krqzVk=
1537+
github.com/masahiro331/go-mvn-version v0.0.0-20250131095131-f4974fa13b8a h1:eLvAzVoRfHEOl64OxFhepPf3vj7SKvXY/tFc3BS0b7s=
1538+
github.com/masahiro331/go-mvn-version v0.0.0-20250131095131-f4974fa13b8a/go.mod h1:jZ3F25l7DbD7l7DcA8aj7eo1EZ84nbzcQHBB4lCSrI8=
15391539
github.com/masahiro331/go-vmdk-parser v0.0.0-20221225061455-612096e4bbbd h1:Y30EzvuoVp97b0unb/GOFXzBUKRXZXUN2e0wYmvC+ic=
15401540
github.com/masahiro331/go-vmdk-parser v0.0.0-20221225061455-612096e4bbbd/go.mod h1:5f7mCJGW9cJb8SDn3z8qodGxpMCOo8d/2nls/tiwRrw=
15411541
github.com/masahiro331/go-xfs-filesystem v0.0.0-20231205045356-1b22259a6c44 h1:VmSjn0UCyfXUNdePDr7uM/uZTnGSp+mKD5+cYkEoLx4=
@@ -1635,6 +1635,8 @@ github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
16351635
github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
16361636
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
16371637
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
1638+
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
1639+
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
16381640
github.com/oleiade/reflections v1.0.1 h1:D1XO3LVEYroYskEsoSiGItp9RUxG6jWnCVvrqH0HHQM=
16391641
github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60=
16401642
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -1678,6 +1680,7 @@ github.com/owenrumney/squealer v1.2.10 h1:Yxxy30sOhaK8/FeneHklV0sA6DP4UjUpky2opj
16781680
github.com/owenrumney/squealer v1.2.10/go.mod h1:V72uafpqPERMaJ/pA1MwK/dI0QRzLHCLnh6MqYmjFzY=
16791681
github.com/package-url/packageurl-go v0.1.3 h1:4juMED3hHiz0set3Vq3KeQ75KD1avthoXLtmE3I0PLs=
16801682
github.com/package-url/packageurl-go v0.1.3/go.mod h1:nKAWB8E6uk1MHqiS/lQb9pYBGH2+mdJ2PJc2s50dQY0=
1683+
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
16811684
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
16821685
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
16831686
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
@@ -1769,6 +1772,8 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g
17691772
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
17701773
github.com/samber/lo v1.49.1 h1:4BIFyVfuQSEpluc7Fua+j1NolZHiEHEpaSEKdsH0tew=
17711774
github.com/samber/lo v1.49.1/go.mod h1:dO6KHFzUKXgP8LDhU0oI8d2hekjXnGOu0DB8Jecxd6o=
1775+
github.com/samber/oops v1.15.0 h1:/mF33KAqA2TugU6y/tomFpK6G6mJB7g0aqRyHkaSIeg=
1776+
github.com/samber/oops v1.15.0/go.mod h1:9LpLZkpjojEt/of7EpG5o65i/Lp23ddDvGhg2L871Ow=
17721777
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
17731778
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
17741779
github.com/sassoftware/go-rpmutils v0.4.0 h1:ojND82NYBxgwrV+mX1CWsd5QJvvEZTKddtCdFLPWhpg=

pkg/db/db.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package db
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -96,16 +97,24 @@ func NewClient(dbDir string, quiet bool, opts ...Option) *Client {
9697

9798
// NeedsUpdate check is DB needs update
9899
func (c *Client) NeedsUpdate(ctx context.Context, cliVersion string, skip bool) (bool, error) {
100+
var noRequiredFiles bool
101+
if _, err := os.Stat(db.Path(c.dbDir)); errors.Is(err, os.ErrNotExist) {
102+
log.DebugContext(ctx, "There is no db file")
103+
noRequiredFiles = true
104+
}
99105
meta, err := c.metadata.Get()
100106
if err != nil {
101107
log.DebugContext(ctx, "There is no valid metadata file", log.Err(err))
102-
if skip {
103-
log.ErrorContext(ctx, "The first run cannot skip downloading DB")
104-
return false, xerrors.New("--skip-update cannot be specified on the first run")
105-
}
108+
noRequiredFiles = true
109+
106110
meta = metadata.Metadata{Version: db.SchemaVersion}
107111
}
108112

113+
if skip && noRequiredFiles {
114+
log.ErrorContext(ctx, "The first run cannot skip downloading DB")
115+
return false, xerrors.New("--skip-update cannot be specified on the first run")
116+
}
117+
109118
if db.SchemaVersion < meta.Version {
110119
log.ErrorContext(ctx, "Trivy version is old. Update to the latest version.", log.String("version", cliVersion))
111120
return false, xerrors.Errorf("the version of DB schema doesn't match. Local DB: %d, Expected: %d",

pkg/db/db_test.go

+47-21
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,45 @@ func TestClient_NeedsUpdate(t *testing.T) {
2121
timeNextUpdateDay2 := time.Date(2019, 10, 2, 0, 0, 0, 0, time.UTC)
2222

2323
tests := []struct {
24-
name string
25-
skip bool
26-
metadata metadata.Metadata
27-
want bool
28-
wantErr string
24+
name string
25+
skip bool
26+
dbFileExists bool
27+
metadata metadata.Metadata
28+
want bool
29+
wantErr string
2930
}{
3031
{
31-
name: "happy path",
32+
name: "happy path",
33+
dbFileExists: true,
3234
metadata: metadata.Metadata{
3335
Version: db.SchemaVersion,
3436
NextUpdate: timeNextUpdateDay1,
3537
},
3638
want: true,
3739
},
3840
{
39-
name: "happy path for first run",
40-
metadata: metadata.Metadata{},
41-
want: true,
41+
name: "happy path for first run",
42+
dbFileExists: true,
43+
metadata: metadata.Metadata{},
44+
want: true,
4245
},
4346
{
44-
name: "happy path with old schema version",
47+
name: "happy path for first run without trivy.db",
48+
dbFileExists: false,
49+
want: true,
50+
},
51+
{
52+
name: "happy path with old schema version",
53+
dbFileExists: true,
4554
metadata: metadata.Metadata{
4655
Version: 0,
4756
NextUpdate: timeNextUpdateDay1,
4857
},
4958
want: true,
5059
},
5160
{
52-
name: "happy path with --skip-update",
61+
name: "happy path with --skip-update",
62+
dbFileExists: true,
5363
metadata: metadata.Metadata{
5464
Version: db.SchemaVersion,
5565
NextUpdate: timeNextUpdateDay1,
@@ -58,15 +68,17 @@ func TestClient_NeedsUpdate(t *testing.T) {
5868
want: false,
5969
},
6070
{
61-
name: "skip downloading DB",
71+
name: "skip downloading DB",
72+
dbFileExists: true,
6273
metadata: metadata.Metadata{
6374
Version: db.SchemaVersion,
6475
NextUpdate: timeNextUpdateDay2,
6576
},
6677
want: false,
6778
},
6879
{
69-
name: "newer schema version",
80+
name: "newer schema version",
81+
dbFileExists: true,
7082
metadata: metadata.Metadata{
7183
Version: db.SchemaVersion + 1,
7284
NextUpdate: timeNextUpdateDay2,
@@ -75,13 +87,21 @@ func TestClient_NeedsUpdate(t *testing.T) {
7587
db.SchemaVersion+1, db.SchemaVersion),
7688
},
7789
{
78-
name: "--skip-update on the first run",
79-
metadata: metadata.Metadata{},
80-
skip: true,
81-
wantErr: "--skip-update cannot be specified on the first run",
90+
name: "--skip-update without trivy.db on the first run",
91+
dbFileExists: false,
92+
skip: true,
93+
wantErr: "--skip-update cannot be specified on the first run",
94+
},
95+
{
96+
name: "--skip-update without metadata.json on the first run",
97+
dbFileExists: true,
98+
metadata: metadata.Metadata{},
99+
skip: true,
100+
wantErr: "--skip-update cannot be specified on the first run",
82101
},
83102
{
84-
name: "--skip-update with different schema version",
103+
name: "--skip-update with different schema version",
104+
dbFileExists: true,
85105
metadata: metadata.Metadata{
86106
Version: 0,
87107
NextUpdate: timeNextUpdateDay1,
@@ -91,7 +111,8 @@ func TestClient_NeedsUpdate(t *testing.T) {
91111
0, db.SchemaVersion),
92112
},
93113
{
94-
name: "happy with old DownloadedAt",
114+
name: "happy with old DownloadedAt",
115+
dbFileExists: true,
95116
metadata: metadata.Metadata{
96117
Version: db.SchemaVersion,
97118
NextUpdate: timeNextUpdateDay1,
@@ -100,7 +121,8 @@ func TestClient_NeedsUpdate(t *testing.T) {
100121
want: true,
101122
},
102123
{
103-
name: "skip downloading DB with recent DownloadedAt",
124+
name: "skip downloading DB with recent DownloadedAt",
125+
dbFileExists: true,
104126
metadata: metadata.Metadata{
105127
Version: db.SchemaVersion,
106128
NextUpdate: timeNextUpdateDay1,
@@ -119,6 +141,11 @@ func TestClient_NeedsUpdate(t *testing.T) {
119141
require.NoError(t, err)
120142
}
121143

144+
if tt.dbFileExists {
145+
err := db.Init(dbDir)
146+
require.NoError(t, err)
147+
}
148+
122149
// Set a fake time
123150
ctx := clock.With(context.Background(), time.Date(2019, 10, 1, 0, 0, 0, 0, time.UTC))
124151

@@ -138,7 +165,6 @@ func TestClient_NeedsUpdate(t *testing.T) {
138165
}
139166

140167
func TestClient_Download(t *testing.T) {
141-
142168
tests := []struct {
143169
name string
144170
input string

pkg/detector/library/driver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestDriver_Detect(t *testing.T) {
155155
pkgName: "symfony/symfony",
156156
pkgVer: "5.1.5",
157157
},
158-
wantErr: "failed to unmarshal advisory JSON",
158+
wantErr: "json unmarshal error",
159159
},
160160
{
161161
name: "duplicated version in advisory",

pkg/detector/ospkg/debian/debian_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestScanner_Detect(t *testing.T) {
107107
},
108108
},
109109
},
110-
wantErr: "failed to unmarshal advisory JSON",
110+
wantErr: "failed to get debian advisories",
111111
},
112112
}
113113
for _, tt := range tests {

pkg/detector/ospkg/oracle/oracle_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func TestScanner_Detect(t *testing.T) {
331331
},
332332
},
333333
},
334-
wantErr: "failed to unmarshal advisory JSON",
334+
wantErr: "failed to get Oracle Linux advisory",
335335
},
336336
}
337337

pkg/detector/ospkg/photon/photon_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestScanner_Detect(t *testing.T) {
8686
},
8787
},
8888
},
89-
wantErr: "failed to get Photon advisories",
89+
wantErr: "failed to get Photon Linux advisory",
9090
},
9191
}
9292
for _, tt := range tests {

pkg/detector/ospkg/suse/suse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func TestScanner_Detect(t *testing.T) {
209209
},
210210
},
211211
},
212-
wantErr: "failed to get SUSE advisories",
212+
wantErr: "failed to get SUSE advisory",
213213
},
214214
}
215215
for _, tt := range tests {

0 commit comments

Comments
 (0)