-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
57 lines (48 loc) · 1.65 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
resource "aws_iam_openid_connect_provider" "this" {
count = var.create_oidc_provider ? 1 : 0
client_id_list = var.audiences
thumbprint_list = var.github_thumbprint
url = "https://token.actions.githubusercontent.com"
}
resource "aws_iam_role" "this" {
count = var.create_oidc_role ? 1 : 0
name = var.role_name
description = var.role_description
max_session_duration = var.max_session_duration
assume_role_policy = join("", data.aws_iam_policy_document.this[0].*.json)
tags = var.tags
depends_on = [aws_iam_openid_connect_provider.this]
}
resource "aws_iam_role_policy_attachment" "attach" {
count = var.create_oidc_role ? length(var.oidc_role_attach_policies) : 0
policy_arn = var.oidc_role_attach_policies[count.index]
role = join("", aws_iam_role.this.*.name)
depends_on = [aws_iam_role.this]
}
data "aws_iam_policy_document" "this" {
count = var.create_oidc_role ? 1 : 0
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
condition {
test = "StringLike"
values = [
for repo in var.repositories :
"repo:%{if length(regexall(":+", repo)) > 0}${repo}%{else}${repo}:*%{endif}"
]
variable = "token.actions.githubusercontent.com:sub"
}
condition {
test = "StringEquals"
values = [
for audience in var.audiences :
"${audience}"
]
variable = "token.actions.githubusercontent.com:aud"
}
principals {
identifiers = [try(aws_iam_openid_connect_provider.this[0].arn, var.oidc_provider_arn)]
type = "Federated"
}
}
}