Browse Source

clean: Remove duplicate code (#6629)

* clean: Remove duplicate code

That triggered my OCD...

Signed-off-by: Jean-Philippe Evrard <jean-philippe.evrard+rochepub@external.roche.com>

* Add transform key unit test

We already covered the Reverse/Convert, now we can check if _anything_
can be used.

Signed-off-by: Jean-Philippe Evrard <jean-philippe.evrard+rochepub@external.roche.com>

---------

Signed-off-by: Jean-Philippe Evrard <jean-philippe.evrard+rochepub@external.roche.com>
Jean-Philippe Evrard 1 week ago
parent
commit
87c2f1ffbd
2 changed files with 59 additions and 10 deletions
  1. 10 10
      runtime/esutils/utils.go
  2. 49 0
      runtime/esutils/utils_test.go

+ 10 - 10
runtime/esutils/utils.go

@@ -255,15 +255,9 @@ func ValidateKeys(log logr.Logger, in map[string][]byte) error {
 // ConvertKeys converts a secret map into a valid key.
 // Replaces any non-alphanumeric characters depending on convert strategy.
 func ConvertKeys(strategy esv1.ExternalSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
-	out := make(map[string][]byte, len(in))
-	for k, v := range in {
-		key := convert(strategy, k)
-		if _, exists := out[key]; exists {
-			return nil, fmt.Errorf("secret name collision during conversion: %s", key)
-		}
-		out[key] = v
-	}
-	return out, nil
+	return transformKeys(in, func(key string) string {
+		return convert(strategy, key)
+	})
 }
 
 func convert(strategy esv1.ExternalSecretConversionStrategy, str string) string {
@@ -293,9 +287,15 @@ func convert(strategy esv1.ExternalSecretConversionStrategy, str string) string
 // ReverseKeys reverses a secret map into a valid key map as expected by push secrets.
 // Replaces the unicode encoded representation characters back to the actual unicode character depending on convert strategy.
 func ReverseKeys(strategy esv1alpha1.PushSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
+	return transformKeys(in, func(key string) string {
+		return reverse(strategy, key)
+	})
+}
+
+func transformKeys(in map[string][]byte, transform func(string) string) (map[string][]byte, error) {
 	out := make(map[string][]byte, len(in))
 	for k, v := range in {
-		key := reverse(strategy, k)
+		key := transform(k)
 		if _, exists := out[key]; exists {
 			return nil, fmt.Errorf("secret name collision during conversion: %s", key)
 		}

+ 49 - 0
runtime/esutils/utils_test.go

@@ -244,6 +244,55 @@ func TestConvertKeys(t *testing.T) {
 	}
 }
 
+func TestTransformKeys(t *testing.T) {
+	tests := []struct {
+		name      string
+		in        map[string][]byte
+		transform func(string) string
+		want      map[string][]byte
+		wantErr   bool
+	}{
+		{
+			name: "transforms keys and preserves values",
+			in: map[string][]byte{
+				"foo": []byte("bar"),
+				"baz": []byte("qux"),
+			},
+			transform: func(key string) string {
+				return key + "-transformed"
+			},
+			want: map[string][]byte{
+				"foo-transformed": []byte("bar"),
+				"baz-transformed": []byte("qux"),
+			},
+		},
+		{
+			name: "errors on transformed key collision",
+			in: map[string][]byte{
+				"foo": []byte("bar"),
+				"baz": []byte("qux"),
+			},
+			transform: func(string) string {
+				return "collision"
+			},
+			wantErr: true,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, err := transformKeys(tt.in, tt.transform)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("transformKeys() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+			if !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("transformKeys() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
+
 func TestReverseKeys(t *testing.T) {
 	type args struct {
 		encodingStrategy esv1.ExternalSecretConversionStrategy