Better agent template to merge multiple secrets

Hello, vault.

I have a vault agent template that renders a JSON merging multiple secrets like below.

{{ with secret "secret/test1" }}{{ $test1 := .Data.data | explodeMap }}
{{ with secret "secret/test2" }}{{ $test2 := .Data.data | explodeMap }}
{{ with secret "secret/test3" }}{{ $test3 := .Data.data | explodeMap }}
{{ with $test1 }}{{ . | mergeMap $test2 | mergeMap $test3 | toJSONPretty }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}

It works ok but looks really ugly, and I hate maintaining this code.
adding one more secret will make the code look 2x ugly.
is there a better way?

Thank you.

How about this?

{{ $collect := (secret "secret/test1").Data.data | explodeMap -}}
{{ $collect := (secret "secret/test2").Data.data | explodeMap | mergeMap $collect -}}
{{ $collect := (secret "secret/test3").Data.data | explodeMap | mergeMap $collect -}}
{{ $collect | toJSONPretty }}

Or if you expect to have lots and lots of them

{{ $collect := sprig_dict -}}
{{ range sprig_list
    "secret/test1"
    "secret/test2"
    "secret/test3"
    "secret/test4"
    "secret/test5"
    "secret/test6"
-}}
{{ $collect := (secret .).Data.data | explodeMap | mergeMap $collect -}}
{{ end -}}
{{ $collect | toJSONPretty }}
1 Like

Nice job! @maxb
Thank you :smiley: