Skip to content

Commit d71cd7c

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 c30c823 commit d71cd7c

File tree

6 files changed

+73
-11
lines changed

6 files changed

+73
-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
@@ -689,6 +689,15 @@ func GetOpenshiftBootstrapClusterRoles() []rbac.ClusterRole {
689689
// TODO: expose other things like /healthz on the node once we figure out non-resource URL policy across systems
690690
},
691691
},
692+
{
693+
ObjectMeta: metav1.ObjectMeta{
694+
Name: NodeConfigReaderRoleName,
695+
},
696+
Rules: []rbac.PolicyRule{
697+
// Allow the reader to read config maps in a given namespace with a given name.
698+
rbac.NewRule("get").Groups(kapiGroup).Resources("configmaps").Names("node-config").RuleOrDie(),
699+
},
700+
},
692701
{
693702
ObjectMeta: metav1.ObjectMeta{
694703
Name: NodeRoleName,
@@ -1083,3 +1092,11 @@ var rolesToShow = sets.NewString(
10831092
"system:image-pusher",
10841093
"view",
10851094
)
1095+
1096+
func GetBootstrapNodeConfigProvisioningRoleBindings(namespace string) []rbac.RoleBinding {
1097+
return []rbac.RoleBinding{
1098+
newOriginRoleBindingForClusterRole(NodeConfigReaderRoleBindingName, NodeConfigReaderRoleName, namespace).
1099+
Groups(NodesGroup).
1100+
BindingOrDie(),
1101+
}
1102+
}

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
@@ -247,6 +247,7 @@ func (c *MasterConfig) Run(kubeAPIServerConfig *kubeapiserver.Config, controller
247247
}
248248

249249
// add post-start hooks
250+
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("node.openshift.io-sharednamespace", c.ensureOpenShiftNodeNamespace)
250251
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("template.openshift.io-sharednamespace", c.ensureOpenShiftSharedResourcesNamespace)
251252
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("authorization.openshift.io-bootstrapclusterroles", bootstrappolicy.Policy().EnsureRBACPolicy())
252253
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/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",

0 commit comments

Comments
 (0)