-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the dependency scanner data source to minder. Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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,54 @@ | ||
// 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" | ||
|
||
mdeps "github.com/mindersec/minder/internal/deps" | ||
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
v1datasources "github.com/mindersec/minder/pkg/datasources/v1" | ||
) | ||
|
||
type depsDataSourceHandler struct { | ||
extractor mdeps.Extractor | ||
} | ||
|
||
func newHandlerFromDef(def *minderv1.DepsDataSource_Def) (*depsDataSourceHandler, error) { | ||
if def == nil { | ||
return nil, errors.New("rest data source handler definition is nil") | ||
} | ||
|
||
return &depsDataSourceHandler{}, nil | ||
} | ||
|
||
func (_ *depsDataSourceHandler) ValidateArgs(_ any) error { return nil } | ||
|
||
func (_ *depsDataSourceHandler) ValidateUpdate(_ any) error { 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) | ||
} | ||
|
||
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