From 5798dba68158669b8812310b81751f12482bc011 Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Tue, 21 May 2024 15:04:16 -0400 Subject: [PATCH] :bug: fix rule parsing when sub-trees contain tests Signed-off-by: Pranav Gaikwad --- parser/rule_parser.go | 11 ++++++----- parser/rule_parser_test.go | 18 ------------------ provider/internal/builtin/service_client.go | 2 +- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/parser/rule_parser.go b/parser/rule_parser.go index 30fdb1fb..a467136e 100644 --- a/parser/rule_parser.go +++ b/parser/rule_parser.go @@ -113,7 +113,6 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr } var ruleSet *engine.RuleSet rules := []engine.Rule{} - foundTree := false parserErr := &parserErrors{} for _, f := range files { info, err := os.Stat(path.Join(filepath, f.Name())) @@ -122,7 +121,6 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr continue } if info.IsDir() { - foundTree = true r, m, err := r.LoadRules(path.Join(filepath, f.Name())) if err != nil { parserErr.errs = append(parserErr.errs, err) @@ -141,6 +139,12 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr ruleSet = r.loadRuleSet(filepath) continue } + // skip rule tests + if strings.HasSuffix(f.Name(), ".test.yaml") || + strings.HasSuffix(f.Name(), ".test.yml") { + r.Log.V(7).Info("excluding test file from parsing", "file", f.Name()) + continue + } r, m, err := r.LoadRule(path.Join(filepath, f.Name())) if err != nil { parserErr.errs = append(parserErr.errs, err) @@ -154,9 +158,6 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr } } - if ruleSet == nil && !foundTree { - return nil, nil, fmt.Errorf("unable to find %v", RULE_SET_GOLDEN_FILE_NAME) - } if ruleSet != nil { ruleSet.Rules = rules ruleSets = append(ruleSets, *ruleSet) diff --git a/parser/rule_parser_test.go b/parser/rule_parser_test.go index c91920d0..1f312dd9 100644 --- a/parser/rule_parser_test.go +++ b/parser/rule_parser_test.go @@ -621,24 +621,6 @@ func TestLoadRules(t *testing.T) { }, }, }, - { - Name: "failure-no-ruleset", - testFileName: "no-ruleset", - ShouldErr: true, - ErrorMessage: "unable to find ruleset.yaml", - providerNameClient: map[string]provider.InternalProviderClient{ - "builtin": testProvider{ - caps: []provider.Capability{{ - Name: "file", - }}, - }, - "notadded": testProvider{ - caps: []provider.Capability{{ - Name: "fake", - }}, - }, - }, - }, { Name: "handle not-valid category", testFileName: "invalid-category.yaml", diff --git a/provider/internal/builtin/service_client.go b/provider/internal/builtin/service_client.go index 18c3c56f..52acbec8 100644 --- a/provider/internal/builtin/service_client.go +++ b/provider/internal/builtin/service_client.go @@ -487,6 +487,6 @@ func (b *builtinServiceClient) isFileIncluded(absolutePath string) bool { return true } } - b.log.V(5).Info("excluding file from search", "file", absolutePath) + b.log.V(7).Info("excluding file from search", "file", absolutePath) return false }