-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
189 lines (163 loc) · 3.8 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#
# General
#
data "cloudflare_zone" "zone" {
account_id = var.account_id
zone_id = var.zone_id
}
locals {
zone_name = data.cloudflare_zone.zone.name
}
#
# MX
#
locals {
mx_sets = flatten([
for name in concat([local.zone_name], var.mx_subdomains) : [
for mx, priority in var.mx : {
name = name
mx = mx
priority = priority
} if name != ""
]
])
mx_records = {
for v in local.mx_sets :
"${v.name == local.zone_name ? "" : "${v.name}:"}${v.mx}" => v
}
}
resource "cloudflare_record" "mx" {
for_each = local.mx_records
name = each.value.name
priority = each.value.priority
proxied = false
ttl = var.record_ttl
type = "MX"
value = each.value.mx
zone_id = var.zone_id
}
#
# SPF
#
resource "cloudflare_record" "spf" {
name = local.zone_name
proxied = false
ttl = var.record_ttl
type = "TXT"
value = join(" ", concat(["v=spf1"], var.spf_terms))
zone_id = var.zone_id
}
#
# TLS SMTP
#
resource "cloudflare_record" "smtp_tls" {
name = "_smtp._tls"
type = "TXT"
value = "v=TLSRPTv1; rua=${join(",", var.tlsrpt_rua)}"
zone_id = var.zone_id
}
#
# MTA-STS
#
locals {
policy = templatefile("${path.module}/mta-sts.txt.tpl", {
mode = var.mta_sts_mode
max_age = var.mta_sts_max_age
mx = sort(distinct(concat(keys(var.mx), var.mta_sts_mx)))
})
policy_sha = sha1(local.policy)
}
resource "cloudflare_record" "mta-sts-a" {
name = "mta-sts"
proxied = true
ttl = var.record_ttl
type = "A"
value = "192.0.2.1"
zone_id = var.zone_id
}
resource "cloudflare_record" "mta-sts-aaaa" {
name = "mta-sts"
proxied = true
ttl = var.record_ttl
type = "AAAA"
value = "100::"
zone_id = var.zone_id
}
resource "cloudflare_record" "mta_sts" {
name = "_mta-sts"
ttl = var.record_ttl
type = "TXT"
value = "v=STSv1; id=${local.policy_sha}"
zone_id = var.zone_id
}
resource "cloudflare_workers_kv_namespace" "mta_sts" {
title = "mta-sts.${local.zone_name}"
account_id = var.account_id
}
resource "cloudflare_workers_kv" "mta_sts" {
namespace_id = cloudflare_workers_kv_namespace.mta_sts.id
key = "mta-sts.txt"
value = local.policy
account_id = var.account_id
}
resource "cloudflare_worker_script" "mta_sts" {
name = "mta-sts-${replace(local.zone_name, "/[^A-Za-z0-9-]/", "-")}"
content = file("${path.module}/mta-sts.js")
account_id = var.account_id
kv_namespace_binding {
name = "FILES"
namespace_id = cloudflare_workers_kv_namespace.mta_sts.id
}
}
resource "cloudflare_worker_route" "mta_sts_route" {
pattern = "mta-sts.${local.zone_name}/*"
script_name = cloudflare_worker_script.mta_sts.name
zone_id = var.zone_id
}
#
# DMARC
#
locals {
dmarc_modes = {
"relaxed" = "r"
"strict" = "s"
}
dmarc_values = {
"rua" = join(",", compact(var.dmarc_rua))
"ruf" = join(",", compact(var.dmarc_ruf))
}
}
resource "cloudflare_record" "dmarc" {
name = "_dmarc"
proxied = false
ttl = floor(var.dmarc_ttl)
type = "TXT"
value = join(" ", flatten([
"v=DMARC1;",
"p=${var.dmarc_policy};",
"pct=${floor(var.dmarc_percent)};",
"aspf=${local.dmarc_modes[var.dmarc_spf_mode]};",
"adkim=${local.dmarc_modes[var.dmarc_dkim_mode]};",
[
for k, v in local.dmarc_values :
"${k}=${v};" if trimspace(v) != ""
],
[
for v in [var.dmarc_fo] :
"fo=${v};" if trimspace(local.dmarc_values["ruf"]) != ""
],
]))
zone_id = var.zone_id
}
#
# Domain Keys (DKIM)
#
resource "cloudflare_record" "domainkeys" {
for_each = var.domainkeys
name = "${each.key}._domainkey"
proxied = false
ttl = var.record_ttl
type = upper(each.value.type)
value = each.value.value
zone_id = var.zone_id
}