2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-14 03:17:55 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-02-15 17:06:37 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-09-05 04:05:27 +00:00
|
|
|
"io/ioutil"
|
2019-02-15 17:06:37 +00:00
|
|
|
"os"
|
2017-09-05 04:05:27 +00:00
|
|
|
"strings"
|
2015-03-14 03:17:55 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func testOperatorUnsealCommand(tb testing.TB) (*cli.MockUi, *OperatorUnsealCommand) {
|
2017-09-05 04:05:27 +00:00
|
|
|
tb.Helper()
|
2015-03-14 03:17:55 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-09-08 02:03:12 +00:00
|
|
|
return ui, &OperatorUnsealCommand{
|
2017-09-05 04:05:27 +00:00
|
|
|
BaseCommand: &BaseCommand{
|
|
|
|
UI: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func TestOperatorUnsealCommand_Run(t *testing.T) {
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("error_non_terminal", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2015-03-14 03:17:55 +00:00
|
|
|
|
2018-03-28 14:34:37 +00:00
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2018-03-28 14:34:37 +00:00
|
|
|
cmd.client = client
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.testOutput = ioutil.Discard
|
|
|
|
|
|
|
|
code := cmd.Run(nil)
|
|
|
|
if exp := 1; code != exp {
|
|
|
|
t.Errorf("expected %d to be %d", code, exp)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
expected := "is not a terminal"
|
|
|
|
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(combined, expected) {
|
|
|
|
t.Errorf("expected %q to contain %q", combined, expected)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2017-09-05 04:05:27 +00:00
|
|
|
})
|
2015-03-14 03:17:55 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
client, keys, closer := testVaultServerUnseal(t)
|
|
|
|
defer closer()
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
// Seal so we can unseal
|
|
|
|
if err := client.Sys().Seal(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
// Enter an unseal key
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Sys().Unseal(keys[0]); err != nil {
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Fatal(err)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.client = client
|
|
|
|
cmd.testOutput = ioutil.Discard
|
|
|
|
|
|
|
|
// Reset and check output
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
"-reset",
|
|
|
|
})
|
|
|
|
if exp := 0; code != exp {
|
|
|
|
t.Errorf("expected %d to be %d", code, exp)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2017-09-22 00:51:12 +00:00
|
|
|
expected := "0/3"
|
2017-09-05 04:05:27 +00:00
|
|
|
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(combined, expected) {
|
|
|
|
t.Errorf("expected %q to contain %q", combined, expected)
|
|
|
|
}
|
|
|
|
})
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Run("full", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, keys, closer := testVaultServerUnseal(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
// Seal so we can unseal
|
|
|
|
if err := client.Sys().Seal(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:51:12 +00:00
|
|
|
for _, key := range keys {
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.client = client
|
|
|
|
cmd.testOutput = ioutil.Discard
|
|
|
|
|
|
|
|
// Reset and check output
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
key,
|
|
|
|
})
|
|
|
|
if exp := 0; code != exp {
|
2017-09-22 00:51:12 +00:00
|
|
|
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
|
2017-09-05 04:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 00:51:12 +00:00
|
|
|
|
2022-04-07 19:12:58 +00:00
|
|
|
status, err := client.Sys().SealStatus()
|
2017-09-22 00:51:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if status.Sealed {
|
|
|
|
t.Error("expected unsealed")
|
|
|
|
}
|
2017-09-05 04:05:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("communication_failure", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServerBad(t)
|
|
|
|
defer closer()
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.client = client
|
|
|
|
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
"abcd",
|
|
|
|
})
|
|
|
|
if exp := 2; code != exp {
|
|
|
|
t.Errorf("expected %d to be %d", code, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Error unsealing: "
|
|
|
|
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(combined, expected) {
|
|
|
|
t.Errorf("expected %q to contain %q", combined, expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no_tabs", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
_, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
assertNoTabs(t, cmd)
|
|
|
|
})
|
2015-04-13 01:39:41 +00:00
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
|
|
|
|
func TestOperatorUnsealCommand_Format(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
os.Setenv(EnvVaultCLINoColor, "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
client, keys, closer := testVaultServerUnseal(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
// Seal so we can unseal
|
|
|
|
if err := client.Sys().Seal(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:45:17 +00:00
|
|
|
stdout := bytes.NewBuffer(nil)
|
|
|
|
stderr := bytes.NewBuffer(nil)
|
|
|
|
runOpts := &RunOptions{
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
Client: client,
|
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
|
Vault CLI: show detailed information with ListResponseWithInfo (#15417)
* CLI: Add ability to display ListResponseWithInfos
The Vault Server API includes a ListResponseWithInfo call, allowing LIST
responses to contain additional information about their keys. This is in
a key=value mapping format (both for each key, to get the additional
metadata, as well as within each metadata).
Expand the `vault list` CLI command with a `-detailed` flag (and env var
VAULT_DETAILED_LISTS) to print this additional metadata. This looks
roughly like the following:
$ vault list -detailed pki/issuers
Keys issuer_name
---- -----------
0cba84d7-bbbe-836a-4ff6-a11b31dc0fb7 n/a
35dfb02d-0cdb-3d35-ee64-d0cd6568c6b0 n/a
382fad1e-e99c-9c54-e147-bb1faa8033d3 n/a
8bb4a793-2ad9-460c-9fa8-574c84a981f7 n/a
8bd231d7-20e2-f21f-ae1a-7aa3319715e7 n/a
9425d51f-cb81-426d-d6ad-5147d092094e n/a
ae679732-b497-ab0d-3220-806a2b9d81ed n/a
c5a44a1f-2ae4-2140-3acf-74b2609448cc utf8
d41d2419-efce-0e36-c96b-e91179a24dc1 something
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Allow detailed printing of LIST responses in JSON
When using the JSON formatter, only the absolute list of keys were
returned. Reuse the `-detailed` flag value for the `-format=json` list
response printer, allowing us to show the complete API response returned
by Vault.
This returns something like the following:
{
"request_id": "e9a25dcd-b67a-97d7-0f08-3670918ef3ff",
"lease_id": "",
"lease_duration": 0,
"renewable": false,
"data": {
"key_info": {
"0cba84d7-bbbe-836a-4ff6-a11b31dc0fb7": {
"issuer_name": ""
},
"35dfb02d-0cdb-3d35-ee64-d0cd6568c6b0": {
"issuer_name": ""
},
"382fad1e-e99c-9c54-e147-bb1faa8033d3": {
"issuer_name": ""
},
"8bb4a793-2ad9-460c-9fa8-574c84a981f7": {
"issuer_name": ""
},
"8bd231d7-20e2-f21f-ae1a-7aa3319715e7": {
"issuer_name": ""
},
"9425d51f-cb81-426d-d6ad-5147d092094e": {
"issuer_name": ""
},
"ae679732-b497-ab0d-3220-806a2b9d81ed": {
"issuer_name": ""
},
"c5a44a1f-2ae4-2140-3acf-74b2609448cc": {
"issuer_name": "utf8"
},
"d41d2419-efce-0e36-c96b-e91179a24dc1": {
"issuer_name": "something"
}
},
"keys": [
"0cba84d7-bbbe-836a-4ff6-a11b31dc0fb7",
"35dfb02d-0cdb-3d35-ee64-d0cd6568c6b0",
"382fad1e-e99c-9c54-e147-bb1faa8033d3",
"8bb4a793-2ad9-460c-9fa8-574c84a981f7",
"8bd231d7-20e2-f21f-ae1a-7aa3319715e7",
"9425d51f-cb81-426d-d6ad-5147d092094e",
"ae679732-b497-ab0d-3220-806a2b9d81ed",
"c5a44a1f-2ae4-2140-3acf-74b2609448cc",
"d41d2419-efce-0e36-c96b-e91179a24dc1"
]
},
"warnings": null
}
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Add changelog
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Use field on UI rather than secret.Data
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Only include headers from visitable key_infos
Certain API endpoints return data from non-visitable key_infos, by
virtue of using a hand-rolled response. Limit our headers to those
from visitable key_infos. This means we won't return entire columns with
n/a entries, if no key matches the key_info key that includes that
header.
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Use setupEnv sourced detailed info
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Fix changelog environment variable
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Fix broken tests using setupEnv
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
2022-05-18 17:00:50 +00:00
|
|
|
args, format, _, _, _ := setupEnv([]string{"operator", "unseal", "-format", "json"})
|
2018-05-23 20:45:17 +00:00
|
|
|
if format != "json" {
|
|
|
|
t.Fatalf("expected %q, got %q", "json", format)
|
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
|
|
|
|
// Unseal with one key
|
2018-05-23 20:45:17 +00:00
|
|
|
code := RunCustom(append(args, []string{
|
2018-02-12 23:12:16 +00:00
|
|
|
keys[0],
|
2018-05-23 20:45:17 +00:00
|
|
|
}...), runOpts)
|
2018-02-12 23:12:16 +00:00
|
|
|
if exp := 0; code != exp {
|
2018-05-23 20:45:17 +00:00
|
|
|
t.Errorf("expected %d to be %d: %s", code, exp, stderr.String())
|
2018-02-12 23:12:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:45:17 +00:00
|
|
|
if !json.Valid(stdout.Bytes()) {
|
2018-02-12 23:12:16 +00:00
|
|
|
t.Error("expected output to be valid JSON")
|
|
|
|
}
|
|
|
|
}
|