mirror of
https://github.com/hashicorp/vault.git
synced 2026-05-05 04:16:31 +02:00
Update list command
This commit is contained in:
parent
f93e3e3e70
commit
1047792f2d
130
command/list.go
130
command/list.go
@ -1,97 +1,103 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
"github.com/hashicorp/vault/meta"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
// Ensure we are implementing the right interfaces.
|
||||
var _ cli.Command = (*ListCommand)(nil)
|
||||
var _ cli.CommandAutocomplete = (*ListCommand)(nil)
|
||||
|
||||
// ListCommand is a Command that lists data from the Vault.
|
||||
type ListCommand struct {
|
||||
meta.Meta
|
||||
*BaseCommand
|
||||
}
|
||||
|
||||
func (c *ListCommand) Synopsis() string {
|
||||
return "Lists data or secrets"
|
||||
}
|
||||
|
||||
func (c *ListCommand) Help() string {
|
||||
helpText := `
|
||||
|
||||
Usage: vault list [options] PATH
|
||||
|
||||
Lists data from Vault at the given path. This can be used to list keys in a,
|
||||
given backend.
|
||||
|
||||
List values under the "my-app" folder:
|
||||
|
||||
$ vault list secret/my-app/
|
||||
|
||||
For a full list of examples and paths, please see the documentation that
|
||||
corresponds to the secret backend in use. Not all backends support listing.
|
||||
|
||||
` + c.Flags().Help()
|
||||
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (c *ListCommand) Flags() *FlagSets {
|
||||
return c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
|
||||
}
|
||||
|
||||
func (c *ListCommand) AutocompleteArgs() complete.Predictor {
|
||||
return c.PredictVaultFolders()
|
||||
}
|
||||
|
||||
func (c *ListCommand) AutocompleteFlags() complete.Flags {
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *ListCommand) Run(args []string) int {
|
||||
var format string
|
||||
var err error
|
||||
var secret *api.Secret
|
||||
var flags *flag.FlagSet
|
||||
flags = c.Meta.FlagSet("list", meta.FlagSetDefault)
|
||||
flags.StringVar(&format, "format", "table", "")
|
||||
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := flags.Parse(args); err != nil {
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
args = flags.Args()
|
||||
if len(args) != 1 || len(args[0]) == 0 {
|
||||
c.Ui.Error("list expects one argument")
|
||||
flags.Usage()
|
||||
args = f.Args()
|
||||
path, kvs, err := extractPath(args)
|
||||
if err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
path := args[0]
|
||||
if path[0] == '/' {
|
||||
path = path[1:]
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
path = path + "/"
|
||||
if len(kvs) > 0 {
|
||||
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
|
||||
return 1
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error initializing client: %s", err))
|
||||
c.UI.Error(err.Error())
|
||||
return 2
|
||||
}
|
||||
|
||||
secret, err = client.Logical().List(path)
|
||||
secret, err := client.Logical().List(path)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error reading %s: %s", path, err))
|
||||
return 1
|
||||
c.UI.Error(fmt.Sprintf("Error listing %s: %s", path, err))
|
||||
return 2
|
||||
}
|
||||
if secret == nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"No value found at %s", path))
|
||||
return 1
|
||||
if secret == nil || secret.Data == nil {
|
||||
c.UI.Error(fmt.Sprintf("No value found at %s", path))
|
||||
return 2
|
||||
}
|
||||
|
||||
// If the secret is wrapped, return the wrapped response.
|
||||
if secret.WrapInfo != nil && secret.WrapInfo.TTL != 0 {
|
||||
return OutputSecret(c.Ui, format, secret)
|
||||
return OutputSecret(c.UI, c.flagFormat, secret)
|
||||
}
|
||||
|
||||
if secret.Data["keys"] == nil {
|
||||
c.Ui.Error("No entries found")
|
||||
return 0
|
||||
if _, ok := extractListData(secret); !ok {
|
||||
c.UI.Error(fmt.Sprintf("No entries found at %s", path))
|
||||
return 2
|
||||
}
|
||||
|
||||
return OutputList(c.Ui, format, secret)
|
||||
}
|
||||
|
||||
func (c *ListCommand) Synopsis() string {
|
||||
return "List data or secrets in Vault"
|
||||
}
|
||||
|
||||
func (c *ListCommand) Help() string {
|
||||
helpText :=
|
||||
`
|
||||
Usage: vault list [options] path
|
||||
|
||||
List data from Vault.
|
||||
|
||||
Retrieve a listing of available data. The data returned, if any, is backend-
|
||||
and endpoint-specific.
|
||||
|
||||
General Options:
|
||||
` + meta.GeneralOptionsUsage() + `
|
||||
Read Options:
|
||||
|
||||
-format=table The format for output. By default it is a whitespace-
|
||||
delimited table. This can also be json or yaml.
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
return OutputList(c.UI, c.flagFormat, secret)
|
||||
}
|
||||
|
||||
@ -1,71 +1,150 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/vault/http"
|
||||
"github.com/hashicorp/vault/meta"
|
||||
"github.com/hashicorp/vault/vault"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
core, _, token := vault.TestCoreUnsealed(t)
|
||||
ln, addr := http.TestServer(t, core)
|
||||
defer ln.Close()
|
||||
func testListCommand(tb testing.TB) (*cli.MockUi, *ListCommand) {
|
||||
tb.Helper()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &ReadCommand{
|
||||
Meta: meta.Meta{
|
||||
ClientToken: token,
|
||||
Ui: ui,
|
||||
ui := cli.NewMockUi()
|
||||
return ui, &ListCommand{
|
||||
BaseCommand: &BaseCommand{
|
||||
UI: ui,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestListCommand_Run(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
args []string
|
||||
out string
|
||||
code int
|
||||
}{
|
||||
{
|
||||
"empty",
|
||||
nil,
|
||||
"Missing PATH!",
|
||||
1,
|
||||
},
|
||||
{
|
||||
"slash",
|
||||
[]string{"/"},
|
||||
"Missing PATH!",
|
||||
1,
|
||||
},
|
||||
{
|
||||
"not_found",
|
||||
[]string{"nope/not/once/never"},
|
||||
"",
|
||||
2,
|
||||
},
|
||||
{
|
||||
"default",
|
||||
[]string{"secret/list"},
|
||||
"bar\nbaz\nfoo",
|
||||
0,
|
||||
},
|
||||
{
|
||||
"default_slash",
|
||||
[]string{"secret/list/"},
|
||||
"bar\nbaz\nfoo",
|
||||
0,
|
||||
},
|
||||
{
|
||||
"format",
|
||||
[]string{
|
||||
"-format", "json",
|
||||
"secret/list/",
|
||||
},
|
||||
"[",
|
||||
0,
|
||||
},
|
||||
{
|
||||
"format_bad",
|
||||
[]string{
|
||||
"-format", "nope-not-real",
|
||||
"secret/list/",
|
||||
},
|
||||
"Invalid output format",
|
||||
1,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-address", addr,
|
||||
"-format", "json",
|
||||
"secret",
|
||||
}
|
||||
t.Run("validations", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Run once so the client is setup, ignore errors
|
||||
c.Run(args)
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
|
||||
// Get the client so we can write data
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
data := map[string]interface{}{"value": "bar"}
|
||||
if _, err := client.Logical().Write("secret/foo", data); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
client, closer := testVaultServer(t)
|
||||
defer closer()
|
||||
|
||||
data = map[string]interface{}{"value": "bar"}
|
||||
if _, err := client.Logical().Write("secret/foo/bar", data); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
keys := []string{
|
||||
"secret/list/foo",
|
||||
"secret/list/bar",
|
||||
"secret/list/baz",
|
||||
}
|
||||
for _, k := range keys {
|
||||
if _, err := client.Logical().Write(k, map[string]interface{}{
|
||||
"foo": "bar",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
secret, err := client.Logical().List("secret/")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
ui, cmd := testListCommand(t)
|
||||
cmd.client = client
|
||||
|
||||
if secret == nil {
|
||||
t.Fatalf("err: No value found at secret/")
|
||||
}
|
||||
code := cmd.Run(tc.args)
|
||||
if code != tc.code {
|
||||
t.Errorf("expected %d to be %d", code, tc.code)
|
||||
}
|
||||
|
||||
if secret.Data == nil {
|
||||
t.Fatalf("err: Data not found")
|
||||
}
|
||||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
||||
if !strings.Contains(combined, tc.out) {
|
||||
t.Errorf("expected %q to contain %q", combined, tc.out)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
exp := map[string]interface{}{
|
||||
"keys": []interface{}{"foo", "foo/"},
|
||||
}
|
||||
t.Run("communication_failure", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if !reflect.DeepEqual(secret.Data, exp) {
|
||||
t.Fatalf("err: expected %#v, got %#v", exp, secret.Data)
|
||||
}
|
||||
client, closer := testVaultServerBad(t)
|
||||
defer closer()
|
||||
|
||||
ui, cmd := testListCommand(t)
|
||||
cmd.client = client
|
||||
|
||||
code := cmd.Run([]string{
|
||||
"secret/list",
|
||||
})
|
||||
if exp := 2; code != exp {
|
||||
t.Errorf("expected %d to be %d", code, exp)
|
||||
}
|
||||
|
||||
expected := "Error listing secret/list: "
|
||||
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()
|
||||
|
||||
_, cmd := testListCommand(t)
|
||||
assertNoTabs(t, cmd)
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user