Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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