|
| 1 | +package resourcebuilder |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + |
| 9 | + "sigs.k8s.io/kustomize/kyaml/yaml" |
| 10 | + |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + "k8s.io/apimachinery/pkg/labels" |
| 15 | + "k8s.io/client-go/tools/cache" |
| 16 | + "k8s.io/klog/v2" |
| 17 | + "k8s.io/utils/clock" |
| 18 | + |
| 19 | + configv1 "github.com/openshift/api/config/v1" |
| 20 | + operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1" |
| 21 | + configclientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" |
| 22 | + configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" |
| 23 | + "github.com/openshift/library-go/pkg/operator/configobserver/apiserver" |
| 24 | + "github.com/openshift/library-go/pkg/operator/events" |
| 25 | + "github.com/openshift/library-go/pkg/operator/resourcesynccontroller" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + // ConfigMapInjectTLSAnnotation is the annotation key that triggers TLS injection into ConfigMaps |
| 30 | + ConfigMapInjectTLSAnnotation = "config.openshift.io/inject-tls" |
| 31 | +) |
| 32 | + |
| 33 | +type optional[T any] struct { |
| 34 | + value T |
| 35 | + found bool |
| 36 | +} |
| 37 | + |
| 38 | +type tlsConfig struct { |
| 39 | + minTLSVersion optional[string] |
| 40 | + cipherSuites optional[[]string] |
| 41 | +} |
| 42 | + |
| 43 | +func (b *builder) modifyConfigMap(ctx context.Context, cm *corev1.ConfigMap) error { |
| 44 | + // Check for TLS injection annotation |
| 45 | + if value, ok := cm.Annotations[ConfigMapInjectTLSAnnotation]; !ok || value != "true" { |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + klog.V(2).Infof("ConfigMap %s/%s has %s annotation set to true", cm.Namespace, cm.Name, ConfigMapInjectTLSAnnotation) |
| 50 | + |
| 51 | + // Empty data, nothing to inject into |
| 52 | + if cm.Data == nil { |
| 53 | + klog.V(2).Infof("ConfigMap %s/%s has empty data, skipping TLS profile injection", cm.Namespace, cm.Name) |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + // Observe TLS configuration from APIServer |
| 58 | + tlsConf, err := b.observeTLSConfiguration(ctx, cm) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("unable to observe TLS configuration: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + minTLSLog := "<not found>" |
| 64 | + if tlsConf.minTLSVersion.found { |
| 65 | + minTLSLog = tlsConf.minTLSVersion.value |
| 66 | + } |
| 67 | + cipherSuitesLog := "<not found>" |
| 68 | + if tlsConf.cipherSuites.found { |
| 69 | + cipherSuitesLog = fmt.Sprintf("%v", tlsConf.cipherSuites.value) |
| 70 | + } |
| 71 | + klog.V(4).Infof("ConfigMap %s/%s: observed minTLSVersion=%v, cipherSuites=%v", |
| 72 | + cm.Namespace, cm.Name, minTLSLog, cipherSuitesLog) |
| 73 | + |
| 74 | + // Process each data entry that contains GenericOperatorConfig |
| 75 | + for key, value := range cm.Data { |
| 76 | + klog.V(4).Infof("Processing %q key", key) |
| 77 | + // Parse YAML into RNode to preserve formatting and field order |
| 78 | + rnode, err := yaml.Parse(value) |
| 79 | + if err != nil { |
| 80 | + klog.V(4).Infof("ConfigMap's %q entry parsing failed: %v", key, err) |
| 81 | + // Not valid YAML, skip this entry |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + // Check if this is a supported config kind |
| 86 | + switch { |
| 87 | + case rnode.GetKind() == "GenericOperatorConfig" && rnode.GetApiVersion() == operatorv1alpha1.GroupVersion.String(): |
| 88 | + case rnode.GetKind() == "GenericControllerConfig" && rnode.GetApiVersion() == configv1.GroupVersion.String(): |
| 89 | + default: |
| 90 | + klog.V(4).Infof("ConfigMap's %q entry is not a supported config type. Only GenericOperatorConfig (%v) and GenericControllerConfig (%v) are. Skipping this entry", key, operatorv1alpha1.GroupVersion.String(), configv1.GroupVersion.String()) |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + klog.V(2).Infof("ConfigMap %s/%s processing GenericOperatorConfig in key %s", cm.Namespace, cm.Name, key) |
| 95 | + |
| 96 | + // Inject TLS settings into the GenericOperatorConfig while preserving structure |
| 97 | + if err := updateRNodeWithTLSSettings(rnode, tlsConf); err != nil { |
| 98 | + return fmt.Errorf("failed to inject the TLS configuration: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + // Marshal the modified RNode back to YAML |
| 102 | + modifiedYAML, err := rnode.String() |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to marshall the modified ConfigMap back to YAML: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Update the ConfigMap data entry with the modified YAML |
| 108 | + cm.Data[key] = modifiedYAML |
| 109 | + klog.V(2).Infof("ConfigMap %s/%s updated GenericOperatorConfig with TLS profile in key %s", cm.Namespace, cm.Name, key) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// observeTLSConfiguration retrieves TLS configuration from the APIServer cluster CR |
| 115 | +// using ObserveTLSSecurityProfile and extracts minTLSVersion and cipherSuites. |
| 116 | +func (b *builder) observeTLSConfiguration(ctx context.Context, cm *corev1.ConfigMap) (*tlsConfig, error) { |
| 117 | + // Create a lister adapter for ObserveTLSSecurityProfile |
| 118 | + lister := &apiServerListerAdapter{ |
| 119 | + client: b.configClientv1.APIServers(), |
| 120 | + ctx: ctx, |
| 121 | + } |
| 122 | + listers := &configObserverListers{ |
| 123 | + apiServerLister: lister, |
| 124 | + } |
| 125 | + |
| 126 | + // Create an in-memory event recorder that doesn't send events to the API server |
| 127 | + recorder := events.NewInMemoryRecorder("configmap-tls-injection", clock.RealClock{}) |
| 128 | + |
| 129 | + // Call ObserveTLSSecurityProfile to get TLS configuration |
| 130 | + observedConfig, errs := apiserver.ObserveTLSSecurityProfile(listers, recorder, map[string]any{}) |
| 131 | + if len(errs) > 0 { |
| 132 | + return nil, fmt.Errorf("error observing TLS profile for ConfigMap %s/%s: %w", cm.Namespace, cm.Name, errors.Join(errs...)) |
| 133 | + } |
| 134 | + |
| 135 | + config := &tlsConfig{} |
| 136 | + |
| 137 | + // Extract minTLSVersion from the observed config |
| 138 | + if minTLSVersion, minTLSFound, err := unstructured.NestedString(observedConfig, "servingInfo", "minTLSVersion"); err != nil { |
| 139 | + return nil, err |
| 140 | + } else if minTLSFound { |
| 141 | + config.minTLSVersion = optional[string]{value: minTLSVersion, found: true} |
| 142 | + } |
| 143 | + |
| 144 | + // Extract cipherSuites from the observed config |
| 145 | + if cipherSuites, ciphersFound, err := unstructured.NestedStringSlice(observedConfig, "servingInfo", "cipherSuites"); err != nil { |
| 146 | + return nil, err |
| 147 | + } else if ciphersFound { |
| 148 | + // Sort cipher suites for consistent ordering |
| 149 | + sort.Strings(cipherSuites) |
| 150 | + config.cipherSuites = optional[[]string]{value: cipherSuites, found: true} |
| 151 | + } |
| 152 | + |
| 153 | + return config, nil |
| 154 | +} |
| 155 | + |
| 156 | +// updateRNodeWithTLSSettings injects TLS settings into a GenericOperatorConfig RNode while preserving structure. |
| 157 | +// If a field in tlsConf is not found, the corresponding field will be deleted from the RNode. |
| 158 | +func updateRNodeWithTLSSettings(rnode *yaml.RNode, tlsConf *tlsConfig) error { |
| 159 | + servingInfo, err := rnode.Pipe(yaml.LookupCreate(yaml.MappingNode, "servingInfo")) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + // Handle cipherSuites field |
| 165 | + if tlsConf.cipherSuites.found { |
| 166 | + seqNode := yaml.NewListRNode(tlsConf.cipherSuites.value...) |
| 167 | + if err := servingInfo.PipeE(yaml.SetField("cipherSuites", seqNode)); err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + } else { |
| 171 | + if err := servingInfo.PipeE(yaml.Clear("cipherSuites")); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Handle minTLSVersion field |
| 177 | + if tlsConf.minTLSVersion.found { |
| 178 | + if err := servingInfo.PipeE(yaml.SetField("minTLSVersion", yaml.NewStringRNode(tlsConf.minTLSVersion.value))); err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + } else { |
| 182 | + if err := servingInfo.PipeE(yaml.Clear("minTLSVersion")); err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +// apiServerListerAdapter adapts a client interface to the lister interface |
| 191 | +type apiServerListerAdapter struct { |
| 192 | + client configclientv1.APIServerInterface |
| 193 | + ctx context.Context |
| 194 | +} |
| 195 | + |
| 196 | +func (a *apiServerListerAdapter) List(selector labels.Selector) ([]*configv1.APIServer, error) { |
| 197 | + // Not implemented - ObserveTLSSecurityProfile only uses Get() |
| 198 | + return nil, nil |
| 199 | +} |
| 200 | + |
| 201 | +func (a *apiServerListerAdapter) Get(name string) (*configv1.APIServer, error) { |
| 202 | + return a.client.Get(a.ctx, name, metav1.GetOptions{}) |
| 203 | +} |
| 204 | + |
| 205 | +// configObserverListers implements the configobserver.Listers interface. |
| 206 | +// It's expected to be used solely for apiserver.ObserveTLSSecurityProfile. |
| 207 | +type configObserverListers struct { |
| 208 | + apiServerLister configlistersv1.APIServerLister |
| 209 | +} |
| 210 | + |
| 211 | +func (l *configObserverListers) APIServerLister() configlistersv1.APIServerLister { |
| 212 | + return l.apiServerLister |
| 213 | +} |
| 214 | + |
| 215 | +func (l *configObserverListers) ResourceSyncer() resourcesynccontroller.ResourceSyncer { |
| 216 | + // Not needed for TLS observation |
| 217 | + return nil |
| 218 | +} |
| 219 | + |
| 220 | +func (l *configObserverListers) PreRunHasSynced() []cache.InformerSynced { |
| 221 | + // Not needed for TLS observation |
| 222 | + return nil |
| 223 | +} |
0 commit comments