|
| 1 | +package apiserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/openshift/client-go/config/informers/externalversions" |
| 10 | + |
| 11 | + apiconfigv1 "github.com/openshift/api/config/v1" |
| 12 | + configv1client "github.com/openshift/client-go/config/clientset/versioned" |
| 13 | + configv1 "github.com/openshift/client-go/config/informers/externalversions/config/v1" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | + "k8s.io/client-go/tools/cache" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // This is the cluster level global apiserver.config.openshift.io/cluster object name. |
| 20 | + globalAPIServerName = "cluster" |
| 21 | + |
| 22 | + // default sync interval |
| 23 | + defaultSyncInterval = 30 * time.Minute |
| 24 | +) |
| 25 | + |
| 26 | +// NewSyncer returns informer and sync functions to enable watch of the apiserver.config.openshift.io/cluster resource. |
| 27 | +func NewSyncer(logger *logrus.Logger, client configv1client.Interface) (apiServerInformer configv1.APIServerInformer, syncer *Syncer, querier Querier, factory externalversions.SharedInformerFactory, err error) { |
| 28 | + factory = externalversions.NewSharedInformerFactoryWithOptions(client, defaultSyncInterval) |
| 29 | + apiServerInformer = factory.Config().V1().APIServers() |
| 30 | + s := &Syncer{ |
| 31 | + logger: logger, |
| 32 | + currentConfig: newTLSConfigHolder(), |
| 33 | + } |
| 34 | + |
| 35 | + syncer = s |
| 36 | + querier = s |
| 37 | + return |
| 38 | +} |
| 39 | + |
| 40 | +// RegisterEventHandlers registers event handlers for apiserver.config.openshift.io/cluster resource changes. |
| 41 | +// This is a convenience function to set up Add/Update/Delete handlers that call |
| 42 | +// the syncer's SyncAPIServer and HandleAPIServerDelete methods. |
| 43 | +func RegisterEventHandlers(informer configv1.APIServerInformer, syncer *Syncer) { |
| 44 | + informer.Informer().AddEventHandler(&cache.ResourceEventHandlerFuncs{ |
| 45 | + AddFunc: func(obj interface{}) { |
| 46 | + if err := syncer.SyncAPIServer(obj); err != nil { |
| 47 | + syncer.logger.WithError(err).Error("error syncing APIServer on add") |
| 48 | + } |
| 49 | + }, |
| 50 | + UpdateFunc: func(_, newObj interface{}) { |
| 51 | + if err := syncer.SyncAPIServer(newObj); err != nil { |
| 52 | + syncer.logger.WithError(err).Error("error syncing APIServer on update") |
| 53 | + } |
| 54 | + }, |
| 55 | + DeleteFunc: func(obj interface{}) { |
| 56 | + syncer.HandleAPIServerDelete(obj) |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +// Syncer deals with watching APIServer type(s) on the cluster and let the caller |
| 62 | +// query for cluster scoped APIServer TLS configuration. |
| 63 | +type Syncer struct { |
| 64 | + logger *logrus.Logger |
| 65 | + currentConfig *tlsConfigHolder |
| 66 | +} |
| 67 | + |
| 68 | +// tlsConfigHolder holds TLS configuration in a thread-safe manner. |
| 69 | +// It always contains a valid configuration with secure defaults. |
| 70 | +type tlsConfigHolder struct { |
| 71 | + mu sync.RWMutex |
| 72 | + config tls.Config |
| 73 | +} |
| 74 | + |
| 75 | +// newTLSConfigHolder creates a new holder initialized with secure defaults. |
| 76 | +func newTLSConfigHolder() *tlsConfigHolder { |
| 77 | + h := &tlsConfigHolder{} |
| 78 | + // Initialize with secure defaults |
| 79 | + _ = ApplySecureDefaults(&h.config) |
| 80 | + return h |
| 81 | +} |
| 82 | + |
| 83 | +// update atomically updates the stored TLS configuration. |
| 84 | +func (h *tlsConfigHolder) update(minVersion uint16, cipherSuites []uint16) { |
| 85 | + h.mu.Lock() |
| 86 | + defer h.mu.Unlock() |
| 87 | + |
| 88 | + h.config.MinVersion = minVersion |
| 89 | + // Make a defensive copy of the slice |
| 90 | + h.config.CipherSuites = make([]uint16, len(cipherSuites)) |
| 91 | + copy(h.config.CipherSuites, cipherSuites) |
| 92 | + h.config.PreferServerCipherSuites = true |
| 93 | +} |
| 94 | + |
| 95 | +// copyTo atomically copies the cached TLS settings to the provided config. |
| 96 | +// All reading and copying happens under the read lock, ensuring thread safety. |
| 97 | +func (h *tlsConfigHolder) copyTo(config *tls.Config) { |
| 98 | + h.mu.RLock() |
| 99 | + defer h.mu.RUnlock() |
| 100 | + |
| 101 | + // Copy all fields while holding the lock |
| 102 | + config.MinVersion = h.config.MinVersion |
| 103 | + config.CipherSuites = make([]uint16, len(h.config.CipherSuites)) |
| 104 | + copy(config.CipherSuites, h.config.CipherSuites) |
| 105 | + config.PreferServerCipherSuites = h.config.PreferServerCipherSuites |
| 106 | +} |
| 107 | + |
| 108 | +// QueryTLSConfig queries the global cluster level APIServer object and updates |
| 109 | +// the provided TLS configuration with the cluster-wide security profile settings. |
| 110 | +func (w *Syncer) QueryTLSConfig(config *tls.Config) error { |
| 111 | + if config == nil { |
| 112 | + return fmt.Errorf("tls.Config cannot be nil") |
| 113 | + } |
| 114 | + |
| 115 | + // Copy the current cached config atomically |
| 116 | + // This always succeeds because currentConfig always has a valid value |
| 117 | + w.currentConfig.copyTo(config) |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// SyncAPIServer is invoked when a cluster scoped APIServer object is added or modified. |
| 122 | +func (w *Syncer) SyncAPIServer(object interface{}) error { |
| 123 | + apiserver, ok := object.(*apiconfigv1.APIServer) |
| 124 | + if !ok { |
| 125 | + w.logger.Error("wrong type in APIServer syncer") |
| 126 | + return nil |
| 127 | + } |
| 128 | + |
| 129 | + // Convert the TLS security profile to get new settings |
| 130 | + minVersion, cipherSuites := GetSecurityProfileConfig(apiserver.Spec.TLSSecurityProfile) |
| 131 | + |
| 132 | + // Check if configuration changed (before updating) |
| 133 | + changed := w.hasConfigChanged(minVersion, cipherSuites) |
| 134 | + |
| 135 | + // Update the stored configuration atomically |
| 136 | + w.currentConfig.update(minVersion, cipherSuites) |
| 137 | + |
| 138 | + // Log if configuration changed |
| 139 | + if changed { |
| 140 | + profileName := getProfileName(apiserver.Spec.TLSSecurityProfile) |
| 141 | + w.logger.Infof("APIServer TLS configuration changed: profile=%s, minVersion=%s, cipherCount=%d", |
| 142 | + profileName, |
| 143 | + tlsVersionToString(minVersion), |
| 144 | + len(cipherSuites)) |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +// HandleAPIServerDelete is invoked when a cluster scoped APIServer object is deleted. |
| 151 | +func (w *Syncer) HandleAPIServerDelete(object interface{}) { |
| 152 | + _, ok := object.(*apiconfigv1.APIServer) |
| 153 | + if !ok { |
| 154 | + w.logger.Error("wrong type in APIServer delete syncer") |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + // Reset to secure defaults (Intermediate profile) |
| 159 | + w.currentConfig.update(GetSecurityProfileConfig(nil)) |
| 160 | + |
| 161 | + w.logger.Info("APIServer TLS configuration deleted, reverted to secure defaults") |
| 162 | + return |
| 163 | +} |
| 164 | + |
| 165 | +// hasConfigChanged checks if the new TLS settings differ from the current cached settings. |
| 166 | +func (w *Syncer) hasConfigChanged(minVersion uint16, cipherSuites []uint16) bool { |
| 167 | + w.currentConfig.mu.RLock() |
| 168 | + defer w.currentConfig.mu.RUnlock() |
| 169 | + |
| 170 | + if w.currentConfig.config.MinVersion != minVersion { |
| 171 | + return true |
| 172 | + } |
| 173 | + if len(w.currentConfig.config.CipherSuites) != len(cipherSuites) { |
| 174 | + return true |
| 175 | + } |
| 176 | + for i := range cipherSuites { |
| 177 | + if w.currentConfig.config.CipherSuites[i] != cipherSuites[i] { |
| 178 | + return true |
| 179 | + } |
| 180 | + } |
| 181 | + return false |
| 182 | +} |
| 183 | + |
| 184 | +// getProfileName returns the TLS security profile name for logging. |
| 185 | +func getProfileName(profile *apiconfigv1.TLSSecurityProfile) string { |
| 186 | + if profile == nil { |
| 187 | + return "Intermediate (default)" |
| 188 | + } |
| 189 | + |
| 190 | + profileType := string(profile.Type) |
| 191 | + if profileType == "" { |
| 192 | + return "Intermediate (default)" |
| 193 | + } |
| 194 | + |
| 195 | + return profileType |
| 196 | +} |
| 197 | + |
| 198 | +// tlsVersionToString converts a TLS version number to a string |
| 199 | +func tlsVersionToString(version uint16) string { |
| 200 | + switch version { |
| 201 | + case tls.VersionTLS10: |
| 202 | + return "TLS 1.0" |
| 203 | + case tls.VersionTLS11: |
| 204 | + return "TLS 1.1" |
| 205 | + case tls.VersionTLS12: |
| 206 | + return "TLS 1.2" |
| 207 | + case tls.VersionTLS13: |
| 208 | + return "TLS 1.3" |
| 209 | + default: |
| 210 | + return "unknown" |
| 211 | + } |
| 212 | +} |
0 commit comments