Skip to content

Commit

Permalink
implement delete for ent, add client side delete and add delete on ce…
Browse files Browse the repository at this point in the history
…rtifier

Signed-off-by: pxp928 <[email protected]>
  • Loading branch information
pxp928 committed Jun 23, 2024
1 parent f21747d commit 24770fc
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 4 deletions.
129 changes: 129 additions & 0 deletions internal/testing/backend/certifyVuln_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/guacsec/guac/internal/testing/ptrfrom"
"github.com/guacsec/guac/internal/testing/testdata"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
"github.com/stretchr/testify/assert"
)

var vmd1 = &model.ScanMetadata{
Expand Down Expand Up @@ -1514,3 +1515,131 @@ func TestIngestCertifyVulns(t *testing.T) {
})
}
}

func TestDeleteCertifyVuln(t *testing.T) {
ctx := context.Background()
b := setupTest(t)
type call struct {
Pkgs []*model.IDorPkgInput
Vulns []*model.IDorVulnerabilityInput
CertifyVulns []*model.ScanMetadataInput
}
tests := []struct {
InPkg []*model.PkgInputSpec
Name string
InVuln []*model.VulnerabilityInputSpec
Calls []call
ExpVuln []*model.CertifyVuln
Query *model.CertifyVulnSpec
ExpIngestErr bool
ExpQueryErr bool
}{
{
Name: "HappyPath",
InVuln: []*model.VulnerabilityInputSpec{testdata.C1, testdata.C2},
InPkg: []*model.PkgInputSpec{testdata.P1, testdata.P2},
Calls: []call{
{
Pkgs: []*model.IDorPkgInput{{PackageInput: testdata.P2}, {PackageInput: testdata.P1}},
Vulns: []*model.IDorVulnerabilityInput{{VulnerabilityInput: testdata.C1}, {VulnerabilityInput: testdata.C2}},
CertifyVulns: []*model.ScanMetadataInput{
{
Collector: "test collector",
Origin: "test origin",
ScannerVersion: "v1.0.0",
ScannerURI: "test scanner uri",
DbVersion: "2023.01.01",
DbURI: "test db uri",
TimeScanned: testdata.T1,
},
{
Collector: "test collector",
Origin: "test origin",
ScannerVersion: "v1.0.0",
ScannerURI: "test scanner uri",
DbVersion: "2023.01.01",
DbURI: "test db uri",
TimeScanned: testdata.T1,
},
},
},
},
Query: &model.CertifyVulnSpec{
Collector: ptrfrom.String("test collector"),
},
ExpVuln: []*model.CertifyVuln{
{
ID: "1",
Package: testdata.P2out,
Vulnerability: &model.Vulnerability{
Type: "cve",
VulnerabilityIDs: []*model.VulnerabilityID{testdata.C1out},
},
Metadata: vmd1,
},
{
ID: "10",
Package: testdata.P1out,
Vulnerability: &model.Vulnerability{
Type: "cve",
VulnerabilityIDs: []*model.VulnerabilityID{testdata.C2out},
},
Metadata: vmd1,
},
},
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
for _, v := range test.InVuln {
if _, err := b.IngestVulnerability(ctx, model.IDorVulnerabilityInput{VulnerabilityInput: v}); err != nil {
t.Fatalf("Could not ingest vulnerabilities: %a", err)
}
}
for _, p := range test.InPkg {
if _, err := b.IngestPackage(ctx, model.IDorPkgInput{PackageInput: p}); err != nil {
t.Fatalf("Could not ingest packages: %v", err)
}
}
for _, o := range test.Calls {
_, err := b.IngestCertifyVulns(ctx, o.Pkgs, o.Vulns, o.CertifyVulns)
if (err != nil) != test.ExpIngestErr {
t.Fatalf("did not get expected ingest error, want: %v, got: %v", test.ExpIngestErr, err)
}
if err != nil {
return
}

}
got, err := b.CertifyVulnList(ctx, *test.Query, nil, nil)
if (err != nil) != test.ExpQueryErr {
t.Fatalf("did not get expected query error, want: %v, got: %v", test.ExpQueryErr, err)
}
if err != nil {
return
}
var returnedObjects []*model.CertifyVuln
if got != nil {
for _, obj := range got.Edges {
returnedObjects = append(returnedObjects, obj.Node)
}
}
if diff := cmp.Diff(test.ExpVuln, returnedObjects, commonOpts); diff != "" {
t.Errorf("Unexpected results. (-want +got):\n%s", diff)
}
deleted, err := b.Delete(ctx, returnedObjects[0].ID)
if err != nil {
t.Fatalf("did not get expected query error, want: %v, got: %v", test.ExpQueryErr, err)
}
assert.True(t, deleted)
secondgot, err := b.CertifyVulnList(ctx, *test.Query, nil, nil)
if (err != nil) != test.ExpQueryErr {
t.Fatalf("did not get expected query error, want: %v, got: %v", test.ExpQueryErr, err)
}
if err != nil {
return
}
assert.True(t, len(secondgot.Edges) == 1)
})
}
}
5 changes: 3 additions & 2 deletions internal/testing/backend/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ var skipMatrix = map[string]map[string]bool{
// redis order issues
"TestVEX": {arango: true, redis: true, tikv: true},
// redis order issues
"TestVEXBulkIngest": {arango: true, redis: true},
"TestFindSoftware": {redis: true, arango: true},
"TestVEXBulkIngest": {arango: true, redis: true},
"TestFindSoftware": {redis: true, arango: true},
"TestDeleteCertifyVuln": {arango: true, memmap: true, redis: true, tikv: true},
}

type backend interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/assembler/backends/arangodb/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,6 @@ func (c *arangoClient) Nodes(ctx context.Context, nodeIDs []string) ([]model.Nod
return rv, nil
}

func (c *demoClient) Delete(ctx context.Context, node string) (bool, error) {
func (c *arangoClient) Delete(ctx context.Context, node string) (bool, error) {
panic(fmt.Errorf("not implemented: Delete"))
}
15 changes: 15 additions & 0 deletions pkg/assembler/backends/ent/backend/certifyVuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ func bulkCertifyVulnGlobalID(ids []string) []string {
return toGlobalIDs(certifyvuln.Table, ids)
}

func (b *EntBackend) DeleteCertifyVuln(ctx context.Context, certifyVulnID uuid.UUID) (bool, error) {
_, txErr := WithinTX(ctx, b.client, func(ctx context.Context) (*string, error) {
tx := ent.TxFromContext(ctx)

if err := tx.CertifyVuln.DeleteOneID(certifyVulnID).Exec(ctx); err != nil {
return nil, errors.Wrap(err, "failed to delete certifyVuln with error")
}
return nil, nil
})
if txErr != nil {
return false, txErr
}
return true, nil
}

func certifyVulnConflictColumns() []string {
return []string{
certifyvuln.FieldPackageID,
Expand Down
Loading

0 comments on commit 24770fc

Please sign in to comment.