-
Notifications
You must be signed in to change notification settings - Fork 42
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
Dependency Extraction Data Source Driver #5094
Open
puerco
wants to merge
7
commits into
mindersec:main
Choose a base branch
from
puerco:deps-datasource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46d6214
Add deps DS types to proto
puerco fb0e291
make gen
puerco 1a2dab9
Wire deps to data source machinery
puerco 9a7a513
Dependency data source driver
puerco 20dde83
Log data source functions errors
puerco 8ad9162
Unit tests for deps DS utility funcs
puerco 50413b6
go mod tidy
puerco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package deps implements a data source that extracts dependencies from | ||
// a filesystem or file. | ||
package deps | ||
|
||
import ( | ||
"errors" | ||
|
||
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
v1datasources "github.com/mindersec/minder/pkg/datasources/v1" | ||
) | ||
|
||
type depsDataSource struct { | ||
handlers map[v1datasources.DataSourceFuncKey]v1datasources.DataSourceFuncDef | ||
} | ||
|
||
// GetFuncs implements the v1datasources.DataSource interface. | ||
func (r *depsDataSource) GetFuncs() map[v1datasources.DataSourceFuncKey]v1datasources.DataSourceFuncDef { | ||
return r.handlers | ||
} | ||
|
||
// NewDepsDataSource returns a new dependencies datasource | ||
func NewDepsDataSource(ds *minderv1.DepsDataSource) (v1datasources.DataSource, error) { | ||
if ds == nil { | ||
return nil, errors.New("rest data source is nil") | ||
} | ||
|
||
if ds.GetDef() == nil { | ||
return nil, errors.New("rest data source definition is nil") | ||
} | ||
|
||
out := &depsDataSource{ | ||
handlers: make(map[v1datasources.DataSourceFuncKey]v1datasources.DataSourceFuncDef, len(ds.GetDef())), | ||
} | ||
|
||
for key, handlerCfg := range ds.GetDef() { | ||
handler, err := newHandlerFromDef(handlerCfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out.handlers[v1datasources.DataSourceFuncKey(key)] = handler | ||
} | ||
|
||
return out, 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,118 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package deps implements a data source that extracts dependencies from | ||
// a filesystem or file. | ||
package deps | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/go-git/go-billy/v5/helper/iofs" | ||
purl "github.com/package-url/packageurl-go" | ||
"github.com/protobom/protobom/pkg/sbom" | ||
"github.com/rs/zerolog/log" | ||
|
||
mdeps "github.com/mindersec/minder/internal/deps" | ||
"github.com/mindersec/minder/internal/deps/scalibr" | ||
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
v1datasources "github.com/mindersec/minder/pkg/datasources/v1" | ||
) | ||
|
||
type depsDataSourceHandler struct { | ||
def *minderv1.DepsDataSource_Def | ||
extractor mdeps.Extractor | ||
} | ||
|
||
func newHandlerFromDef(def *minderv1.DepsDataSource_Def) (*depsDataSourceHandler, error) { | ||
if def == nil { | ||
return nil, errors.New("function definition not found") | ||
} | ||
|
||
// TODO(puerco): Get extractor from type when we have other backends | ||
hndlr := &depsDataSourceHandler{ | ||
extractor: scalibr.NewExtractor(), | ||
def: def, | ||
} | ||
|
||
// Validate the initialization parameters | ||
if err := hndlr.ValidateArgs(map[string]any{ | ||
"ecosystems": def.Ecosystems, | ||
"path": def.Path, | ||
}); err != nil { | ||
return nil, fmt.Errorf("error in function definition: %w", err) | ||
} | ||
return hndlr, nil | ||
} | ||
|
||
func (_ *depsDataSourceHandler) ValidateArgs(args any) error { | ||
if args == nil { | ||
return nil | ||
} | ||
mapobj, ok := args.(map[string]any) | ||
if !ok { | ||
return errors.New("args is not a map") | ||
} | ||
|
||
var errs = []error{} | ||
|
||
// Check the known argumentss | ||
for k, v := range mapobj { | ||
switch k { | ||
case "ecosystems": | ||
errs = append(errs, validateEcosystems(v)...) | ||
case "path": | ||
if _, ok := v.(string); !ok { | ||
errs = append(errs, errors.New("path must be a string")) | ||
} | ||
} | ||
} | ||
|
||
return errors.Join(errs...) | ||
} | ||
|
||
// validateEcosystems checks that the defined ecosystems are valid | ||
func validateEcosystems(raw any) []error { | ||
if raw == nil { | ||
return nil | ||
} | ||
ecosystems, ok := raw.([]string) | ||
if !ok { | ||
return []error{errors.New("ecosystems must be a list of strings")} | ||
} | ||
|
||
var errs = []error{} | ||
for _, es := range ecosystems { | ||
if _, ok := purl.KnownTypes[es]; !ok { | ||
errs = append(errs, fmt.Errorf("unkown ecosystem: %q", es)) | ||
} | ||
} | ||
return errs | ||
} | ||
|
||
func (_ *depsDataSourceHandler) ValidateUpdate(_ any) error { return nil } | ||
func (_ *depsDataSourceHandler) GetArgsSchema() any { return nil } | ||
func (h *depsDataSourceHandler) Call(ctx context.Context, _ any) (any, error) { | ||
// Extract the ingestion results from the context | ||
var ctxData v1datasources.Context | ||
var ok bool | ||
if ctxData, ok = ctx.Value(v1datasources.ContextKey{}).(v1datasources.Context); !ok { | ||
return nil, fmt.Errorf("unable to read execution context") | ||
} | ||
|
||
if ctxData.Ingest.Fs == nil { | ||
return nil, fmt.Errorf("filesystem not found in execution context") | ||
} | ||
|
||
nl, err := h.extractor.ScanFilesystem(ctx, iofs.New(ctxData.Ingest.Fs)) | ||
if err != nil { | ||
return nil, fmt.Errorf("scanning filesystem for dependencies: %w", err) | ||
} | ||
|
||
log.Debug().Msgf("dependency extractor returned %d package nodes", len(nl.Nodes)) | ||
nl.Nodes = append(nl.Nodes, sbom.NewNode()) | ||
|
||
return nl, 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,68 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package deps implements a data source that extracts dependencies from | ||
// a filesystem or file. | ||
package deps | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateArgs(t *testing.T) { | ||
h := depsDataSourceHandler{} | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
name string | ||
args any | ||
mustErr bool | ||
}{ | ||
{name: "no-args", args: nil, mustErr: false}, | ||
{name: "wrong-type", args: struct{}{}, mustErr: true}, | ||
{name: "no-path", args: map[string]any{"ecosystems": []string{"npm"}}, mustErr: false}, | ||
{name: "blank-path", args: map[string]any{"path": "", "ecosystems": []string{"npm"}}, mustErr: false}, | ||
{name: "path-set", args: map[string]any{"path": "directory/", "ecosystems": []string{"npm"}}, mustErr: false}, | ||
{name: "no-ecosystems", args: map[string]any{"path": "directory/"}, mustErr: false}, | ||
{name: "ecosystems-empty", args: map[string]any{"path": "directory/", "ecosystems": []string{}}, mustErr: false}, | ||
{name: "ecosystems-nil", args: map[string]any{"path": "directory/", "ecosystems": nil}, mustErr: false}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
res := h.ValidateArgs(tc.args) | ||
if tc.mustErr { | ||
require.Error(t, res) | ||
return | ||
} | ||
require.NoError(t, res) | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateEcosystems(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
name string | ||
list any | ||
mustErr bool | ||
}{ | ||
{"empty-list", nil, false}, | ||
{"valid-list-0", []string{}, false}, | ||
{"valid-list-1", []string{"npm"}, false}, | ||
{"valid-list-1+", []string{"npm", "pypi", "cargo"}, false}, | ||
{"invalid-type", []string{"npm", "Hello!", "cargo"}, true}, | ||
{"other-something", []struct{}{}, true}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
errs := validateEcosystems(tc.list) | ||
if tc.mustErr { | ||
require.Error(t, errors.Join(errs...)) | ||
return | ||
} | ||
require.Len(t, errs, 0) | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -448,6 +448,23 @@ func addDataSourceFunctions( | |
return fmt.Errorf("failed to create data source function: %w", err) | ||
} | ||
} | ||
case *minderv1.DataSource_Deps: | ||
for name, def := range drv.Deps.GetDef() { | ||
defBytes, err := protojson.Marshal(def) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal REST definition: %w", err) | ||
} | ||
|
||
if _, err := tx.AddDataSourceFunction(ctx, db.AddDataSourceFunctionParams{ | ||
DataSourceID: dsID, | ||
ProjectID: projectID, | ||
Name: name, | ||
Type: v1datasources.DataSourceDriverDeps, | ||
Definition: defBytes, | ||
}); err != nil { | ||
return fmt.Errorf("failed to create data source function: %w", err) | ||
} | ||
} | ||
Comment on lines
+451
to
+467
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: is there a way to share this code path regardless of the type? |
||
default: | ||
return fmt.Errorf("unsupported data source driver type: %T", drv) | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: I guess we could verify something here, like the cardinality of the functions, but might be impractical. Any ideas?