@@ -20,15 +20,15 @@ import (
2020 "k8s.io/apimachinery/pkg/runtime"
2121
2222 configv1 "github.com/openshift/api/config/v1"
23+ operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1"
2324 fakeconfigclientv1 "github.com/openshift/client-go/config/clientset/versioned/fake"
2425 "github.com/openshift/library-go/pkg/crypto"
2526)
2627
27- // makeGenericOperatorConfigYAML generates a GenericOperatorConfig YAML string
28- // with the specified servingInfo fields.
29- func makeGenericOperatorConfigYAML (cipherSuites []string , minTLSVersion string ) string {
30- const tmpl = `apiVersion: operator.openshift.io/v1alpha1
31- kind: GenericOperatorConfig
28+ // makeGenericConfigYAML generates a generic config YAML string with the specified servingInfo fields.
29+ func makeGenericConfigYAML (apiVersion , kind string , cipherSuites []string , minTLSVersion string ) string {
30+ const tmpl = `apiVersion: {{ .APIVersion }}
31+ kind: {{ .Kind }}
3232servingInfo:
3333 bindAddress: 0.0.0.0:8443
3434 bindNetwork: tcp4
@@ -50,6 +50,8 @@ servingInfo:
5050`
5151
5252 data := map [string ]interface {}{
53+ "APIVersion" : apiVersion ,
54+ "Kind" : kind ,
5355 "CipherSuites" : cipherSuites ,
5456 "MinTLSVersion" : minTLSVersion ,
5557 }
@@ -63,6 +65,18 @@ servingInfo:
6365 return buf .String ()
6466}
6567
68+ // makeGenericOperatorConfigYAML generates a GenericOperatorConfig YAML string
69+ // with the specified servingInfo fields.
70+ func makeGenericOperatorConfigYAML (cipherSuites []string , minTLSVersion string ) string {
71+ return makeGenericConfigYAML (operatorv1alpha1 .GroupVersion .String (), "GenericOperatorConfig" , cipherSuites , minTLSVersion )
72+ }
73+
74+ // makeGenericControllerConfigYAML generates a GenericControllerConfig YAML string
75+ // with the specified servingInfo fields.
76+ func makeGenericControllerConfigYAML (cipherSuites []string , minTLSVersion string ) string {
77+ return makeGenericConfigYAML (configv1 .GroupVersion .String (), "GenericControllerConfig" , cipherSuites , minTLSVersion )
78+ }
79+
6680func getDefaultCipherSuitesSorted () []string {
6781 cipherSuites := crypto .CipherSuitesToNamesOrDie (crypto .DefaultCiphers ())
6882 sort .Strings (cipherSuites )
@@ -74,12 +88,14 @@ const (
7488 tlsVersion12 = "VersionTLS12"
7589 tlsVersion13 = "VersionTLS13"
7690
77- genericOperatorConfigCMKey = "config.yaml"
78- genericOperatorConfigCMKey2 = "operator-config.yaml"
79- someKey1 = "plain-text"
80- someKey2 = "another-value"
81- someValue1 = "just some plain text"
82- someValue2 = "12345"
91+ genericOperatorConfigCMKey = "config.yaml"
92+ genericOperatorConfigCMKey2 = "operator-config.yaml"
93+ genericControllerConfigCMKey = "controller-config.yaml"
94+ genericControllerConfigCMKey2 = "controller-config2.yaml"
95+ someKey1 = "plain-text"
96+ someKey2 = "another-value"
97+ someValue1 = "just some plain text"
98+ someValue2 = "12345"
8399)
84100
85101var (
@@ -204,8 +220,8 @@ func validateConfigMapsEqual(original, modified *corev1.ConfigMap) error {
204220 return nil
205221}
206222
207- // validateGenericOperatorConfigTLSInjected validates that TLS settings were injected into GenericOperatorConfig
208- func validateGenericOperatorConfigTLSInjected (modified * corev1.ConfigMap , fieldName string , expectedCiphers []string , expectedMinTLSVersion string ) error {
223+ // validateGenericConfigTLSInjected validates that TLS settings were injected into a generic config
224+ func validateGenericConfigTLSInjected (modified * corev1.ConfigMap , fieldName string , expectedKind , expectedAPIVersion string , expectedCiphers []string , expectedMinTLSVersion string ) error {
209225 // Verify the field is still present
210226 configYAML , ok := modified .Data [fieldName ]
211227 if ! ok {
@@ -218,25 +234,25 @@ func validateGenericOperatorConfigTLSInjected(modified *corev1.ConfigMap, fieldN
218234 return fmt .Errorf ("failed to unmarshal %s: %v" , fieldName , err )
219235 }
220236
221- // Verify it is a GenericOperatorConfig
237+ // Verify kind
222238 kind , found , err := unstructured .NestedString (obj , "kind" )
223239 if err != nil {
224240 return fmt .Errorf ("failed to get kind field: %v" , err )
225241 }
226- if ! found || kind != "GenericOperatorConfig" {
227- return fmt .Errorf ("expected kind GenericOperatorConfig , got %s" , kind )
242+ if ! found || kind != expectedKind {
243+ return fmt .Errorf ("expected kind %s , got %s" , expectedKind , kind )
228244 }
229245
230246 // Verify apiVersion
231247 apiVersion , found , err := unstructured .NestedString (obj , "apiVersion" )
232248 if err != nil {
233249 return fmt .Errorf ("failed to get apiVersion field: %v" , err )
234250 }
235- if ! found || apiVersion != "operator.openshift.io/v1alpha1" {
236- return fmt .Errorf ("expected apiVersion operator.openshift.io/v1alpha1 , got %s" , apiVersion )
251+ if ! found || apiVersion != expectedAPIVersion {
252+ return fmt .Errorf ("expected apiVersion %s , got %s" , expectedAPIVersion , apiVersion )
237253 }
238254
239- // Verify minTLSVersion was injected (should be VersionTLS13 from APIServer)
255+ // Verify minTLSVersion was injected
240256 minTLSVersion , found , err := unstructured .NestedString (obj , "servingInfo" , "minTLSVersion" )
241257 if err != nil {
242258 return fmt .Errorf ("failed to get servingInfo.minTLSVersion: %v" , err )
@@ -248,10 +264,7 @@ func validateGenericOperatorConfigTLSInjected(modified *corev1.ConfigMap, fieldN
248264 return fmt .Errorf ("expected minTLSVersion %s, got %s" , expectedMinTLSVersion , minTLSVersion )
249265 }
250266
251- // Verify TLS cipher suites were injected from APIServer
252- // The APIServer TLSSecurityProfile has these ciphers which get converted to IANA format:
253- // ECDHE-ECDSA-AES128-GCM-SHA256 -> TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
254- // ECDHE-RSA-AES128-GCM-SHA256 -> TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
267+ // Verify TLS cipher suites were injected
255268 cipherSuites , found , err := unstructured .NestedStringSlice (obj , "servingInfo" , "cipherSuites" )
256269 if err != nil {
257270 return fmt .Errorf ("failed to get servingInfo.cipherSuites: %v" , err )
@@ -269,6 +282,16 @@ func validateGenericOperatorConfigTLSInjected(modified *corev1.ConfigMap, fieldN
269282 return nil
270283}
271284
285+ // validateGenericOperatorConfigTLSInjected validates that TLS settings were injected into GenericOperatorConfig
286+ func validateGenericOperatorConfigTLSInjected (modified * corev1.ConfigMap , fieldName string , expectedCiphers []string , expectedMinTLSVersion string ) error {
287+ return validateGenericConfigTLSInjected (modified , fieldName , "GenericOperatorConfig" , operatorv1alpha1 .GroupVersion .String (), expectedCiphers , expectedMinTLSVersion )
288+ }
289+
290+ // validateGenericControllerConfigTLSInjected validates that TLS settings were injected into GenericControllerConfig
291+ func validateGenericControllerConfigTLSInjected (modified * corev1.ConfigMap , fieldName string , expectedCiphers []string , expectedMinTLSVersion string ) error {
292+ return validateGenericConfigTLSInjected (modified , fieldName , "GenericControllerConfig" , configv1 .GroupVersion .String (), expectedCiphers , expectedMinTLSVersion )
293+ }
294+
272295func TestModifyConfigMap (t * testing.T ) {
273296
274297 tests := []struct {
@@ -567,6 +590,38 @@ servingInfo:
567590 }
568591 },
569592 },
593+ {
594+ name : "ConfigMap with valid GenericControllerConfig object - TLS injected" ,
595+ configMap : makeConfigMap (true , map [string ]string {
596+ genericControllerConfigCMKey : makeGenericControllerConfigYAML (testCipherSuites , tlsVersion12 ),
597+ }),
598+ apiServer : makeAPIServerConfig (withCustomTLSProfile (testOpenSSLCipherSuites2 , configv1 .VersionTLS13 )),
599+ expectError : false ,
600+ validateConfigMap : func (t * testing.T , original , modified * corev1.ConfigMap ) {
601+ if err := validateGenericControllerConfigTLSInjected (modified , genericControllerConfigCMKey , testCipherSuites2 , tlsVersion13 ); err != nil {
602+ t .Fatalf ("validation failed: %v" , err )
603+ }
604+ },
605+ },
606+ {
607+ name : "ConfigMap with both GenericOperatorConfig and GenericControllerConfig - TLS injected in both" ,
608+ configMap : makeConfigMap (true , map [string ]string {
609+ genericOperatorConfigCMKey : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion12 ),
610+ genericControllerConfigCMKey : makeGenericControllerConfigYAML (testCipherSuites , tlsVersion12 ),
611+ }),
612+ apiServer : makeAPIServerConfig (withCustomTLSProfile (testOpenSSLCipherSuites2 , configv1 .VersionTLS13 )),
613+ expectError : false ,
614+ validateConfigMap : func (t * testing.T , original , modified * corev1.ConfigMap ) {
615+ // Validate the GenericOperatorConfig field
616+ if err := validateGenericOperatorConfigTLSInjected (modified , genericOperatorConfigCMKey , testCipherSuites2 , tlsVersion13 ); err != nil {
617+ t .Fatalf ("validation failed for GenericOperatorConfig: %v" , err )
618+ }
619+ // Validate the GenericControllerConfig field
620+ if err := validateGenericControllerConfigTLSInjected (modified , genericControllerConfigCMKey , testCipherSuites2 , tlsVersion13 ); err != nil {
621+ t .Fatalf ("validation failed for GenericControllerConfig: %v" , err )
622+ }
623+ },
624+ },
570625 }
571626
572627 for _ , tt := range tests {
0 commit comments