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

Feature/arango neighbors nouns query #1419

Merged
merged 7 commits into from
Oct 20, 2023
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
193 changes: 189 additions & 4 deletions pkg/assembler/backends/arangodb/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ import (
"fmt"
"strings"

jsoniter "github.com/json-iterator/go"

"github.com/arangodb/go-driver"
"github.com/guacsec/guac/internal/testing/ptrfrom"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func (c *arangoClient) Artifacts(ctx context.Context, artifactSpec *model.ArtifactSpec) ([]*model.Artifact, error) {
values := map[string]any{}

Expand Down Expand Up @@ -304,3 +300,192 @@ func (c *arangoClient) buildArtifactResponseByID(ctx context.Context, id string,
return nil, fmt.Errorf("id type does not match for artifact query: %s", id)
}
}

func (c *arangoClient) artifactNeighbors(ctx context.Context, nodeID string, allowedEdges edgeMap) ([]string, error) {
out := []string{}
if allowedEdges[model.EdgeArtifactHashEqual] {
// hashEqualSubjectArtEdgesStr collection query
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(hashEqualSubjectArtEdgesStr, "hashEqual", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hashEqual._id }")

foundSubjectIDsOutBound, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors - hashEqualSubjectArtEdges outbound")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundSubjectIDsOutBound...)

values = map[string]any{}
arangoQueryBuilder = setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forInBound(hashEqualSubjectArtEdgesStr, "hashEqual", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hashEqual._id }")

foundSubjectIDsInBound, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors - hashEqualSubjectArtEdges inbound")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundSubjectIDsInBound...)

//hashEqualArtEdgesStr collection query

values = map[string]any{}
arangoQueryBuilder = setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(hashEqualArtEdgesStr, "hashEqual", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hashEqual._id }")

foundEqualIDsOutBound, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors - hashEqualArtEdges outbound")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundEqualIDsOutBound...)

values = map[string]any{}
arangoQueryBuilder = setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forInBound(hashEqualArtEdgesStr, "hashEqual", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hashEqual._id }")

foundEqualIDsInBound, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors - hashEqualArtEdges inbound")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundEqualIDsInBound...)
}
if allowedEdges[model.EdgeArtifactIsOccurrence] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forInBound(isOccurrenceArtEdgesStr, "isOccurrence", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: isOccurrence._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactHasSbom] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(hasSBOMArtEdgesStr, "hasSBOM", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hasSBOM._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactHasSlsa] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(hasSLSASubjectArtEdgesStr, "hasSLSA", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hasSLSA._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactCertifyVexStatement] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(certifyVexArtEdgesStr, "certifyVex", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: certifyVex._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactCertifyBad] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(certifyBadArtEdgesStr, "certifyBad", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: certifyBad._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactCertifyGood] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(certifyGoodArtEdgesStr, "certifyGood", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: certifyGood._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactHasMetadata] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(hasMetadataArtEdgesStr, "hasMetadata", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hasMetadata._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
if allowedEdges[model.EdgeArtifactPointOfContact] {
values := map[string]any{}
arangoQueryBuilder := setArtifactMatchValues(&model.ArtifactSpec{ID: &nodeID}, values)
arangoQueryBuilder.forOutBound(pointOfContactArtEdgesStr, "pointOfContact", "art")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: pointOfContact._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "artifactNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}

return out, nil
}

func (c *arangoClient) getNeighborIDFromCursor(ctx context.Context, arangoQueryBuilder *arangoQueryBuilder, values map[string]any, callingFuncName string) ([]string, error) {
cursor, err := executeQueryWithRetry(ctx, c.db, arangoQueryBuilder.string(), values, "getNeighborIDFromCursor - "+callingFuncName)
if err != nil {
return nil, fmt.Errorf("failed to query for Neighbors for %s with error: %w", callingFuncName, err)
}
defer cursor.Close()

type dbNeighbor struct {
NeighborID *string `json:"neighbor"`
ParentID *string `json:"parent"`
}

var foundNeighbors []dbNeighbor
for {
var doc dbNeighbor
_, err := cursor.ReadDocument(ctx, &doc)
if err != nil {
if driver.IsNoMoreDocuments(err) {
break
} else {
return nil, fmt.Errorf("failed to get neighbor id from cursor for %s with error: %w", callingFuncName, err)
}
} else {
foundNeighbors = append(foundNeighbors, doc)
}
}

var foundIDs []string
for _, foundNeighbor := range foundNeighbors {
if foundNeighbor.NeighborID != nil {
foundIDs = append(foundIDs, *foundNeighbor.NeighborID)
}
if foundNeighbor.ParentID != nil {
foundIDs = append(foundIDs, *foundNeighbor.ParentID)
}
}
return foundIDs, nil
}
82 changes: 0 additions & 82 deletions pkg/assembler/backends/arangodb/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,88 +28,6 @@ import (
"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

// TODO (pxp928): add tests back in when implemented

// func Test_artifactStruct_Neighbors(t *testing.T) {
// type fields struct {
// id uint32
// algorithm string
// digest string
// hashEquals []uint32
// occurrences []uint32
// hasSLSAs []uint32
// vexLinks []uint32
// badLinks []uint32
// goodLinks []uint32
// }
// tests := []struct {
// name string
// fields fields
// allowedEdges edgeMap
// want []uint32
// }{{
// name: "hashEquals",
// fields: fields{
// hashEquals: []uint32{343, 546},
// },
// allowedEdges: edgeMap{model.EdgeArtifactHashEqual: true},
// want: []uint32{343, 546},
// }, {
// name: "occurrences",
// fields: fields{
// occurrences: []uint32{2324, 1234},
// },
// allowedEdges: edgeMap{model.EdgeArtifactIsOccurrence: true},
// want: []uint32{2324, 1234},
// }, {
// name: "hasSLSAs",
// fields: fields{
// hasSLSAs: []uint32{445, 1232244},
// },
// allowedEdges: edgeMap{model.EdgeArtifactHasSlsa: true},
// want: []uint32{445, 1232244},
// }, {
// name: "vexLinks",
// fields: fields{
// vexLinks: []uint32{987, 9876},
// },
// allowedEdges: edgeMap{model.EdgeArtifactCertifyVexStatement: true},
// want: []uint32{987, 9876},
// }, {
// name: "badLinks",
// fields: fields{
// badLinks: []uint32{5322, 544},
// },
// allowedEdges: edgeMap{model.EdgeArtifactCertifyBad: true},
// want: []uint32{5322, 544},
// }, {
// name: "goodLinks",
// fields: fields{
// goodLinks: []uint32{25468, 1458},
// },
// allowedEdges: edgeMap{model.EdgeArtifactCertifyGood: true},
// want: []uint32{25468, 1458},
// }}
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// a := &artStruct{
// id: tt.fields.id,
// algorithm: tt.fields.algorithm,
// digest: tt.fields.digest,
// hashEquals: tt.fields.hashEquals,
// occurrences: tt.fields.occurrences,
// hasSLSAs: tt.fields.hasSLSAs,
// vexLinks: tt.fields.vexLinks,
// badLinks: tt.fields.badLinks,
// goodLinks: tt.fields.goodLinks,
// }
// if got := a.Neighbors(tt.allowedEdges); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("builderStruct.Neighbors() = %v, want %v", got, tt.want)
// }
// })
// }
// }

func getArangoConfig() *ArangoConfig {
return &ArangoConfig{
User: "root",
Expand Down
5 changes: 5 additions & 0 deletions pkg/assembler/backends/arangodb/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"time"

"github.com/99designs/gqlgen/graphql"

jsoniter "github.com/json-iterator/go"

"github.com/arangodb/go-driver"
arangodbdriverhttp "github.com/arangodb/go-driver/http"
"github.com/guacsec/guac/internal/testing/ptrfrom"
Expand Down Expand Up @@ -195,6 +198,8 @@ const (
certifyLegalsStr string = "certifyLegals"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

var mapEdgeToArangoEdgeCollection = map[model.Edge][]string{
model.EdgeArtifactCertifyBad: {certifyBadArtEdgesStr},
model.EdgeArtifactCertifyGood: {certifyGoodArtEdgesStr},
Expand Down
17 changes: 17 additions & 0 deletions pkg/assembler/backends/arangodb/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,20 @@ func (c *arangoClient) buildBuilderResponseByID(ctx context.Context, id string,
return nil, fmt.Errorf("id type does not match for builder query: %s", id)
}
}

func (c *arangoClient) builderNeighbors(ctx context.Context, nodeID string, allowedEdges edgeMap) ([]string, error) {
out := []string{}
if allowedEdges[model.EdgeBuilderHasSlsa] {
values := map[string]any{}
arangoQueryBuilder := setBuilderMatchValues(&model.BuilderSpec{ID: &nodeID}, values)
arangoQueryBuilder.forInBound(hasSLSABuiltByEdgesStr, "hasSLSA", "build")
arangoQueryBuilder.query.WriteString("\nRETURN { neighbor: hasSLSA._id }")

foundIDs, err := c.getNeighborIDFromCursor(ctx, arangoQueryBuilder, values, "builderNeighbors")
if err != nil {
return out, fmt.Errorf("failed to get neighbors for node ID: %s from arango cursor with error: %w", nodeID, err)
}
out = append(out, foundIDs...)
}
return out, nil
}
35 changes: 0 additions & 35 deletions pkg/assembler/backends/arangodb/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,6 @@ import (
"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

// TODO (pxp928): add tests back in when implemented

// func Test_builderStruct_Neighbors(t *testing.T) {
// type fields struct {
// id uint32
// uri string
// hasSLSAs []uint32
// }
// tests := []struct {
// name string
// fields fields
// allowedEdges edgeMap
// want []uint32
// }{{
// name: "hasSLSAs",
// fields: fields{
// hasSLSAs: []uint32{445, 1232244},
// },
// allowedEdges: edgeMap{model.EdgeBuilderHasSlsa: true},
// want: []uint32{445, 1232244},
// }}
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// b := &builderStruct{
// id: tt.fields.id,
// uri: tt.fields.uri,
// hasSLSAs: tt.fields.hasSLSAs,
// }
// if got := b.Neighbors(tt.allowedEdges); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("builderStruct.Neighbors() = %v, want %v", got, tt.want)
// }
// })
// }
// }

func Test_IngestBuilder(t *testing.T) {
ctx := context.Background()
arangArg := getArangoConfig()
Expand Down
Loading
Loading