Skip to content

Commit b8401d4

Browse files
committed
Add deps data source
This commit adds the dependency scanner data source to minder. Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 5765f09 commit b8401d4

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

internal/datasources/deps/deps.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package deps implements a data source that extracts dependencies from
5+
// a filesystem or file.
6+
package deps
7+
8+
import (
9+
"errors"
10+
11+
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
12+
v1datasources "github.com/mindersec/minder/pkg/datasources/v1"
13+
)
14+
15+
type depsDataSource struct {
16+
handlers map[v1datasources.DataSourceFuncKey]v1datasources.DataSourceFuncDef
17+
}
18+
19+
// GetFuncs implements the v1datasources.DataSource interface.
20+
func (r *depsDataSource) GetFuncs() map[v1datasources.DataSourceFuncKey]v1datasources.DataSourceFuncDef {
21+
return r.handlers
22+
}
23+
24+
// NewDepsDataSource returns a new dependencies datasource
25+
func NewDepsDataSource(ds *minderv1.DepsDataSource) (v1datasources.DataSource, error) {
26+
if ds == nil {
27+
return nil, errors.New("rest data source is nil")
28+
}
29+
30+
if ds.GetDef() == nil {
31+
return nil, errors.New("rest data source definition is nil")
32+
}
33+
34+
out := &depsDataSource{
35+
handlers: make(map[v1datasources.DataSourceFuncKey]v1datasources.DataSourceFuncDef, len(ds.GetDef())),
36+
}
37+
38+
for key, handlerCfg := range ds.GetDef() {
39+
handler, err := newHandlerFromDef(handlerCfg)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
out.handlers[v1datasources.DataSourceFuncKey(key)] = handler
45+
}
46+
47+
return out, nil
48+
}

internal/datasources/deps/handler.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package deps implements a data source that extracts dependencies from
5+
// a filesystem or file.
6+
package deps
7+
8+
import (
9+
"context"
10+
"errors"
11+
"fmt"
12+
13+
"github.com/go-git/go-billy/v5/helper/iofs"
14+
15+
mdeps "github.com/mindersec/minder/internal/deps"
16+
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
17+
v1datasources "github.com/mindersec/minder/pkg/datasources/v1"
18+
)
19+
20+
type depsDataSourceHandler struct {
21+
extractor mdeps.Extractor
22+
}
23+
24+
func newHandlerFromDef(def *minderv1.DepsDataSource_Def) (*depsDataSourceHandler, error) {
25+
if def == nil {
26+
return nil, errors.New("rest data source handler definition is nil")
27+
}
28+
29+
return &depsDataSourceHandler{}, nil
30+
}
31+
32+
func (_ *depsDataSourceHandler) ValidateArgs(_ any) error { return nil }
33+
34+
func (_ *depsDataSourceHandler) ValidateUpdate(_ any) error { return nil }
35+
36+
func (h *depsDataSourceHandler) Call(ctx context.Context, _ any) (any, error) {
37+
// Extract the ingestion results from the context
38+
var ctxData v1datasources.Context
39+
var ok bool
40+
if ctxData, ok = ctx.Value(v1datasources.ContextKey{}).(v1datasources.Context); !ok {
41+
return nil, fmt.Errorf("unable to read execution context")
42+
}
43+
44+
if ctxData.Ingest.Fs == nil {
45+
return nil, fmt.Errorf("filesystem not found in execution context")
46+
}
47+
48+
nl, err := h.extractor.ScanFilesystem(ctx, iofs.New(ctxData.Ingest.Fs))
49+
if err != nil {
50+
return nil, fmt.Errorf("scanning filesystem for dependencies: %w", err)
51+
}
52+
53+
return nl, nil
54+
}

internal/datasources/factory.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package datasources
77
import (
88
"fmt"
99

10+
"github.com/mindersec/minder/internal/datasources/deps"
1011
"github.com/mindersec/minder/internal/datasources/rest"
1112
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
1213
v1datasources "github.com/mindersec/minder/pkg/datasources/v1"
@@ -26,6 +27,8 @@ func BuildFromProtobuf(ds *minderv1.DataSource) (v1datasources.DataSource, error
2627
switch ds.GetDriver().(type) {
2728
case *minderv1.DataSource_Rest:
2829
return rest.NewRestDataSource(ds.GetRest())
30+
case *minderv1.DataSource_Deps:
31+
return deps.NewDepsDataSource(ds.GetDeps())
2932
default:
3033
return nil, fmt.Errorf("unknown data source type: %T", ds)
3134
}

0 commit comments

Comments
 (0)