Skip to content

Commit

Permalink
Added the ability to configure template delimiters on Pod annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviolemos78 committed Sep 6, 2023
1 parent 1b1d69e commit 4b3a42a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.3.0 (August 16, 2023)

Improvements:
* Added support to configure template delimiters through Pod Annotations [GH-517](https://github.com/hashicorp/vault-k8s/pull/517)

Improvements:
* Add `NAMESPACE`, `HOST_IP`, and `POD_IP` environment variables to Agent container using downward API [GH-486](https://github.com/hashicorp/vault-k8s/pull/486)

Expand Down
8 changes: 8 additions & 0 deletions agent-inject/agent/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ const (
// attempts. Defaults to true.
AnnotationTemplateConfigExitOnRetryFailure = "vault.hashicorp.com/template-config-exit-on-retry-failure"

// AnnotationTemplateConfigLeftDelimiters template delimiters
// Defaults to "{{".
AnnotationTemplateConfigLeftDelimiters = "vault.hashicorp.com/template-left-delimiter"

// AnnotationTemplateConfigLeftDelimiters template delimiters
// Defaults to "{{".
AnnotationTemplateConfigRightDelimiters = "vault.hashicorp.com/template-right-delimiter"

// AnnotationTemplateConfigStaticSecretRenderInterval
// If specified, configures how often Vault Agent Template should render non-leased secrets such as KV v2.
// Defaults to 5 minutes.
Expand Down
20 changes: 18 additions & 2 deletions agent-inject/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,13 @@ func (a *Agent) newTemplateConfigs() []*Template {
filePathAndName = filepath.Join(secret.MountPath, secret.FilePathAndName)
}

leftDelim, rightDelim := a.getTemplateConfigDelimeters()
tmpl := &Template{
Source: templateFile,
Contents: template,
Destination: filePathAndName,
LeftDelim: "{{",
RightDelim: "}}",
LeftDelim: leftDelim,
RightDelim: rightDelim,
Command: secret.Command,
}
if secret.FilePermission != "" {
Expand All @@ -214,6 +215,21 @@ func (a *Agent) newTemplateConfigs() []*Template {
return templates
}

func (a *Agent) getTemplateConfigDelimeters() (string, string) {
leftDelim := "{{"
rightDelim := "}}"

if left, defined := a.Annotations[AnnotationTemplateConfigLeftDelimiters]; defined {
leftDelim = left
}

if right, defined := a.Annotations[AnnotationTemplateConfigRightDelimiters]; defined {
rightDelim = right
}

return leftDelim, rightDelim
}

func (a *Agent) newConfig(init bool) ([]byte, error) {
config := Config{
PidFile: PidFile,
Expand Down
64 changes: 64 additions & 0 deletions agent-inject/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,70 @@ func TestConfigVaultAgentTemplateConfig(t *testing.T) {
}
}

func TestConfigVaultAgentTemplateDelimiters(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
expectedTemplate *Template
}{
{
"no_override_annotations_expect_default",
map[string]string{
AnnotationTemplateConfigExitOnRetryFailure: "true",
"vault.hashicorp.com/agent-inject-template-foo": "template foo",
},
&Template{LeftDelim: "{{", RightDelim: "}}"},
},
{
"left_delimiter_annotation_defined_expected_left_delimiter_override",
map[string]string{
AnnotationTemplateConfigLeftDelimiters: "[[",
"vault.hashicorp.com/agent-inject-template-foo": "template foo",
},
&Template{LeftDelim: "[[", RightDelim: "}}"},
},
{
"right_delimiter_annotation_defined_expected_right_delimiter_override",
map[string]string{
AnnotationTemplateConfigRightDelimiters: "]]",
"vault.hashicorp.com/agent-inject-template-foo": "template foo",
},
&Template{LeftDelim: "{{", RightDelim: "]]"},
},
{
"left_right_delimiter_annotations_defined_expected_left_right_delimiters_override",
map[string]string{
AnnotationTemplateConfigLeftDelimiters: "[[",
AnnotationTemplateConfigRightDelimiters: "]]",
"vault.hashicorp.com/agent-inject-template-foo": "template foo",
},
&Template{LeftDelim: "[[", RightDelim: "]]"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pod := testPod(tt.annotations)

agentConfig := basicAgentConfig()
err := Init(pod, agentConfig)
require.NoError(t, err)

agent, err := New(pod)
require.NoError(t, err)
cfg, err := agent.newConfig(true)
require.NoError(t, err)

config := &Config{}
err = json.Unmarshal(cfg, config)
require.NoError(t, err)

assert.Equal(t, tt.expectedTemplate.LeftDelim, config.Templates[0].LeftDelim)
assert.Equal(t, tt.expectedTemplate.RightDelim, config.Templates[0].RightDelim)
})
}
}

func TestInjectTokenSink(t *testing.T) {
tokenHelperSink := &Sink{
Type: "file",
Expand Down

0 comments on commit 4b3a42a

Please sign in to comment.