forked from DNXLabs/terraform-aws-vcs-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam-role.tf
73 lines (61 loc) · 2.02 KB
/
iam-role.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
resource "aws_iam_role" "default" {
for_each = { for role in var.roles : role.name => role }
name = each.value.name
assume_role_policy = data.aws_iam_policy_document.assume_role_saml[each.key].json
}
resource "aws_iam_role_policy_attachment" "default" {
for_each = { for role in local.assume_policies_flatten : role.index => role }
role = each.value.role_name
policy_arn = data.aws_iam_policy.default[each.key].arn
depends_on = [aws_iam_role.default]
}
data "aws_iam_policy" "default" {
for_each = { for role in local.assume_policies_flatten : role.index => role }
arn = each.value.assume_policy
}
data "aws_iam_policy_document" "assume_role_saml" {
for_each = { for role in var.roles : role.name => role }
statement {
principals {
type = "Federated"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/${replace(var.identity_provider_url, "https://", "")}"
]
}
actions = ["sts:AssumeRoleWithWebIdentity"]
dynamic "condition" {
for_each = each.value.conditions
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
resource "aws_iam_role_policy" "assume_role" {
for_each = { for role in local.assume_roles_flatten : role.index => role }
name_prefix = "${each.value.name}-${element(split("/", each.value.assume_role), 1)}"
role = each.value.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"sts:AssumeRole",
]
Effect = "Allow"
Resource = each.value.assume_role
},
]
})
depends_on = [aws_iam_role.default]
}
resource "aws_iam_openid_connect_provider" "default" {
url = var.identity_provider_url
client_id_list = var.audiences
thumbprint_list = compact([
var.oidc_thumbprint != "" ? var.oidc_thumbprint : "",
data.tls_certificate.thumprint.certificates.0.sha1_fingerprint
])
}