From 6af6fed0548454ba3f031b17f404ce2540a73954 Mon Sep 17 00:00:00 2001 From: Arvind Somya Date: Thu, 18 Apr 2024 16:13:11 -0400 Subject: [PATCH] feat: add last successful run for each provider to the metadata adding the last successful run timestamp for all providers to the grype-db metadata.json file Signed-off-by: Arvind Somya --- go.mod | 2 +- go.sum | 4 ++-- pkg/process/build.go | 6 +++--- pkg/process/v5/writer.go | 14 +++++++++++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 0de6bc06..7ddf1ece 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/adrg/xdg v0.4.0 github.com/anchore/go-logger v0.0.0-20230725134548-c21dafa1ec5a - github.com/anchore/grype v0.76.0 + github.com/anchore/grype v0.76.1-0.20240418152742-4584423321a4 github.com/anchore/syft v1.2.0 github.com/dustin/go-humanize v1.0.1 github.com/glebarez/sqlite v1.11.0 diff --git a/go.sum b/go.sum index d2901198..06b73a95 100644 --- a/go.sum +++ b/go.sum @@ -251,8 +251,8 @@ github.com/anchore/go-testutils v0.0.0-20200925183923-d5f45b0d3c04 h1:VzprUTpc0v github.com/anchore/go-testutils v0.0.0-20200925183923-d5f45b0d3c04/go.mod h1:6dK64g27Qi1qGQZ67gFmBFvEHScy0/C8qhQhNe5B5pQ= github.com/anchore/go-version v1.2.2-0.20210903204242-51efa5b487c4 h1:rmZG77uXgE+o2gozGEBoUMpX27lsku+xrMwlmBZJtbg= github.com/anchore/go-version v1.2.2-0.20210903204242-51efa5b487c4/go.mod h1:Bkc+JYWjMCF8OyZ340IMSIi2Ebf3uwByOk6ho4wne1E= -github.com/anchore/grype v0.76.0 h1:MbPZKleMFaIjP0A2rCacFxUHUqgLYomF2nxPdZujAmY= -github.com/anchore/grype v0.76.0/go.mod h1:k6QLcebOqPm+90y8mMesOJM6A6DYQllOic6Tmz507sc= +github.com/anchore/grype v0.76.1-0.20240418152742-4584423321a4 h1:z15ruIAluKza6TaPNjMIV4Uly/wmOoLb+lhwQP4tD9Y= +github.com/anchore/grype v0.76.1-0.20240418152742-4584423321a4/go.mod h1:k6QLcebOqPm+90y8mMesOJM6A6DYQllOic6Tmz507sc= github.com/anchore/packageurl-go v0.1.1-0.20240312213626-055233e539b4 h1:SjemQ90fgflz39HG+VMkNfrpUVJpcFW6ZFA3TDXqzBM= github.com/anchore/packageurl-go v0.1.1-0.20240312213626-055233e539b4/go.mod h1:Blo6OgJNiYF41ufcgHKkbCKF2MDOMlrqhXv/ij6ocR4= github.com/anchore/stereoscope v0.0.2-0.20240229175558-fe426d1b1c84 h1:/E74wU51M87fX5UWHubLZiENXbuAci+xtbSb+JFsIYg= diff --git a/pkg/process/build.go b/pkg/process/build.go index 39acf874..16254505 100644 --- a/pkg/process/build.go +++ b/pkg/process/build.go @@ -43,7 +43,7 @@ func Build(cfg BuildConfig) error { return err } - writer, err := getWriter(cfg.SchemaVersion, cfg.Timestamp, cfg.Directory) + writer, err := getWriter(cfg.SchemaVersion, cfg.Timestamp, cfg.Directory, cfg.States) if err != nil { return err } @@ -106,7 +106,7 @@ func getProcessors(schemaVersion int) ([]data.Processor, error) { } } -func getWriter(schemaVersion int, dataAge time.Time, directory string) (data.Writer, error) { +func getWriter(schemaVersion int, dataAge time.Time, directory string, states provider.States) (data.Writer, error) { switch schemaVersion { case grypeDBv1.SchemaVersion: return v1.NewWriter(directory, dataAge) @@ -117,7 +117,7 @@ func getWriter(schemaVersion int, dataAge time.Time, directory string) (data.Wri case grypeDBv4.SchemaVersion: return v4.NewWriter(directory, dataAge) case grypeDBv5.SchemaVersion: - return v5.NewWriter(directory, dataAge) + return v5.NewWriter(directory, dataAge, states) default: return nil, fmt.Errorf("unable to create writer: unsupported schema version: %+v", schemaVersion) } diff --git a/pkg/process/v5/writer.go b/pkg/process/v5/writer.go index 2ec89bbe..dfd6f6e2 100644 --- a/pkg/process/v5/writer.go +++ b/pkg/process/v5/writer.go @@ -13,6 +13,7 @@ import ( "github.com/anchore/grype-db/internal/file" "github.com/anchore/grype-db/internal/log" "github.com/anchore/grype-db/pkg/data" + "github.com/anchore/grype-db/pkg/provider" "github.com/anchore/grype/grype/db" grypeDB "github.com/anchore/grype/grype/db/v5" grypeDBStore "github.com/anchore/grype/grype/db/v5/store" @@ -26,9 +27,10 @@ var _ data.Writer = (*writer)(nil) type writer struct { dbPath string store grypeDB.Store + states provider.States } -func NewWriter(directory string, dataAge time.Time) (data.Writer, error) { +func NewWriter(directory string, dataAge time.Time, states provider.States) (data.Writer, error) { dbPath := path.Join(directory, grypeDB.VulnerabilityStoreFileName) theStore, err := grypeDBStore.New(dbPath, true) if err != nil { @@ -42,6 +44,7 @@ func NewWriter(directory string, dataAge time.Time) (data.Writer, error) { return &writer{ dbPath: dbPath, store: theStore, + states: states, }, nil } @@ -90,6 +93,15 @@ func (w writer) metadata() (*db.Metadata, error) { Version: storeID.SchemaVersion, Checksum: "sha256:" + hashStr, } + + // Set provider time from states + for _, state := range w.states { + metadata.Providers = append(metadata.Providers, db.Provider{ + Name: state.Provider, + LastSuccessfulRun: state.Timestamp, + }) + } + return &metadata, nil }