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 reading AWS root/config endpoint #7245

Merged
merged 5 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 31 additions & 0 deletions builtin/logical/aws/path_config_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func pathConfigRoot(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathConfigRootRead,
logical.UpdateOperation: b.pathConfigRootWrite,
},

Expand All @@ -50,6 +51,36 @@ func pathConfigRoot(b *backend) *framework.Path {
}
}

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

entry, err := req.Storage.Get(ctx, "config/root")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}

var config rootConfig

if err := entry.DecodeJSON(&config); err != nil {
return nil, err
}
joelthompson marked this conversation as resolved.
Show resolved Hide resolved

configData := map[string]interface{}{
"access_key": config.AccessKey,
"region": config.Region,
"iam_endpoint": config.IAMEndpoint,
"sts_endpoint": config.STSEndpoint,
"max_retries": config.MaxRetries,
}
return &logical.Response{
Data: configData,
}, nil
}

func (b *backend) pathConfigRootWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
region := data.Get("region").(string)
iamendpoint := data.Get("iam_endpoint").(string)
Expand Down
54 changes: 54 additions & 0 deletions builtin/logical/aws/path_config_root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package aws

import (
"context"
"reflect"
"testing"

"github.com/hashicorp/vault/sdk/logical"
)

func TestBackend_PathConfigRoot(t *testing.T) {
config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}

b := Backend()
if err := b.Setup(context.Background(), config); err != nil {
t.Fatal(err)
}

configData := map[string]interface{}{
"access_key": "AKIAEXAMPLE",
"secret_key": "RandomData",
"region": "us-west-2",
"iam_endpoint": "https://iam.amazonaws.com",
"sts_endpoint": "https://sts.us-west-2.amazonaws.com",
"max_retries": 10,
}

configReq := &logical.Request{
Operation: logical.UpdateOperation,
Storage: config.StorageView,
Path: "config/root",
Data: configData,
}

resp, err := b.HandleRequest(context.Background(), configReq)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: config writing failed: resp:%#v\n err: %v", resp, err)
}

resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Storage: config.StorageView,
Path: "config/root",
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: config reading failed: resp:%#v\n err: %v", resp, err)
}

delete(configData, "secret_key")
if !reflect.DeepEqual(resp.Data, configData) {
t.Errorf("bad: expected to read config root as %#v, got %#v instead", configData, resp.Data)
}
}
31 changes: 31 additions & 0 deletions website/source/api/secret/aws/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ $ curl \
http://127.0.0.1:8200/v1/aws/config/root
```

## Read Root Configuration

This endpoint allows you to read non-secure values that have been configured in the
`config/root` endpoint. In particular, the `secret_key` parameter is never returned.

| Method | Path |
| :--------------------------- | :--------------------- |
| `GET` | `/aws/config/root` |

### Sample Request
```
$ curl
--header "X-Vault-Token: ..." \
http://127.0.0.1:8200/v1/aws/config/root

```

### Sample Response
```json
{
"data": {
"access_key": "AKIAEXAMPLE",
"secret_key": "",
joelthompson marked this conversation as resolved.
Show resolved Hide resolved
"region": "us-west-2",
"iam_endpoint": "https://iam.amazonaws.com",
"sts_endpoint": "https://sts.us-west-2.amazonaws.com",
"max_retries": -1
}
}
```

## Rotate Root IAM Credentials

When you have configured Vault with static credentials, you can use this
Expand Down