-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
authorizer.go
279 lines (240 loc) · 7.15 KB
/
authorizer.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
270
271
272
273
274
275
276
277
278
279
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package authorizer provides authorization handlers to the server.
package authorizer
import (
"context"
"io"
"net/http"
"net/url"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/server/identifier"
"github.com/open-policy-agent/opa/server/types"
"github.com/open-policy-agent/opa/server/writer"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/topdown/cache"
"github.com/open-policy-agent/opa/topdown/print"
"github.com/open-policy-agent/opa/util"
)
// Basic provides policy-based authorization over incoming requests.
type Basic struct {
inner http.Handler
compiler func() *ast.Compiler
store storage.Store
runtime *ast.Term
decision func() ast.Ref
printHook print.Hook
enablePrintStatements bool
interQueryCache cache.InterQueryCache
}
// Runtime returns an argument that sets the runtime on the authorizer.
func Runtime(term *ast.Term) func(*Basic) {
return func(b *Basic) {
b.runtime = term
}
}
// Decision returns an argument that sets the path of the authorization decision
// to query.
func Decision(ref func() ast.Ref) func(*Basic) {
return func(b *Basic) {
b.decision = ref
}
}
// PrintHook sets the object to use for handling print statement outputs.
func PrintHook(printHook print.Hook) func(*Basic) {
return func(b *Basic) {
b.printHook = printHook
}
}
// EnablePrintStatements enables print() calls. If this option is not provided,
// print() calls will be erased from the policy. This option only applies to
// queries and policies that passed as raw strings, i.e., this function will not
// have any affect if the caller supplies the ast.Compiler instance.
func EnablePrintStatements(yes bool) func(r *Basic) {
return func(b *Basic) {
b.enablePrintStatements = yes
}
}
// InterQueryCache enables the inter-query cache on the authorizer
func InterQueryCache(interQueryCache cache.InterQueryCache) func(*Basic) {
return func(b *Basic) {
b.interQueryCache = interQueryCache
}
}
// NewBasic returns a new Basic object.
func NewBasic(inner http.Handler, compiler func() *ast.Compiler, store storage.Store, opts ...func(*Basic)) http.Handler {
b := &Basic{
inner: inner,
compiler: compiler,
store: store,
}
for _, opt := range opts {
opt(b)
}
return b
}
func (h *Basic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// TODO(tsandall): Pass AST value as input instead of Go value to avoid unnecessary
// conversions.
r, input, err := makeInput(r)
if err != nil {
writer.ErrorString(w, http.StatusBadRequest, types.CodeInvalidParameter, err)
return
}
rego := rego.New(
rego.Query(h.decision().String()),
rego.Compiler(h.compiler()),
rego.Store(h.store),
rego.Input(input),
rego.Runtime(h.runtime),
rego.EnablePrintStatements(h.enablePrintStatements),
rego.PrintHook(h.printHook),
rego.InterQueryBuiltinCache(h.interQueryCache),
)
rs, err := rego.Eval(r.Context())
if err != nil {
writer.ErrorAuto(w, err)
return
}
if len(rs) == 0 {
// Authorizer was configured but no policy defined. This indicates an internal error or misconfiguration.
writer.Error(w, http.StatusInternalServerError, types.NewErrorV1(types.CodeInternal, types.MsgUnauthorizedUndefinedError))
return
}
switch allowed := rs[0].Expressions[0].Value.(type) {
case bool:
if allowed {
h.inner.ServeHTTP(w, r)
return
}
case map[string]interface{}:
if decision, ok := allowed["allowed"]; ok {
if allow, ok := decision.(bool); ok && allow {
h.inner.ServeHTTP(w, r)
return
}
if reason, ok := allowed["reason"]; ok {
message, ok := reason.(string)
if ok {
writer.Error(w, http.StatusUnauthorized, types.NewErrorV1(types.CodeUnauthorized, message))
return
}
}
} else {
writer.Error(w, http.StatusInternalServerError, types.NewErrorV1(types.CodeInternal, types.MsgUndefinedError))
return
}
}
writer.Error(w, http.StatusUnauthorized, types.NewErrorV1(types.CodeUnauthorized, types.MsgUnauthorizedError))
}
func makeInput(r *http.Request) (*http.Request, interface{}, error) {
path, err := parsePath(r.URL.Path)
if err != nil {
return r, nil, err
}
method := strings.ToUpper(r.Method)
query := r.URL.Query()
var rawBody []byte
if expectBody(r.Method, path) {
var err error
rawBody, err = io.ReadAll(r.Body)
if err != nil {
return r, nil, err
}
}
input := map[string]interface{}{
"path": path,
"method": method,
"params": query,
"headers": r.Header,
}
if len(rawBody) > 0 {
var body interface{}
if expectYAML(r) {
if err := util.Unmarshal(rawBody, &body); err != nil {
return r, nil, err
}
} else if err := util.UnmarshalJSON(rawBody, &body); err != nil {
return r, nil, err
}
// We cache the parsed body on the context so the server does not have
// to parse the input document twice.
input["body"] = body
ctx := SetBodyOnContext(r.Context(), body)
r = r.WithContext(ctx)
}
identity, ok := identifier.Identity(r)
if ok {
input["identity"] = identity
}
clientCertificates, ok := identifier.ClientCertificates(r)
if ok {
input["client_certificates"] = clientCertificates
}
return r, input, nil
}
var dataAPIVersions = map[string]bool{
"v0": true,
"v1": true,
}
func expectBody(method string, path []interface{}) bool {
if method == http.MethodPost {
if len(path) == 1 {
s := path[0].(string)
return s == ""
} else if len(path) >= 2 {
s1 := path[0].(string)
s2 := path[1].(string)
return dataAPIVersions[s1] && s2 == "data"
}
}
return false
}
func expectYAML(r *http.Request) bool {
// NOTE(tsandall): This check comes from the server's HTTP handler code. The docs
// are a bit more strict, but the authorizer should be consistent w/ the original
// server handler implementation.
return strings.Contains(r.Header.Get("Content-Type"), "yaml")
}
func parsePath(path string) ([]interface{}, error) {
if len(path) == 0 {
return []interface{}{}, nil
}
parts := strings.Split(path[1:], "/")
for i := range parts {
var err error
parts[i], err = url.PathUnescape(parts[i])
if err != nil {
return nil, err
}
}
sl := make([]interface{}, len(parts))
for i := range sl {
sl[i] = parts[i]
}
return sl, nil
}
type authorizerCachedBody struct {
parsed interface{}
}
type authorizerCachedBodyKey string
const ctxkey authorizerCachedBodyKey = "authorizerCachedBodyKey"
// SetBodyOnContext adds the parsed input value to the context. This function is only
// exposed for test purposes.
func SetBodyOnContext(ctx context.Context, x interface{}) context.Context {
return context.WithValue(ctx, ctxkey, authorizerCachedBody{
parsed: x,
})
}
// GetBodyOnContext returns the parsed input from the request context if it exists.
// The authorizer saves the parsed input on the context when it runs.
func GetBodyOnContext(ctx context.Context) (interface{}, bool) {
input, ok := ctx.Value(ctxkey).(authorizerCachedBody)
if !ok {
return nil, false
}
return input.parsed, true
}