Skip to content

Commit

Permalink
WIP: deps data source
Browse files Browse the repository at this point in the history
This adds the deps data source logic to the minder codebase

Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Dec 2, 2024
1 parent 009624f commit a7015f7
Show file tree
Hide file tree
Showing 3 changed files with 107 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
}
5 changes: 5 additions & 0 deletions pkg/api/protobuf/go/minder/v1/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 596 in pkg/api/protobuf/go/minder/v1/validators.go

View workflow job for this annotation

GitHub Actions / lint / Run golangci-lint

unused-receiver: method receiver 'driver' is not referenced in method's body, consider removing or renaming it as _ (revive)
return nil
}

0 comments on commit a7015f7

Please sign in to comment.