Skip to content
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 list on password policies to list the policies containing slashes #23155

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/23155.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core: Fixes list password policy to include those with names containing / characters.
```
6 changes: 4 additions & 2 deletions vault/logical_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3211,11 +3211,13 @@ const (

// handlePoliciesPasswordList returns the list of password policies
func (*SystemBackend) handlePoliciesPasswordList(ctx context.Context, req *logical.Request, data *framework.FieldData) (resp *logical.Response, err error) {
keys, err := req.Storage.List(ctx, "password_policy/")
keys, err := logical.CollectKeysWithPrefix(ctx, req.Storage, "password_policy/")
if err != nil {
return nil, err
}

for i := range keys {
keys[i] = strings.TrimPrefix(keys[i], "password_policy/")
}
return logical.ListResponse(keys), nil
}

Expand Down
60 changes: 60 additions & 0 deletions vault/logical_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4739,6 +4739,66 @@ func TestHandlePoliciesPasswordList(t *testing.T) {
},
},
},
"policy with /": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to see a test with another level of paths, similar to the example included in the associated issue (path/to/policy).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also curious to see what happens if our policy ends in /.

Copy link
Contributor Author

@akshya96 akshya96 Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for these two cases listed above

storage: makeStorage(t,
&logical.StorageEntry{
Key: getPasswordPolicyKey("testpolicy/testpolicy1"),
Value: toJson(t,
passwordPolicyConfig{
HCLPolicy: "length = 18\n" +
"rule \"charset\" {\n" +
" charset=\"ABCDEFGHIJ\"\n" +
"}",
}),
},
),

expectedResp: &logical.Response{
Data: map[string]interface{}{
"keys": []string{"testpolicy/testpolicy1"},
},
},
},
"list path/to/policy": {
storage: makeStorage(t,
&logical.StorageEntry{
Key: getPasswordPolicyKey("path/to/policy"),
Value: toJson(t,
passwordPolicyConfig{
HCLPolicy: "length = 18\n" +
"rule \"charset\" {\n" +
" charset=\"ABCDEFGHIJ\"\n" +
"}",
}),
},
),

expectedResp: &logical.Response{
Data: map[string]interface{}{
"keys": []string{"path/to/policy"},
},
},
},
"policy ending with /": {
storage: makeStorage(t,
&logical.StorageEntry{
Key: getPasswordPolicyKey("path/to/policy/"),
Value: toJson(t,
passwordPolicyConfig{
HCLPolicy: "length = 18\n" +
"rule \"charset\" {\n" +
" charset=\"ABCDEFGHIJ\"\n" +
"}",
}),
},
),

expectedResp: &logical.Response{
Data: map[string]interface{}{
"keys": []string{"path/to/policy/"},
},
},
},
"storage failure": {
storage: new(logical.InmemStorage).FailList(true),

Expand Down