Skip to content

Commit

Permalink
Add guarding against invalid as/from combinations
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Manuel Leflet Estrada <[email protected]>
  • Loading branch information
jmle committed Feb 21, 2024
1 parent 105cc50 commit 2e9e0ab
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
15 changes: 12 additions & 3 deletions parser/rule_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"regexp"
"strings"

"gopkg.in/yaml.v2"

"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/engine"
"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"github.com/konveyor/analyzer-lsp/provider"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -555,6 +554,7 @@ func (r *RuleParser) getConditions(conditionsInterface []interface{}) ([]engine.
conditions := []engine.ConditionEntry{}
providers := map[string]provider.InternalProviderClient{}
chainNameToIndex := map[string]int{}
asFound := []string{}
for _, conditionInterface := range conditionsInterface {
// get map from interface
conditionMap, ok := conditionInterface.(map[interface{}]interface{})
Expand Down Expand Up @@ -684,7 +684,16 @@ func (r *RuleParser) getConditions(conditionsInterface []interface{}) ([]engine.
}
providers[providerKey] = provider
}
if ce.As != "" {
if ce.From != "" && ce.As != "" && ce.From == ce.As {
return nil, nil, fmt.Errorf("condition cannot have the same value for fields 'from' and 'as'")
} else if ce.As != "" {
for _, as := range asFound {
if as == ce.As {
return nil, nil, fmt.Errorf("condition cannot have multiple 'as' fields with the same name")
}
}
asFound = append(asFound, ce.As)

index, ok := chainNameToIndex[ce.As]
if !ok {
//prepend
Expand Down
78 changes: 78 additions & 0 deletions parser/rule_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,84 @@ func TestLoadRules(t *testing.T) {
},
},
},
{
Name: "no two conditions should have the same 'as' field within the same block",
testFileName: "rule-chain-same-as.yaml",
ShouldErr: true,
ErrorMessage: "condition cannot have multiple 'as' fields with the same name",
providerNameClient: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "filecontent",
}},
},
},
ExpectedProvider: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "filecontent",
}},
},
},
ExpectedRuleSet: map[string]engine.RuleSet{
"konveyor-analysis": {
Rules: []engine.Rule{
{
RuleMeta: engine.RuleMeta{
RuleID: "chaining-rule",
Description: "",
Category: &konveyor.Potential,
},
Perform: engine.Perform{
Message: engine.Message{
Text: &allGoFiles,
Links: []konveyor.Link{},
},
},
},
},
},
},
},
{
Name: "a condition should not have the same 'as' and 'from' fields",
testFileName: "rule-chain-same-as-from.yaml",
ShouldErr: true,
ErrorMessage: "condition cannot have the same value for fields 'from' and 'as'",
providerNameClient: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "filecontent",
}},
},
},
ExpectedProvider: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "filecontent",
}},
},
},
ExpectedRuleSet: map[string]engine.RuleSet{
"konveyor-analysis": {
Rules: []engine.Rule{
{
RuleMeta: engine.RuleMeta{
RuleID: "chaining-rule",
Description: "",
Category: &konveyor.Potential,
},
Perform: engine.Perform{
Message: engine.Message{
Text: &allGoFiles,
Links: []konveyor.Link{},
},
},
},
},
},
},
},
}

for _, tc := range testCases {
Expand Down
8 changes: 8 additions & 0 deletions parser/testdata/rule-chain-same-as-from.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- message: "all go files"
ruleID: chaining-rule
when:
and:
- builtin.filecontent:
pattern: spring\.datasource
as: file
from: file
10 changes: 10 additions & 0 deletions parser/testdata/rule-chain-same-as.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- message: "all go files"
ruleID: chaining-rule
when:
and:
- builtin.filecontent:
pattern: spring\.datasource
as: file
- builtin.filecontent:
pattern: value
as: file
2 changes: 1 addition & 1 deletion parser/testdata/rule-chain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
ignore: true
not: true
- builtin.file: "*.json"
as: go-files
as: go-files-2

0 comments on commit 2e9e0ab

Please sign in to comment.