cert_rotation.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 transport
  14. import (
  15. "bytes"
  16. "crypto/tls"
  17. "fmt"
  18. "reflect"
  19. "sync"
  20. "time"
  21. utilnet "k8s.io/apimachinery/pkg/util/net"
  22. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. "k8s.io/client-go/util/connrotation"
  25. "k8s.io/client-go/util/workqueue"
  26. "k8s.io/klog/v2"
  27. )
  28. const workItemKey = "key"
  29. // CertCallbackRefreshDuration is exposed so that integration tests can crank up the reload speed.
  30. var CertCallbackRefreshDuration = 5 * time.Minute
  31. type reloadFunc func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
  32. type dynamicClientCert struct {
  33. clientCert *tls.Certificate
  34. certMtx sync.RWMutex
  35. reload reloadFunc
  36. connDialer *connrotation.Dialer
  37. // queue only ever has one item, but it has nice error handling backoff/retry semantics
  38. queue workqueue.RateLimitingInterface
  39. }
  40. func certRotatingDialer(reload reloadFunc, dial utilnet.DialFunc) *dynamicClientCert {
  41. d := &dynamicClientCert{
  42. reload: reload,
  43. connDialer: connrotation.NewDialer(connrotation.DialFunc(dial)),
  44. queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "DynamicClientCertificate"),
  45. }
  46. return d
  47. }
  48. // loadClientCert calls the callback and rotates connections if needed
  49. func (c *dynamicClientCert) loadClientCert() (*tls.Certificate, error) {
  50. cert, err := c.reload(nil)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // check to see if we have a change. If the values are the same, do nothing.
  55. c.certMtx.RLock()
  56. haveCert := c.clientCert != nil
  57. if certsEqual(c.clientCert, cert) {
  58. c.certMtx.RUnlock()
  59. return c.clientCert, nil
  60. }
  61. c.certMtx.RUnlock()
  62. c.certMtx.Lock()
  63. c.clientCert = cert
  64. c.certMtx.Unlock()
  65. // The first certificate requested is not a rotation that is worth closing connections for
  66. if !haveCert {
  67. return cert, nil
  68. }
  69. klog.V(1).Infof("certificate rotation detected, shutting down client connections to start using new credentials")
  70. c.connDialer.CloseAll()
  71. return cert, nil
  72. }
  73. // certsEqual compares tls Certificates, ignoring the Leaf which may get filled in dynamically
  74. func certsEqual(left, right *tls.Certificate) bool {
  75. if left == nil || right == nil {
  76. return left == right
  77. }
  78. if !byteMatrixEqual(left.Certificate, right.Certificate) {
  79. return false
  80. }
  81. if !reflect.DeepEqual(left.PrivateKey, right.PrivateKey) {
  82. return false
  83. }
  84. if !byteMatrixEqual(left.SignedCertificateTimestamps, right.SignedCertificateTimestamps) {
  85. return false
  86. }
  87. if !bytes.Equal(left.OCSPStaple, right.OCSPStaple) {
  88. return false
  89. }
  90. return true
  91. }
  92. func byteMatrixEqual(left, right [][]byte) bool {
  93. if len(left) != len(right) {
  94. return false
  95. }
  96. for i := range left {
  97. if !bytes.Equal(left[i], right[i]) {
  98. return false
  99. }
  100. }
  101. return true
  102. }
  103. // run starts the controller and blocks until stopCh is closed.
  104. func (c *dynamicClientCert) Run(stopCh <-chan struct{}) {
  105. defer utilruntime.HandleCrash()
  106. defer c.queue.ShutDown()
  107. klog.V(3).Infof("Starting client certificate rotation controller")
  108. defer klog.V(3).Infof("Shutting down client certificate rotation controller")
  109. go wait.Until(c.runWorker, time.Second, stopCh)
  110. go wait.PollImmediateUntil(CertCallbackRefreshDuration, func() (bool, error) {
  111. c.queue.Add(workItemKey)
  112. return false, nil
  113. }, stopCh)
  114. <-stopCh
  115. }
  116. func (c *dynamicClientCert) runWorker() {
  117. for c.processNextWorkItem() {
  118. }
  119. }
  120. func (c *dynamicClientCert) processNextWorkItem() bool {
  121. dsKey, quit := c.queue.Get()
  122. if quit {
  123. return false
  124. }
  125. defer c.queue.Done(dsKey)
  126. _, err := c.loadClientCert()
  127. if err == nil {
  128. c.queue.Forget(dsKey)
  129. return true
  130. }
  131. utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err))
  132. c.queue.AddRateLimited(dsKey)
  133. return true
  134. }
  135. func (c *dynamicClientCert) GetClientCertificate(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
  136. return c.loadClientCert()
  137. }