-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
8 changed files
with
296 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package v6 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/anchore/grype/internal/log" | ||
) | ||
|
||
type AffectedCPEStoreWriter interface { | ||
AddAffectedCPEs(packages ...*AffectedCPEHandle) error | ||
} | ||
|
||
type AffectedCPEStoreReader interface { | ||
GetCPEsByProduct(packageName string, config *GetAffectedCPEOptions) ([]AffectedCPEHandle, error) | ||
} | ||
|
||
type GetAffectedCPEOptions struct { | ||
PreloadCPE bool | ||
PreloadBlob bool | ||
} | ||
|
||
type affectedCPEStore struct { | ||
db *gorm.DB | ||
blobStore *blobStore | ||
} | ||
|
||
func newAffectedCPEStore(db *gorm.DB, bs *blobStore) *affectedCPEStore { | ||
return &affectedCPEStore{ | ||
db: db, | ||
blobStore: bs, | ||
} | ||
} | ||
|
||
// AddAffectedCPEs adds one or more affected CPEs to the store | ||
func (s *affectedCPEStore) AddAffectedCPEs(packages ...*AffectedCPEHandle) error { | ||
for _, pkg := range packages { | ||
if err := s.blobStore.addBlobable(pkg); err != nil { | ||
return fmt.Errorf("unable to add affected package blob: %w", err) | ||
} | ||
|
||
if err := s.db.Create(pkg).Error; err != nil { | ||
return fmt.Errorf("unable to add affected CPE: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetCPEsByProduct retrieves a single AffectedCPEHandle by product name | ||
func (s *affectedCPEStore) GetCPEsByProduct(packageName string, config *GetAffectedCPEOptions) ([]AffectedCPEHandle, error) { | ||
if config == nil { | ||
config = &GetAffectedCPEOptions{} | ||
} | ||
|
||
log.WithFields("product", packageName).Trace("fetching AffectedCPE record") | ||
|
||
var pkgs []AffectedCPEHandle | ||
query := s.db. | ||
Joins("JOIN cpes ON cpes.id = affected_cpe_handles.cpe_id"). | ||
Where("cpes.product = ?", packageName) | ||
|
||
query = s.handlePreload(query, *config) | ||
|
||
err := query.Find(&pkgs).Error | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to fetch affected package record: %w", err) | ||
} | ||
|
||
if config.PreloadBlob { | ||
for i := range pkgs { | ||
err := s.attachBlob(&pkgs[i]) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to attach blob %#v: %w", pkgs[i], err) | ||
} | ||
} | ||
} | ||
|
||
return pkgs, nil | ||
} | ||
|
||
func (s *affectedCPEStore) handlePreload(query *gorm.DB, config GetAffectedCPEOptions) *gorm.DB { | ||
if config.PreloadCPE { | ||
query = query.Preload("CPE") | ||
} | ||
|
||
return query | ||
} | ||
|
||
// attachBlob attaches the BlobValue to the AffectedCPEHandle | ||
func (s *affectedCPEStore) attachBlob(cpe *AffectedCPEHandle) error { | ||
var blobValue *AffectedPackageBlob | ||
|
||
rawValue, err := s.blobStore.getBlobValue(cpe.BlobID) | ||
if err != nil { | ||
return fmt.Errorf("unable to fetch blob value for affected CPE: %w", err) | ||
} | ||
|
||
if err := json.Unmarshal([]byte(rawValue), &blobValue); err != nil { | ||
return fmt.Errorf("unable to unmarshal blob value: %w", err) | ||
} | ||
|
||
cpe.BlobValue = blobValue | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package v6 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAffectedCPEStore_AddAffectedCPEs(t *testing.T) { | ||
db := setupTestDB(t) | ||
bw := newBlobStore(db) | ||
s := newAffectedCPEStore(db, bw) | ||
|
||
cpe1 := &AffectedCPEHandle{ | ||
VulnerabilityID: 1, | ||
CpeID: 1, | ||
CPE: &Cpe{ | ||
Type: "a", | ||
Vendor: "vendor-1", | ||
Product: "product-1", | ||
Edition: "edition-1", | ||
}, | ||
BlobValue: &AffectedPackageBlob{ | ||
CVEs: []string{"CVE-2023-5678"}, | ||
}, | ||
} | ||
|
||
cpe2 := testAffectedCPEHandle() | ||
|
||
err := s.AddAffectedCPEs(cpe1, cpe2) | ||
require.NoError(t, err) | ||
|
||
var result1 AffectedCPEHandle | ||
err = db.Where("cpe_id = ?", 1).First(&result1).Error | ||
require.NoError(t, err) | ||
assert.Equal(t, cpe1.VulnerabilityID, result1.VulnerabilityID) | ||
assert.Equal(t, cpe1.ID, result1.ID) | ||
assert.Equal(t, cpe1.BlobID, result1.BlobID) | ||
assert.Nil(t, result1.BlobValue) // since we're not preloading any fields on the fetch | ||
|
||
var result2 AffectedCPEHandle | ||
err = db.Where("cpe_id = ?", 2).First(&result2).Error | ||
require.NoError(t, err) | ||
assert.Equal(t, cpe2.VulnerabilityID, result2.VulnerabilityID) | ||
assert.Equal(t, cpe2.ID, result2.ID) | ||
assert.Equal(t, cpe2.BlobID, result2.BlobID) | ||
assert.Nil(t, result2.BlobValue) // since we're not preloading any fields on the fetch | ||
} | ||
|
||
func TestAffectedCPEStore_GetCPEsByProduct(t *testing.T) { | ||
db := setupTestDB(t) | ||
bw := newBlobStore(db) | ||
s := newAffectedCPEStore(db, bw) | ||
|
||
cpe := testAffectedCPEHandle() | ||
err := s.AddAffectedCPEs(cpe) | ||
require.NoError(t, err) | ||
|
||
results, err := s.GetCPEsByProduct(cpe.CPE.Product, nil) | ||
require.NoError(t, err) | ||
|
||
expected := []AffectedCPEHandle{*cpe} | ||
require.Len(t, results, len(expected)) | ||
result := results[0] | ||
assert.Equal(t, cpe.CpeID, result.CpeID) | ||
assert.Equal(t, cpe.ID, result.ID) | ||
assert.Equal(t, cpe.BlobID, result.BlobID) | ||
require.Nil(t, result.BlobValue) // since we're not preloading any fields on the fetch | ||
|
||
// fetch again with blob & cpe preloaded | ||
results, err = s.GetCPEsByProduct(cpe.CPE.Product, &GetAffectedCPEOptions{PreloadCPE: true, PreloadBlob: true}) | ||
require.NoError(t, err) | ||
require.Len(t, results, len(expected)) | ||
result = results[0] | ||
assert.NotNil(t, result.BlobValue) | ||
if d := cmp.Diff(*cpe, result); d != "" { | ||
t.Errorf("unexpected result (-want +got):\n%s", d) | ||
} | ||
} | ||
|
||
func testAffectedCPEHandle() *AffectedCPEHandle { | ||
return &AffectedCPEHandle{ | ||
CPE: &Cpe{ | ||
Type: "application", | ||
Vendor: "vendor", | ||
Product: "product", | ||
Edition: "edition", | ||
Language: "language", | ||
SoftwareEdition: "software_edition", | ||
TargetHardware: "target_hardware", | ||
TargetSoftware: "target_software", | ||
Other: "other", | ||
}, | ||
BlobValue: &AffectedPackageBlob{ | ||
CVEs: []string{"CVE-2024-4321"}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.