signer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2019 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 signer implements a CA signer that uses keys stored on local disk.
  14. package signer
  15. import (
  16. "context"
  17. "encoding/pem"
  18. "fmt"
  19. "time"
  20. capi "k8s.io/api/certificates/v1beta1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apiserver/pkg/server/dynamiccertificates"
  23. certificatesinformers "k8s.io/client-go/informers/certificates/v1beta1"
  24. clientset "k8s.io/client-go/kubernetes"
  25. capihelper "k8s.io/kubernetes/pkg/apis/certificates/v1beta1"
  26. "k8s.io/kubernetes/pkg/controller/certificates"
  27. "k8s.io/kubernetes/pkg/controller/certificates/authority"
  28. )
  29. type CSRSigningController struct {
  30. certificateController *certificates.CertificateController
  31. dynamicCertReloader dynamiccertificates.ControllerRunner
  32. }
  33. func NewCSRSigningController(
  34. client clientset.Interface,
  35. csrInformer certificatesinformers.CertificateSigningRequestInformer,
  36. caFile, caKeyFile string,
  37. certTTL time.Duration,
  38. ) (*CSRSigningController, error) {
  39. signer, err := newSigner(caFile, caKeyFile, client, certTTL)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &CSRSigningController{
  44. certificateController: certificates.NewCertificateController(
  45. "csrsigning",
  46. client,
  47. csrInformer,
  48. signer.handle,
  49. ),
  50. dynamicCertReloader: signer.caProvider.caLoader,
  51. }, nil
  52. }
  53. // Run the main goroutine responsible for watching and syncing jobs.
  54. func (c *CSRSigningController) Run(workers int, stopCh <-chan struct{}) {
  55. go c.dynamicCertReloader.Run(workers, stopCh)
  56. c.certificateController.Run(workers, stopCh)
  57. }
  58. type signer struct {
  59. caProvider *caProvider
  60. client clientset.Interface
  61. certTTL time.Duration
  62. }
  63. func newSigner(caFile, caKeyFile string, client clientset.Interface, certificateDuration time.Duration) (*signer, error) {
  64. caProvider, err := newCAProvider(caFile, caKeyFile)
  65. if err != nil {
  66. return nil, err
  67. }
  68. ret := &signer{
  69. caProvider: caProvider,
  70. client: client,
  71. certTTL: certificateDuration,
  72. }
  73. return ret, nil
  74. }
  75. func (s *signer) handle(csr *capi.CertificateSigningRequest) error {
  76. if !certificates.IsCertificateRequestApproved(csr) {
  77. return nil
  78. }
  79. csr, err := s.sign(csr)
  80. if err != nil {
  81. return fmt.Errorf("error auto signing csr: %v", err)
  82. }
  83. _, err = s.client.CertificatesV1beta1().CertificateSigningRequests().UpdateStatus(context.TODO(), csr, metav1.UpdateOptions{})
  84. if err != nil {
  85. return fmt.Errorf("error updating signature for csr: %v", err)
  86. }
  87. return nil
  88. }
  89. func (s *signer) sign(csr *capi.CertificateSigningRequest) (*capi.CertificateSigningRequest, error) {
  90. x509cr, err := capihelper.ParseCSR(csr)
  91. if err != nil {
  92. return nil, fmt.Errorf("unable to parse csr %q: %v", csr.Name, err)
  93. }
  94. currCA, err := s.caProvider.currentCA()
  95. if err != nil {
  96. return nil, err
  97. }
  98. der, err := currCA.Sign(x509cr.Raw, authority.PermissiveSigningPolicy{
  99. TTL: s.certTTL,
  100. Usages: csr.Spec.Usages,
  101. })
  102. if err != nil {
  103. return nil, err
  104. }
  105. csr.Status.Certificate = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
  106. return csr, nil
  107. }