forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.go
559 lines (467 loc) · 17.3 KB
/
converter.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package scope
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
kutilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
kauthorizer "k8s.io/apiserver/pkg/authorization/authorizer"
kapi "k8s.io/kubernetes/pkg/api"
kauthorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
"k8s.io/kubernetes/pkg/apis/rbac"
rbaclisters "k8s.io/kubernetes/pkg/client/listers/rbac/internalversion"
authorizerrbac "k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
userapi "github.com/openshift/origin/pkg/user/apis/user"
)
// ScopesToRules takes the scopes and return the rules back. We ALWAYS add the discovery rules and it is possible to get some rules and and
// an error since errors aren't fatal to evaluation
func ScopesToRules(scopes []string, namespace string, clusterRoleGetter rbaclisters.ClusterRoleLister) ([]rbac.PolicyRule, error) {
rules := append([]rbac.PolicyRule{}, authorizationapi.DiscoveryRule)
errors := []error{}
for _, scope := range scopes {
found := false
for _, evaluator := range ScopeEvaluators {
if evaluator.Handles(scope) {
found = true
currRules, err := evaluator.ResolveRules(scope, namespace, clusterRoleGetter)
if err != nil {
errors = append(errors, err)
continue
}
rules = append(rules, currRules...)
}
}
if !found {
errors = append(errors, fmt.Errorf("no scope evaluator found for %q", scope))
}
}
return rules, kutilerrors.NewAggregate(errors)
}
// ScopesToVisibleNamespaces returns a list of namespaces that the provided scopes have "get" access to.
// This exists only to support efficiently list/watch of projects (ACLed namespaces)
func ScopesToVisibleNamespaces(scopes []string, clusterRoleGetter rbaclisters.ClusterRoleLister) (sets.String, error) {
if len(scopes) == 0 {
return sets.NewString("*"), nil
}
visibleNamespaces := sets.String{}
errors := []error{}
for _, scope := range scopes {
found := false
for _, evaluator := range ScopeEvaluators {
if evaluator.Handles(scope) {
found = true
allowedNamespaces, err := evaluator.ResolveGettableNamespaces(scope, clusterRoleGetter)
if err != nil {
errors = append(errors, err)
continue
}
visibleNamespaces.Insert(allowedNamespaces...)
break
}
}
if !found {
errors = append(errors, fmt.Errorf("no scope evaluator found for %q", scope))
}
}
return visibleNamespaces, kutilerrors.NewAggregate(errors)
}
const (
UserIndicator = "user:"
ClusterRoleIndicator = "role:"
)
// ScopeEvaluator takes a scope and returns the rules that express it
type ScopeEvaluator interface {
// Handles returns true if this evaluator can evaluate this scope
Handles(scope string) bool
// Validate returns an error if the scope is malformed
Validate(scope string) error
// Describe returns a description, warning (typically used to warn about escalation dangers), or an error if the scope is malformed
Describe(scope string) (description string, warning string, err error)
// ResolveRules returns the policy rules that this scope allows
ResolveRules(scope, namespace string, clusterRoleGetter rbaclisters.ClusterRoleLister) ([]rbac.PolicyRule, error)
ResolveGettableNamespaces(scope string, clusterRoleGetter rbaclisters.ClusterRoleLister) ([]string, error)
}
// ScopeEvaluators map prefixes to a function that handles that prefix
var ScopeEvaluators = []ScopeEvaluator{
userEvaluator{},
clusterRoleEvaluator{},
}
// scopes are in the format
// <indicator><indicator choice>
// we have the following formats:
// user:<scope name>
// role:<clusterrole name>:<namespace to allow the cluster role, * means all>
// TODO
// cluster:<comma-delimited verbs>:<comma-delimited resources>
// namespace:<namespace name>:<comma-delimited verbs>:<comma-delimited resources>
const (
UserInfo = UserIndicator + "info"
UserAccessCheck = UserIndicator + "check-access"
// UserListScopedProjects gives explicit permission to see the projects that this token can see.
UserListScopedProjects = UserIndicator + "list-scoped-projects"
// UserListAllProjects gives explicit permission to see the projects a user can see. This is often used to prime secondary ACL systems
// unrelated to openshift and to display projects for selection in a secondary UI.
UserListAllProjects = UserIndicator + "list-projects"
// UserFull includes all permissions of the user
UserFull = UserIndicator + "full"
)
var defaultSupportedScopesMap = map[string]string{
UserInfo: "Read-only access to your user information (including username, identities, and group membership)",
UserAccessCheck: `Read-only access to view your privileges (for example, "can I create builds?")`,
UserListScopedProjects: `Read-only access to list your projects viewable with this token and view their metadata (display name, description, etc.)`,
UserListAllProjects: `Read-only access to list your projects and view their metadata (display name, description, etc.)`,
UserFull: `Full read/write access with all of your permissions`,
}
func DefaultSupportedScopes() []string {
return sets.StringKeySet(defaultSupportedScopesMap).List()
}
func DefaultSupportedScopesMap() map[string]string {
return defaultSupportedScopesMap
}
// user:<scope name>
type userEvaluator struct{}
func (userEvaluator) Handles(scope string) bool {
return strings.HasPrefix(scope, UserIndicator)
}
func (userEvaluator) Validate(scope string) error {
switch scope {
case UserFull, UserInfo, UserAccessCheck, UserListScopedProjects, UserListAllProjects:
return nil
}
return fmt.Errorf("unrecognized scope: %v", scope)
}
func (userEvaluator) Describe(scope string) (string, string, error) {
switch scope {
case UserInfo, UserAccessCheck, UserListScopedProjects, UserListAllProjects:
return defaultSupportedScopesMap[scope], "", nil
case UserFull:
return defaultSupportedScopesMap[scope], `Includes any access you have to escalating resources like secrets`, nil
default:
return "", "", fmt.Errorf("unrecognized scope: %v", scope)
}
}
func (userEvaluator) ResolveRules(scope, namespace string, _ rbaclisters.ClusterRoleLister) ([]rbac.PolicyRule, error) {
switch scope {
case UserInfo:
return []rbac.PolicyRule{
rbac.NewRule("get").
Groups(userapi.GroupName, userapi.LegacyGroupName).
Resources("users").
Names("~").
RuleOrDie(),
}, nil
case UserAccessCheck:
return []rbac.PolicyRule{
rbac.NewRule("create").
Groups(kauthorizationapi.GroupName).
Resources("selfsubjectaccessreviews").
RuleOrDie(),
rbac.NewRule("create").
Groups(authorizationapi.GroupName, authorizationapi.LegacyGroupName).
Resources("selfsubjectrulesreviews").
RuleOrDie(),
}, nil
case UserListScopedProjects:
return []rbac.PolicyRule{
rbac.NewRule("list", "watch").
Groups(projectapi.GroupName, projectapi.LegacyGroupName).
Resources("projects").
RuleOrDie(),
}, nil
case UserListAllProjects:
return []rbac.PolicyRule{
rbac.NewRule("list", "watch").
Groups(projectapi.GroupName, projectapi.LegacyGroupName).
Resources("projects").
RuleOrDie(),
rbac.NewRule("get").
Groups(kapi.GroupName).
Resources("namespaces").
RuleOrDie(),
}, nil
case UserFull:
return []rbac.PolicyRule{
rbac.NewRule(rbac.VerbAll).
Groups(rbac.APIGroupAll).
Resources(rbac.ResourceAll).
RuleOrDie(),
rbac.NewRule(rbac.VerbAll).
URLs(rbac.NonResourceAll).
RuleOrDie(),
}, nil
default:
return nil, fmt.Errorf("unrecognized scope: %v", scope)
}
}
func (userEvaluator) ResolveGettableNamespaces(scope string, _ rbaclisters.ClusterRoleLister) ([]string, error) {
switch scope {
case UserFull, UserListAllProjects:
return []string{"*"}, nil
default:
return []string{}, nil
}
}
// escalatingScopeResources are resources that are considered escalating for scope evaluation
var escalatingScopeResources = []schema.GroupResource{
{Group: kapi.GroupName, Resource: "secrets"},
{Group: imageapi.GroupName, Resource: "imagestreams/secrets"},
{Group: imageapi.LegacyGroupName, Resource: "imagestreams/secrets"},
{Group: oauthapi.GroupName, Resource: "oauthauthorizetokens"},
{Group: oauthapi.LegacyGroupName, Resource: "oauthauthorizetokens"},
{Group: oauthapi.GroupName, Resource: "oauthaccesstokens"},
{Group: oauthapi.LegacyGroupName, Resource: "oauthaccesstokens"},
{Group: authorizationapi.GroupName, Resource: "roles"},
{Group: authorizationapi.LegacyGroupName, Resource: "roles"},
{Group: authorizationapi.GroupName, Resource: "rolebindings"},
{Group: authorizationapi.LegacyGroupName, Resource: "rolebindings"},
{Group: authorizationapi.GroupName, Resource: "clusterroles"},
{Group: authorizationapi.LegacyGroupName, Resource: "clusterroles"},
{Group: authorizationapi.GroupName, Resource: "clusterrolebindings"},
{Group: authorizationapi.LegacyGroupName, Resource: "clusterrolebindings"},
}
// role:<clusterrole name>:<namespace to allow the cluster role, * means all>
type clusterRoleEvaluator struct{}
var clusterRoleEvaluatorInstance = clusterRoleEvaluator{}
func (clusterRoleEvaluator) Handles(scope string) bool {
return strings.HasPrefix(scope, ClusterRoleIndicator)
}
func (e clusterRoleEvaluator) Validate(scope string) error {
_, _, _, err := e.parseScope(scope)
return err
}
// parseScope parses the requested scope, determining the requested role name, namespace, and if
// access to escalating objects is required. It will return an error if it doesn't parse cleanly
func (e clusterRoleEvaluator) parseScope(scope string) (string /*role name*/, string /*namespace*/, bool /*escalating*/, error) {
if !e.Handles(scope) {
return "", "", false, fmt.Errorf("bad format for scope %v", scope)
}
return ParseClusterRoleScope(scope)
}
func ParseClusterRoleScope(scope string) (string /*role name*/, string /*namespace*/, bool /*escalating*/, error) {
if !strings.HasPrefix(scope, ClusterRoleIndicator) {
return "", "", false, fmt.Errorf("bad format for scope %v", scope)
}
escalating := false
if strings.HasSuffix(scope, ":!") {
escalating = true
// clip that last segment before parsing the rest
scope = scope[:strings.LastIndex(scope, ":")]
}
tokens := strings.SplitN(scope, ":", 2)
if len(tokens) != 2 {
return "", "", false, fmt.Errorf("bad format for scope %v", scope)
}
// namespaces can't have colons, but roles can. pick last.
lastColonIndex := strings.LastIndex(tokens[1], ":")
if lastColonIndex <= 0 || lastColonIndex == (len(tokens[1])-1) {
return "", "", false, fmt.Errorf("bad format for scope %v", scope)
}
return tokens[1][0:lastColonIndex], tokens[1][lastColonIndex+1:], escalating, nil
}
func (e clusterRoleEvaluator) Describe(scope string) (string, string, error) {
roleName, scopeNamespace, escalating, err := e.parseScope(scope)
if err != nil {
return "", "", err
}
// Anything you can do [in project "foo" | server-wide] that is also allowed by the "admin" role[, except access escalating resources like secrets]
scopePhrase := ""
if scopeNamespace == authorizationapi.ScopesAllNamespaces {
scopePhrase = "server-wide"
} else {
scopePhrase = fmt.Sprintf("in project %q", scopeNamespace)
}
warning := ""
escalatingPhrase := ""
if escalating {
warning = fmt.Sprintf("Includes access to escalating resources like secrets")
} else {
escalatingPhrase = ", except access escalating resources like secrets"
}
description := fmt.Sprintf("Anything you can do %s that is also allowed by the %q role%s", scopePhrase, roleName, escalatingPhrase)
return description, warning, nil
}
func (e clusterRoleEvaluator) ResolveRules(scope, namespace string, clusterRoleGetter rbaclisters.ClusterRoleLister) ([]rbac.PolicyRule, error) {
_, scopeNamespace, _, err := e.parseScope(scope)
if err != nil {
return nil, err
}
// if the scope limit on the clusterrole doesn't match, then don't add any rules, but its not an error
if !(scopeNamespace == authorizationapi.ScopesAllNamespaces || scopeNamespace == namespace) {
return []rbac.PolicyRule{}, nil
}
return e.resolveRules(scope, clusterRoleGetter)
}
func has(set []string, value string) bool {
for _, element := range set {
if value == element {
return true
}
}
return false
}
// resolveRules doesn't enforce namespace checks
func (e clusterRoleEvaluator) resolveRules(scope string, clusterRoleGetter rbaclisters.ClusterRoleLister) ([]rbac.PolicyRule, error) {
roleName, _, escalating, err := e.parseScope(scope)
if err != nil {
return nil, err
}
role, err := clusterRoleGetter.Get(roleName)
if err != nil {
return nil, err
}
rules := []rbac.PolicyRule{}
for _, rule := range role.Rules {
if escalating {
rules = append(rules, rule)
continue
}
// rules with unbounded access shouldn't be allowed in scopes.
if has(rule.Verbs, rbac.VerbAll) ||
has(rule.Resources, rbac.ResourceAll) ||
has(rule.APIGroups, rbac.APIGroupAll) {
continue
}
// rules that allow escalating resource access should be cleaned.
safeRule := removeEscalatingResources(rule)
rules = append(rules, safeRule)
}
return rules, nil
}
func (e clusterRoleEvaluator) ResolveGettableNamespaces(scope string, clusterRoleGetter rbaclisters.ClusterRoleLister) ([]string, error) {
_, scopeNamespace, _, err := e.parseScope(scope)
if err != nil {
return nil, err
}
rules, err := e.resolveRules(scope, clusterRoleGetter)
if err != nil {
return nil, err
}
attributes := kauthorizer.AttributesRecord{
APIGroup: kapi.GroupName,
Verb: "get",
Resource: "namespaces",
ResourceRequest: true,
}
if authorizerrbac.RulesAllow(attributes, rules...) {
return []string{scopeNamespace}, nil
}
return []string{}, nil
}
func remove(array []string, item string) []string {
newar := array[:0]
for _, element := range array {
if element != item {
newar = append(newar, element)
}
}
return newar
}
// removeEscalatingResources inspects a PolicyRule and removes any references to escalating resources.
// It has coarse logic for now. It is possible to rewrite one rule into many for the finest grain control
// but removing the entire matching resource regardless of verb or secondary group is cheaper, easier, and errs on the side removing
// too much, not too little
func removeEscalatingResources(in rbac.PolicyRule) rbac.PolicyRule {
var ruleCopy *rbac.PolicyRule
for _, resource := range escalatingScopeResources {
if !(has(in.APIGroups, resource.Group) && has(in.Resources, resource.Resource)) {
continue
}
if ruleCopy == nil {
// we're using a cache of cache of an object that uses pointers to data. I'm pretty sure we need to do a copy to avoid
// muddying the cache
ruleCopy = in.DeepCopy()
}
ruleCopy.Resources = remove(ruleCopy.Resources, resource.Resource)
}
if ruleCopy != nil {
return *ruleCopy
}
return in
}
func ValidateScopeRestrictions(client *oauthapi.OAuthClient, scopes ...string) error {
if len(client.ScopeRestrictions) == 0 {
return nil
}
if len(scopes) == 0 {
return fmt.Errorf("%v may not request unscoped tokens", client.Name)
}
errs := []error{}
for _, scope := range scopes {
if err := validateScopeRestrictions(client, scope); err != nil {
errs = append(errs, err)
}
}
return kutilerrors.NewAggregate(errs)
}
func validateScopeRestrictions(client *oauthapi.OAuthClient, scope string) error {
errs := []error{}
if len(client.ScopeRestrictions) == 0 {
return nil
}
for _, restriction := range client.ScopeRestrictions {
if len(restriction.ExactValues) > 0 {
if err := ValidateLiteralScopeRestrictions(scope, restriction.ExactValues); err != nil {
errs = append(errs, err)
continue
}
return nil
}
if restriction.ClusterRole != nil {
if !clusterRoleEvaluatorInstance.Handles(scope) {
continue
}
if err := ValidateClusterRoleScopeRestrictions(scope, *restriction.ClusterRole); err != nil {
errs = append(errs, err)
continue
}
return nil
}
}
// if we got here, then nothing matched. If we already have errors, do nothing, otherwise add one to make it report failed.
if len(errs) == 0 {
errs = append(errs, fmt.Errorf("%v did not match any scope restriction", scope))
}
return kutilerrors.NewAggregate(errs)
}
func ValidateLiteralScopeRestrictions(scope string, literals []string) error {
for _, literal := range literals {
if literal == scope {
return nil
}
}
return fmt.Errorf("%v not found in %v", scope, literals)
}
func ValidateClusterRoleScopeRestrictions(scope string, restriction oauthapi.ClusterRoleScopeRestriction) error {
role, namespace, escalating, err := clusterRoleEvaluatorInstance.parseScope(scope)
if err != nil {
return err
}
foundName := false
for _, restrictedRoleName := range restriction.RoleNames {
if restrictedRoleName == "*" || restrictedRoleName == role {
foundName = true
break
}
}
if !foundName {
return fmt.Errorf("%v does not use an approved name", scope)
}
foundNamespace := false
for _, restrictedNamespace := range restriction.Namespaces {
if restrictedNamespace == "*" || restrictedNamespace == namespace {
foundNamespace = true
break
}
}
if !foundNamespace {
return fmt.Errorf("%v does not use an approved namespace", scope)
}
if escalating && !restriction.AllowEscalation {
return fmt.Errorf("%v is not allowed to escalate", scope)
}
return nil
}