Skip to content

Commit

Permalink
feat: add last successful run for each provider to the metadata
Browse files Browse the repository at this point in the history
adding the last successful run timestamp for all providers to the grype-db metadata.json file

Signed-off-by: Arvind Somya <[email protected]>
  • Loading branch information
asomya committed Apr 19, 2024
1 parent 472e458 commit 6af6fed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions pkg/process/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/process/v5/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -42,6 +44,7 @@ func NewWriter(directory string, dataAge time.Time) (data.Writer, error) {
return &writer{
dbPath: dbPath,
store: theStore,
states: states,
}, nil
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 6af6fed

Please sign in to comment.