From 237557c0cfd419700d51fba0fa279049c64c9437 Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Wed, 18 Sep 2024 16:18:46 +0000 Subject: [PATCH] allow secrets with data or string data Signed-off-by: Austin Abro --- src/internal/bigbang/bigbang.go | 30 +++++++++++++++---- src/internal/bigbang/bigbang_test.go | 10 +++++-- .../valid_secret_data.yaml | 6 ++++ ...ret.yaml => valid_secret_string_data.yaml} | 2 +- 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_data.yaml rename src/internal/bigbang/testdata/getValuesFromManifest/{valid_secret.yaml => valid_secret_string_data.yaml} (88%) diff --git a/src/internal/bigbang/bigbang.go b/src/internal/bigbang/bigbang.go index 6a766a1416..bf1aac3b3a 100644 --- a/src/internal/bigbang/bigbang.go +++ b/src/internal/bigbang/bigbang.go @@ -15,6 +15,8 @@ import ( "sort" "strings" + "encoding/base64" + "github.com/Masterminds/semver/v3" "github.com/defenseunicorns/pkg/helpers/v2" fluxHelmCtrl "github.com/fluxcd/helm-controller/api/v2" @@ -48,20 +50,38 @@ func getValuesFromManifest(valuesFileManifest string) (string, error) { if err := yaml.Unmarshal(file, &resource); err != nil { return "", err } - if resource.GetKind() != "Secret" && resource.GetKind() != "ConfigMap" { - return "", errors.New("values manifests must be a Secret or ConfigMap") - } - data, found, err := unstructured.NestedStringMap(resource.Object, "data") - if err != nil || !found { + var data map[string]string + var found bool + var base64Decode bool + if resource.GetKind() == "Secret" { data, found, err = unstructured.NestedStringMap(resource.Object, "stringData") + if err != nil || !found { + data, found, err = unstructured.NestedStringMap(resource.Object, "data") + if err != nil || !found { + return "", fmt.Errorf("failed to get data from resource: %w", err) + } + base64Decode = true + } + } else if resource.GetKind() == "ConfigMap" { + data, found, err = unstructured.NestedStringMap(resource.Object, "data") if err != nil || !found { return "", fmt.Errorf("failed to get data from resource: %w", err) } + } else { + return "", errors.New("values manifests must be a Secret or ConfigMap") } + valuesYaml, found := data["values.yaml"] if !found { return "", errors.New("values.yaml key must exist in data") } + if base64Decode { + b, err := base64.StdEncoding.DecodeString(valuesYaml) + if err != nil { + return "", err + } + valuesYaml = string(b) + } return valuesYaml, nil } diff --git a/src/internal/bigbang/bigbang_test.go b/src/internal/bigbang/bigbang_test.go index 4c20536fde..921bcee344 100644 --- a/src/internal/bigbang/bigbang_test.go +++ b/src/internal/bigbang/bigbang_test.go @@ -108,11 +108,17 @@ func TestGetValuesFromManifest(t *testing.T) { expectedErr error }{ { - name: "Valid Secret", - fileName: "valid_secret.yaml", + name: "Valid Secret string data", + fileName: "valid_secret_string_data.yaml", expectedOutput: "key: value\n", expectedErr: nil, }, + { + name: "Valid Secret regular data", + fileName: "valid_secret_data.yaml", + expectedOutput: "key: value", + expectedErr: nil, + }, { name: "Valid ConfigMap", fileName: "valid_configmap.yaml", diff --git a/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_data.yaml b/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_data.yaml new file mode 100644 index 0000000000..da95ea81aa --- /dev/null +++ b/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_data.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: Secret +metadata: + name: test-secret +data: + values.yaml: a2V5OiB2YWx1ZQ== diff --git a/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret.yaml b/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_string_data.yaml similarity index 88% rename from src/internal/bigbang/testdata/getValuesFromManifest/valid_secret.yaml rename to src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_string_data.yaml index 845fa66998..9b5ccb6b99 100644 --- a/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret.yaml +++ b/src/internal/bigbang/testdata/getValuesFromManifest/valid_secret_string_data.yaml @@ -2,6 +2,6 @@ apiVersion: v1 kind: Secret metadata: name: test-secret -data: +stringData: values.yaml: | key: value