Backport of fix a panic in the CLI when deleting an acl policy with an unknown name into release/1.16.x (#19691)

* backport of commit e07f4da212b2957e90550b7de11f6a783e0c374b

* backport of commit eecf61b67f4bfe14007d3057ed174fe7c185fac9

---------

Co-authored-by: Dhia Ayachi <dhia@hashicorp.com>
This commit is contained in:
hc-github-team-consul-core 2023-11-20 09:01:28 -06:00 committed by GitHub
parent 6ccc8319c4
commit 0846916941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

3
.changelog/19679.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
CLI: fix a panic when deleting a non existing policy by name.
```

View File

@ -102,6 +102,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
}

View File

@ -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()