From 950483e26542dbeabb2d15a1cfbc5cad1526a531 Mon Sep 17 00:00:00 2001 From: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com> Date: Wed, 6 Jul 2022 16:00:01 -0400 Subject: [PATCH 1/2] agent/template: fix exec parsing error for templates --- command/agent/config/config.go | 11 ++++++++--- command/agent/config/config_test.go | 4 ++++ .../config/test-fixtures/config-template-full.hcl | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/command/agent/config/config.go b/command/agent/config/config.go index 8a28dcf63152..b1cad8d84324 100644 --- a/command/agent/config/config.go +++ b/command/agent/config/config.go @@ -678,19 +678,24 @@ func parseTemplates(result *Config, list *ast.ObjectList) error { return errors.New("error converting config") } - // flatten the wait field. The initial "wait" value, if given, is a + // flatten the wait or exec fields. The initial "wait" or "exec" value, if given, is a // []map[string]interface{}, but we need it to be map[string]interface{}. // Consul Template has a method flattenKeys that walks all of parsed and // flattens every key. For Vault Agent, we only care about the wait input. - // Only one wait stanza is supported, however Consul Template does not error + // Only one wait/exec stanza is supported, however Consul Template does not error // with multiple instead it flattens them down, with last value winning. - // Here we take the last element of the parsed["wait"] slice to keep + // Here we take the last element of the parsed["wait"] or parsed["exec"] slice to keep // consistency with Consul Template behavior. wait, ok := parsed["wait"].([]map[string]interface{}) if ok { parsed["wait"] = wait[len(wait)-1] } + exec, ok := parsed["exec"].([]map[string]interface{}) + if ok { + parsed["exec"] = exec[len(exec)-1] + } + var tc ctconfig.TemplateConfig // Use mapstructure to populate the basic config fields diff --git a/command/agent/config/config_test.go b/command/agent/config/config_test.go index c9728543c3b4..38ca2fb72ff6 100644 --- a/command/agent/config/config_test.go +++ b/command/agent/config/config_test.go @@ -728,6 +728,10 @@ func TestLoadConfigFile_Template(t *testing.T) { Perms: pointerutil.FileModePtr(0o655), RightDelim: pointerutil.StringPtr(">>"), SandboxPath: pointerutil.StringPtr("/path/on/disk/where"), + Exec: &ctconfig.ExecConfig{ + Command: []string{"foo"}, + Timeout: pointerutil.TimeDurationPtr("10s"), + }, Wait: &ctconfig.WaitConfig{ Min: pointerutil.TimeDurationPtr("10s"), diff --git a/command/agent/config/test-fixtures/config-template-full.hcl b/command/agent/config/test-fixtures/config-template-full.hcl index ee05c23098cb..5e5cbc62cd7b 100644 --- a/command/agent/config/test-fixtures/config-template-full.hcl +++ b/command/agent/config/test-fixtures/config-template-full.hcl @@ -46,4 +46,9 @@ template { min = "10s" max = "40s" } + + exec { + command = ["foo"] + timeout = "10s" + } } \ No newline at end of file From 2191c6d3c5aaaf02c1b3c9c2aec7504edaa55c5c Mon Sep 17 00:00:00 2001 From: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com> Date: Wed, 6 Jul 2022 16:06:36 -0400 Subject: [PATCH 2/2] changelog --- changelog/16231.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/16231.txt diff --git a/changelog/16231.txt b/changelog/16231.txt new file mode 100644 index 000000000000..aa979d1f8afe --- /dev/null +++ b/changelog/16231.txt @@ -0,0 +1,3 @@ +```release-note:bug +agent/template: Fix parsing error for the exec stanza +```