Debian Codegraph @

pkg/server/dynamiccertificates/dynamic_cafile_content.go

raw
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 "bytes"
21 "context"
22 "crypto/x509"
23 "errors"
24 "fmt"
25 "os"
26 "sync/atomic"
27 "time"
28
29 "github.com/fsnotify/fsnotify"
30 "k8s.io/client-go/util/cert"
31
32 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
33 "k8s.io/apimachinery/pkg/util/wait"
34 "k8s.io/client-go/util/workqueue"
35 "k8s.io/klog/v2"
36 )
37
38 // FileRefreshDuration is exposed so that integration tests can crank up the reload speed.
39 var FileRefreshDuration = 1 * time.Minute
40
41 // ControllerRunner is a generic interface for starting a controller
42 type ControllerRunner interface {
43 // RunOnce runs the sync loop a single time. This useful for synchronous priming
44 RunOnce(ctx context.Context) error
45
46 // Run should be called a go .Run
47 Run(ctx context.Context, workers int)
48 }
49
50 // DynamicFileCAContent provides a CAContentProvider that can dynamically react to new file content
51 // It also fulfills the authenticator interface to provide verifyoptions
52 type DynamicFileCAContent struct {
53 name string
54
55 // filename is the name the file to read.
56 filename string
57
58 // caBundle is a caBundleAndVerifier that contains the last read, non-zero length content of the file
59 caBundle atomic.Value
60
61 listeners []Listener
62
63 // queue only ever has one item, but it has nice error handling backoff/retry semantics
64 queue workqueue.TypedRateLimitingInterface[string]
65 }
66
67 var _ Notifier = &DynamicFileCAContent{}
68 var _ CAContentProvider = &DynamicFileCAContent{}
69 var _ ControllerRunner = &DynamicFileCAContent{}
70
71 type caBundleAndVerifier struct {
72 caBundle []byte
73 verifyOptions x509.VerifyOptions
74 }
75
76 // NewDynamicCAContentFromFile returns a CAContentProvider based on a filename that automatically reloads content
77 func NewDynamicCAContentFromFile(purpose, filename string) (*DynamicFileCAContent, error) {
78 if len(filename) == 0 {
79 return nil, fmt.Errorf("missing filename for ca bundle")
80 }
81 name := fmt.Sprintf("%s::%s", purpose, filename)
82
83 ret := &DynamicFileCAContent{
84 name: name,
85 filename: filename,
86 queue: workqueue.NewTypedRateLimitingQueueWithConfig(
87 workqueue.DefaultTypedControllerRateLimiter[string](),
88 workqueue.TypedRateLimitingQueueConfig[string]{Name: fmt.Sprintf("DynamicCABundle-%s", purpose)},
89 ),
90 }
91 if err := ret.loadCABundle(); err != nil {
92 return nil, err
93 }
94
95 return ret, nil
96 }
97
98 // AddListener adds a listener to be notified when the CA content changes.
99 func (c *DynamicFileCAContent) AddListener(listener Listener) {
100 c.listeners = append(c.listeners, listener)
101 }
102
103 // loadCABundle determines the next set of content for the file.
104 func (c *DynamicFileCAContent) loadCABundle() error {
105 caBundle, err := os.ReadFile(c.filename)
106 if err != nil {
107 return err
108 }
109 if len(caBundle) == 0 {
110 return fmt.Errorf("missing content for CA bundle %q", c.Name())
111 }
112
113 // check to see if we have a change. If the values are the same, do nothing.
114 if !c.hasCAChanged(caBundle) {
115 return nil
116 }
117
118 caBundleAndVerifier, err := newCABundleAndVerifier(c.Name(), caBundle)
119 if err != nil {
120 return err
121 }
122 c.caBundle.Store(caBundleAndVerifier)
123 klog.V(2).InfoS("Loaded a new CA Bundle and Verifier", "name", c.Name())
124
125 for _, listener := range c.listeners {
126 listener.Enqueue()
127 }
128
129 return nil
130 }
131
132 // hasCAChanged returns true if the caBundle is different than the current.
133 func (c *DynamicFileCAContent) hasCAChanged(caBundle []byte) bool {
134 uncastExisting := c.caBundle.Load()
135 if uncastExisting == nil {
136 return true
137 }
138
139 // check to see if we have a change. If the values are the same, do nothing.
140 existing, ok := uncastExisting.(*caBundleAndVerifier)
141 if !ok {
142 return true
143 }
144 if !bytes.Equal(existing.caBundle, caBundle) {
145 return true
146 }
147
148 return false
149 }
150
151 // RunOnce runs a single sync loop
152 func (c *DynamicFileCAContent) RunOnce(ctx context.Context) error {
153 return c.loadCABundle()
154 }
155
156 // Run starts the controller and blocks until stopCh is closed.
157 func (c *DynamicFileCAContent) Run(ctx context.Context, workers int) {
158 defer utilruntime.HandleCrash()
159 defer c.queue.ShutDown()
160
161 klog.InfoS("Starting controller", "name", c.name)
162 defer klog.InfoS("Shutting down controller", "name", c.name)
163
164 // doesn't matter what workers say, only start one.
165 go wait.Until(c.runWorker, time.Second, ctx.Done())
166
167 // start the loop that watches the CA file until stopCh is closed.
168 go wait.Until(func() {
169 if err := c.watchCAFile(ctx.Done()); err != nil {
170 klog.ErrorS(err, "Failed to watch CA file, will retry later")
171 }
172 }, time.Minute, ctx.Done())
173
174 <-ctx.Done()
175 }
176
177 func (c *DynamicFileCAContent) watchCAFile(stopCh <-chan struct{}) error {
178 // Trigger a check here to ensure the content will be checked periodically even if the following watch fails.
179 c.queue.Add(workItemKey)
180
181 w, err := fsnotify.NewWatcher()
182 if err != nil {
183 return fmt.Errorf("error creating fsnotify watcher: %v", err)
184 }
185 defer w.Close()
186
187 if err = w.Add(c.filename); err != nil {
188 return fmt.Errorf("error adding watch for file %s: %v", c.filename, err)
189 }
190 // Trigger a check in case the file is updated before the watch starts.
191 c.queue.Add(workItemKey)
192
193 for {
194 select {
195 case e := <-w.Events:
196 if err := c.handleWatchEvent(e, w); err != nil {
197 return err
198 }
199 case err := <-w.Errors:
200 return fmt.Errorf("received fsnotify error: %v", err)
201 case <-stopCh:
202 return nil
203 }
204 }
205 }
206
207 // handleWatchEvent triggers reloading the CA file, and restarts a new watch if it's a Remove or Rename event.
208 func (c *DynamicFileCAContent) handleWatchEvent(e fsnotify.Event, w *fsnotify.Watcher) error {
209 // This should be executed after restarting the watch (if applicable) to ensure no file event will be missing.
210 defer c.queue.Add(workItemKey)
211 if !e.Has(fsnotify.Remove) && !e.Has(fsnotify.Rename) {
212 return nil
213 }
214 if err := w.Remove(c.filename); err != nil && !errors.Is(err, fsnotify.ErrNonExistentWatch) {
215 klog.InfoS("Failed to remove file watch, it may have been deleted", "file", c.filename, "err", err)
216 }
217 if err := w.Add(c.filename); err != nil {
218 return fmt.Errorf("error adding watch for file %s: %v", c.filename, err)
219 }
220 return nil
221 }
222
223 func (c *DynamicFileCAContent) runWorker() {
224 for c.processNextWorkItem() {
225 }
226 }
227
228 func (c *DynamicFileCAContent) processNextWorkItem() bool {
229 dsKey, quit := c.queue.Get()
230 if quit {
231 return false
232 }
233 defer c.queue.Done(dsKey)
234
235 err := c.loadCABundle()
236 if err == nil {
237 c.queue.Forget(dsKey)
238 return true
239 }
240
241 utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err))
242 c.queue.AddRateLimited(dsKey)
243
244 return true
245 }
246
247 // Name is just an identifier
248 func (c *DynamicFileCAContent) Name() string {
249 return c.name
250 }
251
252 // CurrentCABundleContent provides ca bundle byte content
253 func (c *DynamicFileCAContent) CurrentCABundleContent() (cabundle []byte) {
254 return c.caBundle.Load().(*caBundleAndVerifier).caBundle
255 }
256
257 // VerifyOptions provides verifyoptions compatible with authenticators
258 func (c *DynamicFileCAContent) VerifyOptions() (x509.VerifyOptions, bool) {
259 uncastObj := c.caBundle.Load()
260 if uncastObj == nil {
261 return x509.VerifyOptions{}, false
262 }
263
264 return uncastObj.(*caBundleAndVerifier).verifyOptions, true
265 }
266
267 // newVerifyOptions creates a new verification func from a file. It reads the content and then fails.
268 // It will return a nil function if you pass an empty CA file.
269 func newCABundleAndVerifier(name string, caBundle []byte) (*caBundleAndVerifier, error) {
270 if len(caBundle) == 0 {
271 return nil, fmt.Errorf("missing content for CA bundle %q", name)
272 }
273
274 // Wrap with an x509 verifier
275 var err error
276 verifyOptions := defaultVerifyOptions()
277 verifyOptions.Roots, err = cert.NewPoolFromBytes(caBundle)
278 if err != nil {
279 return nil, fmt.Errorf("error loading CA bundle for %q: %v", name, err)
280 }
281
282 return &caBundleAndVerifier{
283 caBundle: caBundle,
284 verifyOptions: verifyOptions,
285 }, nil
286 }
287
288 // defaultVerifyOptions returns VerifyOptions that use the system root certificates, current time,
289 // and requires certificates to be valid for client auth (x509.ExtKeyUsageClientAuth)
290 func defaultVerifyOptions() x509.VerifyOptions {
291 return x509.VerifyOptions{
292 KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
293 }
294 }