Skip to content

Commit

Permalink
builtin: Return evalerrors.ErrEvaluationSkipSilently in case the buil…
Browse files Browse the repository at this point in the history
…tin evaluator doesn't match the entity

Returning `nil, nil` is wrong as it would be treated the same as
"evaluation was performed an no issues were found"
  • Loading branch information
jhrozek committed Aug 30, 2023
1 parent a6ca94a commit b0c1bc3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
15 changes: 6 additions & 9 deletions internal/engine/ingester/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"strings"

"google.golang.org/protobuf/reflect/protoreflect"

evalerrors "github.com/stacklok/mediator/internal/engine/errors"
"github.com/stacklok/mediator/internal/util"
pb "github.com/stacklok/mediator/pkg/generated/protobuf/go/mediator/v1"
"github.com/stacklok/mediator/pkg/rule_methods"
Expand All @@ -39,6 +39,7 @@ const (
// BuiltinRuleDataIngest is the engine for a rule type that uses builtin methods
type BuiltinRuleDataIngest struct {
builtinCfg *pb.BuiltinType
ruleMethods any
method string
accessToken string
}
Expand All @@ -52,14 +53,14 @@ func NewBuiltinRuleDataIngest(
builtinCfg: builtinCfg,
accessToken: access_token,
method: builtinCfg.GetMethod(),
ruleMethods: rule_methods.RuleMethods{},
}, nil
}

// Ingest calls the builtin method and populates the data to be returned
func (idi *BuiltinRuleDataIngest) Ingest(ctx context.Context, ent protoreflect.ProtoMessage, params map[string]any) (any, error) {
// call internal method stored in pkg and method
rm := rule_methods.RuleMethods{}
value := reflect.ValueOf(rm)
value := reflect.ValueOf(idi.ruleMethods)
method := value.MethodByName(idi.method)

// Check if the method exists
Expand All @@ -70,12 +71,8 @@ func (idi *BuiltinRuleDataIngest) Ingest(ctx context.Context, ent protoreflect.P
matches, err := entityMatchesParams(ctx, ent, params)
if err != nil {
return nil, fmt.Errorf("cannot check if entity matches params: %w", err)
}

// TODO: this should be a warning
if !matches {
log.Printf("entity not matching parameters, skipping")
return nil, nil
} else if !matches {
return nil, evalerrors.ErrEvaluationSkipSilently
}

// call method
Expand Down
32 changes: 32 additions & 0 deletions internal/engine/ingester/builtin/builtin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package builtin

import (
"context"
evalerrors "github.com/stacklok/mediator/internal/engine/errors"
pb "github.com/stacklok/mediator/pkg/generated/protobuf/go/mediator/v1"
"github.com/stretchr/testify/assert"
"testing"

"google.golang.org/protobuf/reflect/protoreflect"
)

type fakeMethods struct{}

func (fm *fakeMethods) Test(ctx context.Context, ent protoreflect.ProtoMessage, params map[string]any) (any, error) {
return nil, nil
}

func TestNewBuiltinRuleDataIngestNoMatch(t *testing.T) {
bi, err := NewBuiltinRuleDataIngest(nil, "")
assert.NoError(t, err)

// set the rule methods to a fake
bi.ruleMethods = &fakeMethods{}
bi.method = "Test"

res, err := bi.Ingest(context.Background(), &pb.Artifact{}, map[string]any{
"foo": "bar",
})
assert.Equal(t, evalerrors.ErrEvaluationSkipSilently, err)
assert.Nil(t, res)
}

0 comments on commit b0c1bc3

Please sign in to comment.