This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolicy.go
163 lines (132 loc) · 3.77 KB
/
policy.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
package consulacl
import (
"github.com/hashicorp/consul/acl"
)
// Policy represents a consul ACL policy
type Policy struct {
agent GrantMap
key GrantMap
node GrantMap
service GrantMap
session GrantMap
event GrantMap
query GrantMap
keyring Grant
operator Grant
}
// Equals checks if the policy matches another policy
func (p *Policy) Equals(other *Policy) bool {
return other != nil &&
p.keyring == other.keyring &&
p.operator == other.operator &&
p.agent.Equals(&other.agent) &&
p.key.Equals(&other.key) &&
p.node.Equals(&other.node) &&
p.service.Equals(&other.service) &&
p.session.Equals(&other.session) &&
p.event.Equals(&other.event) &&
p.query.Equals(&other.query)
}
// SetKeyring configures the keyring grant
func (p *Policy) SetKeyring(grant Grant) {
p.keyring = grant
}
// GetKeyring retrieves the keyring grant
func (p *Policy) GetKeyring() Grant {
return p.keyring
}
// SetOperator configures the operator grant
func (p *Policy) SetOperator(grant Grant) {
p.operator = grant
}
// GetOperator retrieves the operator grant
func (p *Policy) GetOperator() Grant {
return p.operator
}
// Agent returns the agent GrantMap
func (p *Policy) Agent() *GrantMap {
return &p.agent
}
// Key returns the key GrantMap
func (p *Policy) Key() *GrantMap {
return &p.key
}
// Node returns the node GrantMap
func (p *Policy) Node() *GrantMap {
return &p.node
}
// Service returns the service GrantMap
func (p *Policy) Service() *GrantMap {
return &p.service
}
// Session returns the session GrantMap
func (p *Policy) Session() *GrantMap {
return &p.session
}
// Event returns the event GrantMap
func (p *Policy) Event() *GrantMap {
return &p.event
}
// Query returns the query GrantMap
func (p *Policy) Query() *GrantMap {
return &p.query
}
// Clone creates a copy of the policy
func (p *Policy) Clone() *Policy {
// Create a new policy and apply the basic keyring and operator grants
clone := &Policy{
keyring: p.keyring,
operator: p.operator,
}
// Create clones of all embedded GrantMap instances
clone.agent = *p.agent.Clone()
clone.key = *p.key.Clone()
clone.node = *p.node.Clone()
clone.service = *p.service.Clone()
clone.session = *p.session.Clone()
clone.event = *p.event.Clone()
clone.query = *p.query.Clone()
return clone
}
// NewPolicy constructs a new policy
func NewPolicy() *Policy {
return &Policy{}
}
// NewPolicyFromACLPolicy constructs a new policy and fills its state with the state represented by the provided aclPolicy
func NewPolicyFromACLPolicy(aclPolicy *acl.Policy) *Policy {
p := NewPolicy()
// Convert keyring and operator grants
p.keyring = GrantByName(aclPolicy.Keyring)
p.operator = GrantByName(aclPolicy.Operator)
for _, policy := range aclPolicy.Agents {
p.agent.Set(policy.Node, GrantByName(policy.Policy))
}
for _, policy := range aclPolicy.Keys {
p.key.Set(policy.Prefix, GrantByName(policy.Policy))
}
for _, policy := range aclPolicy.Nodes {
p.node.Set(policy.Name, GrantByName(policy.Policy))
}
for _, policy := range aclPolicy.Services {
p.service.Set(policy.Name, GrantByName(policy.Policy))
}
for _, policy := range aclPolicy.Sessions {
p.session.Set(policy.Node, GrantByName(policy.Policy))
}
for _, policy := range aclPolicy.Events {
p.event.Set(policy.Event, GrantByName(policy.Policy))
}
for _, policy := range aclPolicy.PreparedQueries {
p.query.Set(policy.Prefix, GrantByName(policy.Policy))
}
return p
}
// NewPolicyFromRules constructs a new policy and fills its state with the state represented by the rules string
func NewPolicyFromRules(rules string) (*Policy, error) {
// Parse the rules. Sentinel is nil as it is unused except for consul enterprise
aclPolicy, err := acl.Parse(rules, nil)
if err != nil {
return nil, err
}
return NewPolicyFromACLPolicy(aclPolicy), nil
}