From 9823c7d1aa2a47016f4df49163601fab43f7af9d Mon Sep 17 00:00:00 2001 From: = Date: Sat, 18 Jan 2025 18:58:00 +0530 Subject: [PATCH] feat: added base64DecodeBytes function to operator template --- .../infisical-secret-crd-with-template.yml} | 1 + .../infisicalsecret/infisicalsecret_helper.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) rename k8-operator/config/samples/{sample-with-template.yml => crd/infisicalsecret/infisical-secret-crd-with-template.yml} (98%) diff --git a/k8-operator/config/samples/sample-with-template.yml b/k8-operator/config/samples/crd/infisicalsecret/infisical-secret-crd-with-template.yml similarity index 98% rename from k8-operator/config/samples/sample-with-template.yml rename to k8-operator/config/samples/crd/infisicalsecret/infisical-secret-crd-with-template.yml index 9d9d86ab5..e167583a7 100644 --- a/k8-operator/config/samples/sample-with-template.yml +++ b/k8-operator/config/samples/crd/infisicalsecret/infisical-secret-crd-with-template.yml @@ -104,6 +104,7 @@ spec: includeAllSecrets: true data: SSH_KEY: "{{ .KEY.SecretPath }} {{ .KEY.Value }}" + BINARY_KEY: "{{ base64DecodeBytes .BINARY_KEY_BASE64.Value }}" creationPolicy: "Orphan" ## Owner | Orphan # secretType: kubernetes.io/dockerconfigjson diff --git a/k8-operator/controllers/infisicalsecret/infisicalsecret_helper.go b/k8-operator/controllers/infisicalsecret/infisicalsecret_helper.go index 28a9843c9..8fb41621a 100644 --- a/k8-operator/controllers/infisicalsecret/infisicalsecret_helper.go +++ b/k8-operator/controllers/infisicalsecret/infisicalsecret_helper.go @@ -3,6 +3,7 @@ package controllers import ( "bytes" "context" + "encoding/base64" "errors" "fmt" "strings" @@ -261,7 +262,15 @@ func (r *InfisicalSecretReconciler) updateInfisicalManagedKubeSecret(ctx context } for templateKey, userTemplate := range managedTemplateData.Data { - tmpl, err := template.New("secret-templates").Parse(userTemplate) + tmpl, err := template.New("secret-templates").Funcs(template.FuncMap{ + "base64DecodeBytes": func(encodedString string) string { + decoded, err := base64.StdEncoding.DecodeString(encodedString) + if err != nil { + return fmt.Sprintf("Error: %v", err) + } + return string(decoded) + }, + }).Parse(userTemplate) if err != nil { return fmt.Errorf("unable to compile template: %s [err=%v]", templateKey, err) }