8000 Use latest data timestamp for DB timestamp by wagoodman · Pull Request #251 · anchore/grype-db · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use latest data timestamp for DB timestamp #251

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

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions cmd/grype-db/cli/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,55 @@ func runBuild(cfg buildConfig) error {
pvdrs = pvdrs.Filter(cfg.Provider.IncludeFilter...)
}

states, err := providerStates(cfg.SkipValidation, pvdrs)
if err != nil {
return fmt.Errorf("unable to get provider states: %w", err)
}

return process.Build(process.BuildConfig{
SchemaVersion: cfg.SchemaVersion,
Directory: cfg.Directory,
States: states,
Timestamp: latestTimestamp(states),
})
}

func providerStates(skipValidation bool, providers []provider.Provider) ([]provider.State, error) {
var states []provider.State
stateTimestamp := time.Now()
log.Debug("reading all provider state")
for _, p := range pvdrs {

if len(providers) == 0 {
return nil, fmt.Errorf("no providers configured")
}

for _, p := range providers {
log.WithFields("provider", p.ID().Name).Debug("reading state")

sd, err := p.State()
if err != nil {
return fmt.Errorf("unable to read provider state: %w", err)
return nil, fmt.Errorf("unable to read provider state: %w", err)
}

if !cfg.SkipValidation {
if !skipValidation {
log.WithFields("provider", p.ID().Name).Trace("validating state")
if err := sd.Verify(); err != nil {
return fmt.Errorf("invalid provider state: %w", err)
return nil, fmt.Errorf("invalid provider state: %w", err)
}
}

if sd.Timestamp.Before(stateTimestamp) {
stateTimestamp = sd.Timestamp
}
states = append(states, *sd)
}

if !cfg.SkipValidation {
if !skipValidation {
log.Debugf("state validated for all providers")
}
return states, nil
}

return process.Build(process.BuildConfig{
SchemaVersion: cfg.SchemaVersion,
Directory: cfg.Directory,
States: states,
Timestamp: stateTimestamp,
})
func latestTimestamp(states []provider.State) time.Time {
var latest time.Time
for _, s := range states {
if s.Timestamp.After(latest) {
latest = s.Timestamp
}
}
return latest
}
40 changes: 40 additions & 0 deletions cmd/grype-db/cli/commands/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package commands

import (
"reflect"
"testing"
"time"

"github.com/anchore/grype-db/pkg/provider"
)

func Test_latestTimestamp(t *testing.T) {
tests := []struct {
name string
states []provider.State
want time.Time
}{
{
name: "happy path",
states: []provider.State{
{
Timestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
},
{
Timestamp: time.Date(2021, 1, 3, 0, 0, 0, 0, time.UTC),
},
{
Timestamp: time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC),
},
},
want: time.Date(2021, 1, 3, 0, 0, 0, 0, time.UTC),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := latestTimestamp(tt.states); !reflect.DeepEqual(got, tt.want) {
t.Errorf("latestTimestamp() = %v, want %v", got, tt.want)
}
})
}
}
0