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 a panic in the CLI when deleting an acl policy with an unknown name #19679

Merged
merged 2 commits into from
Nov 20, 2023
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
3 changes: 3 additions & 0 deletions .changelog/19679.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
CLI: fix a panic when deleting a non existing policy by name.
```
4 changes: 4 additions & 0 deletions command/acl/acl_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func GetPolicyIDByName(client *api.Client, name string) (string, error) {
return "", err
}

if policy == nil {
return "", fmt.Errorf("No such policy with name: %s", name)
}

return policy.ID, nil
}

Expand Down
30 changes: 30 additions & 0 deletions command/acl/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ func Test_GetPolicyIDByName_Builtins(t *testing.T) {
}
}

func Test_GetPolicyIDByName_NotFound(t *testing.T) {
t.Parallel()

a := agent.StartTestAgent(t,
agent.TestAgent{
LogOutput: io.Discard,
HCL: `
primary_datacenter = "dc1"
acl {
enabled = true
tokens {
initial_management = "root"
}
}
`,
},
)

defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1", testrpc.WithToken("root"))

client := a.Client()
client.AddHeader("X-Consul-Token", "root")

id, err := GetPolicyIDByName(client, "not_found")
require.Error(t, err)
require.Equal(t, "", id)

}

func Test_GetPolicyIDFromPartial_Builtins(t *testing.T) {
t.Parallel()

Expand Down