From d29f6f8685db7dd99a73278043bec6f1b22743b1 Mon Sep 17 00:00:00 2001 From: Steven Borrelli Date: Thu, 3 Aug 2023 11:13:56 -0500 Subject: [PATCH] update hash calculations Signed-off-by: Steven Borrelli (cherry picked from commit 08debbfe4939022b7b4cc4b8e2c829fa5ee650a7) --- .../composite/composition_transforms.go | 17 ++++++++---- .../composite/composition_transforms_test.go | 26 ++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/internal/controller/apiextensions/composite/composition_transforms.go b/internal/controller/apiextensions/composite/composition_transforms.go index 66c66f71c..1da5f8c6f 100644 --- a/internal/controller/apiextensions/composite/composition_transforms.go +++ b/internal/controller/apiextensions/composite/composition_transforms.go @@ -336,12 +336,19 @@ func stringConvertTransform(t *v1.StringConversionType, input any) (string, erro } func stringGenerateHash[THash any](input any, hashFunc func([]byte) THash) (THash, error) { - inputJSON, err := json.Marshal(input) - if err != nil { - var ret THash - return ret, errors.Wrap(err, errMarshalJSON) + var b []byte + var err error + switch v := input.(type) { + case string: + b = []byte(v) + default: + b, err = json.Marshal(input) + if err != nil { + var ret THash + return ret, errors.Wrap(err, errMarshalJSON) + } } - return hashFunc(inputJSON), nil + return hashFunc(b), nil } func stringTrimTransform(input any, t v1.StringTransformType, trim string) string { diff --git a/internal/controller/apiextensions/composite/composition_transforms_test.go b/internal/controller/apiextensions/composite/composition_transforms_test.go index b82945ef2..ef929b1e6 100644 --- a/internal/controller/apiextensions/composite/composition_transforms_test.go +++ b/internal/controller/apiextensions/composite/composition_transforms_test.go @@ -794,7 +794,7 @@ func TestStringResolve(t *testing.T) { i: "Crossplane", }, want: want{ - o: "f9fd1da3c0cc298643ff098a0c59febf1d8b7b84", + o: "3b683dc8ff44122b331a5e4f253dd69d90726d75", }, }, "ConvertToSha1Error": { @@ -815,7 +815,7 @@ func TestStringResolve(t *testing.T) { i: "Crossplane", }, want: want{ - o: "e84ae541a0725d73154ee76b7ac3fec4b007dd01ed701d506cd7e7a45bb48935", + o: "19c8a7c24ed0067f606815b59e5b82d92935ff69deed04171457a55018e31224", }, }, "ConvertToSha256Error": { @@ -836,7 +836,27 @@ func TestStringResolve(t *testing.T) { i: "Crossplane", }, want: want{ - o: "b48622a3f487b8cb7748b356c9531cf54d9125c1456689c115744821f3dafd59c8c7d4dc5627c4a1e4082c67ee9f4528365a644a01a0c46d6dd0a6d979c8f51f", + o: "0016037c62c92b5cc4a282fbe30cdd228fa001624b26fd31baa9fcb76a9c60d48e2e7a16cf8729a2d9cba3d23e1d846e7721a5381b9a92dd813178e9a6686205", + }, + }, + "ConvertToSha512Int": { + args: args{ + stype: v1.StringTransformTypeConvert, + convert: &toSha512, + i: 1234, + }, + want: want{ + o: "d404559f602eab6fd602ac7680dacbfaadd13630335e951f097af3900e9de176b6db28512f2e000b9d04fba5133e8b1c6e8df59db3a8ab9d60be4b97cc9e81db", + }, + }, + "ConvertToSha512IntStr": { + args: args{ + stype: v1.StringTransformTypeConvert, + convert: &toSha512, + i: "1234", + }, + want: want{ + o: "d404559f602eab6fd602ac7680dacbfaadd13630335e951f097af3900e9de176b6db28512f2e000b9d04fba5133e8b1c6e8df59db3a8ab9d60be4b97cc9e81db", }, }, "ConvertToSha512Error": {