diff --git a/internal/datasources/deps/deps.go b/internal/datasources/deps/deps.go new file mode 100644 index 0000000000..6026da4ff5 --- /dev/null +++ b/internal/datasources/deps/deps.go @@ -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 +} diff --git a/internal/datasources/deps/handler.go b/internal/datasources/deps/handler.go new file mode 100644 index 0000000000..4be8837702 --- /dev/null +++ b/internal/datasources/deps/handler.go @@ -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 +} diff --git a/pkg/api/protobuf/go/minder/v1/validators.go b/pkg/api/protobuf/go/minder/v1/validators.go index 1e6bf50aa4..6940b32425 100644 --- a/pkg/api/protobuf/go/minder/v1/validators.go +++ b/pkg/api/protobuf/go/minder/v1/validators.go @@ -591,3 +591,8 @@ func (rest *RestDataSource_Def) Validate() error { return nil } + +// Validate checks the state of the dependencies driver +func (driver *DataSource_Deps) Validate() error { + return nil +}