-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission_index.go
208 lines (176 loc) · 5.42 KB
/
permission_index.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
package rbac
import (
"strings"
"github.com/rancher/norman/types/slice"
"github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/client-go/tools/cache"
)
const (
rbacGroup = "rbac.authorization.k8s.io"
)
func newIndexes(client v1.Interface) (user *permissionIndex, group *permissionIndex) {
client.ClusterRoleBindings("").Controller().Informer().
AddIndexers(cache.Indexers{
"crbUser": func(obj interface{}) ([]string, error) {
return clusterRoleBindingBySubject("User", obj)
},
"crbGroup": func(obj interface{}) ([]string, error) {
return clusterRoleBindingBySubject("Group", obj)
},
})
client.RoleBindings("").Controller().Informer().
AddIndexers(cache.Indexers{
"rbUser": func(obj interface{}) ([]string, error) {
return roleBindingBySubject("User", obj)
},
"rbGroup": func(obj interface{}) ([]string, error) {
return roleBindingBySubject("Group", obj)
},
})
user = &permissionIndex{
clusterRoleLister: client.ClusterRoles("").Controller().Lister(),
roleLister: client.Roles("").Controller().Lister(),
crbIndexer: client.ClusterRoleBindings("").Controller().Informer().GetIndexer(),
rbIndexer: client.RoleBindings("").Controller().Informer().GetIndexer(),
roleIndexKey: "rbUser",
clusterRoleIndexKey: "crbUser",
}
group = &permissionIndex{
clusterRoleLister: client.ClusterRoles("").Controller().Lister(),
roleLister: client.Roles("").Controller().Lister(),
crbIndexer: client.ClusterRoleBindings("").Controller().Informer().GetIndexer(),
rbIndexer: client.RoleBindings("").Controller().Informer().GetIndexer(),
roleIndexKey: "rbGroup",
clusterRoleIndexKey: "crbGroup",
}
return
}
func clusterRoleBindingBySubject(kind string, obj interface{}) ([]string, error) {
var result []string
crb := obj.(*rbacv1.ClusterRoleBinding)
for _, subject := range crb.Subjects {
if subject.Kind == kind {
result = append(result, subject.Name)
}
}
return result, nil
}
func roleBindingBySubject(kind string, obj interface{}) ([]string, error) {
var result []string
crb := obj.(*rbacv1.RoleBinding)
for _, subject := range crb.Subjects {
if subject.Kind == kind {
result = append(result, subject.Name)
}
}
return result, nil
}
type permissionIndex struct {
clusterRoleLister v1.ClusterRoleLister
roleLister v1.RoleLister
crbIndexer cache.Indexer
rbIndexer cache.Indexer
roleIndexKey string
clusterRoleIndexKey string
}
func (p *permissionIndex) get(subjectName, apiGroup, resource, verb string) []ListPermission {
var result []ListPermission
for _, binding := range p.getRoleBindings(subjectName) {
if binding.RoleRef.APIGroup != rbacGroup {
continue
}
result = p.filterPermissions(result, binding.Namespace, binding.RoleRef.Kind, binding.RoleRef.Name, apiGroup, resource, verb)
}
for _, binding := range p.getClusterRoleBindings(subjectName) {
if binding.RoleRef.APIGroup != rbacGroup {
continue
}
result = p.filterPermissions(result, "*", binding.RoleRef.Kind, binding.RoleRef.Name, apiGroup, resource, verb)
}
return result
}
func (p *permissionIndex) filterPermissions(result []ListPermission, namespace, kind, name, apiGroup, resource, verb string) []ListPermission {
nsForResourceNameGets := namespace
if namespace == "*" {
nsForResourceNameGets = ""
}
for _, rule := range p.getRules(namespace, kind, name) {
if !matches(rule.APIGroups, apiGroup) || !matches(rule.Resources, resource) {
continue
}
if len(rule.ResourceNames) > 0 {
// special case: if verb is list and this rule has resourceNames, check for the verb get instead of list
v := verb
if verb == "list" {
v = "get"
}
if slice.ContainsString(rule.Verbs, "*") || slice.ContainsString(rule.Verbs, v) {
for _, resourceName := range rule.ResourceNames {
result = append(result, ListPermission{
Namespace: nsForResourceNameGets,
Name: resourceName,
})
}
}
continue
}
if slice.ContainsString(rule.Verbs, "*") || slice.ContainsString(rule.Verbs, verb) {
result = append(result, ListPermission{
Namespace: namespace,
Name: "*",
})
}
}
return result
}
func (p *permissionIndex) getClusterRoleBindings(subjectName string) []*rbacv1.ClusterRoleBinding {
var result []*rbacv1.ClusterRoleBinding
objs, err := p.crbIndexer.ByIndex(p.clusterRoleIndexKey, subjectName)
if err != nil {
return result
}
for _, obj := range objs {
result = append(result, obj.(*rbacv1.ClusterRoleBinding))
}
return result
}
func (p *permissionIndex) getRoleBindings(subjectName string) []*rbacv1.RoleBinding {
var result []*rbacv1.RoleBinding
objs, err := p.rbIndexer.ByIndex(p.roleIndexKey, subjectName)
if err != nil {
return result
}
for _, obj := range objs {
result = append(result, obj.(*rbacv1.RoleBinding))
}
return result
}
func (p *permissionIndex) getRules(namespace, kind, name string) []rbacv1.PolicyRule {
switch kind {
case "ClusterRole":
role, err := p.clusterRoleLister.Get("", name)
if err != nil {
return nil
}
return role.Rules
case "Role":
role, err := p.roleLister.Get(namespace, name)
if err != nil {
return nil
}
return role.Rules
}
return nil
}
func matches(parts []string, val string) bool {
for _, value := range parts {
if value == "*" {
return true
}
if strings.EqualFold(value, val) {
return true
}
}
return false
}