From e07f4da212b2957e90550b7de11f6a783e0c374b Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Fri, 17 Nov 2023 14:55:26 -0500 Subject: [PATCH 1/2] fix a panic in the CLI when deleting an acl policy with an unknown name --- command/acl/acl_helpers.go | 4 ++++ command/acl/acl_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/command/acl/acl_helpers.go b/command/acl/acl_helpers.go index 9dd40e9e0ee..a847f979fb6 100644 --- a/command/acl/acl_helpers.go +++ b/command/acl/acl_helpers.go @@ -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 } diff --git a/command/acl/acl_test.go b/command/acl/acl_test.go index c2a46f18b04..cc172b3631d 100644 --- a/command/acl/acl_test.go +++ b/command/acl/acl_test.go @@ -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() From eecf61b67f4bfe14007d3057ed174fe7c185fac9 Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Fri, 17 Nov 2023 15:00:37 -0500 Subject: [PATCH 2/2] add changelog --- .changelog/19679.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/19679.txt diff --git a/.changelog/19679.txt b/.changelog/19679.txt new file mode 100644 index 00000000000..42f681be29d --- /dev/null +++ b/.changelog/19679.txt @@ -0,0 +1,3 @@ +```release-note:bug +CLI: fix a panic when deleting a non existing policy by name. +```