tlsbootstrap.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // CSRAutoApprovalClusterRoleName defines the name of the auto-bootstrapped ClusterRole for making the csrapprover controller auto-approve the CSR
  29. // TODO: This value should be defined in an other, generic authz package instead of here
  30. // Starting from v1.8, CSRAutoApprovalClusterRoleName is automatically created by the API server on startup
  31. CSRAutoApprovalClusterRoleName = "system:certificates.k8s.io:certificatesigningrequests:nodeclient"
  32. // NodeSelfCSRAutoApprovalClusterRoleName is a role defined in default 1.8 RBAC policies for automatic CSR approvals for automatically rotated node certificates
  33. NodeSelfCSRAutoApprovalClusterRoleName = "system:certificates.k8s.io:certificatesigningrequests:selfnodeclient"
  34. // NodeAutoApproveBootstrapClusterRoleBinding defines the name of the ClusterRoleBinding that makes the csrapprover approve node CSRs
  35. NodeAutoApproveBootstrapClusterRoleBinding = "kubeadm:node-autoapprove-bootstrap"
  36. // NodeAutoApproveCertificateRotationClusterRoleBinding defines name of the ClusterRoleBinding that makes the csrapprover approve node auto rotated CSRs
  37. NodeAutoApproveCertificateRotationClusterRoleBinding = "kubeadm:node-autoapprove-certificate-rotation"
  38. )
  39. // AllowBootstrapTokensToPostCSRs creates RBAC rules in a way the makes Node Bootstrap Tokens able to post CSRs
  40. func AllowBootstrapTokensToPostCSRs(client clientset.Interface) error {
  41. 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")
  42. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  43. ObjectMeta: metav1.ObjectMeta{
  44. Name: NodeKubeletBootstrap,
  45. },
  46. RoleRef: rbac.RoleRef{
  47. APIGroup: rbac.GroupName,
  48. Kind: "ClusterRole",
  49. Name: NodeBootstrapperClusterRoleName,
  50. },
  51. Subjects: []rbac.Subject{
  52. {
  53. Kind: rbac.GroupKind,
  54. Name: constants.NodeBootstrapTokenAuthGroup,
  55. },
  56. },
  57. })
  58. }
  59. // AutoApproveNodeBootstrapTokens creates RBAC rules in a way that makes Node Bootstrap Tokens' CSR auto-approved by the csrapprover controller
  60. func AutoApproveNodeBootstrapTokens(client clientset.Interface) error {
  61. fmt.Println("[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token")
  62. // Always create this kubeadm-specific binding though
  63. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  64. ObjectMeta: metav1.ObjectMeta{
  65. Name: NodeAutoApproveBootstrapClusterRoleBinding,
  66. },
  67. RoleRef: rbac.RoleRef{
  68. APIGroup: rbac.GroupName,
  69. Kind: "ClusterRole",
  70. Name: CSRAutoApprovalClusterRoleName,
  71. },
  72. Subjects: []rbac.Subject{
  73. {
  74. Kind: "Group",
  75. Name: constants.NodeBootstrapTokenAuthGroup,
  76. },
  77. },
  78. })
  79. }
  80. // AutoApproveNodeCertificateRotation creates RBAC rules in a way that makes Node certificate rotation CSR auto-approved by the csrapprover controller
  81. func AutoApproveNodeCertificateRotation(client clientset.Interface) error {
  82. fmt.Println("[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster")
  83. return apiclient.CreateOrUpdateClusterRoleBinding(client, &rbac.ClusterRoleBinding{
  84. ObjectMeta: metav1.ObjectMeta{
  85. Name: NodeAutoApproveCertificateRotationClusterRoleBinding,
  86. },
  87. RoleRef: rbac.RoleRef{
  88. APIGroup: rbac.GroupName,
  89. Kind: "ClusterRole",
  90. Name: NodeSelfCSRAutoApprovalClusterRoleName,
  91. },
  92. Subjects: []rbac.Subject{
  93. {
  94. Kind: "Group",
  95. Name: constants.NodesGroup,
  96. },
  97. },
  98. })
  99. }