Skip to content

Commit

Permalink
backport of commit c624dc2
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 authored Dec 7, 2023
1 parent 334e7f5 commit da02e25
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/19349.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Fixed a bug that caused the `nomad agent` command to ignore the `VAULT_TOKEN` and `VAULT_NAMESPACE` environment variables
```
4 changes: 4 additions & 0 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ func (c *Command) readConfig() *Config {
return nil
}

// Read Vault configuration for the default cluster again after all
// configuration sources have been merged.
defaultVault = config.defaultVault()

// Check to see if we should read the Vault token from the environment
if defaultVault.Token == "" {
defaultVault.Token = os.Getenv("VAULT_TOKEN")
Expand Down
121 changes: 121 additions & 0 deletions command/agent/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package agent
import (
"math"
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/mitchellh/cli"
"github.com/shoenig/test/must"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -504,3 +506,122 @@ func TestIsValidConfig(t *testing.T) {
})
}
}

func TestCommand_readConfig(t *testing.T) {
// Don't run in parallel since this test modifies environment variables.

configFiles := map[string]string{
"base.hcl": `
data_dir = "/tmp/nomad"
region = "global"
server {
enabled = true
}
client {
enabled = true
}
`,
"vault.hcl": `
data_dir = "/tmp/nomad"
region = "global"
server {
enabled = true
}
client {
enabled = true
}
vault {
token = "token-from-config"
namespace = "ns-from-config"
}
`,
}

configDir := t.TempDir()
for k, v := range configFiles {
err := os.WriteFile(path.Join(configDir, k), []byte(v), 0644)
must.NoError(t, err)
}

testCases := []struct {
name string
args []string
env map[string]string
checkFn func(*testing.T, *Config)
}{
{
name: "vault token and namespace from env var",
args: []string{
"-config", path.Join(configDir, "base.hcl"),
},
env: map[string]string{
"VAULT_TOKEN": "token-from-env",
"VAULT_NAMESPACE": "ns-from-env",
},
checkFn: func(t *testing.T, c *Config) {
must.Eq(t, "token-from-env", c.Vaults[0].Token)
must.Eq(t, "ns-from-env", c.Vaults[0].Namespace)
},
},
{
name: "vault token and namespace from config takes precedence over env var",
args: []string{
"-config", path.Join(configDir, "vault.hcl"),
},
env: map[string]string{
"VAULT_TOKEN": "token-from-env",
"VAULT_NAMESPACE": "ns-from-env",
},
checkFn: func(t *testing.T, c *Config) {
must.Eq(t, "token-from-config", c.Vaults[0].Token)
must.Eq(t, "ns-from-config", c.Vaults[0].Namespace)
},
},
{
name: "vault token and namespace from flag takes precedence over env var and config",
args: []string{
"-config", path.Join(configDir, "vault.hcl"),
"-vault-token", "secret-from-cli",
"-vault-namespace", "ns-from-cli",
},
env: map[string]string{
"VAULT_TOKEN": "secret-from-env",
"VAULT_NAMESPACE": "ns-from-env",
},
checkFn: func(t *testing.T, c *Config) {
must.Eq(t, "secret-from-cli", c.Vaults[0].Token)
must.Eq(t, "ns-from-cli", c.Vaults[0].Namespace)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ui := cli.NewMockUi()
defer func() {
// Print command stderr in case of a failed test case to help
// with debugging.
if t.Failed() {
t.Log(ui.ErrorWriter.String())
}
}()

cmd := &Command{
Ui: ui,
args: tc.args,
}
for k, v := range tc.env {
t.Setenv(k, v)
}

got := cmd.readConfig()
must.NotNil(t, got)
tc.checkFn(t, got)
})
}
}

0 comments on commit da02e25

Please sign in to comment.