Skip to content

Commit

Permalink
Add deps data source
Browse files Browse the repository at this point in the history
This commit adds the dependency scanner data source to minder.

Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Nov 29, 2024
1 parent 5765f09 commit b8401d4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
48 changes: 48 additions & 0 deletions internal/datasources/deps/deps.go
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
}
54 changes: 54 additions & 0 deletions internal/datasources/deps/handler.go
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
}
3 changes: 3 additions & 0 deletions internal/datasources/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package datasources
import (
"fmt"

"github.com/mindersec/minder/internal/datasources/deps"
"github.com/mindersec/minder/internal/datasources/rest"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
v1datasources "github.com/mindersec/minder/pkg/datasources/v1"
Expand All @@ -26,6 +27,8 @@ func BuildFromProtobuf(ds *minderv1.DataSource) (v1datasources.DataSource, error
switch ds.GetDriver().(type) {
case *minderv1.DataSource_Rest:
return rest.NewRestDataSource(ds.GetRest())
case *minderv1.DataSource_Deps:
return deps.NewDepsDataSource(ds.GetDeps())
default:
return nil, fmt.Errorf("unknown data source type: %T", ds)
}
Expand Down

0 comments on commit b8401d4

Please sign in to comment.