Skip to content

Commit

Permalink
Support enforcement actions other than deny and dryrun (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
jalseth authored Jun 16, 2021
1 parent f5db0f3 commit 73e70e0
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func runCreateCommand(path string) error {
"src": violation.Path(),
})

if !isValidEnforcementAction(violation.Enforcement()) {
return fmt.Errorf("enforcement action (%v) is invalid in policy: %s", violation.Enforcement(), violation.Path())
}

templateFileName := "template.yaml"
constraintFileName := "constraint.yaml"
outputDir := filepath.Dir(violation.Path())
Expand Down Expand Up @@ -195,9 +199,15 @@ func getConstraint(violation rego.Rego) (unstructured.Unstructured, error) {
constraint.SetGroupVersionKind(gvk)
constraint.SetName(violation.Name())

if violation.Enforcement() != "deny" {
if err := unstructured.SetNestedField(constraint.Object, violation.Enforcement(), "spec", "enforcementAction"); err != nil {
return unstructured.Unstructured{}, fmt.Errorf("set constraint enforcement: %w", err)
}
}

// the dryrun flag overrides any enforcement action specified in the rego header
dryrun := viper.GetBool("dryrun")
if dryrun || violation.Enforcement() == "dryrun" {
if dryrun {
if err := unstructured.SetNestedField(constraint.Object, "dryrun", "spec", "enforcementAction"); err != nil {
return unstructured.Unstructured{}, fmt.Errorf("set constraint dryrun: %w", err)
}
Expand Down Expand Up @@ -273,3 +283,13 @@ func setMatchLabelsMatcher(constraint *unstructured.Unstructured, matcher rego.M
}
return nil
}

func isValidEnforcementAction(action string) bool {
for _, a := range []string{"deny", "dryrun", "warn"} {
if a == action {
return true
}
}

return false
}

0 comments on commit 73e70e0

Please sign in to comment.