Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent/template: fix exec parsing error for templates #16231

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/16231.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
agent/template: Fix parsing error for the exec stanza
```
11 changes: 8 additions & 3 deletions command/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions command/agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
5 changes: 5 additions & 0 deletions command/agent/config/test-fixtures/config-template-full.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ template {
min = "10s"
max = "40s"
}

exec {
command = ["foo"]
timeout = "10s"
}
}