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

Add support for Vault agent environment variables | VAULT-16059 #1741

Merged
merged 11 commits into from
May 9, 2023
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
28 changes: 26 additions & 2 deletions config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ type TemplateConfig struct {
// and causes an error if a relative path tries to traverse outside that
// prefix.
SandboxPath *string `mapstructure:"sandbox_path"`

// MapToEnvironmentVariable is the name of the environment variable this
// template should map to. It is currently only used by Vault Agent and
// will be ignored otherwise. When specified, Vault Agent will render the
// contents of this template to the given environment variable instead
// of a file. This field is mutually exclusive with `Destination`.
MapToEnvironmentVariable *string `mapstructure:"-"`
}

// DefaultTemplateConfig returns a configuration that is populated with the
Expand Down Expand Up @@ -184,6 +191,8 @@ func (c *TemplateConfig) Copy() *TemplateConfig {

o.SandboxPath = c.SandboxPath

o.MapToEnvironmentVariable = c.MapToEnvironmentVariable

return &o
}

Expand Down Expand Up @@ -288,6 +297,10 @@ func (c *TemplateConfig) Merge(o *TemplateConfig) *TemplateConfig {
r.SandboxPath = o.SandboxPath
}

if o.MapToEnvironmentVariable != nil {
r.MapToEnvironmentVariable = o.MapToEnvironmentVariable
}

return r
}

Expand Down Expand Up @@ -386,6 +399,10 @@ func (c *TemplateConfig) Finalize() {
} else {
c.FunctionDenylist = combineLists(c.FunctionDenylist, c.FunctionDenylistDeprecated)
}

if c.MapToEnvironmentVariable == nil {
c.MapToEnvironmentVariable = String("")
}
}

// GoString defines the printable version of this struct.
Expand All @@ -410,7 +427,8 @@ func (c *TemplateConfig) GoString() string {
"LeftDelim:%s, "+
"RightDelim:%s, "+
"FunctionDenylist:%s, "+
"SandboxPath:%s"+
"SandboxPath:%s "+
"MapToEnvironmentVariable:%s"+
"}",
BoolGoString(c.Backup),
c.Command,
Expand All @@ -428,6 +446,7 @@ func (c *TemplateConfig) GoString() string {
StringGoString(c.RightDelim),
combineLists(c.FunctionDenylist, c.FunctionDenylistDeprecated),
StringGoString(c.SandboxPath),
StringGoString(c.MapToEnvironmentVariable),
)
}

Expand All @@ -444,9 +463,14 @@ func (c *TemplateConfig) Display() string {
source = String("(dynamic)")
}

destination := c.Destination
if StringPresent(c.MapToEnvironmentVariable) {
destination = c.MapToEnvironmentVariable
}

return fmt.Sprintf("%q => %q",
StringVal(source),
StringVal(c.Destination),
StringVal(destination),
)
}

Expand Down
59 changes: 47 additions & 12 deletions config/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ func TestTemplateConfig_Copy(t *testing.T) {
{
"same_enabled",
&TemplateConfig{
Backup: Bool(true),
Command: []string{"command"},
CommandTimeout: TimeDuration(10 * time.Second),
Contents: String("contents"),
CreateDestDirs: Bool(true),
Destination: String("destination"),
Exec: &ExecConfig{Command: []string{"command"}},
Perms: FileMode(0o600),
Source: String("source"),
Wait: &WaitConfig{Min: TimeDuration(10)},
LeftDelim: String("left_delim"),
RightDelim: String("right_delim"),
Backup: Bool(true),
Command: []string{"command"},
CommandTimeout: TimeDuration(10 * time.Second),
Contents: String("contents"),
CreateDestDirs: Bool(true),
Destination: String("destination"),
Exec: &ExecConfig{Command: []string{"command"}},
Perms: FileMode(0o600),
Source: String("source"),
Wait: &WaitConfig{Min: TimeDuration(10)},
LeftDelim: String("left_delim"),
RightDelim: String("right_delim"),
MapToEnvironmentVariable: String(""),
},
},
}
Expand Down Expand Up @@ -395,6 +396,24 @@ func TestTemplateConfig_Merge(t *testing.T) {
&TemplateConfig{RightDelim: String("right_delim")},
&TemplateConfig{RightDelim: String("right_delim")},
},
{
"map_to_env_var_empty_one",
&TemplateConfig{MapToEnvironmentVariable: String("FOO")},
&TemplateConfig{},
&TemplateConfig{MapToEnvironmentVariable: String("FOO")},
},
{
"map_to_env_var_empty_two",
&TemplateConfig{},
&TemplateConfig{MapToEnvironmentVariable: String("FOO")},
&TemplateConfig{MapToEnvironmentVariable: String("FOO")},
},
{
"map_to_env_var_override",
&TemplateConfig{MapToEnvironmentVariable: String("FOO")},
&TemplateConfig{MapToEnvironmentVariable: String("BAR")},
&TemplateConfig{MapToEnvironmentVariable: String("BAR")},
},
}

for i, tc := range cases {
Expand Down Expand Up @@ -454,6 +473,7 @@ func TestTemplateConfig_Finalize(t *testing.T) {
FunctionDenylist: []string{},
FunctionDenylistDeprecated: []string{},
SandboxPath: String(""),
MapToEnvironmentVariable: String(""),
},
},
}
Expand Down Expand Up @@ -501,6 +521,21 @@ func TestTemplateConfig_Display(t *testing.T) {
},
`"/var/my.tpl" => "/var/my.txt"`,
},
{
"with_environment_variable",
&TemplateConfig{
MapToEnvironmentVariable: String("FOO"),
},
`"" => "FOO"`,
},
{
"with_environment_variable_and_contents",
&TemplateConfig{
MapToEnvironmentVariable: String("FOO"),
Contents: String("hello"),
},
`"(dynamic)" => "FOO"`,
},
}

for i, tc := range cases {
Expand Down