forked from terraform-aws-modules/terraform-aws-iam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
88 lines (70 loc) · 2.59 KB
/
main.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
data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
partition = data.aws_partition.current.partition
dns_suffix = data.aws_partition.current.dns_suffix
region = data.aws_region.current.name
role_name_condition = var.role_name != null ? var.role_name : "${var.role_name_prefix}*"
}
data "aws_iam_policy_document" "this" {
count = var.create_role ? 1 : 0
dynamic "statement" {
# https://aws.amazon.com/blogs/security/announcing-an-update-to-iam-role-trust-policy-behavior/
for_each = var.allow_self_assume_role ? [1] : []
content {
sid = "ExplicitSelfRoleAssumption"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "ArnLike"
variable = "aws:PrincipalArn"
values = ["arn:${local.partition}:iam::${local.account_id}:role${var.role_path}${local.role_name_condition}"]
}
}
}
dynamic "statement" {
for_each = var.oidc_providers
content {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [statement.value.provider_arn]
}
condition {
test = var.assume_role_condition_test
variable = "${replace(statement.value.provider_arn, "/^(.*provider/)/", "")}:sub"
values = [for sa in statement.value.namespace_service_accounts : "system:serviceaccount:${sa}"]
}
# https://aws.amazon.com/premiumsupport/knowledge-center/eks-troubleshoot-oidc-and-irsa/?nc1=h_ls
condition {
test = var.assume_role_condition_test
variable = "${replace(statement.value.provider_arn, "/^(.*provider/)/", "")}:aud"
values = ["sts.amazonaws.com"]
}
}
}
}
resource "aws_iam_role" "this" {
count = var.create_role ? 1 : 0
name = var.role_name
name_prefix = var.role_name_prefix
path = var.role_path
description = var.role_description
assume_role_policy = data.aws_iam_policy_document.this[0].json
max_session_duration = var.max_session_duration
permissions_boundary = var.role_permissions_boundary_arn
force_detach_policies = var.force_detach_policies
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "this" {
for_each = { for k, v in var.role_policy_arns : k => v if var.create_role }
role = aws_iam_role.this[0].name
policy_arn = each.value
}