/
oso.go
528 lines (467 loc) · 14.4 KB
/
oso.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package oso
import (
"errors"
"fmt"
"os"
"runtime"
osoErrors "github.com/osohq/go-oso/errors"
"github.com/osohq/go-oso/internal/host"
"github.com/osohq/go-oso/types"
)
/*
The central object to manage policy state and verify requests.
*/
type Oso struct {
p *Polar
readAction interface{}
forbiddenError func() error
notFoundError func() error
}
/*
Construct a new Oso instance.
import oso "github.com/osohq/go-oso"
if o, err := oso.NewOso(); err != nil {
t.Fatalf("Failed to set up Oso: %v", err)
}
*/
func NewOso() (Oso, error) {
if p, e := newPolar(); e != nil {
return Oso{}, e
} else {
return Oso{
p: p,
readAction: "read",
forbiddenError: func() error { return &osoErrors.ForbiddenError{} },
notFoundError: func() error { return &osoErrors.NotFoundError{} },
}, nil
}
}
func (o *Oso) GetHost() *host.Host {
return &o.p.host
}
/*
Override the "read" action, which is used to differentiate between a
NotFoundError and a ForbiddenError on authorization failures.
o, _ = oso.NewOso()
o.SetReadAction("READ")
*/
func (o *Oso) SetReadAction(readAction interface{}) {
o.readAction = readAction
}
/*
Override the default ForbiddenError, returned when authorization fails.
o, _ = oso.NewOso()
o.SetForbiddenError(func() error { return &MyCustomError{} })
*/
func (o *Oso) SetForbiddenError(forbiddenError func() error) {
o.forbiddenError = forbiddenError
}
/*
Override the default NotFoundError, returned by the Authorize method when a user
does not have read permission.
o, _ = oso.NewOso()
o.SetNotFoundError(func() error { return &MyCustomError{} })
*/
func (o *Oso) SetNotFoundError(notFoundError func() error) {
o.notFoundError = notFoundError
}
/*
Load Polar policy from ".polar" files, checking that all inline queries succeed.
*/
func (o Oso) LoadFiles(files []string) error {
return (*o.p).loadFiles(files)
}
/*
Load Polar policy from a ".polar" file, checking that all inline queries succeed.
Deprecated: `Oso.LoadFile` has been deprecated in favor of `Oso.LoadFiles` as
of the 0.20 release. Please see changelog for migration instructions:
https://docs.osohq.com/project/changelogs/2021-09-15.html
*/
func (o Oso) LoadFile(f string) error {
fmt.Fprintln(os.Stderr,
"`Oso.LoadFile` has been deprecated in favor of `Oso.LoadFiles` as of the 0.20 release.\n\n"+
"Please see changelog for migration instructions: https://docs.osohq.com/project/changelogs/2021-09-15.html")
return (*o.p).loadFiles([]string{f})
}
/*
Load Polar policy from a string, checking that all inline queries succeed.
*/
func (o Oso) LoadString(s string) error {
return (*o.p).loadString(s)
}
/*
Clear all rules from the Oso knowledge base (i.e., remove all loaded policies).
*/
func (o Oso) ClearRules() error {
return (*o.p).clearRules()
}
/*
Register a Go type so that it can be referenced within Polar files. Accepts a
concrete value of the Go type and a constructor function or nil if no
constructor is required.
*/
func (o Oso) RegisterClass(cls interface{}, ctor interface{}) error {
return (*o.p).registerClass(cls, ctor, nil, nil)
}
/*
Register a Go type under a certain name/alias so that it can be referenced
within Polar files by that name. Accepts a concrete value of the Go type and a
constructor function or nil if no constructor is required.
*/
func (o Oso) RegisterClassWithName(cls interface{}, ctor interface{}, name string) error {
return (*o.p).registerClass(cls, ctor, &name, nil)
}
/*
Register a Go type under a certain name/alias so that it can be referenced
within Polar files by that name. Accepts a concrete value of the Go type and a
constructor function or nil if no constructor is required.
*/
func (o Oso) RegisterClassWithNameAndFields(cls interface{}, ctor interface{}, name string, fields map[string]interface{}) error {
return (*o.p).registerClass(cls, ctor, &name, fields)
}
/*
Register a Go value as a Polar constant variable called `name`.
*/
func (o Oso) RegisterConstant(value interface{}, name string) error {
return (*o.p).registerConstant(value, name)
}
/*
Query the policy using a query string; the query is run in a new Go routine.
Accepts the string to query for.
Returns a channel of resulting binding maps, and a channel for errors.
As the query is evaluated, all resulting bindings will be written to the results channel,
and any errors will be written to the error channel.
The results channel must be completely consumed or it will leak memory.
*/
func (o Oso) QueryStr(q string) (<-chan map[string]interface{}, <-chan error) {
if query, err := (*o.p).queryStr(q); err != nil {
errors := make(chan error, 1)
go func() {
errors <- err
close(errors)
}()
return nil, errors
} else {
return query.resultsChannel()
}
}
/*
Query the policy for a rule; the query is run in a new Go routine.
Accepts the name of the rule to query, and a variadic list of rule arguments.
Returns a channel of resulting binding maps, and a channel for errors.
As the query is evaluated, all resulting bindings will be written to the results channel,
and any errors will be written to the error channel.
The results channel must be completely consumed or it will leak memory.
*/
func (o Oso) QueryRule(name string, args ...interface{}) (<-chan map[string]interface{}, <-chan error) {
if query, err := (*o.p).queryRule(name, args...); err != nil {
errors := make(chan error, 1)
go func() {
errors <- err
close(errors)
}()
return nil, errors
} else {
return query.resultsChannel()
}
}
/*
Query the policy for a rule, and return true if there are any results. Returns
false if there are no results.
*/
func (o Oso) QueryRuleOnce(name string, args ...interface{}) (bool, error) {
query, err := (*o.p).queryRule(name, args...)
if err != nil {
return false, err
}
results, err := query.Next()
if err != nil {
return false, err
} else if results != nil {
// Manually clean up query since we are not pulling all results.
defer query.Cleanup()
return true, nil
} else {
return false, nil
}
}
/*
Create policy query from a query string.
Accepts the string to query for.
Returns a new *Query, on which `Next()` can be called to get the next result,
or an error.
*/
func (o Oso) NewQueryFromStr(q string) (*Query, error) {
return (*o.p).queryStr(q)
}
/*
Create policy query for a rule.
Accepts the name of the rule to query, and a variadic list of rule arguments.
Returns a new *Query, on which `Next()` can be called to get the next result,
or an error.
*/
func (o Oso) NewQueryFromRule(name string, args ...interface{}) (*Query, error) {
return (*o.p).queryRule(name, args...)
}
/*
Check if an (actor, action, resource) combination is allowed by the policy.
Returns the result as a bool, or an error.
*/
func (o Oso) IsAllowed(actor interface{}, action interface{}, resource interface{}) (bool, error) {
return o.QueryRuleOnce("allow", actor, action, resource)
}
/*
Return a set of actions allowed by the given (actor, resource) combination allowed
by the policy.
Deprecated: Use AuthorizedActions instead.
*/
func (o Oso) GetAllowedActions(actor interface{}, resource interface{}, allowWildcard bool) (map[interface{}]struct{}, error) {
return o.AuthorizedActions(actor, resource, allowWildcard)
}
/*
Ensure that `actor` is allowed to perform `action` on `resource`.
If the action is permitted with an `allow` rule in the policy, then this method
returns `nil`. If the action is not permitted by the policy, this method will
return an error.
The error returned by this method depends on whether the actor can perform the
`"read"` action on the resource. If they cannot read the resource, then a
`NotFoundError` error is returned. Otherwise, a `ForbiddenError` is returned.
You can customize the errors returned by this function using the
`SetReadAction`, `SetForbiddenError`, and `SetNotFoundError` configuration
functions.
*/
func (o Oso) Authorize(actor interface{}, action interface{}, resource interface{}) error {
isAllowed, err := o.QueryRuleOnce("allow", actor, action, resource)
if err != nil {
return err
}
if isAllowed {
return nil
}
// Decide whether to return not found or forbidden error
isNotFound := false
if action == o.readAction {
isNotFound = true
} else {
isReadAllowed, err := o.QueryRuleOnce("allow", actor, o.readAction, resource)
if err != nil {
return err
}
if !isReadAllowed {
isNotFound = true
}
}
if isNotFound {
return o.notFoundError()
} else {
return o.forbiddenError()
}
}
/*
Ensure that `actor` is allowed to send `request` to the server.
Checks the `allow_request` rule of a policy.
If the request is permitted with an `allow_request` rule in the
policy, then this method returns `nil`. Otherwise, this method returns a
`ForbiddenError`.
*/
func (o Oso) AuthorizeRequest(actor interface{}, request interface{}) error {
isAllowed, err := o.QueryRuleOnce("allow_request", actor, request)
if err != nil {
return err
}
if !isAllowed {
return o.forbiddenError()
}
return nil
}
/*
Ensure that `actor` is allowed to perform `action` on a given
`resource`'s `field`.
Checks the `allow_field` rule of a policy.
If the action is permitted by an `allow_field` rule in the policy,
then this method returns `nil`. If the action is not permitted by the
policy, this method returns a `ForbiddenError`.
*/
func (o Oso) AuthorizeField(actor interface{}, action interface{}, resource interface{}, field interface{}) error {
isAllowed, err := o.QueryRuleOnce("allow_field", actor, action, resource, field)
if err != nil {
return err
}
if !isAllowed {
return o.forbiddenError()
}
return nil
}
/*
Return a set of actions allowed by the given (actor, resource) combination allowed
by the policy.
*/
func (o Oso) AuthorizedActions(actor interface{}, resource interface{}, allowWildcard bool) (map[interface{}]struct{}, error) {
results := make(map[interface{}]struct{})
query, err := (*o.p).queryRule("allow", actor, types.Variable("action"), resource)
if err != nil {
return nil, err
}
for {
if v, err := query.Next(); err != nil {
return nil, err
} else if v == nil {
break
} else if action, ok := (*v)["action"].(interface{}); ok {
switch val := (action).(type) {
case types.Variable:
if allowWildcard {
results["*"] = struct{}{}
} else {
return nil, errors.New(`the result of AuthorizedActions() contained an
"unconstrained" action that could represent any
action, but allow_wildcard was set to False. To fix,
set allow_wildcard to True and compare with the "*"
string`)
}
default:
results[val] = struct{}{}
}
}
}
return results, nil
}
/*
Determine the fields of `resource` on which `actor` is allowed to perform
`action`.
Uses `allow_field` rules in the policy to find all allowed fields.
*/
func (o Oso) AuthorizedFields(actor interface{}, action interface{}, resource interface{}, allowWildcard bool) (map[interface{}]struct{}, error) {
results := make(map[interface{}]struct{})
query, err := (*o.p).queryRule("allow_field", actor, action, resource, types.Variable("field"))
if err != nil {
return nil, err
}
for {
if v, err := query.Next(); err != nil {
return nil, err
} else if v == nil {
break
} else if field, ok := (*v)["field"].(interface{}); ok {
switch val := (field).(type) {
case types.Variable:
if allowWildcard {
results["*"] = struct{}{}
} else {
return nil, errors.New(`the result of AuthorizedFields() contained an
"unconstrained" field that could represent any
field, but allow_wildcard was set to False. To fix,
set allow_wildcard to True and compare with the "*"
string`)
}
default:
results[val] = struct{}{}
}
}
}
return results, nil
}
func (o Oso) SetDataFilteringAdapter(adapter types.Adapter) {
(*o.p).host.SetDataFilteringAdapter(adapter)
}
func (o Oso) dataFilter(actor interface{}, action interface{}, resource_type string) (*Query, interface{}, error) {
os := runtime.GOOS
if os == "windows" {
return nil, nil, fmt.Errorf("Data filtering is not yet supported on Windows")
}
query, err := (*o.p).queryRule("allow", actor, action, types.Variable("resource"))
if err != nil {
return nil, nil, err
}
query.SetAcceptExpression(true)
constraint :=
types.Term{
Value: types.Value{
types.ValueExpression{
Operator: types.Operator{types.OperatorAnd{}},
Args: []types.Term{
types.Term{
Value: types.Value{
types.ValueExpression{
Operator: types.Operator{types.OperatorIsa{}},
Args: []types.Term{
types.Term{
Value: types.Value{
types.ValueVariable("resource"),
},
},
types.Term{
Value: types.Value{
types.ValuePattern{
types.PatternInstance{
Tag: types.Symbol(resource_type),
Fields: types.Dictionary{Fields: make(map[types.Symbol]types.Term)},
},
},
},
},
},
},
},
},
},
},
},
}
query.Bind("resource", &constraint)
partials := make([]map[string]map[string]types.Term, 0)
for {
if v, err := query.Next(); err != nil {
return nil, nil, err
} else if v == nil {
break
} else {
m := make(map[string]types.Term)
for k, v := range *v {
polar, err := query.host.ToPolar(v)
if err != nil {
return nil, nil, err
}
m[k] = types.Term{Value: *polar}
}
b := make(map[string]map[string]types.Term, 0)
b["bindings"] = m
partials = append(partials, b)
}
}
types, types_json, err := query.host.SerializeTypes()
if err != nil {
return nil, nil, err
}
filter, err := (*o.p).ffiPolar.BuildDataFilter(types_json, partials, "resource", resource_type)
if err != nil {
return nil, nil, err
}
err = query.host.ParseValues(filter)
if err != nil {
return nil, nil, err
}
filter.Types = types
q, err := query.host.BuildQuery(filter)
if err != nil {
return nil, nil, err
}
return query, q, nil
}
func (o Oso) AuthorizedQuery(actor interface{}, action interface{}, resource_type string) (interface{}, error) {
_, q, err := o.dataFilter(actor, action, resource_type)
return q, err
}
func (o Oso) AuthorizedResources(actor interface{}, action interface{}, resource_type string) ([]interface{}, error) {
query, q, err := o.dataFilter(actor, action, resource_type)
if err != nil {
return nil, err
}
return query.host.ExecuteQuery(q)
}
/*
Start the oso repl where you can make queries and see results printed out.
*/
func (o Oso) Repl() error {
return (*o.p).repl()
}