Skip to content
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

enrichers: Load enrichments from jsonblob storage #1166

Merged
merged 2 commits into from
Jan 11, 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
27 changes: 17 additions & 10 deletions libvuln/jsonblob/jsonblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,22 @@
l.next.Date = l.de.Date
}
i := len(vs)
vs = append(vs, claircore.Vulnerability{})
if err := json.Unmarshal(l.de.Vuln.buf, &vs[i]); err != nil {
l.err = err
return false
switch l.de.Kind {
case driver.VulnerabilityKind:
vs = append(vs, claircore.Vulnerability{})
if err := json.Unmarshal(l.de.Vuln.buf, &vs[i]); err != nil {
l.err = err
return false
}

Check warning on line 94 in libvuln/jsonblob/jsonblob.go

View check run for this annotation

Codecov / codecov/patch

libvuln/jsonblob/jsonblob.go#L92-L94

Added lines #L92 - L94 were not covered by tests
l.next.Vuln = append(l.next.Vuln, &vs[i])
case driver.EnrichmentKind:
en := driver.EnrichmentRecord{}
if err := json.Unmarshal(l.de.Enrichment.buf, &en); err != nil {
l.err = err
return false
}

Check warning on line 101 in libvuln/jsonblob/jsonblob.go

View check run for this annotation

Codecov / codecov/patch

libvuln/jsonblob/jsonblob.go#L99-L101

Added lines #L99 - L101 were not covered by tests
l.next.Enrichment = append(l.next.Enrichment, en)
}
l.next.Vuln = append(l.next.Vuln, &vs[i])

// BUG(hank) The [Loader] type does not handle Enrichments.

// If this was an initial diskEntry, promote the ref.
if id != l.cur {
l.cur = id
Expand Down Expand Up @@ -234,8 +241,8 @@
Date time.Time
}

// DiskEntry is a single vulnerability. It's made from unpacking an Entry's
// slice and adding a uuid for grouping back into an Entry upon read.
// DiskEntry is a single vulnerability or enrichment. It's made from unpacking an
// Entry's slice and adding an uuid for grouping back into an Entry upon read.
//
// "Vuln" and "Enrichment" are populated from the backing disk immediately
// before being serialized.
Expand Down
53 changes: 47 additions & 6 deletions libvuln/jsonblob/jsonblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"golang.org/x/sync/errgroup"

"github.com/quay/claircore"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/test"
)

Expand Down Expand Up @@ -41,8 +42,20 @@ func TestRoundtrip(t *testing.T) {
t.Fatal(err)
}

vs := test.GenUniqueVulnerabilities(10, "test")
ref, err := a.UpdateVulnerabilities(ctx, "test", "", vs)
var want, got struct {
V []*claircore.Vulnerability
E []driver.EnrichmentRecord
}

want.V = test.GenUniqueVulnerabilities(10, "test")
ref, err := a.UpdateVulnerabilities(ctx, "test", "", want.V)
if err != nil {
t.Error(err)
}
t.Logf("ref: %v", ref)

want.E = test.GenEnrichments(15)
ref, err = a.UpdateEnrichments(ctx, "test", "", want.E)
if err != nil {
t.Error(err)
}
Expand All @@ -52,7 +65,6 @@ func TestRoundtrip(t *testing.T) {
defer func() {
t.Logf("wrote:\n%s", buf.String())
}()
var got []*claircore.Vulnerability
r, w := io.Pipe()
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { defer w.Close(); return a.Store(w) })
Expand All @@ -62,7 +74,16 @@ func TestRoundtrip(t *testing.T) {
return err
}
for l.Next() {
got = append(got, l.Entry().Vuln...)
e := l.Entry()
if e.Vuln != nil && e.Enrichment != nil {
t.Error("expecting entry to have either vulnerability or enrichment, got both")
}
if e.Vuln != nil {
got.V = append(got.V, l.Entry().Vuln...)
}
if e.Enrichment != nil {
got.E = append(got.E, l.Entry().Enrichment...)
}
}
if err := l.Err(); err != nil {
return err
Expand All @@ -72,8 +93,28 @@ func TestRoundtrip(t *testing.T) {
if err := eg.Wait(); err != nil {
t.Error(err)
}
if !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
}

if !cmp.Equal(got, vs) {
t.Error(cmp.Diff(got, vs))
func TestEnrichments(t *testing.T) {
s, err := New()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()

en := test.GenEnrichments(5)
ref, err := s.UpdateEnrichments(ctx, "test", "", en)
if err != nil {
t.Error(err)
}
t.Logf("ref: %v", ref)

var buf bytes.Buffer
if err := s.Store(&buf); err != nil {
t.Error(err)
}
t.Logf("wrote:\n%s", buf.String())
}
15 changes: 12 additions & 3 deletions libvuln/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import (
"context"
"fmt"
"io"

"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/quay/zlog"

Expand Down Expand Up @@ -47,9 +49,16 @@
continue Update
}
}
ref, err := s.UpdateVulnerabilities(ctx, e.Updater, e.Fingerprint, e.Vuln)
if err != nil {
return err
var ref uuid.UUID
if e.Enrichment != nil {
if ref, err = s.UpdateEnrichments(ctx, e.Updater, e.Fingerprint, e.Enrichment); err != nil {
return fmt.Errorf("updating enrichements: %w", err)
}

Check warning on line 56 in libvuln/updates.go

View check run for this annotation

Codecov / codecov/patch

libvuln/updates.go#L52-L56

Added lines #L52 - L56 were not covered by tests
}
if e.Vuln != nil {
if ref, err = s.UpdateVulnerabilities(ctx, e.Updater, e.Fingerprint, e.Vuln); err != nil {
return fmt.Errorf("updating vulnerabilities: %w", err)
}

Check warning on line 61 in libvuln/updates.go

View check run for this annotation

Codecov / codecov/patch

libvuln/updates.go#L58-L61

Added lines #L58 - L61 were not covered by tests
Comment on lines +52 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes it technically possible to have both Vulnerabilites and Enrichments in the same UpdateOperation. Nothing in-tree would produce output that way, so I don't think this is a big deal.

}
zlog.Info(ctx).
Str("updater", e.Updater).
Expand Down
24 changes: 24 additions & 0 deletions test/enrichment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test

import (
"encoding/json"
"fmt"
"strconv"

"github.com/quay/claircore/libvuln/driver"
)

// GenEnrichments creates an array of enrichment records, with no meaningful
// content.
func GenEnrichments(n int) []driver.EnrichmentRecord {
var rs []driver.EnrichmentRecord
for i := 0; i < n; i++ {
t := strconv.Itoa(i)
e := fmt.Sprintf(`{"%[1]d":{"id":%[1]d}}`, i)
rs = append(rs, driver.EnrichmentRecord{
Tags: []string{t},
Enrichment: json.RawMessage(e),
})
}
return rs
}