tlsbootstrap.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "fmt"
  16. rbac "k8s.io/api/rbac/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  20. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  21. )
  22. const (
  23. // NodeBootstrapperClusterRoleName defines the name of the auto-bootstrapped ClusterRole for letting someone post a CSR
  24. // TODO: This value should be defined in an other, generic authz package instead of here
  25. NodeBootstrapperClusterRoleName = "system:node-bootstrapper"
  26. // NodeKubeletBootstrap defines the name of the ClusterRoleBinding that lets kubelets post CSRs
  27. NodeKubeletBootstrap = "kubeadm:kubelet-bootstrap"
  28. // GetNodesClusterRoleName defines the name of the ClusterRole and ClusterRoleBinding to get nodes
  29. GetNodesClusterRoleName = "kubeadm:get-nodes"
  30. // CSRAutoApprovalClusterRoleName defines the name of the auto-bootstrapped ClusterRole for making the csrapprover controller auto-approve the CSR
  31. // TODO: This value should be defined in an other, generic authz package instead of here
  32. // Starting from v1.8, CSRAutoApprovalClusterRoleName is automatically created by the API server on startup
  33. CSRAutoApprovalClusterRoleName = "system:certificates.k8s.io:certificatesigningrequests:nodeclient"
  34. // NodeSelfCSRAutoApprovalClusterRoleName is a role defined in default 1.8 RBAC policies for automatic CSR approvals for automatically rotated node certificates
  35. NodeSelfCSRAutoApprovalClusterRoleName = "system:certificates.k8s.io:certificatesigningrequests:selfnodeclient"
  36. // NodeAutoApproveBootstrapClusterRoleBinding defines the name of the ClusterRoleBinding that makes the csrapprover approve node CSRs
  37. NodeAutoApproveBootstrapClusterRoleBinding = "kubeadm:node-autoapprove-bootstrap"
  38. // NodeAutoApproveCertificateRotationClusterRoleBinding defines name of the ClusterRoleBinding that makes the csrapprover approve node auto rotated CSRs
  39. NodeAutoApproveCertificateRotationClusterRoleBinding = "kubeadm:node-autoapprove-certificate-rotation"
  40. )
  41. // AllowBootstrapTokensToPostCSRs creates RBAC rules in a way the makes Node Bootstrap Tokens able to post CSRs
  42. func AllowBootstrapTokensToPostCSRs(client clientset.Interface) error {
  43. fmt.Println("[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials")
  44. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  45. ObjectMeta: metav1.ObjectMeta{
  46. Name: NodeKubeletBootstrap,
  47. },
  48. RoleRef: rbac.RoleRef{
  49. APIGroup: rbac.GroupName,
  50. Kind: "ClusterRole",
  51. Name: NodeBootstrapperClusterRoleName,
  52. },
  53. Subjects: []rbac.Subject{
  54. {
  55. Kind: rbac.GroupKind,
  56. Name: constants.NodeBootstrapTokenAuthGroup,
  57. },
  58. },
  59. })
  60. }
  61. // AllowBoostrapTokensToGetNodes creates RBAC rules to allow Node Bootstrap Tokens to list nodes
  62. func AllowBoostrapTokensToGetNodes(client clientset.Interface) error {
  63. fmt.Println("[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes")
  64. if err := apiclient.CreateOrUpdateClusterRole(client, &rbac.ClusterRole{
  65. ObjectMeta: metav1.ObjectMeta{
  66. Name: GetNodesClusterRoleName,
  67. Namespace: metav1.NamespaceSystem,
  68. },
  69. Rules: []rbac.PolicyRule{
  70. {
  71. Verbs: []string{"get"},
  72. APIGroups: []string{""},
  73. Resources: []string{"nodes"},
  74. },
  75. },
  76. }); err != nil {
  77. return err
  78. }
  79. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  80. ObjectMeta: metav1.ObjectMeta{
  81. Name: GetNodesClusterRoleName,
  82. Namespace: metav1.NamespaceSystem,
  83. },
  84. RoleRef: rbac.RoleRef{
  85. APIGroup: rbac.GroupName,
  86. Kind: "ClusterRole",
  87. Name: GetNodesClusterRoleName,
  88. },
  89. Subjects: []rbac.Subject{
  90. {
  91. Kind: rbac.GroupKind,
  92. Name: constants.NodeBootstrapTokenAuthGroup,
  93. },
  94. },
  95. })
  96. }
  97. // AutoApproveNodeBootstrapTokens creates RBAC rules in a way that makes Node Bootstrap Tokens' CSR auto-approved by the csrapprover controller
  98. func AutoApproveNodeBootstrapTokens(client clientset.Interface) error {
  99. fmt.Println("[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token")
  100. // Always create this kubeadm-specific binding though
  101. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  102. ObjectMeta: metav1.ObjectMeta{
  103. Name: NodeAutoApproveBootstrapClusterRoleBinding,
  104. },
  105. RoleRef: rbac.RoleRef{
  106. APIGroup: rbac.GroupName,
  107. Kind: "ClusterRole",
  108. Name: CSRAutoApprovalClusterRoleName,
  109. },
  110. Subjects: []rbac.Subject{
  111. {
  112. Kind: "Group",
  113. Name: constants.NodeBootstrapTokenAuthGroup,
  114. },
  115. },
  116. })
  117. }
  118. // AutoApproveNodeCertificateRotation creates RBAC rules in a way that makes Node certificate rotation CSR auto-approved by the csrapprover controller
  119. func AutoApproveNodeCertificateRotation(client clientset.Interface) error {
  120. fmt.Println("[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster")
  121. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  122. ObjectMeta: metav1.ObjectMeta{
  123. Name: NodeAutoApproveCertificateRotationClusterRoleBinding,
  124. },
  125. RoleRef: rbac.RoleRef{
  126. APIGroup: rbac.GroupName,
  127. Kind: "ClusterRole",
  128. Name: NodeSelfCSRAutoApprovalClusterRoleName,
  129. },
  130. Subjects: []rbac.Subject{
  131. {
  132. Kind: "Group",
  133. Name: constants.NodesGroup,
  134. },
  135. },
  136. })
  137. }