cfssl_signer.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 signer implements a CA signer that uses keys stored on local disk.
  14. package signer
  15. import (
  16. "crypto"
  17. "crypto/x509"
  18. "fmt"
  19. "io/ioutil"
  20. "os"
  21. "time"
  22. capi "k8s.io/api/certificates/v1beta1"
  23. certificatesinformers "k8s.io/client-go/informers/certificates/v1beta1"
  24. clientset "k8s.io/client-go/kubernetes"
  25. "k8s.io/kubernetes/pkg/controller/certificates"
  26. "github.com/cloudflare/cfssl/config"
  27. "github.com/cloudflare/cfssl/helpers"
  28. "github.com/cloudflare/cfssl/signer"
  29. "github.com/cloudflare/cfssl/signer/local"
  30. )
  31. func NewCSRSigningController(
  32. client clientset.Interface,
  33. csrInformer certificatesinformers.CertificateSigningRequestInformer,
  34. caFile, caKeyFile string,
  35. certificateDuration time.Duration,
  36. ) (*certificates.CertificateController, error) {
  37. signer, err := newCFSSLSigner(caFile, caKeyFile, client, certificateDuration)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return certificates.NewCertificateController(
  42. client,
  43. csrInformer,
  44. signer.handle,
  45. ), nil
  46. }
  47. type cfsslSigner struct {
  48. ca *x509.Certificate
  49. priv crypto.Signer
  50. sigAlgo x509.SignatureAlgorithm
  51. client clientset.Interface
  52. certificateDuration time.Duration
  53. // nowFn returns the current time. We have here for unit testing
  54. nowFn func() time.Time
  55. }
  56. func newCFSSLSigner(caFile, caKeyFile string, client clientset.Interface, certificateDuration time.Duration) (*cfsslSigner, error) {
  57. ca, err := ioutil.ReadFile(caFile)
  58. if err != nil {
  59. return nil, fmt.Errorf("error reading CA cert file %q: %v", caFile, err)
  60. }
  61. cakey, err := ioutil.ReadFile(caKeyFile)
  62. if err != nil {
  63. return nil, fmt.Errorf("error reading CA key file %q: %v", caKeyFile, err)
  64. }
  65. parsedCa, err := helpers.ParseCertificatePEM(ca)
  66. if err != nil {
  67. return nil, fmt.Errorf("error parsing CA cert file %q: %v", caFile, err)
  68. }
  69. strPassword := os.Getenv("CFSSL_CA_PK_PASSWORD")
  70. password := []byte(strPassword)
  71. if strPassword == "" {
  72. password = nil
  73. }
  74. priv, err := helpers.ParsePrivateKeyPEMWithPassword(cakey, password)
  75. if err != nil {
  76. return nil, fmt.Errorf("Malformed private key %v", err)
  77. }
  78. return &cfsslSigner{
  79. priv: priv,
  80. ca: parsedCa,
  81. sigAlgo: signer.DefaultSigAlgo(priv),
  82. client: client,
  83. certificateDuration: certificateDuration,
  84. nowFn: time.Now,
  85. }, nil
  86. }
  87. func (s *cfsslSigner) handle(csr *capi.CertificateSigningRequest) error {
  88. if !certificates.IsCertificateRequestApproved(csr) {
  89. return nil
  90. }
  91. csr, err := s.sign(csr)
  92. if err != nil {
  93. return fmt.Errorf("error auto signing csr: %v", err)
  94. }
  95. _, err = s.client.CertificatesV1beta1().CertificateSigningRequests().UpdateStatus(csr)
  96. if err != nil {
  97. return fmt.Errorf("error updating signature for csr: %v", err)
  98. }
  99. return nil
  100. }
  101. func (s *cfsslSigner) sign(csr *capi.CertificateSigningRequest) (*capi.CertificateSigningRequest, error) {
  102. var usages []string
  103. for _, usage := range csr.Spec.Usages {
  104. usages = append(usages, string(usage))
  105. }
  106. certExpiryDuration := s.certificateDuration
  107. durationUntilExpiry := s.ca.NotAfter.Sub(s.nowFn())
  108. if durationUntilExpiry <= 0 {
  109. return nil, fmt.Errorf("the signer has expired: %v", s.ca.NotAfter)
  110. }
  111. if durationUntilExpiry < certExpiryDuration {
  112. certExpiryDuration = durationUntilExpiry
  113. }
  114. policy := &config.Signing{
  115. Default: &config.SigningProfile{
  116. Usage: usages,
  117. Expiry: certExpiryDuration,
  118. ExpiryString: certExpiryDuration.String(),
  119. },
  120. }
  121. cfs, err := local.NewSigner(s.priv, s.ca, s.sigAlgo, policy)
  122. if err != nil {
  123. return nil, err
  124. }
  125. csr.Status.Certificate, err = cfs.Sign(signer.SignRequest{
  126. Request: string(csr.Spec.Request),
  127. })
  128. if err != nil {
  129. return nil, err
  130. }
  131. return csr, nil
  132. }