Skip to content

Commit

Permalink
enrichers: Load enrichments from jsonblob storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdm committed Dec 1, 2023
1 parent faffb8e commit acfbacc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
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 @@ func (l *Loader) Next() bool {
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
}
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
}
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 @@ type CommonEntry struct {
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())
}
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
}

0 comments on commit acfbacc

Please sign in to comment.