Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 0b83dde

Browse files
AustinAbro321Arun Sannaarunsannadgwhitedbushong1
authored
Update iam policies for csi driver (#9)
* adding tolerations to chart * adding tolerations in a different way * fixing the name for tolerations * fixing tolerations to be in the controller * adding defaults * updating iam policy for the driver * updating to actions * fmt * updating docs * simple code refactor * simple code refactor * adding tolerations to chart * changing back tolerations * fixing service account names * Update iam.tf Co-authored-by: Dustin Whited <dgwhited@gmail.com> * Update variables.tf Co-authored-by: Charles Bushong <bushong1@gmail.com> * adding ingress rules * making the tolerations dynamic * refactoring * fmting --------- Co-authored-by: Arun Sanna <arunsanna@Aruns-MacBook-Pro.local> Co-authored-by: Arun Sanna <arunchowdary.sanna@cms.hhs.gov> Co-authored-by: Dustin Whited <dgwhited@gmail.com> Co-authored-by: Charles Bushong <bushong1@gmail.com>
1 parent bd7eebe commit 0b83dde

File tree

6 files changed

+158
-78
lines changed

6 files changed

+158
-78
lines changed

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# batcave-tf-efs
22
## Requirements
33

4-
* Aws elastic file system should be created and file system id shoud be supplied to this chart
4+
No requirements.
55

66
## Providers
77

@@ -12,29 +12,50 @@
1212

1313
## Modules
1414

15-
No modules.
15+
| Name | Source | Version |
16+
|------|--------|---------|
17+
| <a name="module_iam_assumable_role_admin"></a> [iam\_assumable\_role\_admin](#module\_iam\_assumable\_role\_admin) | terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc | n/a |
1618

1719
## Resources
1820

1921
| Name | Type |
2022
|------|------|
23+
| [aws_efs_backup_policy.policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/efs_backup_policy) | resource |
24+
| [aws_efs_file_system.efs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/efs_file_system) | resource |
25+
| [aws_efs_mount_target.efs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/efs_mount_target) | resource |
2126
| [aws_iam_policy.batcave_efscsidriver](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
22-
| [aws_iam_role_policy_attachment.efsscidriver_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
23-
| [helm_release.aws_efs_csi_diver](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
24-
| [aws_eks_cluster.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster) | data source |
27+
| [aws_security_group.efs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
28+
| [helm_release.aws-efs-csi-driver](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
29+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
30+
| [aws_eks_cluster_auth.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source |
31+
| [aws_iam_policy_document.batcave_efscsidriver](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
32+
| [aws_kms_key.efs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
2533

2634
## Inputs
2735

2836
| Name | Description | Type | Default | Required |
2937
|------|-------------|------|---------|:--------:|
38+
| <a name="input_cluster_certificate_authority_data"></a> [cluster\_certificate\_authority\_data](#input\_cluster\_certificate\_authority\_data) | n/a | `any` | n/a | yes |
39+
| <a name="input_cluster_endpoint"></a> [cluster\_endpoint](#input\_cluster\_endpoint) | n/a | `any` | n/a | yes |
3040
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | n/a | `any` | n/a | yes |
41+
| <a name="input_cluster_oidc_issuer_url"></a> [cluster\_oidc\_issuer\_url](#input\_cluster\_oidc\_issuer\_url) | n/a | `any` | n/a | yes |
42+
| <a name="input_efsid"></a> [efsid](#input\_efsid) | n/a | `string` | `""` | no |
43+
| <a name="input_helm_name"></a> [helm\_name](#input\_helm\_name) | n/a | `string` | `"aws-efs-csi-driver"` | no |
3144
| <a name="input_helm_namespace"></a> [helm\_namespace](#input\_helm\_namespace) | ## Helm variables | `string` | `"kube-system"` | no |
3245
| <a name="input_iam_path"></a> [iam\_path](#input\_iam\_path) | n/a | `string` | `"/delegatedadmin/developer/"` | no |
33-
| <a name="input_permissions_boundary"></a> [permissions\_boundary](#input\_permissions\_boundary) | n/a | `string` | `"arn:aws:iam::373346310182:policy/cms-cloud-admin/developer-boundary-policy"` | no |
34-
| <a name="input_provider_url"></a> [provider\_url](#input\_provider\_url) | n/a | `string` | `""` | no |
35-
| <a name="input_efsid"></a> efsid | n/a | `elasticfilesystem` | n/a | yes |
36-
46+
| <a name="input_imagerepo"></a> [imagerepo](#input\_imagerepo) | # Image repo | `string` | `"602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/aws-efs-csi-driver"` | no |
47+
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | n/a | `string` | `""` | no |
48+
| <a name="input_permissions_boundary"></a> [permissions\_boundary](#input\_permissions\_boundary) | n/a | `string` | `""` | no |
49+
| <a name="input_private_subnet_ids"></a> [private\_subnet\_ids](#input\_private\_subnet\_ids) | n/a | `list(any)` | `[]` | no |
50+
| <a name="input_toleration_effect"></a> [toleration\_effect](#input\_toleration\_effect) | toleration effect | `string` | `""` | no |
51+
| <a name="input_toleration_key"></a> [toleration\_key](#input\_toleration\_key) | toleration key | `string` | `""` | no |
52+
| <a name="input_toleration_operator"></a> [toleration\_operator](#input\_toleration\_operator) | toleration operator | `string` | `""` | no |
53+
| <a name="input_toleration_value"></a> [toleration\_value](#input\_toleration\_value) | toleration value | `string` | `""` | no |
54+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | n/a | `string` | `""` | no |
3755

3856
## Outputs
3957

40-
No outputs.
58+
| Name | Description |
59+
|------|-------------|
60+
| <a name="output_oidc_iam_role_arn"></a> [oidc\_iam\_role\_arn](#output\_oidc\_iam\_role\_arn) | n/a |
61+
<!-- END_TF_DOCS -->

csi-efs.tf

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ resource "helm_release" "aws-efs-csi-driver" {
1111
}
1212
set {
1313
name = "controller.serviceAccount.name"
14-
value = "efs-csi-controller-sa"
14+
value = local.controller_service_account_name
15+
}
16+
set {
17+
name = "node.serviceAccount.name"
18+
value = local.node_service_account_name
1519
}
1620
set {
1721
name = "controller.serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
@@ -68,20 +72,32 @@ resource "helm_release" "aws-efs-csi-driver" {
6872
name = "storageClasses[0].volumeBindingMode"
6973
value = "Immediate"
7074
}
71-
set {
72-
name = "controller.tolerations[0].key"
73-
value = var.toleration_key
74-
}
75-
set {
76-
name = "controller.tolerations[0].value"
77-
value = var.toleration_value
78-
}
79-
set {
80-
name = "controller.tolerations[0].operator"
81-
value = var.toleration_operator
82-
}
83-
set {
84-
name = "controller.tolerations[0].effect"
85-
value = var.toleration_effect
75+
dynamic "set" {
76+
for_each = var.tolerations
77+
content {
78+
name = "controller.tolerations[${set.key}].key"
79+
value = set.value.key
80+
}
81+
}
82+
dynamic "set" {
83+
for_each = var.tolerations
84+
content {
85+
name = "controller.tolerations[${set.key}].value"
86+
value = try(set.value.value, "")
87+
}
88+
}
89+
dynamic "set" {
90+
for_each = var.tolerations
91+
content {
92+
name = "controller.tolerations[${set.key}].operator"
93+
value = set.value.operator
94+
}
95+
}
96+
dynamic "set" {
97+
for_each = var.tolerations
98+
content {
99+
name = "controller.tolerations[${set.key}].effect"
100+
value = try(set.value.effect, "NoSchedule")
101+
}
86102
}
87103
}

efs.tf

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ resource "aws_security_group" "efs" {
33
vpc_id = var.vpc_id
44
}
55

6+
resource "aws_security_group_rule" "efs_ingress" {
7+
security_group_id = aws_security_group.efs.id
8+
type = "ingress"
9+
from_port = 2049
10+
to_port = 2049
11+
protocol = "tcp"
12+
source_security_group_id = var.worker_security_group_id
13+
}
14+
615
data "aws_kms_key" "efs" {
716
key_id = var.kms_key_id
817
}
@@ -14,23 +23,25 @@ resource "aws_efs_file_system" "efs" {
1423
kms_key_id = data.aws_kms_key.efs.arn
1524

1625
tags = {
17-
Name = "${var.cluster_name}"
26+
Name = var.cluster_name
1827
}
28+
1929
lifecycle_policy {
2030
transition_to_ia = "AFTER_60_DAYS"
2131
}
2232
}
2333

2434
resource "aws_efs_mount_target" "efs" {
25-
for_each = toset(var.private_subnet_ids)
35+
count = length(var.private_subnet_ids)
2636
file_system_id = aws_efs_file_system.efs.id
27-
subnet_id = each.key
37+
subnet_id = var.private_subnet_ids[count.index]
2838
security_groups = [aws_security_group.efs.id]
2939
}
40+
3041
resource "aws_efs_backup_policy" "policy" {
3142
file_system_id = aws_efs_file_system.efs.id
3243

3344
backup_policy {
3445
status = "ENABLED"
3546
}
36-
}
47+
}

iam.tf

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
locals {
2-
k8s_service_account_namespace = "kube-system"
3-
k8s_service_account_name = "aws-efs-csi-driver"
2+
k8s_service_account_namespace = "kube-system"
3+
controller_service_account_name = "efs-csi-controller-sa"
4+
node_service_account_name = "efs-csi-node-sa"
45
}
56

67
data "aws_caller_identity" "current" {}
78

89
data "aws_iam_policy_document" "batcave_efscsidriver" {
10+
# Allow EFS access
911
statement {
12+
effect = "Allow"
1013
actions = [
1114
"elasticfilesystem:DescribeAccessPoints",
1215
"elasticfilesystem:DescribeFileSystems",
16+
"elasticfilesystem:DescribeMountTargets",
17+
"ec2:DescribeAvailabilityZones"
1318
]
14-
1519
resources = ["*"]
16-
1720
}
21+
# Allow creating EFS access points with specific tags
1822
statement {
23+
effect = "Allow"
24+
actions = [
25+
"elasticfilesystem:CreateAccessPoint",
26+
"elasticfilesystem:TagResource"
27+
]
28+
resources = ["*"]
29+
condition {
30+
test = "StringLike"
31+
variable = "aws:RequestTag/efs.csi.aws.com/cluster"
32+
values = ["true"]
33+
}
34+
}
35+
# Allow deleting EFS access points with specific tags
36+
statement {
37+
effect = "Allow"
38+
actions = ["elasticfilesystem:DeleteAccessPoint"]
39+
resources = ["*"]
40+
condition {
41+
test = "StringLike"
42+
variable = "aws:ResourceTag/efs.csi.aws.com/cluster"
43+
values = ["true"]
44+
}
45+
}
46+
# Allow creating and deleting EFS resources with specific ARNs
47+
statement {
48+
effect = "Allow"
1949
actions = [
2050
"elasticfilesystem:CreateAccessPoint",
2151
"elasticfilesystem:DeleteAccessPoint"
@@ -25,21 +55,24 @@ data "aws_iam_policy_document" "batcave_efscsidriver" {
2555
"arn:aws:elasticfilesystem:*:${data.aws_caller_identity.current.account_id}:access-point/*"
2656
]
2757
}
28-
2958
}
59+
3060
resource "aws_iam_policy" "batcave_efscsidriver" {
3161
name = "efscsidriver-policy-${var.cluster_name}"
3262
path = var.iam_path
3363
policy = data.aws_iam_policy_document.batcave_efscsidriver.json
34-
3564
}
65+
3666
module "iam_assumable_role_admin" {
37-
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
38-
create_role = true
39-
role_name = "${var.cluster_name}-cluster-efscsidriver"
40-
provider_url = replace(var.cluster_oidc_issuer_url, "https://", "")
41-
role_policy_arns = [aws_iam_policy.batcave_efscsidriver.arn]
42-
oidc_fully_qualified_subjects = ["system:serviceaccount:${local.k8s_service_account_namespace}:efs-csi-controller-sa", "system:serviceaccount:${local.k8s_service_account_namespace}:efs-csi-node-sa"]
67+
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
68+
create_role = true
69+
role_name = "${var.cluster_name}-cluster-efscsidriver"
70+
provider_url = replace(var.cluster_oidc_issuer_url, "https://", "")
71+
role_policy_arns = [aws_iam_policy.batcave_efscsidriver.arn]
72+
oidc_fully_qualified_subjects = [
73+
"system:serviceaccount:${local.k8s_service_account_namespace}:${local.controller_service_account_name}",
74+
"system:serviceaccount:${local.k8s_service_account_namespace}:${local.node_service_account_name}"
75+
]
4376
role_path = var.iam_path
4477
role_permissions_boundary_arn = var.permissions_boundary
4578
}

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
data "aws_eks_cluster_auth" "cluster" {
22
name = var.cluster_name
33
}
4+
45
provider "helm" {
56
kubernetes {
67
host = var.cluster_endpoint
78
cluster_ca_certificate = base64decode(var.cluster_certificate_authority_data)
89
token = data.aws_eks_cluster_auth.cluster.token
910
}
1011
}
12+
1113
provider "kubernetes" {
1214
host = var.cluster_endpoint
1315
cluster_ca_certificate = base64decode(var.cluster_certificate_authority_data)

variables.tf

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,69 @@
1-
variable "cluster_name" {}
2-
variable "cluster_endpoint" {}
3-
variable "cluster_certificate_authority_data" {}
4-
5-
6-
7-
variable "iam_path" {
8-
default = "/delegatedadmin/developer/"
1+
variable "cluster_name" {
2+
description = "Name of EKS cluster"
93
}
104

11-
variable "permissions_boundary" {
12-
default = ""
13-
## check this value in common.hcl file for dev
14-
#arn:aws:iam::373346310182:policy/cms-cloud-admin/developer-boundary-policy
5+
variable "cluster_endpoint" {
6+
description = "Endpoint for EKS cluster"
157
}
168

17-
variable "toleration_key" {
18-
type = string
19-
default = ""
20-
description = "toleration key"
9+
variable "cluster_certificate_authority_data" {
10+
description = "CA certificate data for EKS cluster"
2111
}
2212

23-
variable "toleration_value" {
24-
type = string
25-
default = ""
26-
description = "toleration value"
13+
variable "iam_path" {
14+
description = "Path for IAM roles"
15+
default = "/delegatedadmin/developer/"
2716
}
2817

29-
variable "toleration_operator" {
30-
type = string
18+
variable "permissions_boundary" {
19+
description = "Permissions boundary for IAM roles"
3120
default = ""
32-
description = "toleration operator"
3321
}
3422

35-
variable "toleration_effect" {
36-
type = string
37-
default = ""
38-
description = "toleration effect"
23+
### Helm variables
24+
variable "tolerations" {
25+
type = list(any)
26+
default = []
3927
}
4028

41-
### Helm variables
4229
variable "helm_namespace" {
43-
default = "kube-system"
30+
description = "Namespace for Helm chart"
31+
default = "kube-system"
4432
}
4533

46-
## Image repo
4734
variable "imagerepo" {
48-
default = "602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/aws-efs-csi-driver"
35+
description = "ECR repository for container images"
36+
default = "602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/aws-efs-csi-driver"
4937
}
5038

5139
variable "efsid" {
52-
default = ""
40+
description = "EFS filesystem ID"
41+
default = ""
5342
}
5443

5544
variable "helm_name" {
56-
default = "aws-efs-csi-driver"
45+
description = "Name for Helm release"
46+
default = "aws-efs-csi-driver"
5747
}
5848

59-
variable "cluster_oidc_issuer_url" {}
49+
variable "cluster_oidc_issuer_url" {
50+
description = "OIDC issuer URL for EKS cluster"
51+
}
6052

6153
variable "kms_key_id" {
62-
default = ""
54+
description = "KMS key ID for secrets encryption"
55+
default = ""
6356
}
6457

6558
variable "vpc_id" {
66-
default = ""
59+
description = "VPC ID for EKS cluster"
6760
}
6861

6962
variable "private_subnet_ids" {
7063
type = list(any)
7164
default = []
7265
}
66+
67+
variable "worker_security_group_id" {
68+
type = string
69+
}

0 commit comments

Comments
 (0)