From 7afc36677215d2a824e6a09cecaa3b29bd716982 Mon Sep 17 00:00:00 2001 From: Shawn Hurley Date: Thu, 9 Jan 2025 10:59:40 -0500 Subject: [PATCH] handeling the provider not respecting scope by adding Filter in engine Signed-off-by: Shawn Hurley --- engine/engine.go | 15 ++++++++++----- engine/scopes.go | 24 ++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index d5202615..b2844519 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -194,7 +194,7 @@ func (r *ruleEngine) RunRulesScoped(ctx context.Context, ruleSets []RuleSet, sco taggingRules, otherRules, mapRuleSets := r.filterRules(ruleSets, selectors...) - ruleContext := r.runTaggingRules(ctx, taggingRules, mapRuleSets, conditionContext) + ruleContext := r.runTaggingRules(ctx, taggingRules, mapRuleSets, conditionContext, scopes) // Need a better name for this thing ret := make(chan response) @@ -220,7 +220,7 @@ func (r *ruleEngine) RunRulesScoped(ctx context.Context, ruleSets []RuleSet, sco rs.Errors[response.Rule.RuleID] = response.Err.Error() } } else if response.ConditionResponse.Matched && len(response.ConditionResponse.Incidents) > 0 { - violation, err := r.createViolation(ctx, response.ConditionResponse, response.Rule) + violation, err := r.createViolation(ctx, response.ConditionResponse, response.Rule, scopes) if err != nil { r.logger.Error(err, "unable to create violation from response", "ruleID", response.Rule.RuleID) } @@ -345,7 +345,7 @@ func (r *ruleEngine) filterRules(ruleSets []RuleSet, selectors ...RuleSelector) // runTaggingRules filters and runs info rules synchronously // returns list of non-info rules, a context to pass to them -func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessage, mapRuleSets map[string]*konveyor.RuleSet, context ConditionContext) ConditionContext { +func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessage, mapRuleSets map[string]*konveyor.RuleSet, context ConditionContext, scope Scope) ConditionContext { // track unique tags per ruleset rulesetTagsCache := map[string]map[string]bool{} for _, ruleMessage := range infoRules { @@ -407,7 +407,7 @@ func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessag mapRuleSets[ruleMessage.ruleSetName] = rs } // create an insight for this tag - violation, err := r.createViolation(ctx, response, rule) + violation, err := r.createViolation(ctx, response, rule, scope) if err != nil { r.logger.Error(err, "unable to create violation from response", "ruleID", rule.RuleID) } @@ -487,7 +487,7 @@ func (r *ruleEngine) getRelativePathForViolation(fileURI uri.URI) (uri.URI, erro return fileURI, nil } -func (r *ruleEngine) createViolation(ctx context.Context, conditionResponse ConditionResponse, rule Rule) (konveyor.Violation, error) { +func (r *ruleEngine) createViolation(ctx context.Context, conditionResponse ConditionResponse, rule Rule, scope Scope) (konveyor.Violation, error) { incidents := []konveyor.Incident{} fileCodeSnipCount := map[string]int{} incidentsSet := map[string]struct{}{} // Set of incidents @@ -504,6 +504,11 @@ func (r *ruleEngine) createViolation(ctx context.Context, conditionResponse Cond if r.incidentLimit != 0 && len(incidents) == r.incidentLimit { break } + // If we should remove the incident because the provider didn't filter it + // and the user asked for a certain scope of incidents. + if scope != nil && scope.FilterResponse(m) { + continue + } trimmedUri, err := r.getRelativePathForViolation(m.FileURI) if err != nil { return konveyor.Violation{}, err diff --git a/engine/scopes.go b/engine/scopes.go index 108ee286..b5488b99 100644 --- a/engine/scopes.go +++ b/engine/scopes.go @@ -15,6 +15,7 @@ type Scope interface { // For now this is the only place that we are considering adding a scope // in the future, we could scope other things AddToContext(*ConditionContext) error + FilterResponse(IncidentContext) bool } type scopeWrapper struct { @@ -35,8 +36,8 @@ func (s *scopeWrapper) Name() string { } func (s *scopeWrapper) AddToContext(conditionCTX *ConditionContext) error { - for _, s := range s.scopes { - err := s.AddToContext(conditionCTX) + for _, scope := range s.scopes { + err := scope.AddToContext(conditionCTX) if err != nil { return err } @@ -44,6 +45,16 @@ func (s *scopeWrapper) AddToContext(conditionCTX *ConditionContext) error { return nil } +func (s *scopeWrapper) FilterResponse(response IncidentContext) bool { + for _, scope := range s.scopes { + shouldFilter := scope.FilterResponse(response) + if shouldFilter { + return true + } + } + return false +} + var _ Scope = &scopeWrapper{} func NewScope(scopes ...Scope) Scope { @@ -80,6 +91,15 @@ func (i *includedPathScope) AddToContext(conditionCTX *ConditionContext) error { } +func (i *includedPathScope) FilterResponse(response IncidentContext) bool { + for _, path := range i.paths { + if string(response.FileURI) != "" && response.FileURI.Filename() == path { + return false + } + } + return true +} + func IncludedPathsScope(paths []string, log logr.Logger) Scope { return &includedPathScope{ paths: paths,