Helm Error: parse error at invalid syntax

Viewed 177

How can I escape () in helm template… I tried as below but getting Error: parse error at yaml:17): invalid syntax..

At the end I need this output "{{ (index . "demo-app-secret") | b64dec }}"

Here is the yaml.

{{- range $externalSecretName, $externalSecret := .Values.externalSecrets }}
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: {{ $.Release.Name }}
spec:
  refreshInterval: 10m
  secretStoreRef:
    name: tf-address-handling-fortanix
    kind: SecretStore
  target:
    name: {{ $.Release.Name }}
    creationPolicy: Owner
    template:
      data:
    {{- range $externalSecret.data }}
        {{ .secretKey }}: "{{ printf "{{ \(index . \"%s\"\) | b64dec }}" .secretKey }}"
    {{- end }}
  data:
    {{- toYaml $externalSecret.data | nindent 4 }}
{{- end }}

Here is the values.yaml

externalSecrets:
  master-tf-address-handling:
    data:
      - secretKey: xxxxxxxx
        remoteRef:
          key: xxxxx
      - secretKey: yyyy
        remoteRef:
          key: yyyyy
1 Answers

The parentheses don't need to be escaped at all, and if you remove the backslash escaping before the parentheses then you'll get the output you expect.

{{ .secretKey }}: "{{ printf "{{ (index . \"%s\") | b64dec }}" .secretKey }}"
{{/*                             ^              ^
                                  no backslashes                        */}}

If you run this through helm template, you'll get a second error. The output string you get has unescaped double-quotes inside a double-quoted string

xxxxxxxx: "{{ (index . "xxxxxxxx") | b64dec }}"
#         ^            ^        ^             ^
#                too many double quotes

To fix this you need extra backslashes in the output

xxxxxxxx: "{{ (index . \"xxxxxxxx\") | b64dec }}"
#                      ^         ^
#                 add these backslashes

and to get that you need an additional double backslash inside the template

{{ .secretKey }}: "{{ printf "{{ (index . \\\"%s\\\") | b64dec }}" .secretKey }}"
{{/*                                      ^^    ^^
                                    add these backslashes                   */}}
Related