Skip to content

Commit 50c4cc5

Browse files
authored
Merge pull request #445 from jantman/issues/444
Fixes #444 - Add duration parameter to WithAwsPlugin.withRole()
2 parents 48af506 + d4550dd commit 50c4cc5

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Issue #432](https://github.com/manheim/terraform-pipeline/issues/432) pass TagPlugin through `-var-file={env}-tags.tfvars`
99
- [Issue #417](https://github.com/manheim/terraform-pipeline/issues/417) DestroyPlugin & PassPlanFilePlugin - Terraform Destroy can't be called with a plan file
1010
- [Issue #436](https://github.com/manheim/terraform-pipeline/issues/436) Bug Fix: Omit variables and variable files from apply command if a plan file is specified
11+
- [Issue #444](https://github.com/manheim/terraform-pipeline/issues/444) Expose optional duration parameter on WithAwsPlugin's `withRole()`
1112

1213
# v5.19
1314

docs/WithAwsPlugin.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,15 @@ validate.then(deployQa)
6060
.then(deployProd)
6161
.build()
6262
```
63+
64+
If you want to specify a role session duration other than the default of 1 hour (3600 seconds), you can do so by providing an integer duration to `withDuration()`:
65+
66+
```
67+
WithAwsPlugin.withDuration(43200).init()
68+
```
69+
70+
or, with a specific role ARN
71+
72+
```
73+
WithAwsPlugin.withRole('MY_ROLE_ARN').withDuration(43200).init()
74+
```

src/WithAwsPlugin.groovy

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import static TerraformEnvironmentStage.ALL
22

33
class WithAwsPlugin implements TerraformEnvironmentStagePlugin, Resettable {
44
private static role
5+
private static duration
56

67
public static void init() {
78
WithAwsPlugin plugin = new WithAwsPlugin()
@@ -19,9 +20,10 @@ class WithAwsPlugin implements TerraformEnvironmentStagePlugin, Resettable {
1920
public Closure addWithAwsRole(String environment) {
2021
return { closure ->
2122
String iamRole = getRole(environment)
23+
Integer sessionDuration = getDuration()
2224

2325
if (iamRole != null) {
24-
withAWS(role: iamRole) {
26+
withAWS(role: iamRole, duration: sessionDuration) {
2527
sh "echo Running AWS commands under the role: ${iamRole}"
2628
closure()
2729
}
@@ -38,6 +40,12 @@ class WithAwsPlugin implements TerraformEnvironmentStagePlugin, Resettable {
3840
return this
3941
}
4042

43+
public static withDuration(Integer duration = 3600) {
44+
this.duration = duration
45+
46+
return this
47+
}
48+
4149
public String getRole(String environment) {
4250
def tempRole = this.role
4351

@@ -56,7 +64,18 @@ class WithAwsPlugin implements TerraformEnvironmentStagePlugin, Resettable {
5664
return tempRole
5765
}
5866

67+
public Integer getDuration() {
68+
def tempDuration = this.@duration
69+
70+
if (tempDuration == null) {
71+
tempDuration = 3600
72+
}
73+
74+
return tempDuration
75+
}
76+
5977
public static void reset() {
6078
this.role = null
79+
this.duration = 3600
6180
}
6281
}

test/WithAwsPluginTest.groovy

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,30 @@ class WithAwsPluginTest {
8585
}
8686

8787
@Nested
88-
public class WithExplicitRole {
88+
public class WithDefaultDuration {
8989
@Test
90-
void returnsProvidedRole() {
91-
def expectedRole = "myRole"
90+
void returnsDefaultDuration() {
91+
def expectedDuration = 3600
9292
def plugin = new WithAwsPlugin()
93+
MockJenkinsfile.withEnv(AWS_ROLE_ARN: 'foo')
9394

94-
plugin.withRole(expectedRole)
95-
96-
def actualRole = plugin.getRole()
97-
98-
assertThat(actualRole, is(expectedRole))
99-
}
100-
101-
@Test
102-
void prefersProvidedRoleOverGenericRole() {
103-
def expectedRole = "correctRole"
104-
def plugin = new WithAwsPlugin()
105-
MockJenkinsfile.withEnv(AWS_ROLE_ARN: 'incorrectRole')
106-
107-
plugin.withRole(expectedRole)
108-
109-
def actualRole = plugin.getRole()
110-
111-
assertThat(actualRole, is(expectedRole))
95+
def actualDuration = plugin.getDuration()
96+
assertThat(actualDuration, is(expectedDuration))
11297
}
98+
}
11399

100+
@Nested
101+
public class WithExplicitDuration {
114102
@Test
115-
void prefersProvidedRoleOverEnvironmntSpecificRole() {
116-
def expectedRole = "correctRole"
103+
void returnsExplicitDuration() {
104+
def expectedDuration = 43200
117105
def plugin = new WithAwsPlugin()
118-
MockJenkinsfile.withEnv(QA_AWS_ROLE_ARN: 'incorrectRole')
119106

120-
plugin.withRole(expectedRole)
107+
plugin.withDuration(expectedDuration)
121108

122-
def actualRole = plugin.getRole('qa')
109+
def actualDuration = plugin.getDuration()
123110

124-
assertThat(actualRole, is(expectedRole))
111+
assertThat(actualDuration, is(expectedDuration))
125112
}
126113
}
127114
}
128-

0 commit comments

Comments
 (0)