-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolicies.go
77 lines (66 loc) · 1.87 KB
/
policies.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
package ac
// Policy consists of rules and constranits to validate Resource against.
type Policy struct {
ID string
Version string
Name string
Owner string
Rules []Rule
// Constraints represnet general constraints such as subject constraints
// or temporal/spacial constraints related to the context of the execution.
Constraints []Constraint
}
// ResourceID returns Policy ID.
func (p Policy) ResourceID() string {
return p.ID
}
// Type returns Policy resource type.
func (p Policy) Type() string {
return "policy"
}
// Attributes method returns Policy attributes.
func (p Policy) Attributes() map[string]string {
return map[string]string{
"version": p.Version,
"name": p.Name,
"owner": p.Owner,
}
}
// Evaluate validates the access request against the Policy.
func (p Policy) Evaluate(subject Resource, action Action, object Resource) bool {
for _, c := range p.Constraints {
// Constraints are only evaluated against subject.
if !c.Validate(subject) {
return false
}
}
rt := object.Type()
for _, rule := range p.Rules {
if rule.ResourceType == rt {
for _, a := range rule.Actions {
if a == Any || a == action {
// Matcher can be omitted.
if rule.Matcher == nil || rule.Matcher.Match(subject, object) {
return rule.Effect
}
}
}
}
}
return false
}
// PolicyRepository exposes Policy persistence API.
type PolicyRepository interface {
// Save a single policy.
Save(policy Policy) (string, error)
// RetrieveByID retrieves the Policy by its ID.
RetrieveByID(id string) (Policy, error)
// List returns all the policies that belong to the owner.
List(owner string) ([]Policy, error)
// Remove an existing policy.
Remove(id string) error
// Attach adds policy to the resource.
Attach(policyID, resourceID string) error
// Detach removes policy from the resource.
Detach(policyID, resourceID string) error
}