Skip to content

Commit

Permalink
agent: Fix bug with 'cache' stanza validation (#20934)
Browse files Browse the repository at this point in the history
  • Loading branch information
averche authored Jun 1, 2023
1 parent 8dde8ae commit e4c19ac
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
3 changes: 3 additions & 0 deletions changelog/20934.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
agent: Fix bug with 'cache' stanza validation
```
2 changes: 1 addition & 1 deletion command/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func LoadConfigFile(path string) (*Config, error) {
return nil, fmt.Errorf("error parsing 'env_template': %w", err)
}

if result.Cache != nil && result.APIProxy == nil {
if result.Cache != nil && result.APIProxy == nil && (result.Cache.UseAutoAuthToken || result.Cache.ForceAutoAuthToken) {
result.APIProxy = &APIProxy{
UseAutoAuthToken: result.Cache.UseAutoAuthToken,
ForceAutoAuthToken: result.Cache.ForceAutoAuthToken,
Expand Down
46 changes: 38 additions & 8 deletions command/agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,7 @@ func TestLoadConfigFile_AgentCache_NoAutoAuth(t *testing.T) {
}

expected := &Config{
APIProxy: &APIProxy{},
Cache: &Cache{},
Cache: &Cache{},
SharedConfig: &configutil.SharedConfig{
PidFile: "./pidfile",
Listeners: []*configutil.Listener{
Expand Down Expand Up @@ -935,10 +934,6 @@ func TestLoadConfigFile_AgentCache_AutoAuth_False(t *testing.T) {
},
},
},
APIProxy: &APIProxy{
UseAutoAuthToken: false,
ForceAutoAuthToken: false,
},
Cache: &Cache{
UseAutoAuthToken: false,
UseAutoAuthTokenRaw: "false",
Expand All @@ -959,7 +954,6 @@ func TestLoadConfigFile_AgentCache_Persist(t *testing.T) {
}

expected := &Config{
APIProxy: &APIProxy{},
Cache: &Cache{
Persist: &agentproxyshared.PersistConfig{
Type: "kubernetes",
Expand Down Expand Up @@ -1252,6 +1246,43 @@ func TestLoadConfigFile_Template_NoSinks(t *testing.T) {
}
}

// TestLoadConfigFile_Template_WithCache tests ensures that cache {} stanza is
// permitted in vault agent configuration with template(s)
func TestLoadConfigFile_Template_WithCache(t *testing.T) {
config, err := LoadConfigFile("./test-fixtures/config-template-with-cache.hcl")
if err != nil {
t.Fatalf("err: %s", err)
}

expected := &Config{
SharedConfig: &configutil.SharedConfig{
PidFile: "./pidfile",
},
AutoAuth: &AutoAuth{
Method: &Method{
Type: "aws",
MountPath: "auth/aws",
Namespace: "my-namespace/",
Config: map[string]interface{}{
"role": "foobar",
},
},
},
Cache: &Cache{},
Templates: []*ctconfig.TemplateConfig{
{
Source: pointerutil.StringPtr("/path/on/disk/to/template.ctmpl"),
Destination: pointerutil.StringPtr("/path/on/disk/where/template/will/render.txt"),
},
},
}

config.Prune()
if diff := deep.Equal(config, expected); diff != nil {
t.Fatal(diff)
}
}

func TestLoadConfigFile_Vault_Retry(t *testing.T) {
config, err := LoadConfigFile("./test-fixtures/config-vault-retry.hcl")
if err != nil {
Expand Down Expand Up @@ -1359,7 +1390,6 @@ func TestLoadConfigFile_EnforceConsistency(t *testing.T) {
},
PidFile: "",
},
APIProxy: &APIProxy{},
Cache: &Cache{
EnforceConsistency: "always",
WhenInconsistent: "retry",
Expand Down
22 changes: 22 additions & 0 deletions command/agent/config/test-fixtures/config-template-with-cache.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

pid_file = "./pidfile"

auto_auth {
method {
type = "aws"
namespace = "/my-namespace"

config = {
role = "foobar"
}
}
}

cache {}

template {
source = "/path/on/disk/to/template.ctmpl"
destination = "/path/on/disk/where/template/will/render.txt"
}

0 comments on commit e4c19ac

Please sign in to comment.