2015-03-20 16:59:48 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-04-19 05:13:12 +00:00
|
|
|
"bytes"
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-04-19 05:13:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-03-20 16:59:48 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 16:26:41 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-08-06 16:37:08 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2015-10-30 22:22:48 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-08-06 16:26:41 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-04-12 02:30:30 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2018-03-13 14:35:10 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/sts"
|
2015-10-22 18:37:12 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2015-03-20 16:59:48 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/logical/testing"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2015-12-01 05:05:04 +00:00
|
|
|
func getBackend(t *testing.T) logical.Backend {
|
2018-01-19 06:44:44 +00:00
|
|
|
be, _ := Factory(context.Background(), logical.TestBackendConfig())
|
2015-12-01 05:05:04 +00:00
|
|
|
return be
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: getBackend(t),
|
2015-03-20 16:59:48 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t),
|
|
|
|
testAccStepWritePolicy(t, "test", testPolicy),
|
|
|
|
testAccStepReadUser(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-08 22:19:53 +00:00
|
|
|
func TestBackend_basicSTS(t *testing.T) {
|
2018-03-13 14:35:10 +00:00
|
|
|
accessKey := &awsAccessKey{}
|
2016-01-08 22:19:53 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
2016-04-12 02:30:30 +00:00
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
2018-03-13 14:35:10 +00:00
|
|
|
createUser(t, accessKey)
|
2016-04-12 02:30:30 +00:00
|
|
|
createRole(t)
|
2018-03-13 14:35:10 +00:00
|
|
|
// Sleep sometime because AWS is eventually consistent
|
|
|
|
// Both the createUser and createRole depend on this
|
|
|
|
log.Println("[WARN] Sleeping for 10 seconds waiting for AWS...")
|
|
|
|
time.Sleep(10 * time.Second)
|
2016-04-12 02:30:30 +00:00
|
|
|
},
|
|
|
|
Backend: getBackend(t),
|
2016-01-08 22:19:53 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2018-03-13 14:35:10 +00:00
|
|
|
testAccStepConfigWithCreds(t, accessKey),
|
2016-01-08 22:19:53 +00:00
|
|
|
testAccStepWritePolicy(t, "test", testPolicy),
|
|
|
|
testAccStepReadSTS(t, "test"),
|
2016-02-23 13:55:43 +00:00
|
|
|
testAccStepWriteArnPolicyRef(t, "test", testPolicyArn),
|
|
|
|
testAccStepReadSTSWithArnPolicy(t, "test"),
|
2016-04-12 02:30:30 +00:00
|
|
|
testAccStepWriteArnRoleRef(t, testRoleName),
|
|
|
|
testAccStepReadSTS(t, testRoleName),
|
2016-01-08 22:19:53 +00:00
|
|
|
},
|
2018-03-13 14:35:10 +00:00
|
|
|
Teardown: func() error {
|
|
|
|
return teardown(accessKey)
|
|
|
|
},
|
2016-01-08 22:19:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:13:12 +00:00
|
|
|
func TestBackend_policyCrud(t *testing.T) {
|
|
|
|
var compacted bytes.Buffer
|
|
|
|
if err := json.Compact(&compacted, []byte(testPolicy)); err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
Backend: getBackend(t),
|
2015-04-19 05:13:12 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t),
|
|
|
|
testAccStepWritePolicy(t, "test", testPolicy),
|
|
|
|
testAccStepReadPolicy(t, "test", compacted.String()),
|
|
|
|
testAccStepDeletePolicy(t, "test"),
|
|
|
|
testAccStepReadPolicy(t, "test", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("AWS_DEFAULT_REGION"); v == "" {
|
|
|
|
log.Println("[INFO] Test: Using us-west-2 as test region")
|
|
|
|
os.Setenv("AWS_DEFAULT_REGION", "us-west-2")
|
|
|
|
}
|
2016-04-12 02:30:30 +00:00
|
|
|
|
|
|
|
if v := os.Getenv("AWS_ACCOUNT_ID"); v == "" {
|
|
|
|
accountId, err := getAccountId()
|
|
|
|
if err != nil {
|
2018-04-13 14:18:06 +00:00
|
|
|
t.Logf("Unable to retrive user via iam:GetUser: %#v", err)
|
|
|
|
t.Skip("AWS_ACCOUNT_ID not explicitly set and could not be read from iam:GetUser for acceptance tests, skipping")
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
log.Printf("[INFO] Test: Used %s as AWS_ACCOUNT_ID", accountId)
|
|
|
|
os.Setenv("AWS_ACCOUNT_ID", accountId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAccountId() (string, error) {
|
|
|
|
awsConfig := &aws.Config{
|
2018-03-13 14:35:10 +00:00
|
|
|
Region: aws.String("us-east-1"),
|
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
2018-03-13 14:35:10 +00:00
|
|
|
svc := sts.New(session.New(awsConfig))
|
2016-04-12 02:30:30 +00:00
|
|
|
|
2018-03-13 14:35:10 +00:00
|
|
|
params := &sts.GetCallerIdentityInput{}
|
|
|
|
res, err := svc.GetCallerIdentity(params)
|
2016-04-12 02:30:30 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-03-13 14:35:10 +00:00
|
|
|
if res == nil {
|
|
|
|
return "", fmt.Errorf("got nil response from GetCallerIdentity")
|
|
|
|
}
|
2016-04-12 02:30:30 +00:00
|
|
|
|
2018-03-13 14:35:10 +00:00
|
|
|
return *res.Account, nil
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const testRoleName = "Vault-Acceptance-Test-AWS-Assume-Role"
|
|
|
|
|
|
|
|
func createRole(t *testing.T) {
|
|
|
|
const testRoleAssumePolicy = `{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect":"Allow",
|
|
|
|
"Principal": {
|
|
|
|
"AWS": "arn:aws:iam::%s:root"
|
|
|
|
},
|
|
|
|
"Action": "sts:AssumeRole"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
awsConfig := &aws.Config{
|
2018-03-13 14:35:10 +00:00
|
|
|
Region: aws.String("us-east-1"),
|
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
svc := iam.New(session.New(awsConfig))
|
|
|
|
trustPolicy := fmt.Sprintf(testRoleAssumePolicy, os.Getenv("AWS_ACCOUNT_ID"))
|
|
|
|
|
|
|
|
params := &iam.CreateRoleInput{
|
|
|
|
AssumeRolePolicyDocument: aws.String(trustPolicy),
|
|
|
|
RoleName: aws.String(testRoleName),
|
|
|
|
Path: aws.String("/"),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] AWS CreateRole: %s", testRoleName)
|
|
|
|
_, err := svc.CreateRole(params)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-12-21 18:08:27 +00:00
|
|
|
t.Fatalf("AWS CreateRole failed: %v", err)
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attachment := &iam.AttachRolePolicyInput{
|
|
|
|
PolicyArn: aws.String(testPolicyArn),
|
|
|
|
RoleName: aws.String(testRoleName), // Required
|
|
|
|
}
|
|
|
|
_, err = svc.AttachRolePolicy(attachment)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-12-21 18:08:27 +00:00
|
|
|
t.Fatalf("AWS CreateRole failed: %v", err)
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:35:10 +00:00
|
|
|
const testUserName = "Vault-Acceptance-Test-AWS-FederationToken"
|
|
|
|
|
|
|
|
func createUser(t *testing.T, accessKey *awsAccessKey) {
|
|
|
|
// The sequence of user creation actions is carefully chosen to minimize
|
|
|
|
// impact of stolen IAM user credentials
|
|
|
|
// 1. Create user, without any permissions or credentials. At this point,
|
|
|
|
// nobody cares if creds compromised because this user can do nothing.
|
|
|
|
// 2. Attach the timebomb policy. This grants no access but puts a time limit
|
|
|
|
// on validitity of compromised credentials. If this fails, nobody cares
|
|
|
|
// because the user has no permissions to do anything anyway
|
|
|
|
// 3. Attach the AdminAccess policy. The IAM user still has no credentials to
|
|
|
|
// do anything
|
|
|
|
// 4. Generate API creds to get an actual access key and secret key
|
|
|
|
timebombPolicyTemplate := `{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Deny",
|
|
|
|
"Action": "*",
|
|
|
|
"Resource": "*",
|
|
|
|
"Condition": {
|
|
|
|
"DateGreaterThan": {
|
|
|
|
"aws:CurrentTime": "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
validity := time.Duration(2 * time.Hour)
|
|
|
|
expiry := time.Now().Add(validity)
|
|
|
|
timebombPolicy := fmt.Sprintf(timebombPolicyTemplate, expiry.Format(time.RFC3339))
|
|
|
|
awsConfig := &aws.Config{
|
|
|
|
Region: aws.String("us-east-1"),
|
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
|
|
|
}
|
|
|
|
svc := iam.New(session.New(awsConfig))
|
|
|
|
|
|
|
|
createUserInput := &iam.CreateUserInput{
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] AWS CreateUser: %s", testUserName)
|
|
|
|
_, err := svc.CreateUser(createUserInput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("AWS CreateUser failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
putPolicyInput := &iam.PutUserPolicyInput{
|
|
|
|
PolicyDocument: aws.String(timebombPolicy),
|
|
|
|
PolicyName: aws.String("SelfDestructionTimebomb"),
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
_, err = svc.PutUserPolicy(putPolicyInput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("AWS PutUserPolicy failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attachUserPolicyInput := &iam.AttachUserPolicyInput{
|
|
|
|
PolicyArn: aws.String("arn:aws:iam::aws:policy/AdministratorAccess"),
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
_, err = svc.AttachUserPolicy(attachUserPolicyInput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("AWS AttachUserPolicy failed, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createAccessKeyInput := &iam.CreateAccessKeyInput{
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
createAccessKeyOutput, err := svc.CreateAccessKey(createAccessKeyInput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("AWS CreateAccessKey failed: %v", err)
|
|
|
|
}
|
|
|
|
if createAccessKeyOutput == nil {
|
|
|
|
t.Fatalf("AWS CreateAccessKey returned nil")
|
|
|
|
}
|
|
|
|
genAccessKey := createAccessKeyOutput.AccessKey
|
|
|
|
|
|
|
|
accessKey.AccessKeyId = *genAccessKey.AccessKeyId
|
|
|
|
accessKey.SecretAccessKey = *genAccessKey.SecretAccessKey
|
|
|
|
}
|
2016-04-12 02:30:30 +00:00
|
|
|
|
2018-03-13 14:35:10 +00:00
|
|
|
func teardown(accessKey *awsAccessKey) error {
|
2016-04-12 02:30:30 +00:00
|
|
|
awsConfig := &aws.Config{
|
2018-03-13 14:35:10 +00:00
|
|
|
Region: aws.String("us-east-1"),
|
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
svc := iam.New(session.New(awsConfig))
|
|
|
|
|
|
|
|
attachment := &iam.DetachRolePolicyInput{
|
|
|
|
PolicyArn: aws.String(testPolicyArn),
|
|
|
|
RoleName: aws.String(testRoleName), // Required
|
|
|
|
}
|
|
|
|
_, err := svc.DetachRolePolicy(attachment)
|
2017-07-07 12:23:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] AWS DetachRolePolicy failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2016-04-12 02:30:30 +00:00
|
|
|
|
|
|
|
params := &iam.DeleteRoleInput{
|
|
|
|
RoleName: aws.String(testRoleName),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] AWS DeleteRole: %s", testRoleName)
|
|
|
|
_, err = svc.DeleteRole(params)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] AWS DeleteRole failed: %v", err)
|
2017-07-07 12:23:12 +00:00
|
|
|
return err
|
2016-04-12 02:30:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 14:35:10 +00:00
|
|
|
userDetachment := &iam.DetachUserPolicyInput{
|
|
|
|
PolicyArn: aws.String("arn:aws:iam::aws:policy/AdministratorAccess"),
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
_, err = svc.DetachUserPolicy(userDetachment)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] AWS DetachUserPolicy failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAccessKeyInput := &iam.DeleteAccessKeyInput{
|
|
|
|
AccessKeyId: aws.String(accessKey.AccessKeyId),
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
_, err = svc.DeleteAccessKey(deleteAccessKeyInput)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] AWS DeleteAccessKey failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteUserPolicyInput := &iam.DeleteUserPolicyInput{
|
|
|
|
PolicyName: aws.String("SelfDestructionTimebomb"),
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
_, err = svc.DeleteUserPolicy(deleteUserPolicyInput)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] AWS DeleteUserPolicy failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
deleteUserInput := &iam.DeleteUserInput{
|
|
|
|
UserName: aws.String(testUserName),
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] AWS DeleteUser: %s", testUserName)
|
|
|
|
_, err = svc.DeleteUser(deleteUserInput)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] AWS DeleteUser failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-07 12:23:12 +00:00
|
|
|
return nil
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepConfig(t *testing.T) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-19 05:21:31 +00:00
|
|
|
Path: "config/root",
|
2015-03-20 16:59:48 +00:00
|
|
|
Data: map[string]interface{}{
|
2018-03-13 14:35:10 +00:00
|
|
|
"region": os.Getenv("AWS_DEFAULT_REGION"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepConfigWithCreds(t *testing.T, accessKey *awsAccessKey) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/root",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"region": os.Getenv("AWS_DEFAULT_REGION"),
|
|
|
|
},
|
|
|
|
PreFlight: func(req *logical.Request) error {
|
|
|
|
// Values in Data above get eagerly evaluated due to the testing framework.
|
|
|
|
// In particular, they get evaluated before accessKey gets set by CreateUser
|
|
|
|
// and thus would fail. By moving to a closure in a PreFlight, we ensure that
|
|
|
|
// the creds get evaluated lazily after they've been properly set
|
|
|
|
req.Data["access_key"] = accessKey.AccessKeyId
|
|
|
|
req.Data["secret_key"] = accessKey.SecretAccessKey
|
|
|
|
return nil
|
2015-03-20 16:59:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadUser(t *testing.T, name string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "creds/" + name,
|
2015-03-20 16:59:48 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
|
|
|
// Build a client and verify that the credentials work
|
2015-08-06 16:37:08 +00:00
|
|
|
creds := credentials.NewStaticCredentials(d.AccessKey, d.SecretKey, "")
|
|
|
|
awsConfig := &aws.Config{
|
|
|
|
Credentials: creds,
|
|
|
|
Region: aws.String("us-east-1"),
|
2015-10-22 18:37:12 +00:00
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
2015-08-06 16:37:08 +00:00
|
|
|
}
|
2015-10-30 22:22:48 +00:00
|
|
|
client := ec2.New(session.New(awsConfig))
|
2015-03-20 16:59:48 +00:00
|
|
|
|
|
|
|
log.Printf("[WARN] Verifying that the generated credentials work...")
|
2018-03-13 14:35:10 +00:00
|
|
|
retryCount := 0
|
|
|
|
success := false
|
|
|
|
var err error
|
|
|
|
for !success && retryCount < 10 {
|
|
|
|
_, err = client.DescribeInstances(&ec2.DescribeInstancesInput{})
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
retryCount++
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 14:35:10 +00:00
|
|
|
return err
|
2015-03-20 16:59:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 22:19:53 +00:00
|
|
|
func testAccStepReadSTS(t *testing.T, name string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "sts/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
STSToken string `mapstructure:"security_token"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
|
|
|
// Build a client and verify that the credentials work
|
|
|
|
creds := credentials.NewStaticCredentials(d.AccessKey, d.SecretKey, d.STSToken)
|
|
|
|
awsConfig := &aws.Config{
|
|
|
|
Credentials: creds,
|
|
|
|
Region: aws.String("us-east-1"),
|
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
|
|
|
}
|
|
|
|
client := ec2.New(session.New(awsConfig))
|
|
|
|
|
|
|
|
log.Printf("[WARN] Verifying that the generated credentials work...")
|
|
|
|
_, err := client.DescribeInstances(&ec2.DescribeInstancesInput{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:55:43 +00:00
|
|
|
func testAccStepReadSTSWithArnPolicy(t *testing.T, name string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "sts/" + name,
|
|
|
|
ErrorOk: true,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp.Data["error"] !=
|
2016-04-12 02:30:30 +00:00
|
|
|
"Can't generate STS credentials for a managed policy; use a role to assume or an inline policy instead" {
|
2016-02-23 13:55:43 +00:00
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
func testAccStepWritePolicy(t *testing.T, name string, policy string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "roles/" + name,
|
2015-03-20 16:59:48 +00:00
|
|
|
Data: map[string]interface{}{
|
2015-04-01 00:26:41 +00:00
|
|
|
"policy": testPolicy,
|
2015-03-20 16:59:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:13:12 +00:00
|
|
|
func testAccStepDeletePolicy(t *testing.T, n string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "roles/" + n,
|
2015-04-19 05:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadPolicy(t *testing.T, name string, value string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "roles/" + name,
|
2015-04-19 05:13:12 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if value == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
Policy string `mapstructure:"policy"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Policy != value {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
const testPolicy = `
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "Stmt1426528957000",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": [
|
|
|
|
"ec2:*"
|
|
|
|
],
|
|
|
|
"Resource": [
|
|
|
|
"*"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2015-12-30 18:05:54 +00:00
|
|
|
|
|
|
|
const testPolicyArn = "arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess"
|
|
|
|
|
|
|
|
func testAccStepWriteArnPolicyRef(t *testing.T, name string, arn string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-21 20:04:16 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-12-30 18:05:54 +00:00
|
|
|
Path: "roles/" + name,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"arn": testPolicyArn,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_basicPolicyArnRef(t *testing.T) {
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: getBackend(t),
|
2015-12-30 18:05:54 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t),
|
|
|
|
testAccStepWriteArnPolicyRef(t, "test", testPolicyArn),
|
|
|
|
testAccStepReadUser(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_policyArnCrud(t *testing.T) {
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
Backend: getBackend(t),
|
2015-12-30 18:05:54 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t),
|
|
|
|
testAccStepWriteArnPolicyRef(t, "test", testPolicyArn),
|
|
|
|
testAccStepReadArnPolicy(t, "test", testPolicyArn),
|
|
|
|
testAccStepDeletePolicy(t, "test"),
|
|
|
|
testAccStepReadArnPolicy(t, "test", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadArnPolicy(t *testing.T, name string, value string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if value == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
Policy string `mapstructure:"arn"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Policy != value {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 02:30:30 +00:00
|
|
|
|
|
|
|
func testAccStepWriteArnRoleRef(t *testing.T, roleName string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/" + roleName,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"arn": fmt.Sprintf("arn:aws:iam::%s:role/%s", os.Getenv("AWS_ACCOUNT_ID"), roleName),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 14:35:10 +00:00
|
|
|
|
|
|
|
type awsAccessKey struct {
|
|
|
|
AccessKeyId string
|
|
|
|
SecretAccessKey string
|
|
|
|
}
|