Skip to content

Commit 7da9cb2

Browse files
author
hectorj2f
committed
feat: add rego policy support
Signed-off-by: hectorj2f <[email protected]>
1 parent 4f02c2d commit 7da9cb2

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

pkg/policy/eval.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"cuelang.org/go/cue/cuecontext"
23+
"github.com/sigstore/cosign/pkg/cosign/rego"
2324

2425
"knative.dev/pkg/logging"
2526
)
@@ -42,7 +43,7 @@ func EvaluatePolicyAgainstJSON(ctx context.Context, name, policyType string, pol
4243
case "rego":
4344
regoValidationErr := evaluateRego(ctx, jsonBytes, policyBody)
4445
if regoValidationErr != nil {
45-
return fmt.Errorf("failed evaluating rego policy for type %s", name)
46+
return fmt.Errorf("failed evaluating rego policy for type %s: %s", name, regoValidationErr.Error())
4647
}
4748
default:
4849
return fmt.Errorf("sorry Type %s is not supported yet", policyType)
@@ -73,9 +74,8 @@ func evaluateCue(ctx context.Context, attestation []byte, evaluator string) erro
7374

7475
// evaluateRego evaluates a rego policy `evaluator` against `attestation`
7576
func evaluateRego(ctx context.Context, attestation []byte, evaluator string) error {
76-
// TODO(vaikas) Fix this
77-
// The existing stuff wants files, and it doesn't work. There must be
78-
// a way to load it from a []byte like we can do with cue. Tomorrows problem
79-
// regoValidationErrs := rego.ValidateJSON(payload, regoPolicies)
80-
return fmt.Errorf("TODO(vaikas): Don't know how to this from bytes yet")
77+
logging.FromContext(ctx).Infof("Evaluating attestation: %s", string(attestation))
78+
logging.FromContext(ctx).Infof("Evaluating evaluator: %s", evaluator)
79+
80+
return rego.ValidateJSONWithModuleInput(attestation, evaluator)
8181
}

pkg/policy/eval_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,38 @@ func TestEvalPolicy(t *testing.T) {
167167
keylesssignature: {
168168
signatures: list.MaxItems(1) & list.MinItems(1)
169169
}
170-
}`,
171-
}}
170+
}`}, {
171+
name: "Rego cluster image policy main policy, checks out",
172+
json: cipAttestation,
173+
policyType: "rego",
174+
policyFile: `package sigstore
175+
default isCompliant = false
176+
isCompliant {
177+
attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
178+
count(attestationsKeylessATT) == 1
179+
attestationsKeyATT := input.authorityMatches.keyatt.attestations
180+
count(attestationsKeyATT) == 1
181+
keySignature := input.authorityMatches.keysignature.signatures
182+
count(keySignature) == 1
183+
}`,
184+
},
185+
{
186+
name: "Rego cluster image policy main policy, fails",
187+
json: cipAttestation,
188+
policyType: "rego",
189+
wantErr: true,
190+
wantErrSub: `failed evaluating rego policy for type Rego cluster image policy main policy, fails: policy is not compliant for query 'isCompliant = data.sigstore.isCompliant'`,
191+
policyFile: `package sigstore
192+
default isCompliant = false
193+
isCompliant {
194+
attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
195+
count(attestationsKeylessATT) == 2
196+
attestationsKeyATT := input.authorityMatches.keyatt.attestations
197+
count(attestationsKeyATT) == 1
198+
keySignature := input.authorityMatches.keysignature.signatures
199+
count(keySignature) == 1
200+
}`,
201+
}}
172202
for _, tc := range tests {
173203
ctx := context.Background()
174204
err := EvaluatePolicyAgainstJSON(ctx, tc.name, tc.policyType, tc.policyFile, []byte(tc.json))

0 commit comments

Comments
 (0)