| 1 |
/* |
| 2 |
Copyright 2020 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package transport |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"crypto/tls" |
| 22 |
"reflect" |
| 23 |
"sync" |
| 24 |
"time" |
| 25 |
|
| 26 |
utilnet "k8s.io/apimachinery/pkg/util/net" |
| 27 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 28 |
"k8s.io/apimachinery/pkg/util/wait" |
| 29 |
"k8s.io/client-go/util/connrotation" |
| 30 |
"k8s.io/client-go/util/workqueue" |
| 31 |
"k8s.io/klog/v2" |
| 32 |
) |
| 33 |
|
| 34 |
const workItemKey = "key" |
| 35 |
|
| 36 |
// CertCallbackRefreshDuration is exposed so that integration tests can crank up the reload speed. |
| 37 |
var CertCallbackRefreshDuration = 5 * time.Minute |
| 38 |
|
| 39 |
type reloadFunc func(*tls.CertificateRequestInfo) (*tls.Certificate, error) |
| 40 |
|
| 41 |
type dynamicClientCert struct { |
| 42 |
logger klog.Logger |
| 43 |
clientCert *tls.Certificate |
| 44 |
certMtx sync.RWMutex |
| 45 |
|
| 46 |
reload reloadFunc |
| 47 |
connDialer *connrotation.Dialer |
| 48 |
|
| 49 |
// queue only ever has one item, but it has nice error handling backoff/retry semantics |
| 50 |
queue workqueue.TypedRateLimitingInterface[string] |
| 51 |
} |
| 52 |
|
| 53 |
func certRotatingDialer(logger klog.Logger, reload reloadFunc, dial utilnet.DialFunc) *dynamicClientCert { |
| 54 |
d := &dynamicClientCert{ |
| 55 |
logger: logger, |
| 56 |
reload: reload, |
| 57 |
connDialer: connrotation.NewDialer(connrotation.DialFunc(dial)), |
| 58 |
queue: workqueue.NewTypedRateLimitingQueueWithConfig( |
| 59 |
workqueue.DefaultTypedControllerRateLimiter[string](), |
| 60 |
workqueue.TypedRateLimitingQueueConfig[string]{Name: "DynamicClientCertificate"}, |
| 61 |
), |
| 62 |
} |
| 63 |
|
| 64 |
return d |
| 65 |
} |
| 66 |
|
| 67 |
// loadClientCert calls the callback and rotates connections if needed |
| 68 |
func (c *dynamicClientCert) loadClientCert() (*tls.Certificate, error) { |
| 69 |
cert, err := c.reload(nil) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
|
| 74 |
// check to see if we have a change. If the values are the same, do nothing. |
| 75 |
c.certMtx.RLock() |
| 76 |
haveCert := c.clientCert != nil |
| 77 |
if certsEqual(c.clientCert, cert) { |
| 78 |
c.certMtx.RUnlock() |
| 79 |
return c.clientCert, nil |
| 80 |
} |
| 81 |
c.certMtx.RUnlock() |
| 82 |
|
| 83 |
c.certMtx.Lock() |
| 84 |
c.clientCert = cert |
| 85 |
c.certMtx.Unlock() |
| 86 |
|
| 87 |
// The first certificate requested is not a rotation that is worth closing connections for |
| 88 |
if !haveCert { |
| 89 |
return cert, nil |
| 90 |
} |
| 91 |
|
| 92 |
c.logger.V(1).Info("Certificate rotation detected, shutting down client connections to start using new credentials") |
| 93 |
c.connDialer.CloseAll() |
| 94 |
|
| 95 |
return cert, nil |
| 96 |
} |
| 97 |
|
| 98 |
// certsEqual compares tls Certificates, ignoring the Leaf which may get filled in dynamically |
| 99 |
func certsEqual(left, right *tls.Certificate) bool { |
| 100 |
if left == nil || right == nil { |
| 101 |
return left == right |
| 102 |
} |
| 103 |
|
| 104 |
if !byteMatrixEqual(left.Certificate, right.Certificate) { |
| 105 |
return false |
| 106 |
} |
| 107 |
|
| 108 |
if !reflect.DeepEqual(left.PrivateKey, right.PrivateKey) { |
| 109 |
return false |
| 110 |
} |
| 111 |
|
| 112 |
if !byteMatrixEqual(left.SignedCertificateTimestamps, right.SignedCertificateTimestamps) { |
| 113 |
return false |
| 114 |
} |
| 115 |
|
| 116 |
if !bytes.Equal(left.OCSPStaple, right.OCSPStaple) { |
| 117 |
return false |
| 118 |
} |
| 119 |
|
| 120 |
return true |
| 121 |
} |
| 122 |
|
| 123 |
func byteMatrixEqual(left, right [][]byte) bool { |
| 124 |
if len(left) != len(right) { |
| 125 |
return false |
| 126 |
} |
| 127 |
|
| 128 |
for i := range left { |
| 129 |
if !bytes.Equal(left[i], right[i]) { |
| 130 |
return false |
| 131 |
} |
| 132 |
} |
| 133 |
return true |
| 134 |
} |
| 135 |
|
| 136 |
// run starts the controller and blocks until stopCh is closed. |
| 137 |
func (c *dynamicClientCert) run(stopCh <-chan struct{}) { |
| 138 |
defer utilruntime.HandleCrashWithLogger(c.logger) |
| 139 |
defer c.queue.ShutDown() |
| 140 |
|
| 141 |
c.logger.V(3).Info("Starting client certificate rotation controller") |
| 142 |
defer c.logger.V(3).Info("Shutting down client certificate rotation controller") |
| 143 |
|
| 144 |
go wait.Until(c.runWorker, time.Second, stopCh) |
| 145 |
|
| 146 |
go wait.PollImmediateUntil(CertCallbackRefreshDuration, func() (bool, error) { |
| 147 |
c.queue.Add(workItemKey) |
| 148 |
return false, nil |
| 149 |
}, stopCh) |
| 150 |
|
| 151 |
<-stopCh |
| 152 |
} |
| 153 |
|
| 154 |
func (c *dynamicClientCert) runWorker() { |
| 155 |
for c.processNextWorkItem() { |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func (c *dynamicClientCert) processNextWorkItem() bool { |
| 160 |
dsKey, quit := c.queue.Get() |
| 161 |
if quit { |
| 162 |
return false |
| 163 |
} |
| 164 |
defer c.queue.Done(dsKey) |
| 165 |
|
| 166 |
_, err := c.loadClientCert() |
| 167 |
if err == nil { |
| 168 |
c.queue.Forget(dsKey) |
| 169 |
return true |
| 170 |
} |
| 171 |
|
| 172 |
utilruntime.HandleErrorWithLogger(c.logger, err, "Loading client cert failed", "key", dsKey) |
| 173 |
c.queue.AddRateLimited(dsKey) |
| 174 |
|
| 175 |
return true |
| 176 |
} |
| 177 |
|
| 178 |
func (c *dynamicClientCert) GetClientCertificate(*tls.CertificateRequestInfo) (*tls.Certificate, error) { |
| 179 |
return c.loadClientCert() |
| 180 |
} |