From 110f754d97a511e014a7ab8bf463635b190518ce Mon Sep 17 00:00:00 2001 From: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com> Date: Wed, 6 Jul 2022 16:21:35 -0400 Subject: [PATCH] agent/template: fix exec parsing error for templates (#16231) * agent/template: fix exec parsing error for templates * changelog --- changelog/16231.txt | 3 +++ command/agent/config/config.go | 11 ++++++++--- command/agent/config/config_test.go | 4 ++++ .../config/test-fixtures/config-template-full.hcl | 5 +++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 changelog/16231.txt diff --git a/changelog/16231.txt b/changelog/16231.txt new file mode 100644 index 000000000..aa979d1f8 --- /dev/null +++ b/changelog/16231.txt @@ -0,0 +1,3 @@ +```release-note:bug +agent/template: Fix parsing error for the exec stanza +``` diff --git a/command/agent/config/config.go b/command/agent/config/config.go index 8a28dcf63..b1cad8d84 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 c9728543c..38ca2fb72 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 ee05c2309..5e5cbc62c 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