forked from lablabs/terraform-aws-eks-cert-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam.tf
124 lines (102 loc) · 3.03 KB
/
iam.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
locals {
k8s_irsa_role_create = var.enabled && var.k8s_rbac_create && var.k8s_service_account_create && var.k8s_irsa_role_create
}
data "aws_iam_policy_document" "this" {
count = local.k8s_irsa_role_create ? 1 : 0
dynamic "statement" {
for_each = var.k8s_irsa_policy_enabled ? toset(["true"]) : []
content {
sid = "ChangeResourceRecordSets"
effect = "Allow"
actions = [
"route53:ChangeResourceRecordSets",
]
resources = formatlist(
"arn:aws:route53:::hostedzone/%s",
var.policy_allowed_zone_ids
)
}
}
dynamic "statement" {
for_each = var.k8s_irsa_policy_enabled ? toset(["true"]) : []
content {
sid = "ListResourceRecordSets"
effect = "Allow"
actions = [
"route53:ListHostedZones",
"route53:ListResourceRecordSets",
"route53:ListTagsForResource",
"route53:ListHostedZonesByName"
]
resources = [
"*"
]
}
}
dynamic "statement" {
for_each = var.k8s_irsa_policy_enabled ? toset(["true"]) : []
content {
sid = "GetBatchChangeStatus"
effect = "Allow"
actions = [
"route53:GetChange"
]
resources = [
"*"
]
}
}
dynamic "statement" {
for_each = var.k8s_assume_role_enabled ? toset(["true"]) : []
content {
sid = "AllowAssumeCertManagerRole"
effect = "Allow"
actions = [
"sts:AssumeRole"
]
resources = var.k8s_assume_role_arns
}
}
}
resource "aws_iam_policy" "this" {
count = local.k8s_irsa_role_create && (var.k8s_irsa_policy_enabled || var.k8s_irsa_policy_enabled) ? 1 : 0
name = "${var.k8s_irsa_role_name_prefix}-${var.helm_release_name}"
path = "/"
description = "Policy for cert-manager service"
policy = data.aws_iam_policy_document.this[0].json
tags = var.tags
}
data "aws_iam_policy_document" "this_irsa" {
count = local.k8s_irsa_role_create ? 1 : 0
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [var.cluster_identity_oidc_issuer_arn]
}
condition {
test = "StringEquals"
variable = "${replace(var.cluster_identity_oidc_issuer, "https://", "")}:sub"
values = [
"system:serviceaccount:${var.k8s_namespace}:${var.k8s_service_account_name}",
]
}
effect = "Allow"
}
}
resource "aws_iam_role" "this" {
count = local.k8s_irsa_role_create ? 1 : 0
name = "${var.k8s_irsa_role_name_prefix}-${var.helm_release_name}"
assume_role_policy = data.aws_iam_policy_document.this_irsa[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "this" {
count = local.k8s_irsa_role_create ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.this[0].arn
}
resource "aws_iam_role_policy_attachment" "this_additional" {
for_each = local.k8s_irsa_role_create ? var.k8s_irsa_additional_policies : {}
role = aws_iam_role.this[0].name
policy_arn = each.value
}