-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit addresses multiple issues when applying a rego policy against the payload of an attestation. 1. If `data.signature.deny` evaluated to `true`, the policy verification would pass. This is obviously unexpected. The code now looks for `data.signature.allow` instead, and expects it to be `true`. 2. If a query result returned an undefined results, the policy verification would pass. The code now explicitly checks for this condition and ensure that if `ResultSet.IsAllowed()` returns `false`, the policy verification also fails. 3. Improve error messages to assist user in defining correct variable name and type. 4. Add unit tests to validate behavior and prevent breaking changes in the future. Signed-off-by: Luiz Carvalho <[email protected]>
- Loading branch information
Showing
2 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package rego | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
const simpleJSONBody = `{ | ||
"_type": "https://in-toto.io/Statement/v0.1", | ||
"predicateType": "https://slsa.dev/provenance/v0.2" | ||
}` | ||
|
||
func TestValidationJSON(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
jsonBody string | ||
policy string | ||
pass bool | ||
errors []string | ||
}{ | ||
{ | ||
name: "passing policy", | ||
jsonBody: simpleJSONBody, | ||
policy: ` | ||
package signature | ||
allow { | ||
input.predicateType == "https://slsa.dev/provenance/v0.2" | ||
} | ||
`, | ||
pass: true, | ||
}, | ||
{ | ||
name: "undefined result due to no matching rules", | ||
jsonBody: simpleJSONBody, | ||
policy: ` | ||
package signature | ||
allow { | ||
input.predicateType == "https://slsa.dev/provenance/v99.9" | ||
} | ||
`, | ||
pass: false, | ||
errors: []string{"result is undefined for query 'data.signature.allow'"}, | ||
}, | ||
{ | ||
name: "policy query evaluates to false", | ||
jsonBody: simpleJSONBody, | ||
policy: ` | ||
package signature | ||
default allow = false | ||
`, | ||
pass: false, | ||
errors: []string{"expression value, false, is not true"}, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
policyFileName := filepath.Join(t.TempDir(), "policy.rego") | ||
if err := os.WriteFile(policyFileName, []byte(tt.policy), 0666); err != nil { | ||
t.Fatalf("Unable to create temporary policy file at %s", policyFileName) | ||
} | ||
|
||
if errs := ValidateJSON([]byte(tt.jsonBody), []string{policyFileName}); (errs == nil) != tt.pass { | ||
t.Fatalf("Unexpected result: %v", errs) | ||
} else if errs != nil { | ||
if len(errs) != len(tt.errors) { | ||
t.Fatalf("Expected %d errors, got %d errors: %v", len(tt.errors), len(errs), errs) | ||
} | ||
for i, err := range errs { | ||
if fmt.Sprintf("%s", err) != tt.errors[i] { | ||
t.Errorf("Expected error %q, got %q", tt.errors[i], err) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |