-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
66 lines (61 loc) · 1.75 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
data "tls_certificate" "bitbucket" {
url = var.api_url
}
data "http" "workspace" {
url = "${var.api_url}/workspaces/${var.workspace}"
}
locals {
workspace = jsondecode(data.http.workspace.response_body)
workspace_uuid = trim(local.workspace.uuid, "}{")
audience = "ari:cloud:bitbucket::workspace/${local.workspace_uuid}"
url = "${var.api_url}/workspaces/${local.workspace.slug}/pipelines-config/identity/oidc"
oidc = trimprefix(local.url, "https://")
}
resource "aws_iam_openid_connect_provider" "bitbucket" {
url = local.url
client_id_list = [local.audience]
thumbprint_list = [
for cert in data.tls_certificate.bitbucket.certificates :
cert.sha1_fingerprint
]
}
data "aws_iam_policy_document" "assume_role_with_webid" {
for_each = var.policies
statement {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [aws_iam_openid_connect_provider.bitbucket.arn]
}
condition {
test = "StringEquals"
variable = "${local.oidc}:aud"
values = [local.audience]
}
dynamic "condition" {
for_each = each.value.subjects
content {
test = "StringLike"
variable = "${local.oidc}:sub"
values = [condition.value]
}
}
dynamic "condition" {
for_each = each.value.claims
content {
test = "StringLike"
variable = "${local.oidc}:${condition.key}"
values = condition.value
}
}
dynamic "condition" {
for_each = tobool(length(var.source_ip) > 0) ? [true] : []
content {
test = "IpAddress"
variable = "aws:SourceIp"
values = var.source_ip
}
}
}
}