Skip to content

Commit

Permalink
Merge pull request #3977 from stephencoe/iam_role_mfa_duration
Browse files Browse the repository at this point in the history
Add MaxSesessionDuration to iam_role for federated users
  • Loading branch information
bflad authored Apr 4, 2018
2 parents 6c9ba02 + 3ce0379 commit 06c4a51
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
30 changes: 30 additions & 0 deletions aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsIamRole() *schema.Resource {
Expand Down Expand Up @@ -105,6 +106,13 @@ func resourceAwsIamRole() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"max_session_duration": {
Type: schema.TypeInt,
Optional: true,
Default: 3600,
ValidateFunc: validation.IntBetween(3600, 43200),
},
},
}
}
Expand Down Expand Up @@ -137,6 +145,10 @@ func resourceAwsIamRoleCreate(d *schema.ResourceData, meta interface{}) error {
request.Description = aws.String(v.(string))
}

if v, ok := d.GetOk("max_session_duration"); ok {
request.MaxSessionDuration = aws.Int64(v.(int64))
}

var createResp *iam.CreateRoleOutput
err := resource.Retry(30*time.Second, func() *resource.RetryError {
var err error
Expand Down Expand Up @@ -176,6 +188,9 @@ func resourceAwsIamRoleRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("name", role.RoleName); err != nil {
return err
}
if err := d.Set("max_session_duration", role.MaxSessionDuration); err != nil {
return err
}
if err := d.Set("arn", role.Arn); err != nil {
return err
}
Expand Down Expand Up @@ -239,6 +254,21 @@ func resourceAwsIamRoleUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("max_session_duration") {
roleMaxDurationInput := &iam.UpdateRoleInput{
RoleName: aws.String(d.Id()),
MaxSessionDuration: aws.Int64(int64(d.Get("max_session_duration").(int))),
}
_, err := iamconn.UpdateRole(roleMaxDurationInput)
if err != nil {
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
d.SetId("")
return nil
}
return fmt.Errorf("Error Updating IAM Role (%s) Max Session Duration: %s", d.Id(), err)
}
}

return nil
}

Expand Down
47 changes: 47 additions & 0 deletions aws/resource_aws_iam_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,42 @@ func TestAccAWSIAMRole_force_detach_policies(t *testing.T) {
})
}

func TestAccAWSIAMRole_MaxSessionDuration(t *testing.T) {
var conf iam.GetRoleOutput
rName := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRoleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIAMRoleConfig_MaxSessionDuration(rName, 3700),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists("aws_iam_role.test", &conf),
testAccAddAwsIAMRolePolicy("aws_iam_role.test"),
),
},
{
Config: testAccCheckIAMRoleConfig_MaxSessionDuration(rName, 43201),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists("aws_iam_role.test", &conf),
testAccAddAwsIAMRolePolicy("aws_iam_role.test"),
),
ExpectError: regexp.MustCompile(`.*Max Session Duration: 43201`),
},
{
Config: testAccCheckIAMRoleConfig_MaxSessionDuration(rName, 3599),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists("aws_iam_role.test", &conf),
testAccAddAwsIAMRolePolicy("aws_iam_role.test"),
),
ExpectError: regexp.MustCompile(`.*Max Session Duration: 3599`),
},
},
})
}

func testAccCheckAWSRoleDestroy(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn

Expand Down Expand Up @@ -261,6 +297,17 @@ func testAccAddAwsIAMRolePolicy(n string) resource.TestCheckFunc {
}
}

func testAccCheckIAMRoleConfig_MaxSessionDuration(rName string, maxSessionDuration int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "role" {
name = "test-role-%s"
path = "/"
max_session_duration = 3700
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
}
`, rName)
}

func testAccAWSIAMRoleConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "role" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/iam_role.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ The following arguments are supported:
See [IAM Identifiers](https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html) for more information.
* `description` - (Optional) The description of the role.

* `max_session_duration` - (Optional) The maximum session duration (in seconds) that you want to set for the specified role. If you do not specify a value for this setting, the default maximum of one hour is applied. This setting can have a value from 1 hour to 12 hours.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 06c4a51

Please sign in to comment.