Debian Codegraph @

pkg/authorization/union/union_test.go

raw
1 /*
2 Copyright 2014 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 union
18
19 import (
20 "context"
21 "errors"
22 "fmt"
23 "reflect"
24 "testing"
25
26 "k8s.io/apiserver/pkg/authentication/user"
27 "k8s.io/apiserver/pkg/authorization/authorizer"
28 genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
29 )
30
31 type mockAuthzHandler struct {
32 decision authorizer.Decision
33 err error
34 }
35
36 func (mock *mockAuthzHandler) Authorize(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) {
37 return mock.decision, "", mock.err
38 }
39
40 func TestAuthorizationSecondPasses(t *testing.T) {
41 handler1 := &mockAuthzHandler{decision: authorizer.DecisionNoOpinion}
42 handler2 := &mockAuthzHandler{decision: authorizer.DecisionAllow}
43 authzHandler := New(handler1, handler2)
44
45 authorized, _, _ := authzHandler.Authorize(context.Background(), nil)
46 if authorized != authorizer.DecisionAllow {
47 t.Errorf("Unexpected authorization failure")
48 }
49 }
50
51 func TestAuthorizationFirstPasses(t *testing.T) {
52 handler1 := &mockAuthzHandler{decision: authorizer.DecisionAllow}
53 handler2 := &mockAuthzHandler{decision: authorizer.DecisionNoOpinion}
54 authzHandler := New(handler1, handler2)
55
56 authorized, _, _ := authzHandler.Authorize(context.Background(), nil)
57 if authorized != authorizer.DecisionAllow {
58 t.Errorf("Unexpected authorization failure")
59 }
60 }
61
62 func TestAuthorizationNonePasses(t *testing.T) {
63 handler1 := &mockAuthzHandler{decision: authorizer.DecisionNoOpinion}
64 handler2 := &mockAuthzHandler{decision: authorizer.DecisionNoOpinion}
65 authzHandler := New(handler1, handler2)
66
67 authorized, _, _ := authzHandler.Authorize(context.Background(), nil)
68 if authorized == authorizer.DecisionAllow {
69 t.Errorf("Expected failed authorization")
70 }
71 }
72
73 func TestAuthorizationError(t *testing.T) {
74 handler1 := &mockAuthzHandler{err: fmt.Errorf("foo")}
75 handler2 := &mockAuthzHandler{err: fmt.Errorf("foo")}
76 authzHandler := New(handler1, handler2)
77
78 _, _, err := authzHandler.Authorize(context.Background(), nil)
79 if err == nil {
80 t.Errorf("Expected error: %v", err)
81 }
82 }
83
84 type mockAuthzRuleHandler struct {
85 resourceRules []authorizer.ResourceRuleInfo
86 nonResourceRules []authorizer.NonResourceRuleInfo
87 err error
88 }
89
90 func (mock *mockAuthzRuleHandler) RulesFor(ctx context.Context, user user.Info, namespace string) ([]authorizer.ResourceRuleInfo, []authorizer.NonResourceRuleInfo, bool, error) {
91 if mock.err != nil {
92 return []authorizer.ResourceRuleInfo{}, []authorizer.NonResourceRuleInfo{}, false, mock.err
93 }
94 return mock.resourceRules, mock.nonResourceRules, false, nil
95 }
96
97 func TestAuthorizationResourceRules(t *testing.T) {
98 handler1 := &mockAuthzRuleHandler{
99 resourceRules: []authorizer.ResourceRuleInfo{
100 &authorizer.DefaultResourceRuleInfo{
101 Verbs: []string{"*"},
102 APIGroups: []string{"*"},
103 Resources: []string{"bindings"},
104 },
105 &authorizer.DefaultResourceRuleInfo{
106 Verbs: []string{"get", "list", "watch"},
107 APIGroups: []string{"*"},
108 Resources: []string{"*"},
109 },
110 },
111 }
112 handler2 := &mockAuthzRuleHandler{
113 resourceRules: []authorizer.ResourceRuleInfo{
114 &authorizer.DefaultResourceRuleInfo{
115 Verbs: []string{"*"},
116 APIGroups: []string{"*"},
117 Resources: []string{"events"},
118 },
119 &authorizer.DefaultResourceRuleInfo{
120 Verbs: []string{"get"},
121 APIGroups: []string{"*"},
122 Resources: []string{"*"},
123 ResourceNames: []string{"foo"},
124 },
125 },
126 }
127
128 expected := []authorizer.DefaultResourceRuleInfo{
129 {
130 Verbs: []string{"*"},
131 APIGroups: []string{"*"},
132 Resources: []string{"bindings"},
133 },
134 {
135 Verbs: []string{"get", "list", "watch"},
136 APIGroups: []string{"*"},
137 Resources: []string{"*"},
138 },
139 {
140 Verbs: []string{"*"},
141 APIGroups: []string{"*"},
142 Resources: []string{"events"},
143 },
144 {
145 Verbs: []string{"get"},
146 APIGroups: []string{"*"},
147 Resources: []string{"*"},
148 ResourceNames: []string{"foo"},
149 },
150 }
151
152 authzRulesHandler := NewRuleResolvers(handler1, handler2)
153
154 rules, _, _, _ := authzRulesHandler.RulesFor(genericapirequest.NewContext(), nil, "")
155 actual := getResourceRules(rules)
156 if !reflect.DeepEqual(expected, actual) {
157 t.Errorf("Expected: \n%#v\n but actual: \n%#v\n", expected, actual)
158 }
159 }
160
161 func TestAuthorizationNonResourceRules(t *testing.T) {
162 handler1 := &mockAuthzRuleHandler{
163 nonResourceRules: []authorizer.NonResourceRuleInfo{
164 &authorizer.DefaultNonResourceRuleInfo{
165 Verbs: []string{"get"},
166 NonResourceURLs: []string{"/api"},
167 },
168 },
169 }
170
171 handler2 := &mockAuthzRuleHandler{
172 nonResourceRules: []authorizer.NonResourceRuleInfo{
173 &authorizer.DefaultNonResourceRuleInfo{
174 Verbs: []string{"get"},
175 NonResourceURLs: []string{"/api/*"},
176 },
177 },
178 }
179
180 expected := []authorizer.DefaultNonResourceRuleInfo{
181 {
182 Verbs: []string{"get"},
183 NonResourceURLs: []string{"/api"},
184 },
185 {
186 Verbs: []string{"get"},
187 NonResourceURLs: []string{"/api/*"},
188 },
189 }
190
191 authzRulesHandler := NewRuleResolvers(handler1, handler2)
192
193 _, rules, _, _ := authzRulesHandler.RulesFor(genericapirequest.NewContext(), nil, "")
194 actual := getNonResourceRules(rules)
195 if !reflect.DeepEqual(expected, actual) {
196 t.Errorf("Expected: \n%#v\n but actual: \n%#v\n", expected, actual)
197 }
198 }
199
200 func getResourceRules(infos []authorizer.ResourceRuleInfo) []authorizer.DefaultResourceRuleInfo {
201 rules := make([]authorizer.DefaultResourceRuleInfo, len(infos))
202 for i, info := range infos {
203 rules[i] = authorizer.DefaultResourceRuleInfo{
204 Verbs: info.GetVerbs(),
205 APIGroups: info.GetAPIGroups(),
206 Resources: info.GetResources(),
207 ResourceNames: info.GetResourceNames(),
208 }
209 }
210 return rules
211 }
212
213 func getNonResourceRules(infos []authorizer.NonResourceRuleInfo) []authorizer.DefaultNonResourceRuleInfo {
214 rules := make([]authorizer.DefaultNonResourceRuleInfo, len(infos))
215 for i, info := range infos {
216 rules[i] = authorizer.DefaultNonResourceRuleInfo{
217 Verbs: info.GetVerbs(),
218 NonResourceURLs: info.GetNonResourceURLs(),
219 }
220 }
221 return rules
222 }
223
224 func TestAuthorizationUnequivocalDeny(t *testing.T) {
225 cs := []struct {
226 authorizers []authorizer.Authorizer
227 decision authorizer.Decision
228 }{
229 {
230 authorizers: []authorizer.Authorizer{},
231 decision: authorizer.DecisionNoOpinion,
232 },
233 {
234 authorizers: []authorizer.Authorizer{
235 &mockAuthzHandler{decision: authorizer.DecisionNoOpinion},
236 &mockAuthzHandler{decision: authorizer.DecisionAllow},
237 &mockAuthzHandler{decision: authorizer.DecisionDeny},
238 },
239 decision: authorizer.DecisionAllow,
240 },
241 {
242 authorizers: []authorizer.Authorizer{
243 &mockAuthzHandler{decision: authorizer.DecisionNoOpinion},
244 &mockAuthzHandler{decision: authorizer.DecisionDeny},
245 &mockAuthzHandler{decision: authorizer.DecisionAllow},
246 },
247 decision: authorizer.DecisionDeny,
248 },
249 {
250 authorizers: []authorizer.Authorizer{
251 &mockAuthzHandler{decision: authorizer.DecisionNoOpinion},
252 &mockAuthzHandler{decision: authorizer.DecisionDeny, err: errors.New("webhook failed closed")},
253 &mockAuthzHandler{decision: authorizer.DecisionAllow},
254 },
255 decision: authorizer.DecisionDeny,
256 },
257 }
258 for i, c := range cs {
259 t.Run(fmt.Sprintf("case %v", i), func(t *testing.T) {
260 authzHandler := New(c.authorizers...)
261
262 decision, _, _ := authzHandler.Authorize(context.Background(), nil)
263 if decision != c.decision {
264 t.Errorf("Unexpected authorization failure: %v, expected: %v", decision, c.decision)
265 }
266 })
267 }
268 }