-
Notifications
You must be signed in to change notification settings - Fork 10
/
authorize.go
65 lines (61 loc) · 1.75 KB
/
authorize.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
package cedar
import (
"github.com/cedar-policy/cedar-go/internal/eval"
"github.com/cedar-policy/cedar-go/types"
)
type Request = types.Request
type Decision = types.Decision
type Diagnostic = types.Diagnostic
type DiagnosticReason = types.DiagnosticReason
type DiagnosticError = types.DiagnosticError
const (
Allow = types.Allow
Deny = types.Deny
)
// IsAuthorized uses the combination of the PolicySet and Entities to determine
// if the given Request to determine Decision and Diagnostic.
func (p PolicySet) IsAuthorized(entities types.EntityGetter, req Request) (Decision, Diagnostic) {
if entities == nil {
var zero types.EntityMap
entities = zero
}
env := eval.Env{
Entities: entities,
Principal: req.Principal,
Action: req.Action,
Resource: req.Resource,
Context: req.Context,
}
var diag Diagnostic
var forbids []DiagnosticReason
var permits []DiagnosticReason
// Don't try to short circuit this.
// - Even though single forbid means forbid
// - All policy should be run to collect errors
// - For permit, all permits must be run to collect annotations
// - For forbid, forbids must be run to collect annotations
for id, po := range p.policies {
result, err := po.eval.Eval(env)
if err != nil {
diag.Errors = append(diag.Errors, DiagnosticError{PolicyID: id, Position: po.Position(), Message: err.Error()})
continue
}
if !result {
continue
}
if po.Effect() == Forbid {
forbids = append(forbids, DiagnosticReason{PolicyID: id, Position: po.Position()})
} else {
permits = append(permits, DiagnosticReason{PolicyID: id, Position: po.Position()})
}
}
if len(forbids) > 0 {
diag.Reasons = forbids
return Deny, diag
}
if len(permits) > 0 {
diag.Reasons = permits
return Allow, diag
}
return Deny, diag
}