-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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 policy lookup to allow for slashes #18347
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package acl | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/agent" | ||
"github.com/hashicorp/consul/agent/structs" | ||
"github.com/hashicorp/consul/testrpc" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_GetPolicyIDByName_Builtins(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") | ||
|
||
t.Run("global management policy", func(t *testing.T) { | ||
id, err := GetPolicyIDByName(client, structs.ACLPolicyGlobalManagementName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: It would be good to create a list of the policy names to tests, and iterate over that list and test each one. Then, when new builtin policies are added these tests are slightly easier to update. We could even just iterate over the values in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea. I have changed it to a loop |
||
require.NoError(t, err) | ||
require.Equal(t, structs.ACLPolicyGlobalManagementID, id) | ||
}) | ||
|
||
t.Run("global read-only policy", func(t *testing.T) { | ||
id, err := GetPolicyIDByName(client, structs.ACLPolicyGlobalReadOnlyName) | ||
require.NoError(t, err) | ||
require.Equal(t, structs.ACLPolicyGlobalReadOnlyID, id) | ||
}) | ||
} | ||
|
||
func Test_GetPolicyIDFromPartial_Builtins(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") | ||
|
||
t.Run("global management policy", func(t *testing.T) { | ||
id, err := GetPolicyIDFromPartial(client, structs.ACLPolicyGlobalManagementName) | ||
require.NoError(t, err) | ||
require.Equal(t, structs.ACLPolicyGlobalManagementID, id) | ||
}) | ||
|
||
t.Run("global read-only policy", func(t *testing.T) { | ||
id, err := GetPolicyIDFromPartial(client, structs.ACLPolicyGlobalReadOnlyName) | ||
require.NoError(t, err) | ||
require.Equal(t, structs.ACLPolicyGlobalReadOnlyID, id) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor / non-blocking: This could be a map lookup instead of looping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The map is actually ID -> policy, not Name -> policy, which is what it was doing before. But I added an extra line to do a map lookup in case the partialID is actually an ID and we don't have to list them