ca_provider.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2020 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
  14. import (
  15. "bytes"
  16. "crypto"
  17. "fmt"
  18. "sync/atomic"
  19. "time"
  20. "k8s.io/apiserver/pkg/server/dynamiccertificates"
  21. "k8s.io/client-go/util/cert"
  22. "k8s.io/client-go/util/keyutil"
  23. "k8s.io/kubernetes/pkg/controller/certificates/authority"
  24. )
  25. func newCAProvider(caFile, caKeyFile string) (*caProvider, error) {
  26. caLoader, err := dynamiccertificates.NewDynamicServingContentFromFiles("csr-controller", caFile, caKeyFile)
  27. if err != nil {
  28. return nil, fmt.Errorf("error reading CA cert file %q: %v", caFile, err)
  29. }
  30. ret := &caProvider{
  31. caLoader: caLoader,
  32. }
  33. if err := ret.setCA(); err != nil {
  34. return nil, err
  35. }
  36. return ret, nil
  37. }
  38. type caProvider struct {
  39. caValue atomic.Value
  40. caLoader *dynamiccertificates.DynamicCertKeyPairContent
  41. }
  42. // setCA unconditionally stores the current cert/key content
  43. func (p *caProvider) setCA() error {
  44. certPEM, keyPEM := p.caLoader.CurrentCertKeyContent()
  45. certs, err := cert.ParseCertsPEM(certPEM)
  46. if err != nil {
  47. return fmt.Errorf("error reading CA cert file %q: %v", p.caLoader.Name(), err)
  48. }
  49. if len(certs) != 1 {
  50. return fmt.Errorf("error reading CA cert file %q: expected 1 certificate, found %d", p.caLoader.Name(), len(certs))
  51. }
  52. key, err := keyutil.ParsePrivateKeyPEM(keyPEM)
  53. if err != nil {
  54. return fmt.Errorf("error reading CA key file %q: %v", p.caLoader.Name(), err)
  55. }
  56. priv, ok := key.(crypto.Signer)
  57. if !ok {
  58. return fmt.Errorf("error reading CA key file %q: key did not implement crypto.Signer", p.caLoader.Name())
  59. }
  60. ca := &authority.CertificateAuthority{
  61. RawCert: certPEM,
  62. RawKey: keyPEM,
  63. Certificate: certs[0],
  64. PrivateKey: priv,
  65. Backdate: 5 * time.Minute,
  66. }
  67. p.caValue.Store(ca)
  68. return nil
  69. }
  70. // currentCA provides the curent value of the CA.
  71. // It always check for a stale value. This is cheap because it's all an in memory cache of small slices.
  72. func (p *caProvider) currentCA() (*authority.CertificateAuthority, error) {
  73. certPEM, keyPEM := p.caLoader.CurrentCertKeyContent()
  74. currCA := p.caValue.Load().(*authority.CertificateAuthority)
  75. if bytes.Equal(currCA.RawCert, certPEM) && bytes.Equal(currCA.RawKey, keyPEM) {
  76. return currCA, nil
  77. }
  78. // the bytes weren't equal, so we have to set and then load
  79. if err := p.setCA(); err != nil {
  80. return currCA, err
  81. }
  82. return p.caValue.Load().(*authority.CertificateAuthority), nil
  83. }