| 1 |
/* |
| 2 |
Copyright 2019 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 dynamiccertificates |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"crypto/tls" |
| 22 |
"fmt" |
| 23 |
"os" |
| 24 |
"sync/atomic" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/fsnotify/fsnotify" |
| 28 |
|
| 29 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 30 |
"k8s.io/apimachinery/pkg/util/wait" |
| 31 |
"k8s.io/client-go/util/workqueue" |
| 32 |
"k8s.io/klog/v2" |
| 33 |
) |
| 34 |
|
| 35 |
// DynamicCertKeyPairContent provides a CertKeyContentProvider that can dynamically react to new file content |
| 36 |
type DynamicCertKeyPairContent struct { |
| 37 |
name string |
| 38 |
|
| 39 |
// certFile is the name of the certificate file to read. |
| 40 |
certFile string |
| 41 |
// keyFile is the name of the key file to read. |
| 42 |
keyFile string |
| 43 |
|
| 44 |
// certKeyPair is a certKeyContent that contains the last read, non-zero length content of the key and cert |
| 45 |
certKeyPair atomic.Value |
| 46 |
|
| 47 |
listeners []Listener |
| 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 |
var _ CertKeyContentProvider = &DynamicCertKeyPairContent{} |
| 54 |
var _ ControllerRunner = &DynamicCertKeyPairContent{} |
| 55 |
|
| 56 |
// NewDynamicServingContentFromFiles returns a dynamic CertKeyContentProvider based on a cert and key filename |
| 57 |
func NewDynamicServingContentFromFiles(purpose, certFile, keyFile string) (*DynamicCertKeyPairContent, error) { |
| 58 |
if len(certFile) == 0 || len(keyFile) == 0 { |
| 59 |
return nil, fmt.Errorf("missing filename for serving cert") |
| 60 |
} |
| 61 |
name := fmt.Sprintf("%s::%s::%s", purpose, certFile, keyFile) |
| 62 |
|
| 63 |
ret := &DynamicCertKeyPairContent{ |
| 64 |
name: name, |
| 65 |
certFile: certFile, |
| 66 |
keyFile: keyFile, |
| 67 |
queue: workqueue.NewTypedRateLimitingQueueWithConfig( |
| 68 |
workqueue.DefaultTypedControllerRateLimiter[string](), |
| 69 |
workqueue.TypedRateLimitingQueueConfig[string]{Name: fmt.Sprintf("DynamicCABundle-%s", purpose)}, |
| 70 |
), |
| 71 |
} |
| 72 |
if err := ret.loadCertKeyPair(); err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
|
| 76 |
return ret, nil |
| 77 |
} |
| 78 |
|
| 79 |
// AddListener adds a listener to be notified when the serving cert content changes. |
| 80 |
func (c *DynamicCertKeyPairContent) AddListener(listener Listener) { |
| 81 |
c.listeners = append(c.listeners, listener) |
| 82 |
} |
| 83 |
|
| 84 |
// loadCertKeyPair determines the next set of content for the file. |
| 85 |
func (c *DynamicCertKeyPairContent) loadCertKeyPair() error { |
| 86 |
cert, err := os.ReadFile(c.certFile) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
key, err := os.ReadFile(c.keyFile) |
| 91 |
if err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
if len(cert) == 0 || len(key) == 0 { |
| 95 |
return fmt.Errorf("missing content for serving cert %q", c.Name()) |
| 96 |
} |
| 97 |
|
| 98 |
// Ensure that the key matches the cert and both are valid |
| 99 |
_, err = tls.X509KeyPair(cert, key) |
| 100 |
if err != nil { |
| 101 |
return err |
| 102 |
} |
| 103 |
|
| 104 |
newCertKey := &certKeyContent{ |
| 105 |
cert: cert, |
| 106 |
key: key, |
| 107 |
} |
| 108 |
|
| 109 |
// check to see if we have a change. If the values are the same, do nothing. |
| 110 |
existing, ok := c.certKeyPair.Load().(*certKeyContent) |
| 111 |
if ok && existing != nil && existing.Equal(newCertKey) { |
| 112 |
return nil |
| 113 |
} |
| 114 |
|
| 115 |
c.certKeyPair.Store(newCertKey) |
| 116 |
klog.V(2).InfoS("Loaded a new cert/key pair", "name", c.Name()) |
| 117 |
|
| 118 |
for _, listener := range c.listeners { |
| 119 |
listener.Enqueue() |
| 120 |
} |
| 121 |
|
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
// RunOnce runs a single sync loop |
| 126 |
func (c *DynamicCertKeyPairContent) RunOnce(ctx context.Context) error { |
| 127 |
return c.loadCertKeyPair() |
| 128 |
} |
| 129 |
|
| 130 |
// Run starts the controller and blocks until context is killed. |
| 131 |
func (c *DynamicCertKeyPairContent) Run(ctx context.Context, workers int) { |
| 132 |
defer utilruntime.HandleCrash() |
| 133 |
defer c.queue.ShutDown() |
| 134 |
|
| 135 |
klog.InfoS("Starting controller", "name", c.name) |
| 136 |
defer klog.InfoS("Shutting down controller", "name", c.name) |
| 137 |
|
| 138 |
// doesn't matter what workers say, only start one. |
| 139 |
go wait.Until(c.runWorker, time.Second, ctx.Done()) |
| 140 |
|
| 141 |
// start the loop that watches the cert and key files until stopCh is closed. |
| 142 |
go wait.Until(func() { |
| 143 |
if err := c.watchCertKeyFile(ctx.Done()); err != nil { |
| 144 |
klog.ErrorS(err, "Failed to watch cert and key file, will retry later") |
| 145 |
} |
| 146 |
}, time.Minute, ctx.Done()) |
| 147 |
|
| 148 |
<-ctx.Done() |
| 149 |
} |
| 150 |
|
| 151 |
func (c *DynamicCertKeyPairContent) watchCertKeyFile(stopCh <-chan struct{}) error { |
| 152 |
// Trigger a check here to ensure the content will be checked periodically even if the following watch fails. |
| 153 |
c.queue.Add(workItemKey) |
| 154 |
|
| 155 |
w, err := fsnotify.NewWatcher() |
| 156 |
if err != nil { |
| 157 |
return fmt.Errorf("error creating fsnotify watcher: %v", err) |
| 158 |
} |
| 159 |
defer w.Close() |
| 160 |
|
| 161 |
if err := w.Add(c.certFile); err != nil { |
| 162 |
return fmt.Errorf("error adding watch for file %s: %v", c.certFile, err) |
| 163 |
} |
| 164 |
if err := w.Add(c.keyFile); err != nil { |
| 165 |
return fmt.Errorf("error adding watch for file %s: %v", c.keyFile, err) |
| 166 |
} |
| 167 |
// Trigger a check in case the file is updated before the watch starts. |
| 168 |
c.queue.Add(workItemKey) |
| 169 |
|
| 170 |
for { |
| 171 |
select { |
| 172 |
case e := <-w.Events: |
| 173 |
if err := c.handleWatchEvent(e, w); err != nil { |
| 174 |
return err |
| 175 |
} |
| 176 |
case err := <-w.Errors: |
| 177 |
return fmt.Errorf("received fsnotify error: %v", err) |
| 178 |
case <-stopCh: |
| 179 |
return nil |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// handleWatchEvent triggers reloading the cert and key file, and restarts a new watch if it's a Remove or Rename event. |
| 185 |
// If one file is updated before the other, the loadCertKeyPair method will catch the mismatch and will not apply the |
| 186 |
// change. When an event of the other file is received, it will trigger reloading the files again and the new content |
| 187 |
// will be loaded and used. |
| 188 |
func (c *DynamicCertKeyPairContent) handleWatchEvent(e fsnotify.Event, w *fsnotify.Watcher) error { |
| 189 |
// This should be executed after restarting the watch (if applicable) to ensure no file event will be missing. |
| 190 |
defer c.queue.Add(workItemKey) |
| 191 |
if !e.Has(fsnotify.Remove) && !e.Has(fsnotify.Rename) { |
| 192 |
return nil |
| 193 |
} |
| 194 |
if err := w.Remove(e.Name); err != nil { |
| 195 |
klog.InfoS("Failed to remove file watch, it may have been deleted", "file", e.Name, "err", err) |
| 196 |
} |
| 197 |
if err := w.Add(e.Name); err != nil { |
| 198 |
return fmt.Errorf("error adding watch for file %s: %v", e.Name, err) |
| 199 |
} |
| 200 |
return nil |
| 201 |
} |
| 202 |
|
| 203 |
func (c *DynamicCertKeyPairContent) runWorker() { |
| 204 |
for c.processNextWorkItem() { |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func (c *DynamicCertKeyPairContent) processNextWorkItem() bool { |
| 209 |
dsKey, quit := c.queue.Get() |
| 210 |
if quit { |
| 211 |
return false |
| 212 |
} |
| 213 |
defer c.queue.Done(dsKey) |
| 214 |
|
| 215 |
err := c.loadCertKeyPair() |
| 216 |
if err == nil { |
| 217 |
c.queue.Forget(dsKey) |
| 218 |
return true |
| 219 |
} |
| 220 |
|
| 221 |
utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err)) |
| 222 |
c.queue.AddRateLimited(dsKey) |
| 223 |
|
| 224 |
return true |
| 225 |
} |
| 226 |
|
| 227 |
// Name is just an identifier |
| 228 |
func (c *DynamicCertKeyPairContent) Name() string { |
| 229 |
return c.name |
| 230 |
} |
| 231 |
|
| 232 |
// CurrentCertKeyContent provides cert and key byte content |
| 233 |
func (c *DynamicCertKeyPairContent) CurrentCertKeyContent() ([]byte, []byte) { |
| 234 |
certKeyContent := c.certKeyPair.Load().(*certKeyContent) |
| 235 |
return certKeyContent.cert, certKeyContent.key |
| 236 |
} |