Skip to content

Commit a7015f7

Browse files
committed
WIP: deps data source
This adds the deps data source logic to the minder codebase Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 009624f commit a7015f7

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-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+
}

pkg/api/protobuf/go/minder/v1/validators.go

+5
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,8 @@ func (rest *RestDataSource_Def) Validate() error {
591591

592592
return nil
593593
}
594+
595+
// Validate checks the state of the dependencies driver
596+
func (driver *DataSource_Deps) Validate() error {
597+
return nil
598+
}

0 commit comments

Comments
 (0)