1
0

Fix panic when running capabilities CLI command with multiple paths (#4553)

* Fix panic using 'vault token capabilities' with more than one path

Fixes #4552

* Add test
This commit is contained in:
Jeff Mitchell 2018-05-11 11:58:12 -04:00 committed by GitHub
parent e80234b4a7
commit cb54688f59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -34,8 +34,14 @@ func (c *Sys) Capabilities(token, path string) ([]string, error) {
return nil, err
}
if result["capabilities"] == nil {
return nil, nil
}
var capabilities []string
capabilitiesRaw := result["capabilities"].([]interface{})
capabilitiesRaw, ok := result["capabilities"].([]interface{})
if !ok {
return nil, fmt.Errorf("error interpreting returned capabilities")
}
for _, capability := range capabilitiesRaw {
capabilities = append(capabilities, capability.(string))
}

View File

@ -93,6 +93,10 @@ func (c *TokenCapabilitiesCommand) Run(args []string) int {
c.UI.Error(fmt.Sprintf("Error listing capabilities: %s", err))
return 2
}
if capabilities == nil {
c.UI.Error(fmt.Sprintf("No capabilities found"))
return 1
}
switch Format(c.UI) {
case "table":

View File

@ -165,6 +165,23 @@ func TestTokenCapabilitiesCommand_Run(t *testing.T) {
}
})
t.Run("multiple_paths", func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
_, cmd := testTokenCapabilitiesCommand(t)
cmd.client = client
code := cmd.Run([]string{
"secret/foo,secret/bar",
})
if exp := 1; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
})
t.Run("no_tabs", func(t *testing.T) {
t.Parallel()