-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
acl.go
259 lines (209 loc) · 5.7 KB
/
acl.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
package main
import (
"fmt"
"net/http"
"regexp"
"strings"
"github.com/Luzifer/go_helpers/v2/str"
)
const (
groupAnonymous = "@_anonymous"
groupAuthenticated = "@_authenticated"
)
type (
acl struct {
RuleSets []aclRuleSet `yaml:"rule_sets"`
}
aclRule struct {
Field string `yaml:"field"`
Invert bool `yaml:"invert"`
IsPresent *bool `yaml:"present"`
MatchRegex *string `yaml:"regexp"`
MatchString *string `yaml:"equals"`
}
aclAccessResult uint
aclRuleSet struct {
Rules []aclRule `yaml:"rules"`
Allow []string `yaml:"allow"`
Deny []string `yaml:"deny"`
}
)
const (
accessDunno aclAccessResult = iota
accessAllow
accessDeny
)
// --- ACL
func (a acl) HasAccess(user string, groups []string, r *http.Request) bool {
var (
collectionAllow = map[string]bool{}
collectionDeny = map[string]bool{}
)
for _, rs := range a.RuleSets {
if !rs.AppliesToRequest(r) {
continue
}
// Collect the allows from all matching rulesets
for _, a := range rs.Allow {
collectionAllow[a] = true
}
// Collect the denies from all matching rulesets
for _, d := range rs.Deny {
collectionDeny[d] = true
}
}
// Form lists from the collections
var allowed, denied []string
for k := range collectionAllow {
allowed = append(allowed, k)
}
for k := range collectionDeny {
denied = append(denied, k)
}
return a.checkAccess(user, groups, allowed, denied)
}
func (a acl) Validate() error {
for i, r := range a.RuleSets {
if err := r.Validate(); err != nil {
return fmt.Errorf("RuleSet on position %d is invalid: %s", i+1, err)
}
}
return nil
}
func (a acl) checkAccess(user string, groups, allowed, denied []string) bool {
if !str.StringInSlice(user, []string{"", "\x00"}) {
// The user is set to a non-anon user, we add the pseudo-group
// authenticated to the groups list the user has
groups = append(groups, groupAuthenticated)
} else {
// The user did match anon, therefore we set the pseudo-group
// for anonymous to be used in group matching
groups = []string{groupAnonymous}
}
// Quoting the documentation here:
// "There is a simple logic: Users before groups, denies before allows."
// Lets check the user
if str.StringInSlice(user, denied) {
// Explicit deny on the user, they're out!
return false
}
if str.StringInSlice(user, allowed) {
// Explicit allow on the user, they're in!
return true
}
// The user yielded no result, lets check the groups
for _, group := range groups {
if str.StringInSlice(a.fixGroupName(group), denied) {
// The group is denied access
return false
}
if str.StringInSlice(a.fixGroupName(group), allowed) {
// The group is allowed access
return true
}
}
// We found no match for the user and/or group. Last chance is
// no ruleset denied anonymous access and at least one ruleset
// enabled anonymous access
if !str.StringInSlice(groupAnonymous, denied) && str.StringInSlice(groupAnonymous, allowed) {
return true
}
// We found neither a user nor a group with access or deny config
// so we fall back to the default: No access.
return false
}
func (acl) fixGroupName(group string) string {
return "@" + strings.TrimLeft(group, "@")
}
// --- ACL Rule
// AppliesToFields checks whether the given rule conditions matches
// the given fields
func (a aclRule) AppliesToFields(fields map[string]string) bool {
var field, value string
for f, v := range fields {
if strings.ToLower(a.Field) == f {
field = f
value = v
break
}
}
if a.IsPresent != nil {
if !a.Invert && *a.IsPresent && field == "" {
// Field is expected to be present but isn't, rule does not apply
return false
}
if !a.Invert && !*a.IsPresent && field != "" {
// Field is expected not to be present but is, rule does not apply
return false
}
if a.Invert && *a.IsPresent && field != "" {
// Field is expected not to be present but is, rule does not apply
return false
}
if a.Invert && !*a.IsPresent && field == "" {
// Field is expected to be present but isn't, rule does not apply
return false
}
return true
}
if field == "" {
// We found a rule which has no matching field, rule does not apply
return false
}
if a.MatchString != nil {
if (*a.MatchString != value) == !a.Invert {
// Value does not match expected string, rule does not apply
return false
}
}
if a.MatchRegex != nil {
if regexp.MustCompile(*a.MatchRegex).MatchString(value) == a.Invert {
// Value does not match expected regexp, rule does not apply
return false
}
}
return true
}
func (a aclRule) Validate() error {
if a.Field == "" {
return fmt.Errorf("field is not set")
}
if a.IsPresent == nil && a.MatchRegex == nil && a.MatchString == nil {
return fmt.Errorf("no matcher (present, regexp, equals) is set")
}
if a.MatchRegex != nil {
if _, err := regexp.Compile(*a.MatchRegex); err != nil {
return fmt.Errorf("regexp is invalid: %s", err)
}
}
return nil
}
// --- ACL Rule Set
// AppliesToRequest checks whether every rule in the aclRuleSet
// matches the http.Request. If not this rule-set must not be applied
// to the given request
func (a aclRuleSet) AppliesToRequest(r *http.Request) bool {
fields := a.buildFieldSet(r)
for _, rule := range a.Rules {
if !rule.AppliesToFields(fields) {
// At least one rule does not match the request
return false
}
}
return true
}
func (a aclRuleSet) Validate() error {
for i, r := range a.Rules {
if err := r.Validate(); err != nil {
return fmt.Errorf("rule on position %d is invalid: %s", i+1, err)
}
}
return nil
}
func (a aclRuleSet) buildFieldSet(r *http.Request) map[string]string {
result := map[string]string{}
for k := range r.Header {
result[strings.ToLower(k)] = r.Header.Get(k)
}
return result
}