Skip to content

Commit b8e2b3b

Browse files
committed
fix: Add condition to IAM assume role policy
1 parent 1ceb4b7 commit b8e2b3b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ module "github_runner" {
215215
| <a name="input_environment_type"></a> [environment\_type](#input\_environment\_type) | Type of build environment to use for related builds. Valid values: `LINUX_CONTAINER`, `LINUX_GPU_CONTAINER`, `WINDOWS_CONTAINER` (deprecated), `WINDOWS_SERVER_2019_CONTAINER`, `ARM_CONTAINER`, `LINUX_LAMBDA_CONTAINER`, `ARM_LAMBDA_CONTAINER` | `string` | `"LINUX_CONTAINER"` | no |
216216
| <a name="input_github_personal_access_token"></a> [github\_personal\_access\_token](#input\_github\_personal\_access\_token) | The GitHub personal access token to use for accessing the repository. If not specified then GitHub auth must be configured separately. | `string` | `null` | no |
217217
| <a name="input_github_personal_access_token_ssm_parameter"></a> [github\_personal\_access\_token\_ssm\_parameter](#input\_github\_personal\_access\_token\_ssm\_parameter) | The GitHub personal access token to use for accessing the repository. If not specified then GitHub auth must be configured separately. | `string` | `null` | no |
218+
| <a name="input_iam_role_assume_role_policy"></a> [iam\_role\_assume\_role\_policy](#input\_iam\_role\_assume\_role\_policy) | The IAM role assume role policy document to use. If not specified then a default is used. | `string` | `null` | no |
218219
| <a name="input_iam_role_name"></a> [iam\_role\_name](#input\_iam\_role\_name) | Name of the IAM role to be used. If not specified then a role will be created | `string` | `null` | no |
219220
| <a name="input_iam_role_permissions_boundary"></a> [iam\_role\_permissions\_boundary](#input\_iam\_role\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the IAM service role | `string` | `null` | no |
220221
| <a name="input_iam_role_policies"></a> [iam\_role\_policies](#input\_iam\_role\_policies) | Map of IAM role policy ARNs to attach to the IAM role | `map(string)` | `{}` | no |

iam.tf

+10-4
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,32 @@ resource "aws_iam_role_policy" "ecr_required" {
135135
}
136136

137137
data "aws_iam_policy_document" "assume_role" {
138-
count = local.create_iam_role ? 1 : 0
139138
statement {
140139
effect = "Allow"
141-
142140
principals {
143141
type = "Service"
144142
identifiers = ["codebuild.amazonaws.com"]
145143
}
146-
147144
actions = ["sts:AssumeRole"]
145+
condition {
146+
test = "StringEquals"
147+
variable = "aws:SourceAccount"
148+
values = [data.aws_caller_identity.current.account_id]
149+
}
148150
}
149151
}
150152

153+
locals {
154+
assume_role_policy = var.iam_role_assume_role_policy == null ? data.aws_iam_policy_document.assume_role.json : var.iam_role_assume_role_policy
155+
}
156+
151157
################################################################################
152158
# Create role
153159
################################################################################
154160
resource "aws_iam_role" "this" {
155161
count = local.create_iam_role ? 1 : 0
156162
name = var.name
157-
assume_role_policy = data.aws_iam_policy_document.assume_role[0].json
163+
assume_role_policy = local.assume_role_policy
158164
permissions_boundary = var.iam_role_permissions_boundary == null ? null : var.iam_role_permissions_boundary
159165
}
160166

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ variable "iam_role_name" {
120120
default = null
121121
}
122122

123+
variable "iam_role_assume_role_policy" {
124+
description = "The IAM role assume role policy document to use. If not specified then a default is used."
125+
type = string
126+
default = null
127+
}
128+
123129
variable "iam_role_policies" {
124130
description = "Map of IAM role policy ARNs to attach to the IAM role"
125131
type = map(string)

0 commit comments

Comments
 (0)