Skip to content

Commit 657c287

Browse files
backport of commit 9ae8f48 (hashicorp#20420)
Co-authored-by: Peter Wilson <peter.wilson@hashicorp.com>
1 parent ba82d7a commit 657c287

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed
 

‎changelog/20418.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
command/server: fixes panic in Vault server command when running in recovery mode
3+
```

‎command/server.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func (c *ServerCommand) runRecoveryMode() int {
446446
}
447447

448448
// Update the 'log' related aspects of shared config based on config/env var/cli
449-
c.Flags().applyLogConfigOverrides(config.SharedConfig)
449+
c.flags.applyLogConfigOverrides(config.SharedConfig)
450450
l, err := c.configureLogging(config)
451451
if err != nil {
452452
c.UI.Error(err.Error())
@@ -661,6 +661,12 @@ func (c *ServerCommand) runRecoveryMode() int {
661661

662662
c.UI.Output("")
663663

664+
// Tests might not want to start a vault server and just want to verify
665+
// the configuration.
666+
if c.flagTestVerifyOnly {
667+
return 0
668+
}
669+
664670
for _, ln := range lns {
665671
handler := vaulthttp.Handler.Handler(&vault.HandlerProperties{
666672
Core: core,

‎command/server_test.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ func TestServer(t *testing.T) {
283283
0,
284284
[]string{"-test-verify-only"},
285285
},
286+
{
287+
"recovery_mode",
288+
testBaseHCL(t, "") + inmemHCL,
289+
"",
290+
0,
291+
[]string{"-test-verify-only", "-recovery"},
292+
},
286293
}
287294

288295
for _, tc := range cases {
@@ -292,26 +299,21 @@ func TestServer(t *testing.T) {
292299
t.Parallel()
293300

294301
ui, cmd := testServerCommand(t)
295-
f, err := ioutil.TempFile("", "")
296-
if err != nil {
297-
t.Fatalf("error creating temp dir: %v", err)
298-
}
299-
f.WriteString(tc.contents)
300-
f.Close()
301-
defer os.Remove(f.Name())
302302

303-
args := append(tc.args, "-config", f.Name())
303+
f, err := os.CreateTemp(t.TempDir(), "")
304+
require.NoErrorf(t, err, "error creating temp dir: %v", err)
304305

305-
code := cmd.Run(args)
306-
output := ui.ErrorWriter.String() + ui.OutputWriter.String()
306+
_, err = f.WriteString(tc.contents)
307+
require.NoErrorf(t, err, "cannot write temp file contents")
307308

308-
if code != tc.code {
309-
t.Errorf("expected %d to be %d: %s", code, tc.code, output)
310-
}
309+
err = f.Close()
310+
require.NoErrorf(t, err, "unable to close temp file")
311311

312-
if !strings.Contains(output, tc.exp) {
313-
t.Fatalf("expected %q to contain %q", output, tc.exp)
314-
}
312+
args := append(tc.args, "-config", f.Name())
313+
code := cmd.Run(args)
314+
output := ui.ErrorWriter.String() + ui.OutputWriter.String()
315+
require.Equal(t, tc.code, code, "expected %d to be %d: %s", code, tc.code, output)
316+
require.Contains(t, output, tc.exp, "expected %q to contain %q", output, tc.exp)
315317
})
316318
}
317319
}

0 commit comments

Comments
 (0)