Skip to content

Commit

Permalink
feat: Add support for future keyword (#217)
Browse files Browse the repository at this point in the history
Also add e2e tests that prove future works.

Signed-off-by: Will Beason <willbeason@google.com>
  • Loading branch information
Will Beason committed Apr 7, 2022
1 parent 7b86d0c commit 370fa37
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
32 changes: 32 additions & 0 deletions constraint/pkg/client/clienttest/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
KindCheckData = "CheckData"
KindRuntimeError = "RuntimeError"
KindForbidDuplicates = "ForbidDuplicates"
KindFuture = "Future"
)

// ModuleAllow defines a Rego package which allows all objects it reviews.
Expand Down Expand Up @@ -264,3 +265,34 @@ func TemplateForbidDuplicates() *templates.ConstraintTemplate {

return ct
}

const moduleFuture = `
package foo
import future.keywords.in
violation[{"msg": msg}] {
some n in ["1", "2"]
n == input.review.object.data
msg := "bad data"
}
`

func TemplateFuture() *templates.ConstraintTemplate {
ct := &templates.ConstraintTemplate{}

ct.SetName("future")
ct.Spec.CRD.Spec.Names.Kind = KindFuture
ct.Spec.CRD.Spec.Validation = &templates.Validation{
OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
Type: "object",
},
}

ct.Spec.Targets = []templates.Target{{
Target: handlertest.TargetName,
Rego: moduleFuture,
}}

return ct
}
37 changes: 35 additions & 2 deletions constraint/pkg/client/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestClient_Review(t *testing.T) {
Target: "foo2",
Msg: "denied",
Constraint: cts.MakeConstraint(t, cts.MockTemplate, "bar"),
EnforcementAction: "deny",
EnforcementAction: constraints.EnforcementActionDeny,
}},
},
{
Expand Down Expand Up @@ -360,9 +360,42 @@ func TestClient_Review(t *testing.T) {
Target: handlertest.TargetName,
Msg: "duplicate data bar",
Constraint: cts.MakeConstraint(t, clienttest.KindForbidDuplicates, "constraint"),
EnforcementAction: "deny",
EnforcementAction: constraints.EnforcementActionDeny,
}},
},
{
name: "deny future",
namespaces: nil,
targets: []handler.TargetHandler{&handlertest.Handler{}},
templates: []*templates.ConstraintTemplate{
clienttest.TemplateFuture(),
},
constraints: []*unstructured.Unstructured{
cts.MakeConstraint(t, clienttest.KindFuture, "constraint"),
},
inventory: nil,
toReview: handlertest.NewReview("", "foo", "1"),
wantResults: []*types.Result{{
Target: handlertest.TargetName,
Msg: "bad data",
Constraint: cts.MakeConstraint(t, clienttest.KindFuture, "constraint"),
EnforcementAction: constraints.EnforcementActionDeny,
}},
},
{
name: "allow future",
namespaces: nil,
targets: []handler.TargetHandler{&handlertest.Handler{}},
templates: []*templates.ConstraintTemplate{
clienttest.TemplateFuture(),
},
constraints: []*unstructured.Unstructured{
cts.MakeConstraint(t, clienttest.KindFuture, "constraint"),
},
inventory: nil,
toReview: handlertest.NewReview("", "foo", "3"),
wantResults: nil,
},
}

for _, tt := range tests {
Expand Down
10 changes: 10 additions & 0 deletions constraint/pkg/regorewriter/ast_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var (
dataVarTerm = ast.VarTerm("data")
inputRefPrefix = ast.MustParseRef("input")
futureVarTerm = ast.VarTerm("future")
)

// isDataRef returns true if the ast.Ref is referring to the "data" document.
Expand All @@ -22,6 +23,15 @@ func isDataRef(ref ast.Ref) bool {
return firstTerm.Equal(dataVarTerm)
}

func isFutureRef(ref ast.Ref) bool {
if len(ref) == 0 {
return false
}

firstTerm := ref[0]
return firstTerm.Equal(futureVarTerm)
}

// isSubRef returns true if sub is contained within base.
func isSubRef(base, sub ast.Ref) bool {
glog.V(vLogDetail).Infof("Subref check %s %s", base, sub)
Expand Down
8 changes: 6 additions & 2 deletions constraint/pkg/regorewriter/regorewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (r *RegoRewriter) checkImport(i *ast.Import) error {
}

if isSubRef(inputRefPrefix, importRef) {
return fmt.Errorf("%w: bad import", ErrInvalidImport)
return fmt.Errorf("%w: cannot import input: %q", ErrInvalidImport, importRef)
}

for _, libPrefix := range r.allowedLibPrefixes {
Expand All @@ -289,7 +289,11 @@ func (r *RegoRewriter) checkImport(i *ast.Import) error {
}
}

return fmt.Errorf("%w: bad import", ErrInvalidImport)
if isFutureRef(importRef) {
return nil
}

return fmt.Errorf("%w: bad import: %q", ErrInvalidImport, importRef)
}

// checkDataReferences checks that all data references are directed to allowed lib prefixes or
Expand Down

0 comments on commit 370fa37

Please sign in to comment.