Skip to content

Commit

Permalink
Fix panic when running capabilities CLI command with multiple paths (h…
Browse files Browse the repository at this point in the history
…ashicorp#4553)

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

Fixes hashicorp#4552

* Add test
  • Loading branch information
jefferai authored May 11, 2018
1 parent e80234b commit cb54688
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/sys_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
4 changes: 4 additions & 0 deletions command/token_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
17 changes: 17 additions & 0 deletions command/token_capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit cb54688

Please sign in to comment.