Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opa check: only check schema types when user provides schemas #7124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ast/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ func (tc *typeChecker) checkRule(env *TypeEnv, as *AnnotationSet, rule *Rule) {
}

ref := schemaAnnot.Path
if ref == nil && refType == nil {
// if we do not have a ref or a reftype, we should not evaluate this rule.
if ref == nil || refType == nil {
continue
}

Expand Down
22 changes: 16 additions & 6 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestCheckFailsOnInvalidRego(t *testing.T) {

// Assert that 'schemas' annotations with schema refs are only informing the type checker when the --schema flag is used
func TestCheckWithSchemasAnnotationButNoSchemaFlag(t *testing.T) {
policyWithSchemaRef := `
policiesWithSchemaRef := []string{`
package test
import rego.v1
# METADATA
Expand All @@ -220,11 +220,21 @@ import rego.v1
p if {
rego.metadata.rule() # presence of rego.metadata.* calls must not trigger unwanted schema evaluation
input.foo == 42 # type mismatch with schema that should be ignored
}`
}`,
`
package p

err := testCheckWithSchemasAnnotationButNoSchemaFlag(policyWithSchemaRef)
if err != nil {
t.Fatalf("unexpected error from eval with schema ref: %v", err)
# METADATA
# schemas:
# - data.p.x: schema["nope"]
bug := data.p.x
`}

for i, pol := range policiesWithSchemaRef {
err := testCheckWithSchemasAnnotationButNoSchemaFlag(pol)
if err != nil {
t.Fatalf("unexpected error from eval policy %d with schema ref: %v", i, err)
}
}

policyWithInlinedSchema := `
Expand All @@ -238,7 +248,7 @@ p if {
input.foo == 42 # type mismatch with schema that should be ignored
}`

err = testCheckWithSchemasAnnotationButNoSchemaFlag(policyWithInlinedSchema)
err := testCheckWithSchemasAnnotationButNoSchemaFlag(policyWithInlinedSchema)
// We expect an error here, as inlined schemas are always used for type checking
if !strings.Contains(err.Error(), "rego_type_error: match error") {
t.Fatalf("unexpected error from eval with inlined schema, got: %v", err)
Expand Down