Skip to content

Commit

Permalink
Feature/arango node query (guacsec#1301)
Browse files Browse the repository at this point in the history
* inital commit for path query

Signed-off-by: pxp928 <parth.psu@gmail.com>

* add unit test for buildPackageResponseFromID

Signed-off-by: pxp928 <parth.psu@gmail.com>

* add node query for arango

Signed-off-by: pxp928 <parth.psu@gmail.com>

* rename to nodeID

Signed-off-by: pxp928 <parth.psu@gmail.com>

---------

Signed-off-by: pxp928 <parth.psu@gmail.com>
  • Loading branch information
pxp928 authored Sep 20, 2023
1 parent 861288d commit f8b701e
Show file tree
Hide file tree
Showing 7 changed files with 843 additions and 14 deletions.
14 changes: 0 additions & 14 deletions pkg/assembler/backends/arangodb/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,20 +912,6 @@ func getPreloadString(prefix, name string) string {
return name
}

// Topological queries: queries where node connectivity matters more than node type
func (c *arangoClient) Neighbors(ctx context.Context, node string, usingOnly []model.Edge) ([]model.Node, error) {
panic(fmt.Errorf("not implemented: Neighbors - Neighbors"))
}
func (c *arangoClient) Node(ctx context.Context, node string) (model.Node, error) {
panic(fmt.Errorf("not implemented: Node - Node"))
}
func (c *arangoClient) Nodes(ctx context.Context, nodes []string) ([]model.Node, error) {
panic(fmt.Errorf("not implemented: Nodes - Nodes"))
}
func (c *arangoClient) Path(ctx context.Context, subject string, target string, maxPathLength int, usingOnly []model.Edge) ([]model.Node, error) {
panic(fmt.Errorf("not implemented: Path - Path"))
}

func (c *arangoClient) Licenses(ctx context.Context, licenseSpec *model.LicenseSpec) ([]*model.License, error) {
panic(fmt.Errorf("not implemented: Licenses"))
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/assembler/backends/arangodb/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package arangodb

import (
"context"
"fmt"
"strings"

"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

func (c *arangoClient) Path(ctx context.Context, startNodeID string, targetNodeID string, maxPathLength int, usingOnly []model.Edge) ([]model.Node, error) {
panic(fmt.Errorf("not implemented: Path"))
}

func (c *arangoClient) Neighbors(ctx context.Context, nodeID string, usingOnly []model.Edge) ([]model.Node, error) {
panic(fmt.Errorf("not implemented: Neighbors"))
}

func (c *arangoClient) Node(ctx context.Context, nodeID string) (model.Node, error) {
idSplit := strings.Split(nodeID, "/")
if len(idSplit) != 2 {
return nil, fmt.Errorf("invalid ID: %s", nodeID)
}
switch idSplit[0] {
case pkgVersionsStr, pkgNamesStr, pkgNamespacesStr, pkgTypesStr:
return c.buildPackageResponseFromID(ctx, nodeID, nil)
case srcNamesStr, srcNamespacesStr, srcTypesStr:
return c.buildSourceResponseFromID(ctx, nodeID, nil)
}
return nil, nil
}

func (c *arangoClient) Nodes(ctx context.Context, nodeIDs []string) ([]model.Node, error) {
panic(fmt.Errorf("not implemented: Nodes"))
}
91 changes: 91 additions & 0 deletions pkg/assembler/backends/arangodb/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration

package arangodb

import (
"context"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/guacsec/guac/internal/testing/ptrfrom"
"github.com/guacsec/guac/internal/testing/testdata"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

func Test_Node(t *testing.T) {
ctx := context.Background()
arangArg := getArangoConfig()
err := deleteDatabase(ctx, arangArg)
if err != nil {
t.Fatalf("error deleting arango database: %v", err)
}
b, err := getBackend(ctx, arangArg)
if err != nil {
t.Fatalf("error creating arango backend: %v", err)
}
tests := []struct {
name string
pkgInput *model.PkgInputSpec
pkgFilter *model.PkgSpec
idInFilter bool
want *model.Package
wantErr bool
}{{
name: "tensorflow empty version, ID search",
pkgInput: testdata.P1,
pkgFilter: &model.PkgSpec{
Name: ptrfrom.String("tensorflow"),
},
idInFilter: true,
want: testdata.P1out,
wantErr: false,
}, {
name: "openssl with match empty qualifiers",
pkgInput: testdata.P4,
pkgFilter: &model.PkgSpec{
Name: ptrfrom.String("openssl"),
Namespace: ptrfrom.String("openssl.org"),
Version: ptrfrom.String("3.0.3"),
MatchOnlyEmptyQualifiers: ptrfrom.Bool(true),
},
idInFilter: true,
want: testdata.P4out,
wantErr: false,
}}
ignoreID := cmp.FilterPath(func(p cmp.Path) bool {
return strings.Compare(".ID", p[len(p)-1].String()) == 0
}, cmp.Ignore())
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ingestedPkg, err := b.IngestPackage(ctx, *tt.pkgInput)
if (err != nil) != tt.wantErr {
t.Errorf("arangoClient.IngestPackage() error = %v, wantErr %v", err, tt.wantErr)
return
}
got, err := b.Node(ctx, ingestedPkg.Namespaces[0].Names[0].Versions[0].ID)
if (err != nil) != tt.wantErr {
t.Errorf("arangoClient.Packages() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, ignoreID); diff != "" {
t.Errorf("Unexpected results. (-want +got):\n%s", diff)
}
})
}
}
Loading

0 comments on commit f8b701e

Please sign in to comment.