Debian Codegraph @

pkg/authorization/authorizerfactory/builtin.go

raw
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package authorizerfactory
18
19 import (
20 "context"
21 "errors"
22
23 "k8s.io/apiserver/pkg/authentication/user"
24 "k8s.io/apiserver/pkg/authorization/authorizer"
25 )
26
27 // alwaysAllowAuthorizer is an implementation of authorizer.Attributes
28 // which always says yes to an authorization request.
29 // It is useful in tests and when using kubernetes in an open manner.
30 type alwaysAllowAuthorizer struct{}
31
32 func (alwaysAllowAuthorizer) Authorize(ctx context.Context, a authorizer.Attributes) (authorized authorizer.Decision, reason string, err error) {
33 return authorizer.DecisionAllow, "", nil
34 }
35
36 func (alwaysAllowAuthorizer) RulesFor(ctx context.Context, user user.Info, namespace string) ([]authorizer.ResourceRuleInfo, []authorizer.NonResourceRuleInfo, bool, error) {
37 return []authorizer.ResourceRuleInfo{
38 &authorizer.DefaultResourceRuleInfo{
39 Verbs: []string{"*"},
40 APIGroups: []string{"*"},
41 Resources: []string{"*"},
42 },
43 }, []authorizer.NonResourceRuleInfo{
44 &authorizer.DefaultNonResourceRuleInfo{
45 Verbs: []string{"*"},
46 NonResourceURLs: []string{"*"},
47 },
48 }, false, nil
49 }
50
51 func NewAlwaysAllowAuthorizer() *alwaysAllowAuthorizer {
52 return new(alwaysAllowAuthorizer)
53 }
54
55 // alwaysDenyAuthorizer is an implementation of authorizer.Attributes
56 // which always says no to an authorization request.
57 // It is useful in unit tests to force an operation to be forbidden.
58 type alwaysDenyAuthorizer struct{}
59
60 func (alwaysDenyAuthorizer) Authorize(ctx context.Context, a authorizer.Attributes) (decision authorizer.Decision, reason string, err error) {
61 return authorizer.DecisionNoOpinion, "Everything is forbidden.", nil
62 }
63
64 func (alwaysDenyAuthorizer) RulesFor(ctx context.Context, user user.Info, namespace string) ([]authorizer.ResourceRuleInfo, []authorizer.NonResourceRuleInfo, bool, error) {
65 return []authorizer.ResourceRuleInfo{}, []authorizer.NonResourceRuleInfo{}, false, nil
66 }
67
68 func NewAlwaysDenyAuthorizer() *alwaysDenyAuthorizer {
69 return new(alwaysDenyAuthorizer)
70 }
71
72 type privilegedGroupAuthorizer struct {
73 groups []string
74 }
75
76 func (r *privilegedGroupAuthorizer) Authorize(ctx context.Context, attr authorizer.Attributes) (authorizer.Decision, string, error) {
77 if attr.GetUser() == nil {
78 return authorizer.DecisionNoOpinion, "Error", errors.New("no user on request.")
79 }
80 for _, attr_group := range attr.GetUser().GetGroups() {
81 for _, priv_group := range r.groups {
82 if priv_group == attr_group {
83 return authorizer.DecisionAllow, "", nil
84 }
85 }
86 }
87 return authorizer.DecisionNoOpinion, "", nil
88 }
89
90 // NewPrivilegedGroups is for use in loopback scenarios
91 func NewPrivilegedGroups(groups ...string) *privilegedGroupAuthorizer {
92 return &privilegedGroupAuthorizer{
93 groups: groups,
94 }
95 }