-
Notifications
You must be signed in to change notification settings - Fork 31
/
opa.go
269 lines (224 loc) · 7.35 KB
/
opa.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package authorization
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"sync"
"github.com/kuadrant/authorino/pkg/auth"
"github.com/kuadrant/authorino/pkg/log"
"github.com/kuadrant/authorino/pkg/workers"
opaParser "github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
)
const (
policyTemplate = `package %s
default allow = false
%s`
policyUIDHashSeparator = "|"
allowQuery = "allow"
msg_opaPolicyInvalidResponseError = "invalid response from policy evaluation"
msg_OpaPolicyPrecompileError = "failed to precompile policy"
msg_opaPolicyDownloadError = "failed to download policy from external registry"
msg_opaPolicyRefreshFromRegistryError = "failed to refresh policy from external registry"
msg_opaPolicyRefreshFromRegistrySkipped = "external policy unchanged"
msg_opaPolicyRefreshFromRegistrySuccess = "policy updated from external registry"
msg_opaPolicyRefreshFromRegistryDisabled = "auto-refresh of external policy disabled"
)
func NewOPAAuthorization(policyName string, rego string, externalSource *OPAExternalSource, allValues bool, nonce int, ctx context.Context) (*OPA, error) {
logger := log.FromContext(ctx).WithName("opa")
pullFromRegistry := rego == "" && externalSource != nil && externalSource.Endpoint != ""
if pullFromRegistry {
if downloadedRego, err := externalSource.downloadRegoDataFromUrl(); err != nil {
logger.Error(err, msg_opaPolicyDownloadError, "policy", policyName, "endpoint", externalSource.Endpoint)
return nil, err
} else {
rego = downloadedRego
}
}
o := &OPA{
ExternalSource: externalSource,
AllValues: allValues,
policyName: policyName,
policyUID: generatePolicyUID(policyName, rego, nonce),
opaContext: context.TODO(),
}
if _, err := o.updateRego(rego, ctx, true); err != nil {
return nil, err
} else {
if pullFromRegistry {
externalSource.setupRefresher(log.IntoContext(ctx, logger), o)
}
return o, nil
}
}
type OPA struct {
Rego string `yaml:"rego"`
ExternalSource *OPAExternalSource
AllValues bool
opaContext context.Context
policy *rego.PreparedEvalQuery
policyName string
policyUID string
mu sync.RWMutex
}
func (opa *OPA) Call(pipeline auth.AuthPipeline, ctx context.Context) (interface{}, error) {
opa.mu.RLock()
defer opa.mu.RUnlock()
var authJSON interface{}
if err := json.Unmarshal([]byte(pipeline.GetAuthorizationJSON()), &authJSON); err != nil {
return false, err
} else {
options := rego.EvalInput(authJSON)
results, err := opa.policy.Eval(opa.opaContext, options)
if err != nil {
return nil, err
} else if len(results) == 0 {
return nil, fmt.Errorf(msg_opaPolicyInvalidResponseError)
} else if allowed, ok := results[0].Bindings[allowQuery].(bool); !ok || !allowed {
return nil, fmt.Errorf(unauthorizedErrorMsg)
} else {
return results[0].Bindings, nil
}
}
}
// Clean ensures the goroutine started by ExternalSource.setupRefresher is cleaned up
func (opa *OPA) Clean(_ context.Context) error {
if opa.ExternalSource == nil {
return nil
}
return opa.ExternalSource.cleanupRefresher()
}
func (opa *OPA) updateRego(rego string, ctx context.Context, force bool) (bool, error) {
opa.mu.Lock()
defer opa.mu.Unlock()
newRego := cleanUpRegoDocument(rego)
currentRego := opa.Rego
if !force && hash(newRego) == hash(currentRego) {
return false, nil
}
opa.Rego = newRego
if policy, err := precompilePolicy(opa.opaContext, opa.policyUID, opa.Rego, opa.AllValues); err != nil {
opa.Rego = currentRego
log.FromContext(ctx).Error(err, msg_OpaPolicyPrecompileError, "policy", opa.policyName)
return false, err
} else {
opa.policy = policy
return true, nil
}
}
func precompilePolicy(ctx context.Context, policyUID, policyRego string, allValues bool) (*rego.PreparedEvalQuery, error) {
policyName := fmt.Sprintf(`authorino.authz["%s"]`, policyUID)
policyContent := fmt.Sprintf(policyTemplate, policyName, policyRego)
policyFileName := policyUID + ".rego"
queryTemplate := `%s = object.get(data.` + policyName + `, "%s", null)`
var module *opaParser.Module
queries := []string{fmt.Sprintf(queryTemplate, allowQuery, allowQuery)}
var err error
if module, err = opaParser.ParseModule(policyFileName, policyContent); err != nil {
return nil, err
}
if allValues {
rules := map[string]interface{}{allowQuery: nil}
for _, rule := range module.Rules {
name := string(rule.Head.Name)
if _, found := rules[name]; !found {
queries = append(queries, fmt.Sprintf(queryTemplate, name, name))
rules[name] = nil
}
}
}
r := rego.New(
rego.Query(strings.Join(queries, ";")),
rego.ParsedModule(module),
)
if regoPolicy, err := r.PrepareForEval(ctx); err != nil {
return nil, err
} else {
return ®oPolicy, nil
}
}
func cleanUpRegoDocument(rego string) string {
r, _ := regexp.Compile("(\\s)*package.*[;\\n]+")
return r.ReplaceAllString(rego, "")
}
func generatePolicyUID(policyName string, policyContent string, nonce int) string {
return hash(fmt.Sprint(nonce) + policyUIDHashSeparator + policyName + policyUIDHashSeparator + policyContent)
}
func hash(s string) string {
data := []byte(s)
return fmt.Sprintf("%x", sha256.Sum256(data))
}
type responseOpaJson struct {
Result resultJson `json:"result"`
}
type resultJson struct {
Raw string `json:"raw"`
}
type OPAExternalSource struct {
Endpoint string
SharedSecret string
auth.AuthCredentials
TTL int
refresher workers.Worker
}
func (ext *OPAExternalSource) downloadRegoDataFromUrl() (string, error) {
req, err := ext.BuildRequestWithCredentials(context.TODO(), ext.Endpoint, "GET", ext.SharedSecret, nil)
if err != nil {
return "", err
}
if resp, err := http.DefaultClient.Do(req); err != nil {
return "", fmt.Errorf("failed to fetch Rego config: %v", err)
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("unable to read response body: %v", err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("%s: %s", resp.Status, body)
}
result := string(body)
//json
if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
var jsonResponse responseOpaJson
if err := json.Unmarshal(body, &jsonResponse); err != nil {
return "", fmt.Errorf("unable to unmarshal json response: %v", err)
}
result = jsonResponse.Result.Raw
}
return result, nil
}
}
func (ext *OPAExternalSource) setupRefresher(ctx context.Context, opa *OPA) {
logger := log.FromContext(ctx).WithValues("policy", opa.policyName, "endpoint", ext.Endpoint)
var startErr error
ext.refresher, startErr = workers.StartWorker(ctx, ext.TTL, func() {
if downloadedRego, err := ext.downloadRegoDataFromUrl(); err == nil {
if updated, err := opa.updateRego(downloadedRego, ctx, false); updated {
logger.Info(msg_opaPolicyRefreshFromRegistrySuccess)
} else {
if err != nil {
logger.Error(err, msg_opaPolicyRefreshFromRegistryError)
} else {
logger.V(1).Info(msg_opaPolicyRefreshFromRegistrySkipped)
}
}
} else {
logger.Error(err, msg_opaPolicyDownloadError)
}
})
if startErr != nil {
logger.V(1).Info(msg_opaPolicyRefreshFromRegistryDisabled, "reason", startErr)
}
}
func (ext *OPAExternalSource) cleanupRefresher() error {
if ext.refresher == nil {
return nil
}
return ext.refresher.Stop()
}