From e21cee9d7cae86d5082e14617d24c61cbe337f79 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 4 Dec 2024 15:48:54 -0500 Subject: [PATCH] latest doc from reader should allow for empty (#2294) Signed-off-by: Alex Goodman --- grype/db/v6/distribution/client.go | 4 ++-- grype/db/v6/distribution/latest.go | 6 ++---- grype/db/v6/distribution/latest_test.go | 11 ++++++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/grype/db/v6/distribution/client.go b/grype/db/v6/distribution/client.go index 1fdd590243d..afac7b61dad 100644 --- a/grype/db/v6/distribution/client.go +++ b/grype/db/v6/distribution/client.go @@ -233,12 +233,12 @@ func isSupersededBy(current *v6.Description, candidate v6.Description) bool { otherModelPart, otherOk := candidate.SchemaVersion.ModelPart() currentModelPart, currentOk := current.SchemaVersion.ModelPart() - if !otherOk { + if !currentOk { log.Error("existing database has no schema version, doing nothing...") return false } - if !currentOk { + if !otherOk { log.Error("update has no schema version, doing nothing...") return false } diff --git a/grype/db/v6/distribution/latest.go b/grype/db/v6/distribution/latest.go index c171fa4c88c..6c2ff33a0e8 100644 --- a/grype/db/v6/distribution/latest.go +++ b/grype/db/v6/distribution/latest.go @@ -59,14 +59,12 @@ func NewLatestDocument(entries ...Archive) *LatestDocument { func NewLatestFromReader(reader io.Reader) (*LatestDocument, error) { var l LatestDocument - if err := json.NewDecoder(reader).Decode(&l); err != nil { return nil, fmt.Errorf("unable to parse DB latest.json: %w", err) } - // inflate entry data from parent - if l.Archive.Description.SchemaVersion != "" { - l.Archive.Description.SchemaVersion = l.SchemaVersion + if l == (LatestDocument{}) { + return nil, nil } return &l, nil diff --git a/grype/db/v6/distribution/latest_test.go b/grype/db/v6/distribution/latest_test.go index a28b7c59b2f..f841a3e3a92 100644 --- a/grype/db/v6/distribution/latest_test.go +++ b/grype/db/v6/distribution/latest_test.go @@ -68,6 +68,7 @@ func TestNewLatestDocument(t *testing.T) { } func TestNewLatestFromReader(t *testing.T) { + t.Run("valid JSON", func(t *testing.T) { latestDoc := LatestDocument{ Archive: Archive{ @@ -87,11 +88,19 @@ func TestNewLatestFromReader(t *testing.T) { require.Equal(t, latestDoc.Archive.Description.Built.Time, result.Archive.Description.Built.Time) }) + t.Run("empty", func(t *testing.T) { + emptyJSON := []byte("{}") + val, err := NewLatestFromReader(bytes.NewReader(emptyJSON)) + require.NoError(t, err) + assert.Nil(t, val) + }) + t.Run("invalid JSON", func(t *testing.T) { invalidJSON := []byte("invalid json") - _, err := NewLatestFromReader(bytes.NewReader(invalidJSON)) + val, err := NewLatestFromReader(bytes.NewReader(invalidJSON)) require.Error(t, err) require.Contains(t, err.Error(), "unable to parse DB latest.json") + assert.Nil(t, val) }) }