-
Notifications
You must be signed in to change notification settings - Fork 499
/
rego.go
121 lines (100 loc) · 3.33 KB
/
rego.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rego
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/open-policy-agent/opa/rego"
"knative.dev/pkg/logging"
)
// The query below should meet the following requirements:
// * Provides no Bindings. Do not use a query that sets a variable, e.g. x := data.signature.allow
// * Queries for a single value.
const QUERY = "data.signature.allow"
// CosignRegoPackageName defines the expected package name of a provided rego module
const CosignRegoPackageName = "sigstore"
// CosignEvaluationRule defines the expected evaluation role of a provided rego module
const CosignEvaluationRule = "isCompliant"
func ValidateJSON(jsonBody []byte, entrypoints []string) []error {
ctx := context.Background()
r := rego.New(
rego.Query(QUERY),
rego.Load(entrypoints, nil))
query, err := r.PrepareForEval(ctx)
if err != nil {
return []error{err}
}
var input interface{}
dec := json.NewDecoder(bytes.NewBuffer(jsonBody))
dec.UseNumber()
if err := dec.Decode(&input); err != nil {
return []error{err}
}
rs, err := query.Eval(ctx, rego.EvalInput(input))
if err != nil {
return []error{err}
}
// Ensure the resultset contains a single result where the Expression contains a single value
// which is true and there are no Bindings.
if rs.Allowed() {
return nil
}
var errs []error
for _, result := range rs {
for _, expression := range result.Expressions {
errs = append(errs, fmt.Errorf("expression value, %v, is not true", expression))
}
}
// When rs.Allowed() is not true and len(rs) is 0, the result is undefined. This is a policy
// check failure.
if len(errs) == 0 {
errs = append(errs, fmt.Errorf("result is undefined for query '%s'", QUERY))
}
return errs
}
// ValidateJSONWithModuleInput takes the body of the results to evaluate and the defined module
// in a policy to validate against the input data
func ValidateJSONWithModuleInput(jsonBody []byte, moduleInput string) error {
ctx := context.Background()
query := fmt.Sprintf("%s = data.%s.%s", CosignEvaluationRule, CosignRegoPackageName, CosignEvaluationRule)
module := fmt.Sprintf("%s.rego", CosignRegoPackageName)
r := rego.New(
rego.Query(query),
rego.Module(module, moduleInput))
evalQuery, err := r.PrepareForEval(ctx)
if err != nil {
return err
}
var input interface{}
dec := json.NewDecoder(bytes.NewBuffer(jsonBody))
dec.UseNumber()
if err := dec.Decode(&input); err != nil {
return err
}
rs, err := evalQuery.Eval(ctx, rego.EvalInput(input))
if err != nil {
return err
}
for _, result := range rs {
isCompliant, ok := result.Bindings[CosignEvaluationRule].(bool)
if ok && isCompliant {
logging.FromContext(ctx).Info("Validated policy is compliant")
return nil
}
}
return fmt.Errorf("policy is not compliant for query '%s'", query)
}