Browse code

add detail to forbidden message

deads2k authored on 2015/03/11 23:23:47
Showing 3 changed files
... ...
@@ -52,7 +52,24 @@ func (a *openshiftAuthorizer) Authorize(ctx kapi.Context, passedAttributes Autho
52 52
 		return false, "", kerrors.NewAggregate(errs)
53 53
 	}
54 54
 
55
-	return false, "denied by default", nil
55
+	username := "MISSING"
56
+	if user, userExists := kapi.UserFrom(ctx); userExists {
57
+		username = user.GetName()
58
+	}
59
+
60
+	denyReason := "denied by default"
61
+	if passedAttributes.IsNonResourceURL() {
62
+		denyReason = fmt.Sprintf("%v cannot %v on %v", username, attributes.GetVerb(), attributes.GetURL())
63
+
64
+	} else {
65
+		resourceNamePart := ""
66
+		if len(attributes.GetResourceName()) > 0 {
67
+			resourceNamePart = fmt.Sprintf(" with name \"%v\"", attributes.GetResourceName())
68
+		}
69
+		denyReason = fmt.Sprintf("%v cannot %v on %v%v in %v", username, attributes.GetVerb(), attributes.GetResource(), resourceNamePart, namespace)
70
+	}
71
+
72
+	return false, denyReason, nil
56 73
 }
57 74
 
58 75
 func (a *openshiftAuthorizer) GetAllowedSubjects(ctx kapi.Context, attributes AuthorizationAttributes) (util.StringSet, util.StringSet, error) {
... ...
@@ -41,7 +41,7 @@ func TestResourceNameDeny(t *testing.T) {
41 41
 			ResourceName: "just-a-user",
42 42
 		},
43 43
 		expectedAllowed: false,
44
-		expectedReason:  "denied by default",
44
+		expectedReason:  `just-a-user cannot get on users with name "just-a-user"`,
45 45
 	}
46 46
 	test.policies = newDefaultGlobalPolicies()
47 47
 	test.bindings = newDefaultGlobalBinding()
... ...
@@ -166,7 +166,7 @@ func TestNonResourceDeny(t *testing.T) {
166 166
 			URL:            "not-allowed",
167 167
 		},
168 168
 		expectedAllowed: false,
169
-		expectedReason:  "denied by default",
169
+		expectedReason:  `no-one cannot get on not-allowed`,
170 170
 	}
171 171
 	test.policies = newDefaultGlobalPolicies()
172 172
 	test.bindings = newDefaultGlobalBinding()
... ...
@@ -183,7 +183,7 @@ func TestHealthDeny(t *testing.T) {
183 183
 			URL:            "/healthz",
184 184
 		},
185 185
 		expectedAllowed: false,
186
-		expectedReason:  "denied by default",
186
+		expectedReason:  `no-one cannot get on /healthz`,
187 187
 	}
188 188
 	test.policies = newDefaultGlobalPolicies()
189 189
 	test.bindings = newDefaultGlobalBinding()
... ...
@@ -215,7 +215,7 @@ func TestDisallowedViewingGlobalPods(t *testing.T) {
215 215
 			Resource: "pods",
216 216
 		},
217 217
 		expectedAllowed: false,
218
-		expectedReason:  "denied by default",
218
+		expectedReason:  `SomeYahoo cannot get on pods`,
219 219
 	}
220 220
 	test.policies = newDefaultGlobalPolicies()
221 221
 	test.bindings = newDefaultGlobalBinding()
... ...
@@ -282,7 +282,7 @@ func TestResourceRestrictionsWork(t *testing.T) {
282 282
 			Resource: "pods",
283 283
 		},
284 284
 		expectedAllowed: false,
285
-		expectedReason:  "denied by default",
285
+		expectedReason:  `Rachel cannot get on pods in adze`,
286 286
 	}
287 287
 	test2.policies = newDefaultGlobalPolicies()
288 288
 	test2.policies = append(test2.policies, newAdzePolicies()...)
... ...
@@ -331,7 +331,7 @@ func TestLocalRightsDoNotGrantGlobalRights(t *testing.T) {
331 331
 			Resource: "buildConfigs",
332 332
 		},
333 333
 		expectedAllowed: false,
334
-		expectedReason:  "denied by default",
334
+		expectedReason:  `Rachel cannot get on buildConfigs in backsaw`,
335 335
 	}
336 336
 	test.policies = newDefaultGlobalPolicies()
337 337
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -364,7 +364,7 @@ func TestVerbRestrictionsWork(t *testing.T) {
364 364
 			Resource: "buildConfigs",
365 365
 		},
366 366
 		expectedAllowed: false,
367
-		expectedReason:  "denied by default",
367
+		expectedReason:  `Valerie cannot create on buildConfigs in adze`,
368 368
 	}
369 369
 	test2.policies = newDefaultGlobalPolicies()
370 370
 	test2.policies = append(test2.policies, newAdzePolicies()...)
... ...
@@ -37,7 +37,7 @@ func TestViewerGetAllowedKindInAdze(t *testing.T) {
37 37
 			Resource: "pods",
38 38
 		},
39 39
 		expectedAllowed: false,
40
-		expectedReason:  "denied by default",
40
+		expectedReason:  "Victor cannot get on pods in adze",
41 41
 	}
