Skip to content

Commit

Permalink
Passing the graphql client from the top
Browse files Browse the repository at this point in the history
Signed-off-by: nathannaveen <[email protected]>
  • Loading branch information
nathannaveen committed Mar 26, 2024
1 parent c897daa commit 0d00394
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 48 deletions.
4 changes: 2 additions & 2 deletions internal/testing/cmd/ingest/cmd/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type options struct {
graphqlEndpoint string
}

func ingestExample(cmd *cobra.Command, args []string) {
func ingestExample(cmd *cobra.Command, args []string, gqlClient graphql.Client) {
ctx := logging.WithLogger(context.Background())
logger := logging.FromContext(ctx)

Expand All @@ -59,7 +59,7 @@ func ingestExample(cmd *cobra.Command, args []string) {
var inputs []assembler.IngestPredicates
for _, doc := range docs {
// This is a test example, so we will ignore calling out to a collectsub service
input, _, err := parser.ParseDocumentTree(ctx, doc)
input, _, err := parser.ParseDocumentTree(ctx, doc, gqlClient)
if err != nil {
logger.Fatalf("unable to parse document: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/testing/cmd/ingest/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package cmd
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"
"net/http"
"os"
"strings"

Expand Down Expand Up @@ -85,7 +87,9 @@ var rootCmd = &cobra.Command{
Use: "ingest",
Short: "example ingestor for ingesting a set of example documents and populating a graph for GUAC",
Run: func(cmd *cobra.Command, args []string) {
ingestExample(cmd, args)
httpClient := http.Client{}
gqlClient := graphql.NewClient(flags.graphqlEndpoint, &httpClient)
ingestExample(cmd, args, gqlClient)
},
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/ingestor/ingestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ import (

// Synchronously ingest document using GraphQL endpoint
func Ingest(ctx context.Context, d *processor.Document, graphqlEndpoint string, csubClient csub_client.Client) error {
httpClient := http.Client{}
gqlclient := graphql.NewClient(graphqlEndpoint, &httpClient)

logger := logging.FromContext(ctx)
// Get pipeline of components
processorFunc := GetProcessor(ctx)
ingestorFunc := GetIngestor(ctx)
ingestorFunc := GetIngestor(ctx, gqlclient)
collectSubEmitFunc := GetCollectSubEmit(ctx, csubClient)
assemblerFunc := GetAssembler(ctx, graphqlEndpoint)

Expand Down Expand Up @@ -70,10 +73,13 @@ func Ingest(ctx context.Context, d *processor.Document, graphqlEndpoint string,
}

func MergedIngest(ctx context.Context, docs []*processor.Document, graphqlEndpoint string, csubClient csub_client.Client) error {
httpClient := http.Client{}
gqlclient := graphql.NewClient(graphqlEndpoint, &httpClient)

logger := logging.FromContext(ctx)
// Get pipeline of components
processorFunc := GetProcessor(ctx)
ingestorFunc := GetIngestor(ctx)
ingestorFunc := GetIngestor(ctx, gqlclient)
collectSubEmitFunc := GetCollectSubEmit(ctx, csubClient)
assemblerFunc := GetAssembler(ctx, graphqlEndpoint)

Expand Down Expand Up @@ -146,9 +152,9 @@ func GetProcessor(ctx context.Context) func(*processor.Document) (processor.Docu
}
}

func GetIngestor(ctx context.Context) func(processor.DocumentTree) ([]assembler.IngestPredicates, []*parser_common.IdentifierStrings, error) {
func GetIngestor(ctx context.Context, gqlClient graphql.Client) func(processor.DocumentTree) ([]assembler.IngestPredicates, []*parser_common.IdentifierStrings, error) {
return func(doc processor.DocumentTree) ([]assembler.IngestPredicates, []*parser_common.IdentifierStrings, error) {
return parser.ParseDocumentTree(ctx, doc)
return parser.ParseDocumentTree(ctx, doc, gqlClient)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/ingestor/parser/csaf/parser_csaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package csaf
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"
"strconv"

jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -68,7 +69,7 @@ type visitedProductRef struct {
category string
}

func NewCsafParser() common.DocumentParser {
func NewCsafParser(gqlClient graphql.Client) common.DocumentParser {
return &csafParser{
identifierStrings: &common.IdentifierStrings{},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ingestor/parser/csaf/parser_csaf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_csafParser(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewCsafParser()
s := NewCsafParser(nil)
err := s.Parse(ctx, tt.doc)
if (err != nil) != tt.wantErr {
t.Errorf("csafParse.Parse() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -295,7 +295,7 @@ func Test_csafParser_GetIdentifiers(t *testing.T) {
},
}

c := NewCsafParser()
c := NewCsafParser(nil)

err := c.Parse(test.ctx, test.fields.doc)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ingestor/parser/deps_dev/deps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package deps_dev
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"
"time"

jsoniter "github.com/json-iterator/go"
Expand All @@ -37,7 +38,7 @@ type depsDevParser struct {
packComponent *deps_dev.PackageComponent
}

func NewDepsDevParser() common.DocumentParser {
func NewDepsDevParser(gqlClient graphql.Client) common.DocumentParser {
return &depsDevParser{}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/ingestor/parser/deps_dev/deps_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestNewDepsDevParser(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewDepsDevParser(); !reflect.DeepEqual(got, tt.want) {
if got := NewDepsDevParser(nil); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDepsDevParser() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -522,7 +522,7 @@ func Test_depsDevParser_Parse(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDepsDevParser()
d := NewDepsDevParser(nil)
if err := d.Parse(ctx, tt.doc); (err != nil) != tt.wantErr {
t.Errorf("deps.dev.Parse() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down Expand Up @@ -567,7 +567,7 @@ func Test_depsDevParser_GetIdentifiers(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDepsDevParser()
d := NewDepsDevParser(nil)
if err := d.Parse(ctx, tt.doc); (err != nil) != tt.wantErr {
t.Errorf("deps.dev.Parse() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ingestor/parser/dsse/parser_dsse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package dsse
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"

"github.com/guacsec/guac/pkg/assembler"
"github.com/guacsec/guac/pkg/handler/processor"
Expand All @@ -33,7 +34,7 @@ type dsseParser struct {
}

// NewDSSEParser initializes the dsseParser
func NewDSSEParser() common.DocumentParser {
func NewDSSEParser(gqlClient graphql.Client) common.DocumentParser {
return &dsseParser{
identities: []common.TrustInformation{},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ingestor/parser/dsse/parser_dsse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_DsseParser(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDSSEParser()
d := NewDSSEParser(nil)
err := d.Parse(ctx, tt.doc)
if (err != nil) != tt.wantErr {
t.Errorf("slsa.Parse() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package ingest_predicates

import (
"context"
"github.com/Khan/genqlient/graphql"

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

Expand All @@ -32,12 +33,15 @@ import (
var json = jsoniter.ConfigCompatibleWithStandardLibrary

type ingestPredicatesParser struct {
preds assembler.IngestPredicates
preds assembler.IngestPredicates
gqlClient graphql.Client
}

// NewIngestPredicatesParser initializes the ingestPredicatesParser
func NewIngestPredicatesParser() common.DocumentParser {
return &ingestPredicatesParser{}
func NewIngestPredicatesParser(gqlClient graphql.Client) common.DocumentParser {
return &ingestPredicatesParser{
gqlClient: gqlClient,
}
}

// Parse breaks out the document into the graph components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Test_ingestPredicatesParser(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewIngestPredicatesParser()
s := NewIngestPredicatesParser(nil)
err := s.Parse(ctx, tt.doc)
if (err != nil) != tt.wantErr {
t.Errorf("slsa.Parse() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
3 changes: 2 additions & 1 deletion pkg/ingestor/parser/open_vex/parser_open_vex.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package open_vex
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"

json "github.com/json-iterator/go"
"github.com/openvex/go-vex/pkg/vex"
Expand Down Expand Up @@ -52,7 +53,7 @@ type openVEXParser struct {
cvs []assembler.CertifyVulnIngest
}

func NewOpenVEXParser() common.DocumentParser {
func NewOpenVEXParser(gqlClient graphql.Client) common.DocumentParser {
return &openVEXParser{
identifierStrings: &common.IdentifierStrings{},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingestor/parser/open_vex/parser_open_vex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_openVEXParser_Parse(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
newParser := NewOpenVEXParser()
newParser := NewOpenVEXParser(nil)
if err := newParser.Parse(tt.args.ctx, tt.args.doc); (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func Test_openVEXParser_GetPredicates(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewOpenVEXParser()
c := NewOpenVEXParser(nil)

err := c.Parse(tt.args.ctx, tt.fields.doc)
if err != nil {
Expand Down Expand Up @@ -187,7 +187,7 @@ func Test_openVEXParser_GetIdentifiers(t *testing.T) {
},
}

c := NewOpenVEXParser()
c := NewOpenVEXParser(nil)

err := c.Parse(test.ctx, test.fields.doc)
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions pkg/ingestor/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package parser
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"

"github.com/guacsec/guac/pkg/assembler"
"github.com/guacsec/guac/pkg/handler/processor"
Expand Down Expand Up @@ -47,7 +48,7 @@ func init() {
}

var (
documentParser = map[processor.DocumentType]func() common.DocumentParser{}
documentParser = map[processor.DocumentType]func(gqlClient graphql.Client) common.DocumentParser{}
)

type docTreeBuilder struct {
Expand All @@ -62,7 +63,7 @@ func newDocTreeBuilder() *docTreeBuilder {
}
}

func RegisterDocumentParser(p func() common.DocumentParser, d processor.DocumentType) error {
func RegisterDocumentParser(p func(gqlClient graphql.Client) common.DocumentParser, d processor.DocumentType) error {
if _, ok := documentParser[d]; ok {
documentParser[d] = p
return fmt.Errorf("the document parser is being overwritten: %s", d)
Expand All @@ -72,14 +73,14 @@ func RegisterDocumentParser(p func() common.DocumentParser, d processor.Document
}

// ParseDocumentTree takes the DocumentTree and create graph inputs (nodes and edges) per document node.
func ParseDocumentTree(ctx context.Context, docTree processor.DocumentTree) ([]assembler.IngestPredicates, []*common.IdentifierStrings, error) {
func ParseDocumentTree(ctx context.Context, docTree processor.DocumentTree, gqlClient graphql.Client) ([]assembler.IngestPredicates, []*common.IdentifierStrings, error) {
assemblerInputs := []assembler.IngestPredicates{}
identifierStrings := []*common.IdentifierStrings{}
logger := logging.FromContext(ctx)
docTreeBuilder := newDocTreeBuilder()

logger.Infof("parsing document tree with root type: %v", docTree.Document.Type)
err := docTreeBuilder.parse(ctx, docTree, map[visitedKey]bool{})
err := docTreeBuilder.parse(ctx, docTree, map[visitedKey]bool{}, gqlClient)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -108,8 +109,8 @@ type visitedKey struct {
}

// The visited map is used to keep track of the document nodes that have already been visited to avoid infinite loops.
func (t *docTreeBuilder) parse(ctx context.Context, root processor.DocumentTree, visited map[visitedKey]bool) error {
builder, err := parseHelper(ctx, root.Document)
func (t *docTreeBuilder) parse(ctx context.Context, root processor.DocumentTree, visited map[visitedKey]bool, gqlClient graphql.Client) error {
builder, err := parseHelper(ctx, root.Document, gqlClient)
if err != nil {
return err
}
Expand All @@ -124,20 +125,20 @@ func (t *docTreeBuilder) parse(ctx context.Context, root processor.DocumentTree,
t.identities = append(t.identities, builder.GetIdentities()...)

for _, c := range root.Children {
if err := t.parse(ctx, c, visited); err != nil {
if err := t.parse(ctx, c, visited, gqlClient); err != nil {
return err
}
}
return nil
}

func parseHelper(ctx context.Context, doc *processor.Document) (*common.GraphBuilder, error) {
func parseHelper(ctx context.Context, doc *processor.Document, gqlClient graphql.Client) (*common.GraphBuilder, error) {
pFunc, ok := documentParser[doc.Type]
if !ok {
return nil, fmt.Errorf("no document parser registered for type: %s", doc.Type)
}

p := pFunc()
p := pFunc(gqlClient)
err := p.Parse(ctx, doc)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0d00394

Please sign in to comment.