Skip to content

Commit ebc7d4e

Browse files
Backport of Stop use of templated-policy and templated-policy-file simultaneously into release/1.17.x (#19390)
backport of commit 2c5af2f Co-authored-by: Ronald Ekambi <[email protected]>
1 parent ae4d657 commit ebc7d4e

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

.changelog/19389.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
cli: stop simultaneous usage of -templated-policy and -templated-policy-file when creating a role or token.
3+
```

command/acl/role/create/role_create.go

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ func (c *cmd) Run(args []string) int {
9494
return 1
9595
}
9696

97+
if len(c.templatedPolicyFile) != 0 && len(c.templatedPolicy) != 0 {
98+
c.UI.Error("Cannot combine the use of templated-policy flag with templated-policy-file. " +
99+
"To create a role with a single templated policy and simple use case, use -templated-policy. " +
100+
"For multiple templated policies and more complicated use cases, use -templated-policy-file")
101+
return 1
102+
}
103+
97104
client, err := c.http.APIClient()
98105
if err != nil {
99106
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))

command/acl/role/create/role_create_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ func TestRoleCreateCommand_Pretty(t *testing.T) {
115115

116116
require.Len(t, role.NodeIdentities, 1)
117117
})
118+
119+
t.Run("prevent templated-policy and templated-policy-file simultaneous use", func(t *testing.T) {
120+
ui := cli.NewMockUi()
121+
cmd := New(ui)
122+
123+
code := cmd.Run([]string{
124+
"-http-addr=" + a.HTTPAddr(),
125+
"-token=root",
126+
"-name=role-with-node-identity",
127+
"-templated-policy=builtin/node",
128+
"-var=name:" + a.Config.NodeName,
129+
"-templated-policy-file=test.hcl",
130+
})
131+
require.Equal(t, 1, code)
132+
require.Contains(t, ui.ErrorWriter.String(), "Cannot combine the use of templated-policy flag with templated-policy-file.")
133+
})
118134
}
119135

120136
func TestRoleCreateCommand_JSON(t *testing.T) {

command/acl/token/create/token_create.go

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ func (c *cmd) Run(args []string) int {
105105
return 1
106106
}
107107

108+
if len(c.templatedPolicyFile) != 0 && len(c.templatedPolicy) != 0 {
109+
c.UI.Error("Cannot combine the use of templated-policy flag with templated-policy-file. " +
110+
"To create a token with a single templated policy and simple use case, use -templated-policy. " +
111+
"For multiple templated policies and more complicated use cases, use -templated-policy-file")
112+
return 1
113+
}
114+
108115
client, err := c.http.APIClient()
109116
if err != nil {
110117
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))

command/acl/token/create/token_create_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ func TestTokenCreateCommand_Pretty(t *testing.T) {
128128
require.Equal(t, a.Config.NodeName, nodes[0].Node)
129129
})
130130

131+
t.Run("prevent templated-policy and templated-policy-file simultaneous use", func(t *testing.T) {
132+
ui := cli.NewMockUi()
133+
cmd := New(ui)
134+
135+
code := cmd.Run(append([]string{
136+
"-http-addr=" + a.HTTPAddr(),
137+
"-token=root",
138+
"-templated-policy=builtin/node",
139+
"-var=name:" + a.Config.NodeName,
140+
"-templated-policy-file=test.hcl",
141+
}, "-format=json"))
142+
require.Equal(t, 1, code)
143+
require.Contains(t, ui.ErrorWriter.String(), "Cannot combine the use of templated-policy flag with templated-policy-file.")
144+
})
145+
131146
// create with accessor and secret
132147
t.Run("predefined-ids", func(t *testing.T) {
133148
token := run(t, []string{

0 commit comments

Comments
 (0)