Skip to content

Commit 53c0830

Browse files
author
OpenShift Bot
authored
Merge pull request #13649 from Miciah/RestrictUsersAdmission-allow-SA-with-implicit-NS
Merged by openshift-bot
2 parents dbcd81b + 8d8b3e4 commit 53c0830

File tree

2 files changed

+122
-4
lines changed

2 files changed

+122
-4
lines changed

pkg/authorization/admission/restrictusers/subjectchecker.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,19 @@ func (checker ServiceAccountSubjectChecker) Allowed(subject kapi.ObjectReference
279279
return false, nil
280280
}
281281

282+
subjectNamespace := subject.Namespace
283+
if len(subjectNamespace) == 0 {
284+
// If a RoleBinding has a subject that is a ServiceAccount with
285+
// no namespace specified, the namespace will be defaulted to
286+
// that of the RoleBinding. However, admission control plug-ins
287+
// execute before this happens, so in order not to reject such
288+
// subjects erroneously, we copy the logic here of using the
289+
// RoleBinding's namespace if the subject's is empty.
290+
subjectNamespace = ctx.namespace
291+
}
292+
282293
for _, namespace := range checker.serviceAccountRestriction.Namespaces {
283-
if subject.Namespace == namespace {
294+
if subjectNamespace == namespace {
284295
return true, nil
285296
}
286297
}
@@ -292,7 +303,7 @@ func (checker ServiceAccountSubjectChecker) Allowed(subject kapi.ObjectReference
292303
}
293304

294305
if subject.Name == serviceAccountRef.Name &&
295-
subject.Namespace == serviceAccountNamespace {
306+
subjectNamespace == serviceAccountNamespace {
296307
return true, nil
297308
}
298309
}

pkg/authorization/admission/restrictusers/subjectchecker_test.go

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func TestSubjectCheckers(t *testing.T) {
192192
shouldAllow: true,
193193
},
194194
{
195-
name: "allow service account by literal name match",
195+
name: "allow service account with explicit namespace by match on literal name and explicit namespace",
196196
checker: mustNewSubjectChecker(t,
197197
&authorizationapi.RoleBindingRestrictionSpec{
198198
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
@@ -208,7 +208,7 @@ func TestSubjectCheckers(t *testing.T) {
208208
shouldAllow: true,
209209
},
210210
{
211-
name: "allow service account by literal name match with implicit namespace",
211+
name: "allow service account with explicit namespace by match on literal name and implicit namespace",
212212
checker: mustNewSubjectChecker(t,
213213
&authorizationapi.RoleBindingRestrictionSpec{
214214
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
@@ -220,6 +220,113 @@ func TestSubjectCheckers(t *testing.T) {
220220
subject: serviceaccountRef,
221221
shouldAllow: true,
222222
},
223+
{
224+
name: "prohibit service account with explicit namespace where literal name matches but explicit namespace does not",
225+
checker: mustNewSubjectChecker(t,
226+
&authorizationapi.RoleBindingRestrictionSpec{
227+
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
228+
ServiceAccounts: []authorizationapi.ServiceAccountReference{
229+
{
230+
Namespace: serviceaccountRef.Namespace,
231+
Name: serviceaccountRef.Name,
232+
},
233+
},
234+
},
235+
}),
236+
subject: kapi.ObjectReference{
237+
Kind: authorizationapi.ServiceAccountKind,
238+
Namespace: "othernamespace",
239+
Name: serviceaccountRef.Name,
240+
},
241+
shouldAllow: false,
242+
},
243+
{
244+
name: "prohibit service account with explicit namespace where literal name matches but implicit namespace does not",
245+
checker: mustNewSubjectChecker(t,
246+
&authorizationapi.RoleBindingRestrictionSpec{
247+
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
248+
ServiceAccounts: []authorizationapi.ServiceAccountReference{
249+
{Name: serviceaccountRef.Name},
250+
},
251+
},
252+
}),
253+
subject: kapi.ObjectReference{
254+
Kind: authorizationapi.ServiceAccountKind,
255+
Namespace: "othernamespace",
256+
Name: serviceaccountRef.Name,
257+
},
258+
shouldAllow: false,
259+
},
260+
{
261+
name: "allow service account with implicit namespace by match on literal name and explicit namespace",
262+
checker: mustNewSubjectChecker(t,
263+
&authorizationapi.RoleBindingRestrictionSpec{
264+
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
265+
ServiceAccounts: []authorizationapi.ServiceAccountReference{
266+
{
267+
Name: serviceaccountRef.Name,
268+
Namespace: serviceaccountRef.Namespace,
269+
},
270+
},
271+
},
272+
}),
273+
subject: kapi.ObjectReference{
274+
Kind: authorizationapi.ServiceAccountKind,
275+
Name: serviceaccountRef.Name,
276+
},
277+
shouldAllow: true,
278+
},
279+
{
280+
name: "allow service account with implicit namespace by match on literal name and implicit namespace",
281+
checker: mustNewSubjectChecker(t,
282+
&authorizationapi.RoleBindingRestrictionSpec{
283+
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
284+
ServiceAccounts: []authorizationapi.ServiceAccountReference{
285+
{Name: serviceaccountRef.Name},
286+
},
287+
},
288+
}),
289+
subject: kapi.ObjectReference{
290+
Kind: authorizationapi.ServiceAccountKind,
291+
Name: serviceaccountRef.Name,
292+
},
293+
shouldAllow: true,
294+
},
295+
{
296+
name: "prohibit service account with implicit namespace where literal name matches but explicit namespace does not",
297+
checker: mustNewSubjectChecker(t,
298+
&authorizationapi.RoleBindingRestrictionSpec{
299+
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
300+
ServiceAccounts: []authorizationapi.ServiceAccountReference{
301+
{
302+
Namespace: "othernamespace",
303+
Name: serviceaccountRef.Name,
304+
},
305+
},
306+
},
307+
}),
308+
subject: kapi.ObjectReference{
309+
Kind: authorizationapi.ServiceAccountKind,
310+
Name: serviceaccountRef.Name,
311+
},
312+
shouldAllow: false,
313+
},
314+
{
315+
name: "prohibit service account with explicit namespace where explicit namespace matches but literal name does not",
316+
checker: mustNewSubjectChecker(t,
317+
&authorizationapi.RoleBindingRestrictionSpec{
318+
ServiceAccountRestriction: &authorizationapi.ServiceAccountRestriction{
319+
ServiceAccounts: []authorizationapi.ServiceAccountReference{
320+
{
321+
Namespace: serviceaccountRef.Namespace,
322+
Name: "othername",
323+
},
324+
},
325+
},
326+
}),
327+
subject: serviceaccountRef,
328+
shouldAllow: false,
329+
},
223330
{
224331
name: "allow service account by match on namespace",
225332
checker: mustNewSubjectChecker(t,

0 commit comments

Comments
 (0)