|
| 1 | +/* |
| 2 | +Copyright 2021 The cert-manager 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 rootca |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "crypto/x509" |
| 23 | + "fmt" |
| 24 | + "os" |
| 25 | + |
| 26 | + "github.com/fsnotify/fsnotify" |
| 27 | + "github.com/go-logr/logr" |
| 28 | + "github.com/jetstack/cert-manager/pkg/util/pki" |
| 29 | +) |
| 30 | + |
| 31 | +// RootCAs is a Root CAs bundle that contains raw PEM encoded CAs, as well as |
| 32 | +// their x509.CertPool encoding. |
| 33 | +type RootCAs struct { |
| 34 | + // PEM is the raw PEM encoding of the CA certificates. |
| 35 | + PEM []byte |
| 36 | + |
| 37 | + // CertPool is the x509.CertPool encoding of the CA certificates. |
| 38 | + CertPool *x509.CertPool |
| 39 | +} |
| 40 | + |
| 41 | +// watcher is used for loading and watching a file that contains a root CAs |
| 42 | +// bundle. |
| 43 | +type watcher struct { |
| 44 | + log logr.Logger |
| 45 | + |
| 46 | + filepath string |
| 47 | + rootCAsPEM []byte |
| 48 | + broadcastChan chan RootCAs |
| 49 | +} |
| 50 | + |
| 51 | +// Watch watches the given filepath for changes, and writes to the returned |
| 52 | +// channel the new state when it changes. The first event is the initial state |
| 53 | +// of the root CAs file. |
| 54 | +func Watch(ctx context.Context, log logr.Logger, filepath string) (<-chan RootCAs, error) { |
| 55 | + w := &watcher{ |
| 56 | + log: log.WithName("root-ca-watcher").WithValues("file", filepath), |
| 57 | + filepath: filepath, |
| 58 | + broadcastChan: make(chan RootCAs), |
| 59 | + } |
| 60 | + |
| 61 | + w.log.Info("loading root CAs bundle") |
| 62 | + rootCAs, err := w.loadRootCAsFile() |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("failed to load root CA bundle: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + watcher, err := fsnotify.NewWatcher() |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed to create file watch: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + if err := watcher.Add(w.filepath); err != nil { |
| 73 | + return nil, fmt.Errorf("failed to add root CAs file for watching %q: %w", w.filepath, err) |
| 74 | + } |
| 75 | + |
| 76 | + go func() { |
| 77 | + defer watcher.Close() |
| 78 | + |
| 79 | + // Send initial root CAs state |
| 80 | + w.broadcastChan <- *rootCAs |
| 81 | + w.rootCAsPEM = rootCAs.PEM |
| 82 | + |
| 83 | + for { |
| 84 | + select { |
| 85 | + case <-ctx.Done(): |
| 86 | + w.log.Info("closing root CAs file watcher") |
| 87 | + return |
| 88 | + |
| 89 | + case event := <-watcher.Events: |
| 90 | + w.log.V(3).Info("received event from file watcher", "event", event.Op.String()) |
| 91 | + |
| 92 | + // Watch for remove events, since this is actually the syslink being |
| 93 | + // changed in the volume mount. |
| 94 | + if event.Op == fsnotify.Remove { |
| 95 | + watcher.Remove(event.Name) |
| 96 | + if err := watcher.Add(w.filepath); err != nil { |
| 97 | + w.log.Error(err, "failed to add new file watch") |
| 98 | + } |
| 99 | + w.reloadConfig() |
| 100 | + } |
| 101 | + |
| 102 | + // Also allow normal files to be modified and reloaded. |
| 103 | + if event.Op&fsnotify.Write == fsnotify.Write { |
| 104 | + w.reloadConfig() |
| 105 | + } |
| 106 | + |
| 107 | + case err := <-watcher.Errors: |
| 108 | + w.log.Error(err, "errors watching root CAs file") |
| 109 | + } |
| 110 | + } |
| 111 | + }() |
| 112 | + |
| 113 | + return w.broadcastChan, nil |
| 114 | +} |
| 115 | + |
| 116 | +// reloadConfig will load root CAs file, and if changed from the current state, |
| 117 | +// will broadcast the update. |
| 118 | +func (w *watcher) reloadConfig() { |
| 119 | + rootCAs, err := w.loadRootCAsFile() |
| 120 | + if err != nil { |
| 121 | + w.log.Error(err, "failed to load root CAs file") |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if rootCAs == nil { |
| 126 | + w.log.V(3).Info("no root CA changes on file") |
| 127 | + } else { |
| 128 | + w.log.Info("root CAs changed on file, broadcasting update") |
| 129 | + w.rootCAsPEM = rootCAs.PEM |
| 130 | + w.broadcastChan <- *rootCAs |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// loadRootCAsFile will read the root CAs from the configured file, and if |
| 135 | +// changed from the previous state, will return the updated root CAs. Will |
| 136 | +// return nil if there has been no state change. |
| 137 | +func (w *watcher) loadRootCAsFile() (*RootCAs, error) { |
| 138 | + rootCAsPEM, err := os.ReadFile(w.filepath) |
| 139 | + if err != nil { |
| 140 | + return nil, fmt.Errorf("failed to read root CAs certificate file %q: %w", w.filepath, err) |
| 141 | + } |
| 142 | + |
| 143 | + // If the root CAs PEM hasn't changed, return nil |
| 144 | + if bytes.Equal(rootCAsPEM, w.rootCAsPEM) { |
| 145 | + return nil, nil |
| 146 | + } |
| 147 | + |
| 148 | + w.log.Info("updating root CAs from file") |
| 149 | + |
| 150 | + rootCAsCerts, err := pki.DecodeX509CertificateChainBytes(rootCAsPEM) |
| 151 | + if err != nil { |
| 152 | + return nil, fmt.Errorf("failed to decode root CAs in certificate file %q: %w", w.filepath, err) |
| 153 | + } |
| 154 | + |
| 155 | + rootCAsPool := x509.NewCertPool() |
| 156 | + for _, rootCert := range rootCAsCerts { |
| 157 | + rootCAsPool.AddCert(rootCert) |
| 158 | + } |
| 159 | + |
| 160 | + return &RootCAs{rootCAsPEM, rootCAsPool}, nil |
| 161 | +} |
0 commit comments