Skip to content

Commit 9a6fa55

Browse files
Auto-create openshift-node and given nodes read on node-config
Other config variants will be stored in this location. The new namespace ensures clean security isolation.
1 parent bf63246 commit 9a6fa55

File tree

10 files changed

+106
-11
lines changed

10 files changed

+106
-11
lines changed

pkg/cmd/server/bootstrappolicy/constants.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package bootstrappolicy
44
const (
55
DefaultOpenShiftSharedResourcesNamespace = "openshift"
66
DefaultOpenShiftInfraNamespace = "openshift-infra"
7+
DefaultOpenShiftNodeNamespace = "openshift-node"
78
)
89

910
// users
@@ -98,7 +99,8 @@ const (
9899

99100
OpenshiftSharedResourceViewRoleName = "shared-resource-viewer"
100101

101-
NodeBootstrapRoleName = "system:node-bootstrapper"
102+
NodeBootstrapRoleName = "system:node-bootstrapper"
103+
NodeConfigReaderRoleName = "system:node-config-reader"
102104
)
103105

104106
// RoleBindings
@@ -120,6 +122,7 @@ const (
120122
NodeProxierRoleBindingName = NodeProxierRoleName + "s"
121123
NodeAdminRoleBindingName = NodeAdminRoleName + "s"
122124
NodeReaderRoleBindingName = NodeReaderRoleName + "s"
125+
NodeConfigReaderRoleBindingName = NodeConfigReaderRoleName + "s"
123126
SDNReaderRoleBindingName = SDNReaderRoleName + "s"
124127
SDNManagerRoleBindingName = SDNManagerRoleName + "s"
125128
WebHooksRoleBindingName = WebHooksRoleName + "s"

pkg/cmd/server/bootstrappolicy/policy.go

+17
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,15 @@ func GetOpenshiftBootstrapClusterRoles() []rbac.ClusterRole {
664664
// TODO: expose other things like /healthz on the node once we figure out non-resource URL policy across systems
665665
},
666666
},
667+
{
668+
ObjectMeta: metav1.ObjectMeta{
669+
Name: NodeConfigReaderRoleName,
670+
},
671+
Rules: []rbac.PolicyRule{
672+
// Allow the reader to read config maps in a given namespace with a given name.
673+
rbac.NewRule("get").Groups(kapiGroup).Resources("configmaps").RuleOrDie(),
674+
},
675+
},
667676
{
668677
ObjectMeta: metav1.ObjectMeta{
669678
Name: NodeRoleName,
@@ -1099,3 +1108,11 @@ func GetBootstrapNamespaceRoleBindings() map[string][]rbac.RoleBinding {
10991108
}
11001109
return ret
11011110
}
1111+
1112+
func GetBootstrapNodeConfigProvisioningRoleBindings(namespace string) []rbac.RoleBinding {
1113+
return []rbac.RoleBinding{
1114+
newOriginRoleBindingForClusterRole(NodeConfigReaderRoleBindingName, NodeConfigReaderRoleName, namespace).
1115+
Groups(NodesGroup).
1116+
BindingOrDie(),
1117+
}
1118+
}

pkg/cmd/server/bootstrappolicy/web_console_role_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var rolesToHide = sets.NewString(
4242
"system:node",
4343
"system:node-admin",
4444
"system:node-bootstrapper",
45+
"system:node-config-reader",
4546
"system:node-problem-detector",
4647
"system:node-proxier",
4748
"system:node-reader",

pkg/cmd/server/origin/ensure.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,38 @@ package origin
22

33
import (
44
genericapiserver "k8s.io/apiserver/pkg/server"
5+
"k8s.io/kubernetes/pkg/apis/rbac"
6+
rbacrest "k8s.io/kubernetes/pkg/registry/rbac/rest"
7+
8+
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
59
)
610

711
// ensureOpenShiftSharedResourcesNamespace is called as part of global policy initialization to ensure shared namespace exists
812
func (c *MasterConfig) ensureOpenShiftSharedResourcesNamespace(context genericapiserver.PostStartHookContext) error {
9-
ensureNamespaceServiceAccountRoleBindings(context, c.Options.PolicyConfig.OpenShiftSharedResourcesNamespace)
13+
ns := c.Options.PolicyConfig.OpenShiftSharedResourcesNamespace
14+
ensureNamespaceServiceAccountRoleBindings(
15+
context,
16+
ns,
17+
&rbacrest.PolicyData{
18+
RoleBindings: map[string][]rbac.RoleBinding{
19+
ns: bootstrappolicy.GetBootstrapServiceAccountProjectRoleBindings(ns),
20+
},
21+
},
22+
)
23+
return nil
24+
}
25+
26+
// ensureOpenShiftNodeNamespace is called as part of global policy initialization to ensure a node namespace exists
27+
func (c *MasterConfig) ensureOpenShiftNodeNamespace(context genericapiserver.PostStartHookContext) error {
28+
ns := bootstrappolicy.DefaultOpenShiftNodeNamespace
29+
ensureNamespaceServiceAccountRoleBindings(
30+
context,
31+
ns,
32+
&rbacrest.PolicyData{
33+
RoleBindings: map[string][]rbac.RoleBinding{
34+
ns: bootstrappolicy.GetBootstrapNodeConfigProvisioningRoleBindings(ns),
35+
},
36+
},
37+
)
1038
return nil
1139
}

pkg/cmd/server/origin/master.go

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ func (c *MasterConfig) Run(kubeAPIServerConfig *kubeapiserver.Config, controller
251251
}
252252

253253
// add post-start hooks
254+
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("node.openshift.io-sharednamespace", c.ensureOpenShiftNodeNamespace)
254255
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("template.openshift.io-sharednamespace", c.ensureOpenShiftSharedResourcesNamespace)
255256
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("authorization.openshift.io-bootstrapclusterroles", bootstrappolicy.Policy().EnsureRBACPolicy())
256257
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("admission.openshift.io-RefreshRESTMapper", func(context apiserver.PostStartHookContext) error {

pkg/cmd/server/origin/openshift_apiserver.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,15 @@ func (c *OpenshiftAPIConfig) bootstrapSCC(context genericapiserver.PostStartHook
672672
func (c *OpenshiftAPIConfig) ensureOpenShiftInfraNamespace(context genericapiserver.PostStartHookContext) error {
673673
ns := bootstrappolicy.DefaultOpenShiftInfraNamespace
674674

675-
ensureNamespaceServiceAccountRoleBindings(context, ns)
675+
ensureNamespaceServiceAccountRoleBindings(
676+
context,
677+
ns,
678+
&rbacrest.PolicyData{
679+
RoleBindings: map[string][]rbac.RoleBinding{
680+
ns: bootstrappolicy.GetBootstrapServiceAccountProjectRoleBindings(ns),
681+
},
682+
},
683+
)
676684

677685
var coreClient coreclient.CoreInterface
678686
err := wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
@@ -692,20 +700,29 @@ func (c *OpenshiftAPIConfig) ensureOpenShiftInfraNamespace(context genericapiser
692700
// Ensure we have the bootstrap SA for Nodes
693701
_, err = coreClient.ServiceAccounts(ns).Create(&kapi.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: bootstrappolicy.InfraNodeBootstrapServiceAccountName}})
694702
if err != nil && !kapierror.IsAlreadyExists(err) {
695-
glog.Errorf("Error creating service account %s/%s: %v", ns, bootstrappolicy.InfraNodeBootstrapServiceAccountName, err)
703+
utilruntime.HandleError(fmt.Errorf("Error creating service account %s/%s: %v", ns, bootstrappolicy.InfraNodeBootstrapServiceAccountName, err))
704+
return err
696705
}
697706

698707
return nil
699708
}
700709

701710
// ensureDefaultNamespaceServiceAccountRoles initializes roles for service accounts in the default namespace
702711
func (c *OpenshiftAPIConfig) ensureDefaultNamespaceServiceAccountRoles(context genericapiserver.PostStartHookContext) error {
703-
ensureNamespaceServiceAccountRoleBindings(context, metav1.NamespaceDefault)
712+
ensureNamespaceServiceAccountRoleBindings(
713+
context,
714+
metav1.NamespaceDefault,
715+
&rbacrest.PolicyData{
716+
RoleBindings: map[string][]rbac.RoleBinding{
717+
metav1.NamespaceDefault: bootstrappolicy.GetBootstrapServiceAccountProjectRoleBindings(metav1.NamespaceDefault),
718+
},
719+
},
720+
)
704721
return nil
705722
}
706723

707724
// ensureNamespaceServiceAccountRoleBindings initializes roles for service accounts in the namespace
708-
func ensureNamespaceServiceAccountRoleBindings(context genericapiserver.PostStartHookContext, namespaceName string) {
725+
func ensureNamespaceServiceAccountRoleBindings(context genericapiserver.PostStartHookContext, namespaceName string, policyData *rbacrest.PolicyData) {
709726
const ServiceAccountRolesInitializedAnnotation = "openshift.io/sa.initialized-roles"
710727

711728
var coreClient coreclient.CoreInterface
@@ -742,11 +759,6 @@ func ensureNamespaceServiceAccountRoleBindings(context genericapiserver.PostStar
742759
return
743760
}
744761

745-
policyData := &rbacrest.PolicyData{
746-
RoleBindings: map[string][]rbac.RoleBinding{
747-
namespace.Name: bootstrappolicy.GetBootstrapServiceAccountProjectRoleBindings(namespace.Name),
748-
},
749-
}
750762
if err := policyData.EnsureRBACPolicy()(context); err != nil {
751763
utilruntime.HandleError(err)
752764
return

test/integration/front_proxy_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func TestFrontProxy(t *testing.T) {
158158
"kube-system",
159159
"openshift",
160160
"openshift-infra",
161+
"openshift-node",
161162
),
162163
},
163164
} {

test/integration/master_routes_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ var expectedIndex = []string{
100100
// "/healthz/poststarthook/extensions/third-party-resources", // Do not enable this controller, we do not support it
101101
"/healthz/poststarthook/generic-apiserver-start-informers",
102102
"/healthz/poststarthook/kube-apiserver-autoregistration",
103+
"/healthz/poststarthook/node.openshift.io-sharednamespace",
103104
"/healthz/poststarthook/oauth.openshift.io-EnsureBootstrapOAuthClients",
104105
"/healthz/poststarthook/project.openshift.io-projectauthorizationcache",
105106
"/healthz/poststarthook/project.openshift.io-projectcache",

test/testdata/bootstrappolicy/bootstrap_cluster_roles.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,21 @@ items:
21772177
verbs:
21782178
- create
21792179
- get
2180+
- apiVersion: rbac.authorization.k8s.io/v1beta1
2181+
kind: ClusterRole
2182+
metadata:
2183+
annotations:
2184+
authorization.openshift.io/system-only: "true"
2185+
rbac.authorization.kubernetes.io/autoupdate: "true"
2186+
creationTimestamp: null
2187+
name: system:node-config-reader
2188+
rules:
2189+
- apiGroups:
2190+
- ""
2191+
resources:
2192+
- configmaps
2193+
verbs:
2194+
- get
21802195
- apiVersion: rbac.authorization.k8s.io/v1beta1
21812196
kind: ClusterRole
21822197
metadata:

test/testdata/bootstrappolicy/bootstrap_policy_file.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,22 @@ items:
23792379
verbs:
23802380
- create
23812381
- get
2382+
- apiVersion: v1
2383+
kind: ClusterRole
2384+
metadata:
2385+
annotations:
2386+
authorization.openshift.io/system-only: "true"
2387+
openshift.io/reconcile-protect: "false"
2388+
creationTimestamp: null
2389+
name: system:node-config-reader
2390+
rules:
2391+
- apiGroups:
2392+
- ""
2393+
attributeRestrictions: null
2394+
resources:
2395+
- configmaps
2396+
verbs:
2397+
- get
23822398
- apiVersion: v1
23832399
kind: ClusterRole
23842400
metadata:

0 commit comments

Comments
 (0)