forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subjectchecker.go
317 lines (256 loc) · 9.36 KB
/
subjectchecker.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package restrictusers
import (
"fmt"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/labels"
kerrors "k8s.io/kubernetes/pkg/util/errors"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
oclient "github.com/openshift/origin/pkg/client"
userapi "github.com/openshift/origin/pkg/user/api"
usercache "github.com/openshift/origin/pkg/user/cache"
)
// SubjectChecker determines whether rolebindings on a subject (user, group, or
// service account) are allowed in a project.
type SubjectChecker interface {
Allowed(kapi.ObjectReference, *RoleBindingRestrictionContext) (bool, error)
}
// UnionSubjectChecker represents the union of zero or more SubjectCheckers.
type UnionSubjectChecker []SubjectChecker
// NewUnionSubjectChecker returns a new UnionSubjectChecker.
func NewUnionSubjectChecker(checkers []SubjectChecker) UnionSubjectChecker {
return UnionSubjectChecker(checkers)
}
// Allowed determines whether the given subject is allowed in rolebindings in
// the project.
func (checkers UnionSubjectChecker) Allowed(subject kapi.ObjectReference, ctx *RoleBindingRestrictionContext) (bool, error) {
errs := []error{}
for _, checker := range []SubjectChecker(checkers) {
allowed, err := checker.Allowed(subject, ctx)
if err != nil {
errs = append(errs, err)
} else if allowed {
return true, nil
}
}
return false, kerrors.NewAggregate(errs)
}
// RoleBindingRestrictionContext holds context that is used when determining
// whether a RoleBindingRestriction allows rolebindings on a particular subject.
type RoleBindingRestrictionContext struct {
oclient oclient.Interface
kclient kclientset.Interface
// groupCache maps user name to groups.
groupCache *usercache.GroupCache
// userToLabels maps user name to labels.Set.
userToLabelSet map[string]labels.Set
// groupToLabels maps group name to labels.Set.
groupToLabelSet map[string]labels.Set
// namespace is the namespace for which the RoleBindingRestriction makes
// determinations.
namespace string
}
// NewRoleBindingRestrictionContext returns a new RoleBindingRestrictionContext
// object.
func NewRoleBindingRestrictionContext(ns string, kc kclientset.Interface, oc oclient.Interface, groupCache *usercache.GroupCache) (*RoleBindingRestrictionContext, error) {
return &RoleBindingRestrictionContext{
namespace: ns,
kclient: kc,
oclient: oc,
groupCache: groupCache,
userToLabelSet: map[string]labels.Set{},
groupToLabelSet: map[string]labels.Set{},
}, nil
}
// labelSetForUser returns the label set for the given user subject.
func (ctx *RoleBindingRestrictionContext) labelSetForUser(subject kapi.ObjectReference) (labels.Set, error) {
if subject.Kind == authorizationapi.SystemUserKind {
return labels.Set{}, nil
}
if subject.Kind != authorizationapi.UserKind {
return labels.Set{}, fmt.Errorf("not a user: %q", subject.Name)
}
labelSet, ok := ctx.userToLabelSet[subject.Name]
if ok {
return labelSet, nil
}
user, err := ctx.oclient.Users().Get(subject.Name)
if err != nil {
return labels.Set{}, err
}
ctx.userToLabelSet[subject.Name] = labels.Set(user.Labels)
return ctx.userToLabelSet[subject.Name], nil
}
// groupsForUser returns the groups for the given user subject.
func (ctx *RoleBindingRestrictionContext) groupsForUser(subject kapi.ObjectReference) ([]*userapi.Group, error) {
if subject.Kind == authorizationapi.SystemUserKind {
return []*userapi.Group{}, nil
}
if subject.Kind != authorizationapi.UserKind {
return []*userapi.Group{}, fmt.Errorf("not a user: %q", subject.Name)
}
return ctx.groupCache.GroupsFor(subject.Name)
}
// labelSetForGroup returns the label set for the given group subject.
func (ctx *RoleBindingRestrictionContext) labelSetForGroup(subject kapi.ObjectReference) (labels.Set, error) {
if subject.Kind == authorizationapi.SystemGroupKind {
return labels.Set{}, nil
}
if subject.Kind != authorizationapi.GroupKind {
return labels.Set{}, fmt.Errorf("not a group: %q", subject.Name)
}
labelSet, ok := ctx.groupToLabelSet[subject.Name]
if ok {
return labelSet, nil
}
group, err := ctx.oclient.Groups().Get(subject.Name)
if err != nil {
return labels.Set{}, err
}
ctx.groupToLabelSet[subject.Name] = labels.Set(group.Labels)
return ctx.groupToLabelSet[subject.Name], nil
}
// UserSubjectChecker determines whether a user subject is allowed in
// rolebindings in the project.
type UserSubjectChecker struct {
userRestriction *authorizationapi.UserRestriction
}
// NewUserSubjectChecker returns a new UserSubjectChecker.
func NewUserSubjectChecker(userRestriction *authorizationapi.UserRestriction) UserSubjectChecker {
return UserSubjectChecker{userRestriction: userRestriction}
}
// Allowed determines whether the given user subject is allowed in rolebindings
// in the project.
func (checker UserSubjectChecker) Allowed(subject kapi.ObjectReference, ctx *RoleBindingRestrictionContext) (bool, error) {
if subject.Kind != authorizationapi.UserKind &&
subject.Kind != authorizationapi.SystemUserKind {
return false, nil
}
for _, userName := range checker.userRestriction.Users {
if subject.Name == userName {
return true, nil
}
}
// System users can match only by name.
if subject.Kind != authorizationapi.UserKind {
return false, nil
}
if len(checker.userRestriction.Groups) != 0 {
subjectGroups, err := ctx.groupsForUser(subject)
if err != nil {
return false, err
}
for _, groupName := range checker.userRestriction.Groups {
for _, group := range subjectGroups {
if group.Name == groupName {
return true, nil
}
}
}
}
if len(checker.userRestriction.Selectors) != 0 {
labelSet, err := ctx.labelSetForUser(subject)
if err != nil {
return false, err
}
for _, labelSelector := range checker.userRestriction.Selectors {
selector, err := unversioned.LabelSelectorAsSelector(&labelSelector)
if err != nil {
return false, err
}
if selector.Matches(labelSet) {
return true, nil
}
}
}
return false, nil
}
// GroupSubjectChecker determines whether a group subject is allowed in
// rolebindings in the project.
type GroupSubjectChecker struct {
groupRestriction *authorizationapi.GroupRestriction
}
// NewGroupSubjectChecker returns a new GroupSubjectChecker.
func NewGroupSubjectChecker(groupRestriction *authorizationapi.GroupRestriction) GroupSubjectChecker {
return GroupSubjectChecker{groupRestriction: groupRestriction}
}
// Allowed determines whether the given group subject is allowed in rolebindings
// in the project.
func (checker GroupSubjectChecker) Allowed(subject kapi.ObjectReference, ctx *RoleBindingRestrictionContext) (bool, error) {
if subject.Kind != authorizationapi.GroupKind &&
subject.Kind != authorizationapi.SystemGroupKind {
return false, nil
}
for _, groupName := range checker.groupRestriction.Groups {
if subject.Name == groupName {
return true, nil
}
}
// System groups can match only by name.
if subject.Kind != authorizationapi.GroupKind {
return false, nil
}
if len(checker.groupRestriction.Selectors) != 0 {
labelSet, err := ctx.labelSetForGroup(subject)
if err != nil {
return false, err
}
for _, labelSelector := range checker.groupRestriction.Selectors {
selector, err := unversioned.LabelSelectorAsSelector(&labelSelector)
if err != nil {
return false, err
}
if selector.Matches(labelSet) {
return true, nil
}
}
}
return false, nil
}
// ServiceAccountSubjectChecker determines whether a serviceaccount subject is
// allowed in rolebindings in the project.
type ServiceAccountSubjectChecker struct {
serviceAccountRestriction *authorizationapi.ServiceAccountRestriction
}
// NewServiceAccountSubjectChecker returns a new ServiceAccountSubjectChecker.
func NewServiceAccountSubjectChecker(serviceAccountRestriction *authorizationapi.ServiceAccountRestriction) ServiceAccountSubjectChecker {
return ServiceAccountSubjectChecker{
serviceAccountRestriction: serviceAccountRestriction,
}
}
// Allowed determines whether the given serviceaccount subject is allowed in
// rolebindings in the project.
func (checker ServiceAccountSubjectChecker) Allowed(subject kapi.ObjectReference, ctx *RoleBindingRestrictionContext) (bool, error) {
if subject.Kind != authorizationapi.ServiceAccountKind {
return false, nil
}
for _, namespace := range checker.serviceAccountRestriction.Namespaces {
if subject.Namespace == namespace {
return true, nil
}
}
for _, serviceAccountRef := range checker.serviceAccountRestriction.ServiceAccounts {
serviceAccountNamespace := serviceAccountRef.Namespace
if len(serviceAccountNamespace) == 0 {
serviceAccountNamespace = ctx.namespace
}
if subject.Name == serviceAccountRef.Name &&
subject.Namespace == serviceAccountNamespace {
return true, nil
}
}
return false, nil
}
// NewSubjectChecker returns a new SubjectChecker.
func NewSubjectChecker(spec *authorizationapi.RoleBindingRestrictionSpec) (SubjectChecker, error) {
switch {
case spec.UserRestriction != nil:
return NewUserSubjectChecker(spec.UserRestriction), nil
case spec.GroupRestriction != nil:
return NewGroupSubjectChecker(spec.GroupRestriction), nil
case spec.ServiceAccountRestriction != nil:
return NewServiceAccountSubjectChecker(spec.ServiceAccountRestriction), nil
}
return nil, fmt.Errorf("invalid RoleBindingRestrictionSpec: %v", spec)
}