-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(db): Add dbRepository flag to get advisory database from OCI registry #1873
Changes from 3 commits
bd243bc
6b8cc3e
003173f
1dceb4f
cbe276a
f96fd19
ceaa7c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,10 +15,7 @@ import ( | |||||
"github.com/aquasecurity/trivy/pkg/log" | ||||||
) | ||||||
|
||||||
const ( | ||||||
dbRepository = "ghcr.io/aquasecurity/trivy-db" | ||||||
dbMediaType = "application/vnd.aquasec.trivy.db.layer.v1.tar+gzip" | ||||||
) | ||||||
const dbMediaType = "application/vnd.aquasec.trivy.db.layer.v1.tar+gzip" | ||||||
|
||||||
// Operation defines the DB operations | ||||||
type Operation interface { | ||||||
|
@@ -27,8 +24,9 @@ type Operation interface { | |||||
} | ||||||
|
||||||
type options struct { | ||||||
artifact *oci.Artifact | ||||||
clock clock.Clock | ||||||
artifact *oci.Artifact | ||||||
clock clock.Clock | ||||||
dbRepository string | ||||||
} | ||||||
|
||||||
// Option is a functional option | ||||||
|
@@ -41,6 +39,15 @@ func WithOCIArtifact(art *oci.Artifact) Option { | |||||
} | ||||||
} | ||||||
|
||||||
|
||||||
// WithDBRepository takes a dbRepository | ||||||
func WithDBRepository(dbRepository string) Option { | ||||||
knqyf263 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return func(opts *options) { | ||||||
opts.dbRepository = dbRepository | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
// WithClock takes a clock | ||||||
func WithClock(clock clock.Clock) Option { | ||||||
return func(opts *options) { | ||||||
|
@@ -138,7 +145,7 @@ func (c *Client) Download(ctx context.Context, dst string) error { | |||||
log.Logger.Debug("no metadata file") | ||||||
} | ||||||
|
||||||
if err := c.populateOCIArtifact(); err != nil { | ||||||
if err := c.populateOCIArtifact(c.dbRepository); err != nil { | ||||||
return xerrors.Errorf("OCI artifact error: %w", err) | ||||||
} | ||||||
|
||||||
|
@@ -171,7 +178,7 @@ func (c *Client) updateDownloadedAt(dst string) error { | |||||
return nil | ||||||
} | ||||||
|
||||||
func (c *Client) populateOCIArtifact() error { | ||||||
func (c *Client) populateOCIArtifact(dbRepository string) error { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have |
||||||
if c.artifact == nil { | ||||||
repo := fmt.Sprintf("%s:%d", dbRepository, db.SchemaVersion) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like
Suggested change
|
||||||
art, err := oci.NewArtifact(repo, dbMediaType, c.quiet) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,6 +197,7 @@ func TestClient_Download(t *testing.T) { | |
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cacheDir := t.TempDir() | ||
dbRepository := "ghcr.io/aquasecurity/trivy-db" | ||
|
||
// Mock image | ||
img := new(fakei.FakeImage) | ||
|
@@ -206,7 +207,7 @@ func TestClient_Download(t *testing.T) { | |
art, err := oci.NewArtifact("db", mediaType, true, oci.WithImage(img)) | ||
require.NoError(t, err) | ||
|
||
client := db.NewClient(cacheDir, true, db.WithOCIArtifact(art), db.WithClock(timeDownloadedAt)) | ||
client := db.NewClient(cacheDir, true, db.WithOCIArtifact(art), db.WithClock(timeDownloadedAt), db.WithDBRepository(dbRepository)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
err = client.Download(context.Background(), cacheDir) | ||
if tt.wantErr != "" { | ||
require.Error(t, err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
) | ||
|
||
const updateInterval = 1 * time.Hour | ||
const dbRepository = "ghcr.io/aquasecurity/trivy-db" | ||
|
||
// Server represents Trivy server | ||
type Server struct { | ||
|
@@ -50,7 +51,7 @@ func (s Server) ListenAndServe(serverCache cache.Cache) error { | |
dbUpdateWg := &sync.WaitGroup{} | ||
|
||
go func() { | ||
worker := newDBWorker(dbc.NewClient(s.cacheDir, true)) | ||
worker := newDBWorker(dbc.NewClient(s.cacheDir, true, dbc.WithDBRepository(dbRepository))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then, we don't have to pass the default value here. |
||
ctx := context.Background() | ||
for { | ||
time.Sleep(updateInterval) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I want to put it before
Downloading DB...
.