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

Allow empty permissions boundary field in AWS role. #5740

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func resourceAwsIamRole() *schema.Resource {
"permissions_boundary": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(20, 2048),
ValidateFunc: validateMaxLength(2048),
},

"description": {
Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_iam_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func TestAccAWSIAMRole_PermissionsBoundary(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "permissions_boundary", permissionsBoundary1),
testAccCheckAWSRolePermissionsBoundary(resourceName, permissionsBoundary1),
),
},
// Test update
Expand All @@ -250,6 +251,7 @@ func TestAccAWSIAMRole_PermissionsBoundary(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "permissions_boundary", permissionsBoundary2),
testAccCheckAWSRolePermissionsBoundary(resourceName, permissionsBoundary2),
),
},
// Test import
Expand All @@ -265,6 +267,7 @@ func TestAccAWSIAMRole_PermissionsBoundary(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "permissions_boundary", ""),
testAccCheckAWSRolePermissionsBoundary(resourceName, ""),
),
},
// Test addition
Expand All @@ -273,6 +276,16 @@ func TestAccAWSIAMRole_PermissionsBoundary(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "permissions_boundary", permissionsBoundary1),
testAccCheckAWSRolePermissionsBoundary(resourceName, permissionsBoundary1),
),
},
// Test empty value
{
Config: testAccCheckIAMRoleConfig_PermissionsBoundary(rName, ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "permissions_boundary", ""),
testAccCheckAWSRolePermissionsBoundary(resourceName, ""),
),
},
},
Expand Down Expand Up @@ -399,6 +412,41 @@ func testAccAddAwsIAMRolePolicy(n string) resource.TestCheckFunc {
}
}

func testAccCheckAWSRolePermissionsBoundary(n string, expectedPermissionsBoundaryArn string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Role name is set")
}

iamconn := testAccProvider.Meta().(*AWSClient).iamconn

resp, err := iamconn.GetRole(&iam.GetRoleInput{
mgarstecki marked this conversation as resolved.
Show resolved Hide resolved
RoleName: aws.String(rs.Primary.ID),
})

if err != nil {
return err
}

actualPermissionsBoundaryArn := ""

if resp.Role.PermissionsBoundary != nil {
actualPermissionsBoundaryArn = *resp.Role.PermissionsBoundary.PermissionsBoundaryArn
}

if actualPermissionsBoundaryArn != expectedPermissionsBoundaryArn {
return fmt.Errorf("PermissionsBoundary: '%q', expected '%q'.", actualPermissionsBoundaryArn, expectedPermissionsBoundaryArn)
}

return nil
}
}

func testAccCheckIAMRoleConfig_MaxSessionDuration(rName string, maxSessionDuration int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
Expand Down