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

Add MaxSesessionDuration to iam_role for federated users #3977

Merged
merged 7 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== RUN   TestAccAWSIAMRole_MaxSessionDuration
panic: interface conversion: interface {} is int, not int64

goroutine 227 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsIamRoleCreate(0xc4202c3810, 0x3542900, 0xc4208a0500, 0xc4202c3810, 0x0)
	/Users/bflad/go/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_iam_role.go:149 +0x6da

I will fix this on merge.

}

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- FAIL: TestAccAWSIAMRole_MaxSessionDuration (6.39s)
	testing.go:518: Step 0 error: Check failed: Check 1/2 error: Not found: aws_iam_role.test

The resource is named role in the test configuration. I will fix this on merge.

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