Skip to content

Commit 6b3f5f4

Browse files
goruhaDavid ValentincloudpossebotGowiemDavid
authored
port #137 - feat: add possibiblity to use AWS IAM roles for service accounts (#209)
* feat: add possibiblity to use AWS IAM roles for service accounts * Auto Format * Update main.tf --------- Co-authored-by: David Valentin <[email protected]> Co-authored-by: cloudpossebot <[email protected]> Co-authored-by: Matt Gowie <[email protected]> Co-authored-by: David <[email protected]>
1 parent ae94764 commit 6b3f5f4

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Here are automated tests for the complete example using [bats](https://github.co
208208
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
209209
| <a name="input_iam_actions"></a> [iam\_actions](#input\_iam\_actions) | List of actions to allow for the user IAM roles, _e.g._ `es:ESHttpGet`, `es:ESHttpPut`, `es:ESHttpPost` | `list(string)` | `[]` | no |
210210
| <a name="input_iam_authorizing_role_arns"></a> [iam\_authorizing\_role\_arns](#input\_iam\_authorizing\_role\_arns) | List of IAM role ARNs to permit to assume the Elasticsearch user role | `list(string)` | `[]` | no |
211+
| <a name="input_iam_irsa_openid_connect_provider_arn"></a> [iam\_irsa\_openid\_connect\_provider\_arn](#input\_iam\_irsa\_openid\_connect\_provider\_arn) | ARN of the OpenID connect provider to allow usage of IRSA | `string` | `""` | no |
212+
| <a name="input_iam_irsa_openid_connect_provider_url"></a> [iam\_irsa\_openid\_connect\_provider\_url](#input\_iam\_irsa\_openid\_connect\_provider\_url) | URL of the OpenID connect provider to allow usage of IRSA | `string` | `""` | no |
213+
| <a name="input_iam_irsa_service_account"></a> [iam\_irsa\_service\_account](#input\_iam\_irsa\_service\_account) | Kubernetes ServiceAccount to allow to access the Elastic Domain via IRSA | `string` | `"system:serviceaccount:default:*"` | no |
211214
| <a name="input_iam_role_arns"></a> [iam\_role\_arns](#input\_iam\_role\_arns) | List of IAM role ARNs to permit access to the Elasticsearch domain | `list(string)` | `[]` | no |
212215
| <a name="input_iam_role_max_session_duration"></a> [iam\_role\_max\_session\_duration](#input\_iam\_role\_max\_session\_duration) | The maximum session duration (in seconds) for the user role. Can have a value from 1 hour to 12 hours | `number` | `3600` | no |
213216
| <a name="input_iam_role_permissions_boundary"></a> [iam\_role\_permissions\_boundary](#input\_iam\_role\_permissions\_boundary) | The ARN of the permissions boundary policy which will be attached to the Elasticsearch user role | `string` | `null` | no |
@@ -284,6 +287,7 @@ For additional context, refer to some of these links.
284287
- [Control Access to Amazon Elasticsearch Service Domain](https://aws.amazon.com/blogs/security/how-to-control-access-to-your-amazon-elasticsearch-service-domain/) - Describes how to Control Access to Amazon Elasticsearch Service Domain
285288
- [elasticsearch_domain](https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain.html) - Terraform reference documentation for the `elasticsearch_domain` resource
286289
- [elasticsearch_domain_policy](https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain_policy.html) - Terraform reference documentation for the `elasticsearch_domain_policy` resource
290+
- [AWS IAM roles for service accounts](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) - Associate an IAM role with a Kubernetes service account
287291

288292

289293

README.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ references:
109109
- name: "elasticsearch_domain_policy"
110110
description: "Terraform reference documentation for the `elasticsearch_domain_policy` resource"
111111
url: "https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain_policy.html"
112+
- name: "AWS IAM roles for service accounts"
113+
description: "Associate an IAM role with a Kubernetes service account"
114+
url: "https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html"
112115

113116
# Contributors to this project
114117
contributors: []

main.tf

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,46 @@ data "aws_iam_policy_document" "assume_role" {
107107
identifiers = var.aws_ec2_service_name
108108
}
109109

110-
principals {
111-
type = "AWS"
112-
identifiers = compact(concat(var.iam_authorizing_role_arns, var.iam_role_arns))
110+
effect = "Allow"
111+
}
112+
113+
dynamic "statement" {
114+
for_each = length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0 ? [true] : []
115+
116+
content {
117+
effect = "Allow"
118+
119+
actions = [
120+
"sts:AssumeRole"
121+
]
122+
123+
principals {
124+
type = "AWS"
125+
identifiers = compact(concat(var.iam_authorizing_role_arns, var.iam_role_arns))
126+
}
113127
}
128+
}
114129

115-
effect = "Allow"
130+
dynamic "statement" {
131+
for_each = var.iam_irsa_openid_connect_provider_arn != "" ? [true] : []
132+
content {
133+
effect = "Allow"
134+
135+
actions = [
136+
"sts:AssumeRoleWithWebIdentity"
137+
]
138+
139+
principals {
140+
type = "Federated"
141+
identifiers = [var.iam_irsa_openid_connect_provider_arn]
142+
}
143+
144+
condition {
145+
test = "StringLike"
146+
variable = join(":", [var.iam_irsa_openid_connect_provider_url, "sub"])
147+
values = [var.iam_irsa_service_account]
148+
}
149+
}
116150
}
117151
}
118152

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ variable "anonymous_iam_actions" {
124124
description = "List of actions to allow for the anonymous (`*`) IAM roles, _e.g._ `es:ESHttpGet`, `es:ESHttpPut`, `es:ESHttpPost`"
125125
}
126126

127+
variable "iam_irsa_openid_connect_provider_arn" {
128+
type = string
129+
default = ""
130+
description = "ARN of the OpenID connect provider to allow usage of IRSA"
131+
}
132+
133+
variable "iam_irsa_openid_connect_provider_url" {
134+
type = string
135+
default = ""
136+
description = "URL of the OpenID connect provider to allow usage of IRSA"
137+
}
138+
139+
variable "iam_irsa_service_account" {
140+
type = string
141+
default = "system:serviceaccount:default:*"
142+
description = "Kubernetes ServiceAccount to allow to access the Elastic Domain via IRSA"
143+
}
144+
127145
variable "zone_awareness_enabled" {
128146
type = bool
129147
default = true

0 commit comments

Comments
 (0)