Skip to content

Commit

Permalink
Merge branch 'main' into count-entity-type-metric
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjb authored Jul 16, 2024
2 parents 491a54f + efb471b commit 91f9f80
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 106 deletions.
4 changes: 1 addition & 3 deletions cmd/dev/app/rule_type/rttst.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ func runEvaluationForRules(
frags []*minderv1.Profile_Rule,
actionEngine *actions.RuleActionsEngine,
) error {
for idx := range frags {
frag := frags[idx]

for _, frag := range frags {
val := eng.GetRuleInstanceValidator()
err := val.ValidateRuleDefAgainstSchema(frag.Def.AsMap())
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions internal/engine/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ import (
"github.com/stacklok/minder/internal/engine/actions/remediate/pull_request"
enginerr "github.com/stacklok/minder/internal/engine/errors"
engif "github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provinfv1 "github.com/stacklok/minder/pkg/providers/v1"
)

// RuleActionsEngine is the engine responsible for processing all actions i.e., remediation and alerts
type RuleActionsEngine struct {
actions map[engif.ActionType]engif.Action
actionsOnOff map[engif.ActionType]engif.ActionOpt
actionsOnOff map[engif.ActionType]models.ActionOpt
}

// NewRuleActions creates a new rule actions engine
Expand Down Expand Up @@ -70,15 +71,15 @@ func NewRuleActions(
},
// The on/off state of the actions is an integral part of the action engine
// and should be set upon creation.
actionsOnOff: map[engif.ActionType]engif.ActionOpt{
actionsOnOff: map[engif.ActionType]models.ActionOpt{
remEngine.Class(): remEngine.GetOnOffState(profile),
alertEngine.Class(): alertEngine.GetOnOffState(profile),
},
}, nil
}

// GetOnOffState returns the on/off state of the actions
func (rae *RuleActionsEngine) GetOnOffState() map[engif.ActionType]engif.ActionOpt {
func (rae *RuleActionsEngine) GetOnOffState() map[engif.ActionType]models.ActionOpt {
return rae.actionsOnOff
}

Expand Down Expand Up @@ -277,15 +278,15 @@ func (rae *RuleActionsEngine) isSkippable(ctx context.Context, actionType engif.
}
// Check the action option
switch actionOnOff {
case engif.ActionOptOff:
case models.ActionOptOff:
// Action is off, skip
logger.Msg("action is off, skipping")
return true
case engif.ActionOptUnknown:
case models.ActionOptUnknown:
// Action is unknown, skip
logger.Msg("unknown action option, skipping")
return true
case engif.ActionOptDryRun, engif.ActionOptOn:
case models.ActionOptDryRun, models.ActionOptOn:
// Action is on or dry-run, do not skip yet. Check the evaluation error
skipAction =
// rule evaluation was skipped, skip action too
Expand Down
7 changes: 4 additions & 3 deletions internal/engine/actions/alert/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

enginerr "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

Expand All @@ -49,15 +50,15 @@ func (_ *Alert) Type() string {
}

// GetOnOffState returns the off state of the noop engine
func (_ *Alert) GetOnOffState(_ *pb.Profile) interfaces.ActionOpt {
return interfaces.ActionOptOff
func (_ *Alert) GetOnOffState(_ *pb.Profile) models.ActionOpt {
return models.ActionOptOff
}

// Do perform the noop alert
func (a *Alert) Do(
_ context.Context,
_ interfaces.ActionCmd,
_ interfaces.ActionOpt,
_ models.ActionOpt,
_ protoreflect.ProtoMessage,
_ interfaces.ActionsParams,
_ *json.RawMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stacklok/minder/internal/profiles/models"
htmltemplate "html/template"
"strings"

Expand Down Expand Up @@ -180,15 +181,15 @@ func (_ *Alert) Type() string {
}

// GetOnOffState returns the alert action state read from the profile
func (_ *Alert) GetOnOffState(p *pb.Profile) interfaces.ActionOpt {
return interfaces.ActionOptFromString(p.Alert, interfaces.ActionOptOn)
func (_ *Alert) GetOnOffState(p *pb.Profile) models.ActionOpt {
return models.ActionOptFromString(p.Alert, models.ActionOptOn)
}

// Do alerts through security advisory
func (alert *Alert) Do(
ctx context.Context,
cmd interfaces.ActionCmd,
setting interfaces.ActionOpt,
setting models.ActionOpt,
entity protoreflect.ProtoMessage,
params interfaces.ActionsParams,
metadata *json.RawMessage,
Expand All @@ -201,11 +202,11 @@ func (alert *Alert) Do(

// Process the command based on the action setting
switch setting {
case interfaces.ActionOptOn:
case models.ActionOptOn:
return alert.run(ctx, p, cmd)
case interfaces.ActionOptDryRun:
case models.ActionOptDryRun:
return alert.runDry(ctx, p, cmd)
case interfaces.ActionOptOff, interfaces.ActionOptUnknown:
case models.ActionOptOff, models.ActionOptUnknown:
return nil, fmt.Errorf("unexpected action setting: %w", enginerr.ErrActionFailed)
}
return nil, enginerr.ErrActionSkipped
Expand Down Expand Up @@ -373,7 +374,7 @@ func (alert *Alert) getParamsForSecurityAdvisory(

var descriptionStr strings.Builder
// Get the description template depending if remediation is available
if interfaces.ActionOptFromString(params.GetProfile().Remediate, interfaces.ActionOptOff) == interfaces.ActionOptOn {
if models.ActionOptFromString(params.GetProfile().Remediate, models.ActionOptOff) == models.ActionOptOn {
err = alert.descriptionTmpl.Execute(&descriptionStr, result.Template)
} else {
err = alert.descriptionNoRemTmpl.Execute(&descriptionStr, result.Template)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

engerrors "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
mindergh "github.com/stacklok/minder/internal/providers/github"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
Expand Down Expand Up @@ -97,15 +98,15 @@ func (_ *GhBranchProtectRemediator) Type() string {
}

// GetOnOffState returns the alert action state read from the profile
func (_ *GhBranchProtectRemediator) GetOnOffState(p *pb.Profile) interfaces.ActionOpt {
return interfaces.ActionOptFromString(p.Remediate, interfaces.ActionOptOff)
func (_ *GhBranchProtectRemediator) GetOnOffState(p *pb.Profile) models.ActionOpt {
return models.ActionOptFromString(p.Remediate, models.ActionOptOff)
}

// Do perform the remediation
func (r *GhBranchProtectRemediator) Do(
ctx context.Context,
cmd interfaces.ActionCmd,
remAction interfaces.ActionOpt,
remAction models.ActionOpt,
ent protoreflect.ProtoMessage,
params interfaces.ActionsParams,
_ *json.RawMessage,
Expand Down Expand Up @@ -172,11 +173,11 @@ func (r *GhBranchProtectRemediator) Do(
}

switch remAction {
case interfaces.ActionOptOn:
case models.ActionOptOn:
err = r.cli.UpdateBranchProtection(ctx, repo.Owner, repo.Name, branch, updatedRequest)
case interfaces.ActionOptDryRun:
case models.ActionOptDryRun:
err = dryRun(ctx, r.cli.GetBaseURL(), repo.Owner, repo.Name, branch, updatedRequest)
case interfaces.ActionOptOff, interfaces.ActionOptUnknown:
case models.ActionOptOff, models.ActionOptUnknown:
err = errors.New("unexpected action")
}
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"google.golang.org/protobuf/types/known/structpb"

"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/github/clients"
mock_ghclient "github.com/stacklok/minder/internal/providers/github/mock"
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestBranchProtectionRemediate(t *testing.T) {
})

type remediateArgs struct {
remAction interfaces.ActionOpt
remAction models.ActionOpt
ent protoreflect.ProtoMessage
pol map[string]any
params map[string]any
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestBranchProtectionRemediate(t *testing.T) {
mockSetup: func(_ *mock_ghclient.MockGitHub) {
},
remArgs: &remediateArgs{
remAction: interfaces.ActionOptOn,
remAction: models.ActionOptOn,
ent: &pb.Repository{
Owner: repoOwner,
Name: repoName,
Expand All @@ -185,7 +186,7 @@ func TestBranchProtectionRemediate(t *testing.T) {
actionType: TestActionTypeValid,
},
remArgs: &remediateArgs{
remAction: interfaces.ActionOptOn,
remAction: models.ActionOptOn,
ent: &pb.Repository{
Owner: repoOwner,
Name: repoName,
Expand Down Expand Up @@ -224,7 +225,7 @@ func TestBranchProtectionRemediate(t *testing.T) {
actionType: TestActionTypeValid,
},
remArgs: &remediateArgs{
remAction: interfaces.ActionOptOn,
remAction: models.ActionOptOn,
ent: &pb.Repository{
Owner: repoOwner,
Name: repoName,
Expand Down
7 changes: 4 additions & 3 deletions internal/engine/actions/remediate/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

enginerr "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

Expand All @@ -49,15 +50,15 @@ func (_ *Remediator) Type() string {
}

// GetOnOffState returns the off state of the noop engine
func (_ *Remediator) GetOnOffState(_ *pb.Profile) interfaces.ActionOpt {
return interfaces.ActionOptOff
func (_ *Remediator) GetOnOffState(_ *pb.Profile) models.ActionOpt {
return models.ActionOptOff
}

// Do perform the remediation
func (r *Remediator) Do(
_ context.Context,
_ interfaces.ActionCmd,
_ interfaces.ActionOpt,
_ models.ActionOpt,
_ protoreflect.ProtoMessage,
_ interfaces.ActionsParams,
_ *json.RawMessage,
Expand Down
13 changes: 7 additions & 6 deletions internal/engine/actions/remediate/pull_request/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/stacklok/minder/internal/db"
enginerr "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
Expand Down Expand Up @@ -149,15 +150,15 @@ func (_ *Remediator) Type() string {
}

// GetOnOffState returns the alert action state read from the profile
func (_ *Remediator) GetOnOffState(p *pb.Profile) interfaces.ActionOpt {
return interfaces.ActionOptFromString(p.Remediate, interfaces.ActionOptOff)
func (_ *Remediator) GetOnOffState(p *pb.Profile) models.ActionOpt {
return models.ActionOptFromString(p.Remediate, models.ActionOptOff)
}

// Do perform the remediation
func (r *Remediator) Do(
ctx context.Context,
cmd interfaces.ActionCmd,
setting interfaces.ActionOpt,
setting models.ActionOpt,
ent protoreflect.ProtoMessage,
params interfaces.ActionsParams,
metadata *json.RawMessage,
Expand All @@ -168,11 +169,11 @@ func (r *Remediator) Do(
}
var remErr error
switch setting {
case interfaces.ActionOptOn:
case models.ActionOptOn:
return r.run(ctx, cmd, p)
case interfaces.ActionOptDryRun:
case models.ActionOptDryRun:
return r.dryRun(ctx, cmd, p)
case interfaces.ActionOptOff, interfaces.ActionOptUnknown:
case models.ActionOptOff, models.ActionOptUnknown:
remErr = errors.New("unexpected action")
}
return nil, remErr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

"github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/github/clients"
mockghclient "github.com/stacklok/minder/internal/providers/github/mock"
Expand Down Expand Up @@ -140,15 +141,15 @@ func frizbeePrRemWithExcludes(e []string) *pb.RuleType_Definition_Remediate_Pull
}

type remediateArgs struct {
remAction interfaces.ActionOpt
remAction models.ActionOpt
ent protoreflect.ProtoMessage
pol map[string]any
params map[string]any
}

func createTestRemArgs() *remediateArgs {
return &remediateArgs{
remAction: interfaces.ActionOptOn,
remAction: models.ActionOptOn,
ent: &pb.Repository{
Owner: repoOwner,
Name: repoName,
Expand All @@ -166,7 +167,7 @@ func createTestRemArgs() *remediateArgs {

func createTestRemArgsWithExcludes() *remediateArgs {
return &remediateArgs{
remAction: interfaces.ActionOptOn,
remAction: models.ActionOptOn,
ent: &pb.Repository{
Owner: repoOwner,
Name: repoName,
Expand Down
13 changes: 7 additions & 6 deletions internal/engine/actions/remediate/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

engerrors "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/profiles/models"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
Expand Down Expand Up @@ -106,15 +107,15 @@ func (_ *Remediator) Type() string {
}

// GetOnOffState returns the alert action state read from the profile
func (_ *Remediator) GetOnOffState(p *pb.Profile) interfaces.ActionOpt {
return interfaces.ActionOptFromString(p.Remediate, interfaces.ActionOptOff)
func (_ *Remediator) GetOnOffState(p *pb.Profile) models.ActionOpt {
return models.ActionOptFromString(p.Remediate, models.ActionOptOff)
}

// Do perform the remediation
func (r *Remediator) Do(
ctx context.Context,
cmd interfaces.ActionCmd,
setting interfaces.ActionOpt,
setting models.ActionOpt,
entity protoreflect.ProtoMessage,
params interfaces.ActionsParams,
_ *json.RawMessage,
Expand Down Expand Up @@ -148,11 +149,11 @@ func (r *Remediator) Do(

var err error
switch setting {
case interfaces.ActionOptOn:
case models.ActionOptOn:
err = r.run(ctx, endpoint.String(), body.Bytes())
case interfaces.ActionOptDryRun:
case models.ActionOptDryRun:
err = r.dryRun(ctx, endpoint.String(), body.String())
case interfaces.ActionOptOff, interfaces.ActionOptUnknown:
case models.ActionOptOff, models.ActionOptUnknown:
err = errors.New("unexpected action")
}
return nil, err
Expand Down
Loading

0 comments on commit 91f9f80

Please sign in to comment.