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 +``` diff --git a/command/agent/config/config.go b/command/agent/config/config.go index 7a105573da4f..949b2655ec96 100644 --- a/command/agent/config/config.go +++ b/command/agent/config/config.go @@ -640,19 +640,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 27c48d1afef6..35fa2dd955cb 100644 --- a/command/agent/config/config_test.go +++ b/command/agent/config/config_test.go @@ -685,6 +685,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