sarapprove.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright 2016 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 approver implements an automated approver for kubelet certificates.
  14. package approver
  15. import (
  16. "crypto/x509"
  17. "fmt"
  18. "reflect"
  19. "strings"
  20. authorization "k8s.io/api/authorization/v1beta1"
  21. capi "k8s.io/api/certificates/v1beta1"
  22. certificatesinformers "k8s.io/client-go/informers/certificates/v1beta1"
  23. clientset "k8s.io/client-go/kubernetes"
  24. k8s_certificates_v1beta1 "k8s.io/kubernetes/pkg/apis/certificates/v1beta1"
  25. "k8s.io/kubernetes/pkg/controller/certificates"
  26. )
  27. type csrRecognizer struct {
  28. recognize func(csr *capi.CertificateSigningRequest, x509cr *x509.CertificateRequest) bool
  29. permission authorization.ResourceAttributes
  30. successMessage string
  31. }
  32. type sarApprover struct {
  33. client clientset.Interface
  34. recognizers []csrRecognizer
  35. }
  36. // NewCSRApprovingController creates a new CSRApprovingController.
  37. func NewCSRApprovingController(client clientset.Interface, csrInformer certificatesinformers.CertificateSigningRequestInformer) *certificates.CertificateController {
  38. approver := &sarApprover{
  39. client: client,
  40. recognizers: recognizers(),
  41. }
  42. return certificates.NewCertificateController(
  43. client,
  44. csrInformer,
  45. approver.handle,
  46. )
  47. }
  48. func recognizers() []csrRecognizer {
  49. recognizers := []csrRecognizer{
  50. {
  51. recognize: isSelfNodeClientCert,
  52. permission: authorization.ResourceAttributes{Group: "certificates.k8s.io", Resource: "certificatesigningrequests", Verb: "create", Subresource: "selfnodeclient"},
  53. successMessage: "Auto approving self kubelet client certificate after SubjectAccessReview.",
  54. },
  55. {
  56. recognize: isNodeClientCert,
  57. permission: authorization.ResourceAttributes{Group: "certificates.k8s.io", Resource: "certificatesigningrequests", Verb: "create", Subresource: "nodeclient"},
  58. successMessage: "Auto approving kubelet client certificate after SubjectAccessReview.",
  59. },
  60. }
  61. return recognizers
  62. }
  63. func (a *sarApprover) handle(csr *capi.CertificateSigningRequest) error {
  64. if len(csr.Status.Certificate) != 0 {
  65. return nil
  66. }
  67. if approved, denied := certificates.GetCertApprovalCondition(&csr.Status); approved || denied {
  68. return nil
  69. }
  70. x509cr, err := k8s_certificates_v1beta1.ParseCSR(csr)
  71. if err != nil {
  72. return fmt.Errorf("unable to parse csr %q: %v", csr.Name, err)
  73. }
  74. tried := []string{}
  75. for _, r := range a.recognizers {
  76. if !r.recognize(csr, x509cr) {
  77. continue
  78. }
  79. tried = append(tried, r.permission.Subresource)
  80. approved, err := a.authorize(csr, r.permission)
  81. if err != nil {
  82. return err
  83. }
  84. if approved {
  85. appendApprovalCondition(csr, r.successMessage)
  86. _, err = a.client.CertificatesV1beta1().CertificateSigningRequests().UpdateApproval(csr)
  87. if err != nil {
  88. return fmt.Errorf("error updating approval for csr: %v", err)
  89. }
  90. return nil
  91. }
  92. }
  93. if len(tried) != 0 {
  94. return certificates.IgnorableError("recognized csr %q as %v but subject access review was not approved", csr.Name, tried)
  95. }
  96. return nil
  97. }
  98. func (a *sarApprover) authorize(csr *capi.CertificateSigningRequest, rattrs authorization.ResourceAttributes) (bool, error) {
  99. extra := make(map[string]authorization.ExtraValue)
  100. for k, v := range csr.Spec.Extra {
  101. extra[k] = authorization.ExtraValue(v)
  102. }
  103. sar := &authorization.SubjectAccessReview{
  104. Spec: authorization.SubjectAccessReviewSpec{
  105. User: csr.Spec.Username,
  106. UID: csr.Spec.UID,
  107. Groups: csr.Spec.Groups,
  108. Extra: extra,
  109. ResourceAttributes: &rattrs,
  110. },
  111. }
  112. sar, err := a.client.AuthorizationV1beta1().SubjectAccessReviews().Create(sar)
  113. if err != nil {
  114. return false, err
  115. }
  116. return sar.Status.Allowed, nil
  117. }
  118. func appendApprovalCondition(csr *capi.CertificateSigningRequest, message string) {
  119. csr.Status.Conditions = append(csr.Status.Conditions, capi.CertificateSigningRequestCondition{
  120. Type: capi.CertificateApproved,
  121. Reason: "AutoApproved",
  122. Message: message,
  123. })
  124. }
  125. func hasExactUsages(csr *capi.CertificateSigningRequest, usages []capi.KeyUsage) bool {
  126. if len(usages) != len(csr.Spec.Usages) {
  127. return false
  128. }
  129. usageMap := map[capi.KeyUsage]struct{}{}
  130. for _, u := range usages {
  131. usageMap[u] = struct{}{}
  132. }
  133. for _, u := range csr.Spec.Usages {
  134. if _, ok := usageMap[u]; !ok {
  135. return false
  136. }
  137. }
  138. return true
  139. }
  140. var kubeletClientUsages = []capi.KeyUsage{
  141. capi.UsageKeyEncipherment,
  142. capi.UsageDigitalSignature,
  143. capi.UsageClientAuth,
  144. }
  145. func isNodeClientCert(csr *capi.CertificateSigningRequest, x509cr *x509.CertificateRequest) bool {
  146. if !reflect.DeepEqual([]string{"system:nodes"}, x509cr.Subject.Organization) {
  147. return false
  148. }
  149. if (len(x509cr.DNSNames) > 0) || (len(x509cr.EmailAddresses) > 0) || (len(x509cr.IPAddresses) > 0) {
  150. return false
  151. }
  152. if !hasExactUsages(csr, kubeletClientUsages) {
  153. return false
  154. }
  155. if !strings.HasPrefix(x509cr.Subject.CommonName, "system:node:") {
  156. return false
  157. }
  158. return true
  159. }
  160. func isSelfNodeClientCert(csr *capi.CertificateSigningRequest, x509cr *x509.CertificateRequest) bool {
  161. if !isNodeClientCert(csr, x509cr) {
  162. return false
  163. }
  164. if csr.Spec.Username != x509cr.Subject.CommonName {
  165. return false
  166. }
  167. return true
  168. }