42 42
 	test.policies = newDefaultGlobalPolicies()
43 43
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -57,7 +57,7 @@ func TestViewerGetDisallowedKindInMallet(t *testing.T) {
57 57
 			Resource: "policies",
58 58
 		},
59 59
 		expectedAllowed: false,
60
-		expectedReason:  "denied by default",
60
+		expectedReason:  "Victor cannot get on policies in mallet",
61 61
 	}
62 62
 	test.policies = newDefaultGlobalPolicies()
63 63
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -76,7 +76,7 @@ func TestViewerGetDisallowedKindInAdze(t *testing.T) {
76 76
 			Resource: "policies",
77 77
 		},
78 78
 		expectedAllowed: false,
79
-		expectedReason:  "denied by default",
79
+		expectedReason:  "Victor cannot get on policies in adze",
80 80
 	}
81 81
 	test.policies = newDefaultGlobalPolicies()
82 82
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -96,7 +96,7 @@ func TestViewerCreateAllowedKindInMallet(t *testing.T) {
96 96
 			Resource: "pods",
97 97
 		},
98 98
 		expectedAllowed: false,
99
-		expectedReason:  "denied by default",
99
+		expectedReason:  "Victor cannot create on pods in mallet",
100 100
 	}
101 101
 	test.policies = newDefaultGlobalPolicies()
102 102
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -115,7 +115,7 @@ func TestViewerCreateAllowedKindInAdze(t *testing.T) {
115 115
 			Resource: "pods",
116 116
 		},
117 117
 		expectedAllowed: false,
118
-		expectedReason:  "denied by default",
118
+		expectedReason:  "Victor cannot create on pods in adze",
119 119
 	}
120 120
 	test.policies = newDefaultGlobalPolicies()
121 121
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -154,7 +154,7 @@ func TestEditorUpdateAllowedKindInAdze(t *testing.T) {
154 154
 			Resource: "pods",
155 155
 		},
156 156
 		expectedAllowed: false,
157
-		expectedReason:  "denied by default",
157
+		expectedReason:  "Edgar cannot update on pods in adze",
158 158
 	}
159 159
 	test.policies = newDefaultGlobalPolicies()
160 160
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -174,7 +174,7 @@ func TestEditorUpdateDisallowedKindInMallet(t *testing.T) {
174 174
 			Resource: "roleBindings",
175 175
 		},
176 176
 		expectedAllowed: false,
177
-		expectedReason:  "denied by default",
177
+		expectedReason:  "Edgar cannot update on roleBindings in mallet",
178 178
 	}
179 179
 	test.policies = newDefaultGlobalPolicies()
180 180
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -193,7 +193,7 @@ func TestEditorUpdateDisallowedKindInAdze(t *testing.T) {
193 193
 			Resource: "roleBindings",
194 194
 		},
195 195
 		expectedAllowed: false,
196
-		expectedReason:  "denied by default",
196
+		expectedReason:  "Edgar cannot update on roleBindings in adze",
197 197
 	}
198 198
 	test.policies = newDefaultGlobalPolicies()
199 199
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -232,7 +232,7 @@ func TestEditorGetAllowedKindInAdze(t *testing.T) {
232 232
 			Resource: "pods",
233 233
 		},
234 234
 		expectedAllowed: false,
235
-		expectedReason:  "denied by default",
235
+		expectedReason:  "Edgar cannot get on pods in adze",
236 236
 	}
237 237
 	test.policies = newDefaultGlobalPolicies()
238 238
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -271,7 +271,7 @@ func TestAdminUpdateAllowedKindInAdze(t *testing.T) {
271 271
 			Resource: "roleBindings",
272 272
 		},
273 273
 		expectedAllowed: false,
274
-		expectedReason:  "denied by default",
274
+		expectedReason:  "Matthew cannot update on roleBindings in adze",
275 275
 	}
276 276
 	test.policies = newDefaultGlobalPolicies()
277 277
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -291,7 +291,7 @@ func TestAdminUpdateDisallowedKindInMallet(t *testing.T) {
291 291
 			Resource: "policies",
292 292
 		},
293 293
 		expectedAllowed: false,
294
-		expectedReason:  "denied by default",
294
+		expectedReason:  "Matthew cannot update on policies in mallet",
295 295
 	}
296 296
 	test.policies = newDefaultGlobalPolicies()
297 297
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -310,7 +310,7 @@ func TestAdminUpdateDisallowedKindInAdze(t *testing.T) {
310 310
 			Resource: "roles",
311 311
 		},
312 312
 		expectedAllowed: false,
313
-		expectedReason:  "denied by default",
313
+		expectedReason:  "Matthew cannot update on roles in adze",
314 314
 	}
315 315
 	test.policies = newDefaultGlobalPolicies()
316 316
 	test.policies = append(test.policies, newAdzePolicies()...)
... ...
@@ -349,7 +349,7 @@ func TestAdminGetAllowedKindInAdze(t *testing.T) {
349 349
 			Resource: "policies",
350 350
 		},
351 351
 		expectedAllowed: false,
352
-		expectedReason:  "denied by default",
352
+		expectedReason:  "Matthew cannot get on policies in adze",
353 353
 	}
354 354
 	test.policies = newDefaultGlobalPolicies()
355 355
 	test.policies = append(test.policies, newAdzePolicies()...)