Skip to content

Commit e9141f6

Browse files
author
David Valentin
committed
feat: add possibiblity to use AWS IAM roles for service accounts
1 parent 7ede088 commit e9141f6

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ Available targets:
246246
| <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 |
247247
| <a name="input_iam_actions"></a> [iam\_actions](#input\_iam\_actions) | List of actions to allow for the IAM roles, _e.g._ `es:ESHttpGet`, `es:ESHttpPut`, `es:ESHttpPost` | `list(string)` | `[]` | no |
248248
| <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 |
249+
| <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 |
250+
| <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 |
251+
| <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 |
249252
| <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 |
250253
| <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 |
251254
| <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 |
@@ -317,6 +320,7 @@ For additional context, refer to some of these links.
317320
- [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
318321
- [elasticsearch_domain](https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain.html) - Terraform reference documentation for the `elasticsearch_domain` resource
319322
- [elasticsearch_domain_policy](https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain_policy.html) - Terraform reference documentation for the `elasticsearch_domain_policy` resource
323+
- [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
320324

321325

322326
## Help

README.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ references:
115115
- name: "elasticsearch_domain_policy"
116116
description: "Terraform reference documentation for the `elasticsearch_domain_policy` resource"
117117
url: "https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain_policy.html"
118+
- name: "AWS IAM roles for service accounts"
119+
description: "Associate an IAM role with a Kubernetes service account"
120+
url: "https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html"
118121

119122
# Contributors to this project
120123
contributors:

docs/terraform.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
| <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 |
8484
| <a name="input_iam_actions"></a> [iam\_actions](#input\_iam\_actions) | List of actions to allow for the IAM roles, _e.g._ `es:ESHttpGet`, `es:ESHttpPut`, `es:ESHttpPost` | `list(string)` | `[]` | no |
8585
| <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 |
86+
| <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 |
87+
| <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 |
88+
| <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 |
8689
| <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 |
8790
| <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 |
8891
| <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 |

main.tf

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ resource "aws_iam_role" "elasticsearch_user" {
8282
}
8383

8484
data "aws_iam_policy_document" "assume_role" {
85-
count = module.this.enabled && (length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0) ? 1 : 0
85+
count = module.this.enabled ? 1 : 0
8686

8787
statement {
8888
actions = [
@@ -94,12 +94,46 @@ data "aws_iam_policy_document" "assume_role" {
9494
identifiers = var.aws_ec2_service_name
9595
}
9696

97-
principals {
98-
type = "AWS"
99-
identifiers = compact(concat(var.iam_authorizing_role_arns, var.iam_role_arns))
97+
effect = "Allow"
98+
}
99+
100+
dynamic "statement" {
101+
for_each = length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0 ? [true] : []
102+
103+
content {
104+
effect = "Allow"
105+
106+
actions = [
107+
"sts:AssumeRole"
108+
]
109+
110+
principals {
111+
type = "AWS"
112+
identifiers = compact(concat(var.iam_authorizing_role_arns, var.iam_role_arns))
113+
}
100114
}
115+
}
101116

102-
effect = "Allow"
117+
dynamic "statement" {
118+
for_each = var.iam_irsa_openid_connect_provider_arn != "" ? [true] : []
119+
content {
120+
effect = "Allow"
121+
122+
actions = [
123+
"sts:AssumeRoleWithWebIdentity"
124+
]
125+
126+
principals {
127+
type = "Federated"
128+
identifiers = compact([var.iam_irsa_openid_connect_provider_arn])
129+
}
130+
131+
condition {
132+
test = "StringLike"
133+
variable = join(":", [var.iam_irsa_openid_connect_provider_url, "sub"])
134+
values = [var.iam_irsa_service_account]
135+
}
136+
}
103137
}
104138
}
105139

@@ -238,7 +272,7 @@ data "aws_iam_policy_document" "default" {
238272
# https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-ip
239273
# https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/
240274
dynamic "statement" {
241-
for_each = length(var.allowed_cidr_blocks) > 0 && ! var.vpc_enabled ? [true] : []
275+
for_each = length(var.allowed_cidr_blocks) > 0 && !var.vpc_enabled ? [true] : []
242276
content {
243277
effect = "Allow"
244278

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ variable "iam_actions" {
106106
description = "List of actions to allow for the IAM roles, _e.g._ `es:ESHttpGet`, `es:ESHttpPut`, `es:ESHttpPost`"
107107
}
108108

109+
variable "iam_irsa_openid_connect_provider_arn" {
110+
type = string
111+
default = ""
112+
description = "ARN of the OpenID connect provider to allow usage of IRSA"
113+
}
114+
115+
variable "iam_irsa_openid_connect_provider_url" {
116+
type = string
117+
default = ""
118+
description = "URL of the OpenID connect provider to allow usage of IRSA"
119+
}
120+
121+
variable "iam_irsa_service_account" {
122+
type = string
123+
default = "system:serviceaccount:default:*"
124+
description = "Kubernetes ServiceAccount to allow to access the Elastic Domain via IRSA"
125+
}
126+
109127
variable "zone_awareness_enabled" {
110128
type = bool
111129
default = true

0 commit comments

Comments
 (0)