Skip to content

Commit b666996

Browse files
authored
feat: Add additional IAM policy to allow cluster role to use KMS key provided for cluster encryption (#1915)
1 parent c1f9acf commit b666996

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,10 @@ Full contributing [guidelines are covered here](https://github.com/terraform-aws
830830
| [aws_eks_cluster.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_cluster) | resource |
831831
| [aws_eks_identity_provider_config.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_identity_provider_config) | resource |
832832
| [aws_iam_openid_connect_provider.oidc_provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_openid_connect_provider) | resource |
833+
| [aws_iam_policy.cluster_encryption](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
833834
| [aws_iam_policy.cni_ipv6_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
834835
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
836+
| [aws_iam_role_policy_attachment.cluster_encryption](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
835837
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
836838
| [aws_security_group.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
837839
| [aws_security_group.node](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
@@ -846,6 +848,7 @@ Full contributing [guidelines are covered here](https://github.com/terraform-aws
846848

847849
| Name | Description | Type | Default | Required |
848850
|------|-------------|------|---------|:--------:|
851+
| <a name="input_attach_cluster_encryption_policy"></a> [attach\_cluster\_encryption\_policy](#input\_attach\_cluster\_encryption\_policy) | Indicates whether or not to attach an additional policy for the cluster IAM role to utilize the encryption key provided | `bool` | `true` | no |
849852
| <a name="input_cloudwatch_log_group_kms_key_id"></a> [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html) | `string` | `null` | no |
850853
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events. Default retention - 90 days | `number` | `90` | no |
851854
| <a name="input_cluster_additional_security_group_ids"></a> [cluster\_additional\_security\_group\_ids](#input\_cluster\_additional\_security\_group\_ids) | List of additional, externally created security group IDs to attach to the cluster control plane | `list(string)` | `[]` | no |

examples/eks_managed_node_group/main.tf

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ module "eks" {
3131
# IPV6
3232
cluster_ip_family = "ipv6"
3333

34+
# We are using the IRSA created below for permissions
35+
# However, we have to deploy with the policy attached FIRST (when creating a fresh cluster)
36+
# and then turn this off after the cluster/node group is created. Without this initial policy,
37+
# the VPC CNI fails to assign IPs and nodes cannot join the cluster
38+
# See https://github.com/aws/containers-roadmap/issues/1666 for more context
39+
# TODO - remove this policy once AWS releases a managed version similar to AmazonEKS_CNI_Policy (IPv4)
40+
create_cni_ipv6_iam_policy = true
41+
3442
cluster_addons = {
3543
coredns = {
3644
resolve_conflicts = "OVERWRITE"

main.tf

+36-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ resource "aws_eks_cluster" "this" {
3030

3131
content {
3232
provider {
33-
key_arn = encryption_config.value["provider_key_arn"]
33+
key_arn = encryption_config.value.provider_key_arn
3434
}
35-
resources = encryption_config.value["resources"]
35+
resources = encryption_config.value.resources
3636
}
3737
}
3838

@@ -218,6 +218,40 @@ resource "aws_iam_role_policy_attachment" "this" {
218218
role = aws_iam_role.this[0].name
219219
}
220220

221+
# Using separate attachment due to `The "for_each" value depends on resource attributes that cannot be determined until apply`
222+
resource "aws_iam_role_policy_attachment" "cluster_encryption" {
223+
count = var.create && var.attach_cluster_encryption_policy && length(var.cluster_encryption_config) > 0 ? 1 : 0
224+
225+
policy_arn = aws_iam_policy.cluster_encryption[0].arn
226+
role = aws_iam_role.this[0].name
227+
}
228+
229+
resource "aws_iam_policy" "cluster_encryption" {
230+
count = var.create && var.attach_cluster_encryption_policy && length(var.cluster_encryption_config) > 0 ? 1 : 0
231+
232+
name_prefix = "${local.iam_role_name}-ClusterEncryption-"
233+
description = "Cluster encryption policy to allow cluster role to utilize CMK provided"
234+
235+
policy = jsonencode({
236+
Version = "2012-10-17"
237+
Statement = [
238+
{
239+
Action = [
240+
"kms:Encrypt",
241+
"kms:Decrypt",
242+
"kms:ListGrants",
243+
"kms:DescribeKey",
244+
]
245+
Effect = "Allow"
246+
# TODO - does cluster_encryption_config need to be a list?!
247+
Resource = [for config in var.cluster_encryption_config : config.provider_key_arn]
248+
},
249+
]
250+
})
251+
252+
tags = var.tags
253+
}
254+
221255
################################################################################
222256
# EKS Addons
223257
################################################################################

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ variable "cluster_encryption_config" {
8989
default = []
9090
}
9191

92+
variable "attach_cluster_encryption_policy" {
93+
description = "Indicates whether or not to attach an additional policy for the cluster IAM role to utilize the encryption key provided"
94+
type = bool
95+
default = true
96+
}
97+
9298
variable "cluster_tags" {
9399
description = "A map of additional tags to add to the cluster"
94100
type = map(string)

0 commit comments

Comments
 (0)