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

Add LIST endpoint to AWS Secrets static roles #29842

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions builtin/logical/aws/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Backend(_ *logical.BackendConfig) *backend {
pathRoles(&b),
pathListRoles(&b),
pathStaticRoles(&b),
pathListStaticRoles(&b),
pathStaticCredentials(&b),
pathUser(&b),
},
Expand Down
28 changes: 28 additions & 0 deletions builtin/logical/aws/path_static_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ func pathStaticRoles(b *backend) *framework.Path {
}
}

func pathListStaticRoles(b *backend) *framework.Path {
return &framework.Path{
Pattern: fmt.Sprintf("%s/?$", pathStaticRole),
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathStaticRolesList,
},
},
HelpSynopsis: pathStaticRolesHelpSyn,
HelpDescription: pathStaticRolesHelpDesc,
}
}

func (b *backend) pathStaticRolesRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName, ok := data.GetOk(paramRoleName)
if !ok {
Expand Down Expand Up @@ -289,6 +302,21 @@ func (b *backend) pathStaticRolesDelete(ctx context.Context, req *logical.Reques
return nil, req.Storage.Delete(ctx, formatRoleStoragePath(roleName.(string)))
}

func (b *backend) pathStaticRolesList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.roleMutex.RLock()
defer b.roleMutex.RUnlock()

roles, err := req.Storage.List(ctx, pathStaticRole+"/")
if err != nil {
return nil, fmt.Errorf("error listing static roles at %s: %w", pathStaticRole, err)
}
if len(roles) == 0 {
return &logical.Response{Data: map[string]interface{}{}}, nil
}

return logical.ListResponse(roles), nil
}

func (b *backend) validateRoleName(name string) error {
if name == "" {
return errors.New("empty role name attribute given")
Expand Down
56 changes: 56 additions & 0 deletions builtin/logical/aws/path_static_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package aws
import (
"context"
"errors"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -533,6 +534,61 @@ func TestStaticRoleDelete(t *testing.T) {
}
}

// TestStaticRolesList validates that we can list all the static roles in the storage backend.
func TestStaticRolesList(t *testing.T) {
config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
bgCTX := context.Background()

staticRoles := []staticRoleEntry{}
for i := 1; i <= 10; i++ {
roles := staticRoleEntry{
Name: "testrole" + strconv.Itoa(i),
Username: "jane-doe",
RotationPeriod: 24 * time.Hour,
}
staticRoles = append(staticRoles, roles)
}

for _, role := range staticRoles {
entry, err := logical.StorageEntryJSON(formatRoleStoragePath(role.Name), role)
if err != nil {
t.Fatalf("failed to create storage entry for %s: %v", role.Name, err)
}
err = config.StorageView.Put(bgCTX, entry)
if err != nil {
t.Fatalf("failed to store role %s: %v", role.Name, err)
}
}

b := Backend(config)
resp, err := b.HandleRequest(bgCTX, &logical.Request{
Operation: logical.ListOperation,
Path: "static-roles",
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: listing roles failed. resp:%#v\n err:%v", resp, err)
}

if len(resp.Data["keys"].([]string)) != 10 {
t.Fatalf("failed to list all 10 roles")
}

resp, err = b.HandleRequest(bgCTX, &logical.Request{
Operation: logical.ListOperation,
Path: "static-roles/",
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: listing roles failed. resp:%#v\n err:%v", resp, err)
}

if len(resp.Data["keys"].([]string)) != 10 {
t.Fatalf("failed to list all 10 roles")
}
}

func staticRoleFieldData(data map[string]interface{}) *framework.FieldData {
schema := map[string]*framework.FieldSchema{
paramRoleName: {
Expand Down
3 changes: 3 additions & 0 deletions changelog/29842.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
secrets/aws: Add LIST endpoint to the AWS secrets engine static roles.
```
27 changes: 27 additions & 0 deletions website/content/api-docs/secret/aws.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,33 @@ $ curl \
}
```

## List static roles

Use the list static roles endpoint to fetch all existing static roles in the secrets engine.

| Method | Path |
| :----- | :------------------ |
| `LIST` | `/aws/static-roles` |

### Sample request

```shell-session
$ curl
--header "X-Vault-Token: ..." \
--request LIST \
http://127.0.0.1:8200/v1/aws/static-roles
```

### Sample response

```json
{
"data": {
"keys": ["example-role"]
}
}
```

## Delete static role

This endpoint deletes the static role definition. The user, having been defined externally,
Expand Down
Loading