Skip to content

Commit d66977f

Browse files
authored
Allow disabling of rules in a package (#67)
* Change structure of enabled packages list in config Signed-off-by: wwwil <[email protected]> * Pass list of disabled rules to NewResultCollectionFromRegoResultSet so they can be skipped Signed-off-by: wwwil <[email protected]> * Add function to list all rule IDs from policy manifest Signed-off-by: wwwil <[email protected]> * Use map of string to bool rather than list of strings to indicate which rules are enabled or disabled Signed-off-by: wwwil <[email protected]> * Add functionality to specify list of enabled rules Signed-off-by: wwwil <[email protected]> * Move result filtering to separate function Signed-off-by: wwwil <[email protected]> * Add backwards compatible config loading Signed-off-by: wwwil <[email protected]> * Change name field to be ID Signed-off-by: wwwil <[email protected]>
1 parent 111e1ba commit d66977f

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

cmd/check.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jetstack/preflight/pkg/packagesources/local"
2222
"github.com/jetstack/preflight/pkg/packaging"
2323
"github.com/jetstack/preflight/pkg/reports"
24+
"github.com/jetstack/preflight/pkg/results"
2425

2526
"github.com/spf13/cobra"
2627
"github.com/spf13/viper"
@@ -301,19 +302,31 @@ func check() {
301302
outputs = append(outputs, op)
302303
}
303304

304-
// Loop over enabled packages and evaluate.
305-
enabledPackages := viper.GetStringSlice("enabled-packages")
306-
305+
type EnabledPackage struct {
306+
ID string
307+
EnabledRuleIDs []string `mapstructure:"enabled-rules"`
308+
DisabledRuleIDs []string `mapstructure:"disabled-rules"`
309+
}
310+
var enabledPackages []EnabledPackage
311+
err := viper.UnmarshalKey("enabled-packages", &enabledPackages)
312+
if err != nil {
313+
log.Printf("unable to decode into struct, %v", err)
314+
log.Print("using legacy enabled-packages format")
315+
enabledPackageIDs := viper.GetStringSlice("enabled-packages")
316+
for _, enabledPackageID := range enabledPackageIDs {
317+
enabledPackages = append(enabledPackages, EnabledPackage{ID: enabledPackageID})
318+
}
319+
}
307320
if len(enabledPackages) == 0 {
308321
log.Fatal("No packages were enabled. Use 'enables-packages' option in configuration to enable the packages you want to use.")
309322
}
310323

311324
missingRules := false
312-
for _, pkgID := range enabledPackages {
325+
for _, enabledPackage := range enabledPackages {
313326
// Make sure we loaded the package for this.
314-
pkg := packages[pkgID]
327+
pkg := packages[enabledPackage.ID]
315328
if pkg == nil {
316-
log.Fatalf("Package with ID %q was specified in configuration but it wasn't found.", pkgID)
329+
log.Fatalf("Package with ID %q was specified in configuration but it wasn't found.", enabledPackage.ID)
317330
}
318331

319332
manifest := pkg.PolicyManifest()
@@ -340,6 +353,8 @@ func check() {
340353
}
341354
}
342355

356+
rc = results.FilterResultCollection(rc, enabledPackage.DisabledRuleIDs, enabledPackage.EnabledRuleIDs)
357+
343358
intermediateBytes, err := json.Marshal(input)
344359
if err != nil {
345360
log.Fatalf("Cannot marshal intermediate result: %v", err)

pkg/packaging/eval.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@ func EvalPackage(ctx context.Context, pkg Package, input interface{}) (*results.
2727
allResults = append(allResults, rs...)
2828
}
2929

30-
return results.NewResultCollectionFromRegoResultSet(&allResults)
30+
rc, err := results.NewResultCollectionFromRegoResultSet(&allResults)
31+
if err != nil {
32+
return nil, fmt.Errorf("cannot read results from rego: %s", err)
33+
}
34+
35+
return rc, nil
3136
}

pkg/packaging/packaging.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ func (m *PolicyManifest) GlobalID() string {
4747
return fmt.Sprintf("%s/%s", m.Namespace, m.ID)
4848
}
4949

50+
// RuleIDs returns a list of the IDs of all the rules in this policy manifest
51+
func (m *PolicyManifest) RuleIDs() []string {
52+
var ruleIDs []string
53+
for _, section := range m.Sections {
54+
for _, rule := range section.Rules {
55+
ruleIDs = append(ruleIDs, rule.ID)
56+
}
57+
}
58+
return ruleIDs
59+
}
60+
5061
// Section holds the information for a section of the policy manifest.
5162
type Section struct {
5263
// ID is the ID of the section.

pkg/results/results.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ func NewResultCollectionFromRegoResultSet(rs *rego.ResultSet) (*ResultCollection
162162
return &rc, nil
163163
}
164164

165+
// FilterResultCollection filters a collection of results based on lists of
166+
// disabled and enabled rule IDs and returns a filtered ResultCollection. The
167+
// filtered ResultCollection does not include results for disabled rules. If the
168+
// enabled rules list is not empty the filtered ResultCollection only contains
169+
// results for enabled rules.
170+
func FilterResultCollection(resultCollection *ResultCollection, disabledRuleIDs, enabledRuleIDs []string) *ResultCollection {
171+
filteredResultCollection := NewResultCollection()
172+
for _, result := range resultCollection.ByID() {
173+
filterResult := false
174+
if len(enabledRuleIDs) != 0 {
175+
filterResult = true
176+
for _, enabledRuleID := range enabledRuleIDs {
177+
if result.ID == enabledRuleID {
178+
filterResult = true
179+
}
180+
}
181+
}
182+
for _, disabledRuleID := range disabledRuleIDs {
183+
if result.ID == disabledRuleID {
184+
filterResult = true
185+
}
186+
}
187+
if !filterResult {
188+
filteredResultCollection.Add([]*Result{result})
189+
}
190+
}
191+
return filteredResultCollection
192+
}
193+
165194
// Parse takes the raw result of evaluating a set of rego rules in preflight and returns a ResultCollection collection.
166195
func Parse(rawResult []byte) (*ResultCollection, error) {
167196
// parse raw data with opa.rego package

0 commit comments

Comments
 (0)