2015-04-18 22:58:08 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-01-04 21:09:16 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2016-03-07 23:35:07 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-04-18 22:58:08 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccAWSIAMRolePolicy_basic(t *testing.T) {
|
2016-03-07 23:35:07 +01:00
|
|
|
role := acctest.RandString(10)
|
|
|
|
policy1 := acctest.RandString(10)
|
|
|
|
policy2 := acctest.RandString(10)
|
|
|
|
|
2015-04-18 22:58:08 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckIAMRolePolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-03-07 23:35:07 +01:00
|
|
|
Config: testAccIAMRolePolicyConfig(role, policy1),
|
2015-04-18 22:58:08 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckIAMRolePolicy(
|
|
|
|
"aws_iam_role.role",
|
|
|
|
"aws_iam_role_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-03-07 23:35:07 +01:00
|
|
|
Config: testAccIAMRolePolicyConfigUpdate(role, policy1, policy2),
|
2015-04-18 22:58:08 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckIAMRolePolicy(
|
|
|
|
"aws_iam_role.role",
|
|
|
|
"aws_iam_role_policy.bar",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:48:42 +01:00
|
|
|
func TestAccAWSIAMRolePolicy_namePrefix(t *testing.T) {
|
|
|
|
role := acctest.RandString(10)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_iam_role_policy.test",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckIAMRolePolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccIAMRolePolicyConfig_namePrefix(role),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckIAMRolePolicy(
|
|
|
|
"aws_iam_role.test",
|
|
|
|
"aws_iam_role_policy.test",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSIAMRolePolicy_generatedName(t *testing.T) {
|
|
|
|
role := acctest.RandString(10)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_iam_role_policy.test",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckIAMRolePolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccIAMRolePolicyConfig_generatedName(role),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckIAMRolePolicy(
|
|
|
|
"aws_iam_role.test",
|
|
|
|
"aws_iam_role_policy.test",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-18 22:58:08 +02:00
|
|
|
func testAccCheckIAMRolePolicyDestroy(s *terraform.State) error {
|
2016-01-04 21:09:16 +01:00
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_iam_role_policy" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:45:09 +02:00
|
|
|
role, name, err := resourceAwsIamRolePolicyParseId(rs.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-04 21:09:16 +01:00
|
|
|
|
|
|
|
request := &iam.GetRolePolicyInput{
|
|
|
|
PolicyName: aws.String(name),
|
|
|
|
RoleName: aws.String(role),
|
|
|
|
}
|
|
|
|
|
|
|
|
getResp, err := iamconn.GetRolePolicy(request)
|
|
|
|
if err != nil {
|
|
|
|
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
|
|
|
|
// none found, that's good
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error reading IAM policy %s from role %s: %s", name, role, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if getResp != nil {
|
|
|
|
return fmt.Errorf("Found IAM Role, expected none: %s", getResp)
|
|
|
|
}
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckIAMRolePolicy(
|
|
|
|
iamRoleResource string,
|
|
|
|
iamRolePolicyResource string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[iamRoleResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", iamRoleResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, ok := s.RootModule().Resources[iamRolePolicyResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", iamRolePolicyResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
|
2016-10-17 13:45:09 +02:00
|
|
|
role, name, err := resourceAwsIamRolePolicyParseId(policy.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = iamconn.GetRolePolicy(&iam.GetRolePolicyInput{
|
2015-04-18 22:58:08 +02:00
|
|
|
RoleName: aws.String(role),
|
|
|
|
PolicyName: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 23:35:07 +01:00
|
|
|
func testAccIAMRolePolicyConfig(role, policy1 string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-04-18 22:58:08 +02:00
|
|
|
resource "aws_iam_role" "role" {
|
2016-08-10 18:18:03 +02:00
|
|
|
name = "tf_test_role_%s"
|
|
|
|
path = "/"
|
2016-08-10 22:34:38 +02:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "foo" {
|
2016-08-10 18:18:03 +02:00
|
|
|
name = "tf_test_policy_%s"
|
|
|
|
role = "${aws_iam_role.role.name}"
|
2016-08-10 22:34:38 +02:00
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": {
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": "*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
2016-03-07 23:35:07 +01:00
|
|
|
`, role, policy1)
|
|
|
|
}
|
2015-04-18 22:58:08 +02:00
|
|
|
|
2017-03-17 17:48:42 +01:00
|
|
|
func testAccIAMRolePolicyConfig_namePrefix(role string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_iam_role" "test" {
|
|
|
|
name = "tf_test_role_%s"
|
|
|
|
path = "/"
|
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "test" {
|
|
|
|
name_prefix = "tf_test_policy_"
|
|
|
|
role = "${aws_iam_role.test.name}"
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": {
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": "*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
`, role)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccIAMRolePolicyConfig_generatedName(role string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_iam_role" "test" {
|
|
|
|
name = "tf_test_role_%s"
|
|
|
|
path = "/"
|
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "test" {
|
|
|
|
role = "${aws_iam_role.test.name}"
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": {
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": "*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
`, role)
|
|
|
|
}
|
|
|
|
|
2016-03-07 23:35:07 +01:00
|
|
|
func testAccIAMRolePolicyConfigUpdate(role, policy1, policy2 string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-04-18 22:58:08 +02:00
|
|
|
resource "aws_iam_role" "role" {
|
2016-08-10 18:18:03 +02:00
|
|
|
name = "tf_test_role_%s"
|
|
|
|
path = "/"
|
2016-08-10 22:34:38 +02:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "foo" {
|
2016-08-10 18:18:03 +02:00
|
|
|
name = "tf_test_policy_%s"
|
|
|
|
role = "${aws_iam_role.role.name}"
|
2016-08-10 22:34:38 +02:00
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": {
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": "*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "bar" {
|
2016-08-10 18:18:03 +02:00
|
|
|
name = "tf_test_policy_2_%s"
|
|
|
|
role = "${aws_iam_role.role.name}"
|
2016-08-10 22:34:38 +02:00
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": {
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": "*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
2015-04-18 22:58:08 +02:00
|
|
|
}
|
2016-03-07 23:35:07 +01:00
|
|
|
`, role, policy1, policy2)
|
|
|
|
}
|