2020-09-15 22:26:56 +00:00
|
|
|
package awsauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam/iamiface"
|
2021-07-21 00:42:00 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/awsutil"
|
2020-09-15 22:26:56 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPathConfigRotateRoot(t *testing.T) {
|
|
|
|
getIAMClient = func(sess *session.Session) iamiface.IAMAPI {
|
|
|
|
return &awsutil.MockIAM{
|
|
|
|
CreateAccessKeyOutput: &iam.CreateAccessKeyOutput{
|
|
|
|
AccessKey: &iam.AccessKey{
|
|
|
|
AccessKeyId: aws.String("fizz2"),
|
|
|
|
SecretAccessKey: aws.String("buzz2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DeleteAccessKeyOutput: &iam.DeleteAccessKeyOutput{},
|
|
|
|
GetUserOutput: &iam.GetUserOutput{
|
|
|
|
User: &iam.User{
|
|
|
|
UserName: aws.String("ellen"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-10-19 14:26:47 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
logical.TestBackendConfig()
|
2020-09-15 22:26:56 +00:00
|
|
|
storage := &logical.InmemStorage{}
|
2021-10-19 14:26:47 +00:00
|
|
|
config.StorageView = storage
|
|
|
|
|
|
|
|
b, err := Backend(config)
|
2020-09-15 22:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConf := &clientConfig{
|
|
|
|
AccessKey: "fizz1",
|
|
|
|
SecretKey: "buzz1",
|
|
|
|
}
|
|
|
|
entry, err := logical.StorageEntryJSON("config/client", clientConf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := storage.Put(ctx, entry); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/rotate-root",
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
resp, err := b.HandleRequest(ctx, req)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\nerr:%v", resp, err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected nil response to represent a 204")
|
|
|
|
}
|
|
|
|
if resp.Data == nil {
|
|
|
|
t.Fatal("expected resp.Data")
|
|
|
|
}
|
|
|
|
if resp.Data["access_key"].(string) != "fizz2" {
|
|
|
|
t.Fatalf("expected new access key buzz2 but received %s", resp.Data["access_key"])
|
|
|
|
}
|
2021-10-19 14:26:47 +00:00
|
|
|
newClientConf, err := b.nonLockedClientConfigEntry(ctx, req.Storage)
|
2022-01-10 13:58:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-10-19 14:26:47 +00:00
|
|
|
if resp.Data["access_key"].(string) != newClientConf.AccessKey {
|
|
|
|
t.Fatalf("expected new access key buzz2 to be saved to storage but receieved %s", clientConf.AccessKey)
|
|
|
|
}
|
2020-09-15 22:26:56 +00:00
|
|
|
}
|