Skip to content

Commit 12b480d

Browse files
committed
setup default impersonation rules
1 parent 68e1ee2 commit 12b480d

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

pkg/cmd/server/bootstrappolicy/constants.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const (
1616
RouterUnqualifiedUsername = "openshift-router"
1717
RegistryUnqualifiedUsername = "openshift-registry"
1818

19-
MasterUsername = "system:" + MasterUnqualifiedUsername
20-
RouterUsername = "system:" + RouterUnqualifiedUsername
21-
RegistryUsername = "system:" + RegistryUnqualifiedUsername
19+
MasterUsername = "system:" + MasterUnqualifiedUsername
20+
RouterUsername = "system:" + RouterUnqualifiedUsername
21+
RegistryUsername = "system:" + RegistryUnqualifiedUsername
22+
SystemAdminUsername = "system:admin"
2223

2324
// Not granted any API permissions, just an identity for a client certificate for the API proxy to use
2425
// Should not be changed without considering impact to pods that may be verifying this identity by default
@@ -51,6 +52,7 @@ const (
5152
// Roles
5253
const (
5354
ClusterAdminRoleName = "cluster-admin"
55+
SudoerRoleName = "sudoer"
5456
ClusterReaderRoleName = "cluster-reader"
5557
AdminRoleName = "admin"
5658
EditRoleName = "edit"

pkg/cmd/server/bootstrappolicy/policy.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ func GetBootstrapClusterRoles() []authorizationapi.ClusterRole {
6464
},
6565
},
6666
},
67+
{
68+
ObjectMeta: kapi.ObjectMeta{
69+
Name: SudoerRoleName,
70+
},
71+
Rules: []authorizationapi.PolicyRule{
72+
{
73+
APIGroups: []string{kapi.GroupName},
74+
Verbs: sets.NewString("impersonate"),
75+
Resources: sets.NewString(authorizationapi.SystemUserResource),
76+
ResourceNames: sets.NewString(SystemAdminUsername),
77+
},
78+
},
79+
},
6780
{
6881
ObjectMeta: kapi.ObjectMeta{
6982
Name: ClusterReaderRoleName,
@@ -162,6 +175,11 @@ func GetBootstrapClusterRoles() []authorizationapi.ClusterRole {
162175
"replicationcontrollers/scale",
163176
),
164177
},
178+
{
179+
APIGroups: []string{kapi.GroupName},
180+
Verbs: sets.NewString("impersonate"),
181+
Resources: sets.NewString("serviceaccounts"),
182+
},
165183
{
166184
APIGroups: []string{api.GroupName},
167185
Verbs: sets.NewString("get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"),
@@ -225,6 +243,11 @@ func GetBootstrapClusterRoles() []authorizationapi.ClusterRole {
225243
"replicationcontrollers/scale",
226244
),
227245
},
246+
{
247+
APIGroups: []string{kapi.GroupName},
248+
Verbs: sets.NewString("impersonate"),
249+
Resources: sets.NewString("serviceaccounts"),
250+
},
228251
{
229252
APIGroups: []string{api.GroupName},
230253
Verbs: sets.NewString("get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"),
@@ -858,7 +881,11 @@ func GetBootstrapClusterRoleBindings() []authorizationapi.ClusterRoleBinding {
858881
RoleRef: kapi.ObjectReference{
859882
Name: ClusterAdminRoleName,
860883
},
861-
Subjects: []kapi.ObjectReference{{Kind: authorizationapi.SystemGroupKind, Name: ClusterAdminGroup}},
884+
Subjects: []kapi.ObjectReference{
885+
{Kind: authorizationapi.SystemGroupKind, Name: ClusterAdminGroup},
886+
// add system:admin to this binding so that members of the sudoer group can use --as=system:admin to run a command as a cluster-admin
887+
{Kind: authorizationapi.SystemUserKind, Name: SystemAdminUsername},
888+
},
862889
},
863890
{
864891
ObjectMeta: kapi.ObjectMeta{

test/cmd/policy.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ os::test::junit::declare_suite_start "cmd/policy"
1515
# This test validates user level policy
1616
os::cmd::expect_success_and_text 'oc whoami --as deads' "deads"
1717

18+
os::cmd::expect_success 'oadm policy add-cluster-role-to-user sudoer wheel'
19+
os::cmd::expect_success 'oc login -u wheel -p pw'
20+
os::cmd::expect_success_and_text 'oc whoami' "wheel"
21+
os::cmd::expect_failure 'oc whoami --as deads'
22+
os::cmd::expect_success_and_text 'oc whoami --as=system:admin' "system:admin"
23+
24+
os::cmd::expect_success 'oc login -u local-admin -p pw'
25+
os::cmd::expect_success 'oc new-project foo'
26+
os::cmd::expect_failure 'oc whoami --as=system:admin'
27+
os::cmd::expect_success_and_text 'oc whoami --as=system:serviceaccount:foo:default' "system:serviceaccount:foo:default"
28+
os::cmd::expect_failure 'oc whoami --as=system:serviceaccount:another:default'
29+
os::cmd::expect_success 'oc login -u system:admin -n cmd-policy'
30+
31+
1832
# This test validates user level policy
1933
os::cmd::expect_failure_and_text 'oc policy add-role-to-user' 'you must specify a role'
2034
os::cmd::expect_failure_and_text 'oc policy add-role-to-user -z NamespaceWithoutRole' 'you must specify a role'

test/fixtures/bootstrappolicy/bootstrap_cluster_role_bindings.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ items:
4141
subjects:
4242
- kind: SystemGroup
4343
name: system:cluster-admins
44-
userNames: null
44+
- kind: SystemUser
45+
name: system:admin
46+
userNames:
47+
- system:admin
4548
- apiVersion: v1
4649
groupNames:
4750
- system:cluster-readers

test/fixtures/bootstrappolicy/bootstrap_cluster_roles.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ items:
2020
resources: []
2121
verbs:
2222
- '*'
23+
- apiVersion: v1
24+
kind: ClusterRole
25+
metadata:
26+
creationTimestamp: null
27+
name: sudoer
28+
rules:
29+
- apiGroups:
30+
- ""
31+
attributeRestrictions: null
32+
resourceNames:
33+
- system:admin
34+
resources:
35+
- systemusers
36+
verbs:
37+
- impersonate
2338
- apiVersion: v1
2439
kind: ClusterRole
2540
metadata:
@@ -231,6 +246,13 @@ items:
231246
- patch
232247
- update
233248
- watch
249+
- apiGroups:
250+
- ""
251+
attributeRestrictions: null
252+
resources:
253+
- serviceaccounts
254+
verbs:
255+
- impersonate
234256
- apiGroups:
235257
- ""
236258
attributeRestrictions: null
@@ -408,6 +430,13 @@ items:
408430
- patch
409431
- update
410432
- watch
433+
- apiGroups:
434+
- ""
435+
attributeRestrictions: null
436+
resources:
437+
- serviceaccounts
438+
verbs:
439+
- impersonate
411440
- apiGroups:
412441
- ""
413442
attributeRestrictions: null

0 commit comments

Comments
 (0)