Skip to content

Commit

Permalink
backport of commit 9692586
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Aug 24, 2022
1 parent bb5795c commit cb07bc5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
42 changes: 21 additions & 21 deletions command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,34 +1060,34 @@ func TestServer_Reload_TLS_DowngradeFromTLS(t *testing.T) {
func TestServer_Reload_VaultConfig(t *testing.T) {
ci.Parallel(t)

logger := testlog.HCLogger(t)

agentConfig := &Config{
TLSConfig: &config.TLSConfig{},
Vault: &config.VaultConfig{
agent := NewTestAgent(t, t.Name(), func(c *Config) {
c.Server.NumSchedulers = pointer.Of(0)
c.Vault = &config.VaultConfig{
Enabled: pointer.Of(true),
Token: "vault-token",
Namespace: "vault-namespace",
},
}
Addr: "https://vault.consul:8200",
}
})
defer agent.Shutdown()

agent := &Agent{
auditor: &noOpAuditor{},
logger: logger,
config: agentConfig,
newConfig := agent.GetConfig().Copy()
newConfig.Vault = &config.VaultConfig{
Enabled: pointer.Of(true),
Token: "vault-token",
Namespace: "another-namespace",
Addr: "https://vault.consul:8200",
}

newConfig := &Config{
TLSConfig: &config.TLSConfig{},
Vault: &config.VaultConfig{
Enabled: pointer.Of(true),
Token: "vault-token",
Namespace: "vault-namespace",
},
}
sconf, err := convertServerConfig(newConfig)
must.NoError(t, err)
agent.finalizeServerConfig(sconf)

must.NoError(t, agent.Reload(newConfig))
must.Equals(t, agent.config.Vault, newConfig.Vault)
// TODO: the vault client isn't accessible here, and we don't actually
// overwrite the agent's server config on reload. We probably should? See
// tests in nomad/server_test.go for verification of this code path's
// behavior on the VaultClient
must.NoError(t, agent.server.Reload(sconf))
}

func TestServer_ShouldReload_ReturnFalseForNoChanges(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions nomad/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func TestServer_Reload_Vault(t *testing.T) {
if !s1.vault.Running() {
t.Fatalf("Vault client should be running")
}

if s1.vault.GetConfig().Namespace != "nondefault" {
t.Fatalf("Vault client did not get new namespace")
}
}

func connectionReset(msg string) bool {
Expand Down
2 changes: 1 addition & 1 deletion nomad/structs/config/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestVaultConfig_Merge(t *testing.T) {
}
}

func TestVaultConfig_IsEqual(t *testing.T) {
func TestVaultConfig_Equals(t *testing.T) {
ci.Parallel(t)

c1 := &VaultConfig{
Expand Down
11 changes: 11 additions & 0 deletions nomad/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ type VaultClient interface {
// SetConfig updates the config used by the Vault client
SetConfig(config *config.VaultConfig) error

// GetConfig returns a copy of the config used by the Vault client, for
// testing
GetConfig() *config.VaultConfig

// CreateToken takes an allocation and task and returns an appropriate Vault
// Secret
CreateToken(ctx context.Context, a *structs.Allocation, task string) (*vapi.Secret, error)
Expand Down Expand Up @@ -350,6 +354,13 @@ func (v *vaultClient) flush() {
v.tomb = &tomb.Tomb{}
}

// GetConfig returns a copy of this vault client's configuration, for testing.
func (v *vaultClient) GetConfig() *config.VaultConfig {
v.setConfigLock.Lock()
defer v.setConfigLock.Unlock()
return v.config.Copy()
}

// SetConfig is used to update the Vault config being used. A temporary outage
// may occur after calling as it re-establishes a connection to Vault
func (v *vaultClient) SetConfig(config *config.VaultConfig) error {
Expand Down
1 change: 1 addition & 0 deletions nomad/vault_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (v *TestVaultClient) MarkForRevocation(accessors []*structs.VaultAccessor)

func (v *TestVaultClient) Stop() {}
func (v *TestVaultClient) SetActive(enabled bool) {}
func (v *TestVaultClient) GetConfig() *config.VaultConfig { return nil }
func (v *TestVaultClient) SetConfig(config *config.VaultConfig) error { return nil }
func (v *TestVaultClient) Running() bool { return true }
func (v *TestVaultClient) Stats() map[string]string { return map[string]string{} }
Expand Down

0 comments on commit cb07bc5

Please sign in to comment.