From 452ec19a42afe00e06023700b396f668df351760 Mon Sep 17 00:00:00 2001 From: cskh Date: Wed, 1 Mar 2023 14:50:03 -0500 Subject: [PATCH] fix (cli): return error msg if acl policy not found (#16485) * fix: return error msg if acl policy not found * changelog * add test --- .changelog/16485.txt | 3 +++ command/acl/policy/read/policy_read.go | 5 +++++ command/acl/policy/read/policy_read_test.go | 11 +++++++++++ 3 files changed, 19 insertions(+) create mode 100644 .changelog/16485.txt diff --git a/.changelog/16485.txt b/.changelog/16485.txt new file mode 100644 index 000000000..7e1938b00 --- /dev/null +++ b/.changelog/16485.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: fix panic read non-existent acl policy +``` diff --git a/command/acl/policy/read/policy_read.go b/command/acl/policy/read/policy_read.go index 455f5e5f7..57e9397b2 100644 --- a/command/acl/policy/read/policy_read.go +++ b/command/acl/policy/read/policy_read.go @@ -92,6 +92,11 @@ func (c *cmd) Run(args []string) int { return 1 } + if pol == nil { + c.UI.Error(fmt.Sprintf("Error policy not found: %s", c.policyName)) + return 1 + } + formatter, err := policy.NewFormatter(c.format, c.showMeta) if err != nil { c.UI.Error(err.Error()) diff --git a/command/acl/policy/read/policy_read_test.go b/command/acl/policy/read/policy_read_test.go index af5bf1c84..bd8d99dd8 100644 --- a/command/acl/policy/read/policy_read_test.go +++ b/command/acl/policy/read/policy_read_test.go @@ -82,6 +82,17 @@ func TestPolicyReadCommand(t *testing.T) { output = ui.OutputWriter.String() assert.Contains(t, output, fmt.Sprintf("test-policy")) assert.Contains(t, output, policy.ID) + + // Test querying non-existent policy + argsName = []string{ + "-http-addr=" + a.HTTPAddr(), + "-token=root", + "-name=test-policy-not-exist", + } + + cmd = New(ui) + code = cmd.Run(argsName) + assert.Equal(t, code, 1) } func TestPolicyReadCommand_JSON(t *testing.T) {