2022-02-14 20:26:57 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-07 18:29:51 +00:00
|
|
|
"github.com/hashicorp/vault/version"
|
2022-02-14 20:26:57 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testVersionHistoryCommand(tb testing.TB) (*cli.MockUi, *VersionHistoryCommand) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
return ui, &VersionHistoryCommand{
|
|
|
|
BaseCommand: &BaseCommand{
|
|
|
|
UI: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionHistoryCommand_TableOutput(t *testing.T) {
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ui, cmd := testVersionHistoryCommand(t)
|
|
|
|
cmd.client = client
|
|
|
|
|
|
|
|
code := cmd.Run([]string{})
|
|
|
|
|
|
|
|
if expectedCode := 0; code != expectedCode {
|
|
|
|
t.Fatalf("expected %d to be %d: %s", code, expectedCode, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorString := ui.ErrorWriter.String(); !strings.Contains(errorString, versionTrackingWarning) {
|
|
|
|
t.Errorf("expected %q to contain %q", errorString, versionTrackingWarning)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
|
|
|
|
if !strings.Contains(output, version.Version) {
|
|
|
|
t.Errorf("expected %q to contain version %q", output, version.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionHistoryCommand_JsonOutput(t *testing.T) {
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
stdout := bytes.NewBuffer(nil)
|
|
|
|
stderr := bytes.NewBuffer(nil)
|
|
|
|
runOpts := &RunOptions{
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
|
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{"version-history", "-format", "json"})
|
2022-02-14 20:26:57 +00:00
|
|
|
if format != "json" {
|
|
|
|
t.Fatalf("expected format to be %q, actual %q", "json", format)
|
|
|
|
}
|
|
|
|
|
|
|
|
code := RunCustom(args, runOpts)
|
|
|
|
|
|
|
|
if expectedCode := 0; code != expectedCode {
|
|
|
|
t.Fatalf("expected %d to be %d: %s", code, expectedCode, stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderrString := stderr.String(); !strings.Contains(stderrString, versionTrackingWarning) {
|
|
|
|
t.Errorf("expected %q to contain %q", stderrString, versionTrackingWarning)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdoutBytes := stdout.Bytes()
|
|
|
|
|
|
|
|
if !json.Valid(stdoutBytes) {
|
|
|
|
t.Fatalf("expected output %q to be valid JSON", stdoutBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
var versionHistoryResp map[string]interface{}
|
|
|
|
err := json.Unmarshal(stdoutBytes, &versionHistoryResp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal json from STDOUT, err: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var respData map[string]interface{}
|
|
|
|
var ok bool
|
|
|
|
var keys []interface{}
|
|
|
|
var keyInfo map[string]interface{}
|
|
|
|
|
|
|
|
if respData, ok = versionHistoryResp["data"].(map[string]interface{}); !ok {
|
|
|
|
t.Fatalf("expected data key to be map, actual: %#v", versionHistoryResp["data"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys, ok = respData["keys"].([]interface{}); !ok {
|
|
|
|
t.Fatalf("expected keys to be array, actual: %#v", respData["keys"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyInfo, ok = respData["key_info"].(map[string]interface{}); !ok {
|
|
|
|
t.Fatalf("expected key_info to be map, actual: %#v", respData["key_info"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("expected single version history entry for %q", version.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyInfo[version.Version] == nil {
|
|
|
|
t.Fatalf("expected version %s to be present in key_info, actual: %#v", version.Version, keyInfo)
|
|
|
|
}
|
|
|
|
}
|