-
Notifications
You must be signed in to change notification settings - Fork 3
/
permission_parser.go
211 lines (182 loc) · 5.35 KB
/
permission_parser.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
// Copyright 2017 Keydap. All rights reserved.
// Licensed under the Apache License, Version 2.0, see LICENSE.
package base
import (
"encoding/json"
"sparrow/schema"
"strings"
)
type opsObj struct {
Op string
AllowAttrs string
DenyAttrs string
Filter string
}
func ParseResPerms(group *Resource, resTypes map[string]*schema.ResourceType) map[string]*ResourcePermission {
resPerms := make(map[string]*ResourcePermission)
perms := group.GetAttr("permissions")
if perms != nil {
permSubAts := perms.GetComplexAt().SubAts
// TODO avoid looping over all the perms once just for detecting a wildcard entry
for _, subAtMap := range permSubAts {
resNameAt := subAtMap["value"] // resName is stored in the value attribute
resName := ""
if resNameAt != nil {
resName = resNameAt.Values[0].(string)
}
// wildcard for all resources
if resName == "*" {
opsArrAt := subAtMap["opsarr"]
opsArr := "[]"
if opsArrAt != nil {
opsArr = opsArrAt.Values[0].(string)
}
for _, rt := range resTypes {
rp, err := createResPerms(opsArr, rt)
if err == nil {
resPerms[rt.Name] = rp
}
}
break // break out and loop again to process the non-wildcard entries
}
}
for _, subAtMap := range permSubAts {
resNameAt := subAtMap["value"] // resName is stored in the value attribute
resName := ""
if resNameAt != nil {
resName = resNameAt.Values[0].(string)
}
if resName == "*" {
continue // this was already processed in the above loop
} else {
opsArrAt := subAtMap["opsarr"]
opsArr := "[]"
if opsArrAt != nil {
opsArr = opsArrAt.Values[0].(string)
}
rt := resTypes[resName]
if rt == nil {
log.Warningf("No resource type found with the name %s, ignoring permissions", resName)
continue
}
rp, err := createResPerms(opsArr, rt)
if err == nil {
resPerms[resName] = rp
}
}
}
}
return resPerms
}
func createResPerms(opsArr string, rt *schema.ResourceType) (*ResourcePermission, error) {
rp := &ResourcePermission{RType: rt}
opsPerms, err := parseOpsArr(opsArr, rt)
if err != nil {
log.Debugf("failed to parse the operation permissions %s", err)
return nil, err
}
rp.ReadPerm = opsPerms["read"] // the keys are lowered in the call to parseOpsObj
if rp.ReadPerm == nil {
log.Debugf("No read permission present using default")
rp.ReadPerm = &Permission{Name: "read"}
}
rp.WritePerm = opsPerms["write"]
if rp.WritePerm == nil {
log.Debugf("No write permission present using default")
rp.WritePerm = &Permission{Name: "write"}
}
return rp, nil
}
func parseOpsArr(opsArr string, rt *schema.ResourceType) (map[string]*Permission, error) {
var ops []opsObj
err := json.Unmarshal([]byte(opsArr), &ops)
if err != nil {
return nil, err
}
opsPerms := make(map[string]*Permission)
for _, rop := range ops {
p := &Permission{}
rop.AllowAttrs = strings.TrimSpace(rop.AllowAttrs)
if len(rop.AllowAttrs) != 0 {
if allAttrsRegex.MatchString(rop.AllowAttrs) {
p.AllowAll = true
} else {
p.AllowAttrs = parseAttrs(rop.AllowAttrs, rt)
}
} else if len(rop.DenyAttrs) != 0 {
rop.DenyAttrs = strings.TrimSpace(rop.DenyAttrs)
if allAttrsRegex.MatchString(rop.DenyAttrs) {
p.AllowAttrs = nil
} else {
deniedAttrMap := parseAttrs(rop.DenyAttrs, rt)
// remove the denied attributes and mark the rest as allowed attributes
p.AllowAttrs = filterDenied(deniedAttrMap, rt)
}
}
// only parse the filter if either all attributes are allowed
// or there are some attributes specified
if p.AllowAll || p.AllowAttrs != nil {
rop.Filter = strings.TrimSpace(rop.Filter)
if len(rop.Filter) > 0 {
tmp := strings.ToUpper(rop.Filter)
if "ANY" == tmp {
p.OnAnyResource = true
} else if "NONE" == tmp {
p.Filter = nil
p.AllowAll = false
p.AllowAttrs = nil
} else {
node, err := ParseFilter(rop.Filter)
if err != nil {
log.Debugf("Error while parsing filter %s, dropping all the permissions for the %s operation", rop.Filter, rop.Op)
continue // deny the operation completely, this is safer than allowing without filter
}
p.Filter = node
}
}
}
opsPerms[strings.ToLower(rop.Op)] = p
}
return opsPerms, nil
}
func parseAttrs(attrCsv string, rt *schema.ResourceType) map[string]*AttributeParam {
attrMap, subAtPresent := SplitAttrCsv(attrCsv, rt)
// the mandatory attributes that will always be returned
for k, _ := range rt.AtsAlwaysRtn {
attrMap[k] = 1
}
// and those that are never returned
for k, _ := range rt.AtsNeverRtn {
if _, ok := attrMap[k]; ok {
delete(attrMap, k)
}
}
m := ConvertToParamAttributes(attrMap, subAtPresent)
if len(m) == 0 {
m = nil
}
return m
}
func filterDenied(denied map[string]*AttributeParam, rt *schema.ResourceType) map[string]*AttributeParam {
if denied == nil {
return nil
}
attrMap := make(map[string]int)
collectAllAtNames(rt.GetMainSchema(), attrMap, true)
allowed := ConvertToParamAttributes(attrMap, false)
for k, v := range denied {
if v.SubAts == nil {
delete(allowed, k)
} else {
allowedParam := allowed[k]
atType := rt.GetAtType(allowedParam.Name)
allowedParam.SubAts = make(map[string]string)
for _, s := range atType.SubAttributes {
if _, ok := v.SubAts[s.NormName]; !ok {
allowedParam.SubAts[s.NormName] = s.NormName
}
}
}
}
return allowed
}