Skip to content

Commit ea43574

Browse files
irvingpopclaude
andauthored
DNS best practices for operationcode.org and operationcode.net (#193)
* Add DNS best practices: CAA, Google MX, DMARC reporting, null SPF, operationcode.net protection, DNSSEC signing, query logging Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Remove applied import blocks for operationcode.net Imports applied locally (3 imported). Stray operationcode.org.operationcode.net MX/SPF/TXT records deleted by hand. terraform plan reports No changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ffea3f6 commit ea43574

3 files changed

Lines changed: 294 additions & 13 deletions

File tree

‎terraform/dns_operationcode_net.tf‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Route53 zone: operationcode.net
2+
# The domain sends no mail. It only redirects web traffic to operationcode.org,
3+
# so it carries anti-spoofing records (null SPF, null MX, strict DMARC).
4+
5+
# Hosted zone for operationcode.net (imported; never destroy)
6+
resource "aws_route53_zone" "operationcode_net" {
7+
name = "operationcode.net"
8+
# Live zone has an empty comment; keep it empty to avoid drift (provider would otherwise default it)
9+
comment = ""
10+
11+
lifecycle {
12+
prevent_destroy = true
13+
}
14+
}
15+
16+
# ======================================================================
17+
# Apex and web
18+
# ======================================================================
19+
20+
# S3 website endpoint (us-east-1): answers 301 to https://operationcode.org/
21+
resource "aws_route53_record" "net_apex_a" {
22+
zone_id = aws_route53_zone.operationcode_net.zone_id
23+
name = "operationcode.net"
24+
type = "A"
25+
26+
alias {
27+
name = "s3-website-us-east-1.amazonaws.com."
28+
zone_id = "Z3AQBSTGFYJSTF"
29+
evaluate_target_health = true
30+
}
31+
}
32+
33+
# www CNAME to the apex (live value has no trailing dot; keep it as is)
34+
# Note: the S3 website endpoint returns 403 for the www Host header, so www does not redirect today
35+
resource "aws_route53_record" "net_www" {
36+
zone_id = aws_route53_zone.operationcode_net.zone_id
37+
name = "www.operationcode.net"
38+
type = "CNAME"
39+
ttl = 300
40+
records = ["operationcode.net"]
41+
}
42+
43+
# ======================================================================
44+
# Anti-spoofing (domain sends no mail)
45+
# ======================================================================
46+
47+
# Null SPF: No sender is authorized to use operationcode.net as its SPF identity.
48+
resource "aws_route53_record" "net_apex_spf" {
49+
zone_id = aws_route53_zone.operationcode_net.zone_id
50+
name = "operationcode.net"
51+
type = "TXT"
52+
ttl = 3600
53+
records = ["v=spf1 -all"]
54+
}
55+
56+
# Null MX (RFC 7505): the domain accepts no mail
57+
resource "aws_route53_record" "net_apex_mx" {
58+
zone_id = aws_route53_zone.operationcode_net.zone_id
59+
name = "operationcode.net"
60+
type = "MX"
61+
ttl = 3600
62+
records = ["0 ."]
63+
}
64+
65+
# Strict DMARC: reject all spoofed mail, send aggregate reports to tech@
66+
resource "aws_route53_record" "net_dmarc" {
67+
zone_id = aws_route53_zone.operationcode_net.zone_id
68+
name = "_dmarc.operationcode.net"
69+
type = "TXT"
70+
ttl = 3600
71+
records = ["v=DMARC1; p=reject; sp=reject; adkim=s; aspf=s; rua=mailto:tech@operationcode.org"]
72+
}
73+
74+
# DMARC report authorization (RFC 7489 section 7.1)
75+
# The rua address is in operationcode.org, a different domain than the one being reported on.
76+
# Receivers only send reports there if the .org zone publishes this record.
77+
# It lives in this file (not dns_operationcode_org.tf) because it exists only to support the .net DMARC record.
78+
# It is created in the operationcode.org zone, so it uses that zone's id.
79+
resource "aws_route53_record" "net_dmarc_report_auth" {
80+
zone_id = aws_route53_zone.operationcode.zone_id
81+
name = "operationcode.net._report._dmarc.operationcode.org"
82+
type = "TXT"
83+
ttl = 3600
84+
records = ["v=DMARC1"]
85+
}

‎terraform/dns_operationcode_org.tf‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ resource "aws_route53_record" "apex_a" {
2525
records = ["76.76.21.21"]
2626
}
2727

28-
# CA policy: restricts wildcard issuance to Amazon, reports to tech@
28+
# CA policy: restricts issuance to the CAs in use, reports to tech@
29+
# issue: amazon.com and amazontrust.com (ACM wildcard cert on the ALB: api, pybot, resources, api.staging)
30+
# letsencrypt.org (Vercel certs for apex, www, storybook; renewals fail without it)
31+
# issuewild: Amazon only (the only wildcard cert is ACM; no Let's Encrypt wildcard is served)
32+
# Cloudways hosts (services family) serve a *.cloudwaysapps.com cert from SSL.com, not a cert for our names, so no CA is added for them
2933
resource "aws_route53_record" "apex_caa" {
3034
zone_id = aws_route53_zone.operationcode.zone_id
3135
name = "operationcode.org"
3236
type = "CAA"
3337
ttl = 300
3438
records = [
39+
"0 issue \"amazon.com\"",
40+
"0 issue \"amazontrust.com\"",
41+
"0 issue \"letsencrypt.org\"",
3542
"0 issuewild \"amazontrust.com\"",
3643
"0 issuewild \"amazon.com\"",
3744
"0 iodef \"mailto:tech@operationcode.org\"",
@@ -174,18 +181,13 @@ resource "aws_route53_record" "api_staging_aaaa" {
174181
# ======================================================================
175182

176183
# Google Workspace mail
184+
# Replaced this zone's legacy ASPMX records with Google's current single MX on 2026-11-01. Google still supports the old values.
177185
resource "aws_route53_record" "apex_mx" {
178186
zone_id = aws_route53_zone.operationcode.zone_id
179187
name = "operationcode.org"
180188
type = "MX"
181189
ttl = 3600
182-
records = [
183-
"1 ASPMX.L.GOOGLE.COM.",
184-
"5 ALT1.ASPMX.L.GOOGLE.COM.",
185-
"5 ALT2.ASPMX.L.GOOGLE.COM.",
186-
"10 ALT3.ASPMX.L.GOOGLE.COM.",
187-
"10 ALT4.ASPMX.L.GOOGLE.COM.",
188-
]
190+
records = ["1 smtp.google.com."]
189191
}
190192

191193
# SPF (Google, Mailgun, Mandrill) and Google site verification. Keep the Mandrill include (production mail uses it). INVESTIGATE: Mailgun include is unused by code, confirm with the Mailgun account
@@ -201,13 +203,14 @@ resource "aws_route53_record" "apex_txt" {
201203
]
202204
}
203205

204-
# DMARC policy for the apex domain (monitoring only, p=none)
206+
# DMARC policy for the apex domain (monitoring only, p=none, aggregate reports to tech@)
207+
# Next steps: enable Google DKIM, then move to p=quarantine, then p=reject
205208
resource "aws_route53_record" "apex_dmarc" {
206209
zone_id = aws_route53_zone.operationcode.zone_id
207210
name = "_dmarc.operationcode.org"
208211
type = "TXT"
209-
ttl = 60
210-
records = ["v=DMARC1; p=none;"]
212+
ttl = 3600
213+
records = ["v=DMARC1; p=none; rua=mailto:tech@operationcode.org"]
211214
}
212215

213216
# ======================================================================
@@ -319,7 +322,7 @@ resource "aws_route53_record" "mailchimp_dkim_k2" {
319322
zone_id = aws_route53_zone.operationcode.zone_id
320323
name = "k2._domainkey.operationcode.org"
321324
type = "CNAME"
322-
ttl = 60
325+
ttl = 3600
323326
records = ["dkim2.mcsv.net"]
324327
}
325328

@@ -328,7 +331,7 @@ resource "aws_route53_record" "mailchimp_dkim_k3" {
328331
zone_id = aws_route53_zone.operationcode.zone_id
329332
name = "k3._domainkey.operationcode.org"
330333
type = "CNAME"
331-
ttl = 60
334+
ttl = 3600
332335
records = ["dkim3.mcsv.net"]
333336
}
334337

@@ -461,3 +464,28 @@ resource "aws_route53_record" "test" {
461464
ttl = 60
462465
records = ["services.operationcode.org."]
463466
}
467+
468+
# ======================================================================
469+
# Null SPF for non-sending hosts
470+
# ======================================================================
471+
472+
# v=spf1 -all on hosts with an address record, no other TXT, and no known mail sending.
473+
# Excluded: Cloudways and DigitalOcean hosts (out of scope), apex, coders.* (sends via SES), CNAME names (TXT cannot coexist), o1.notify (SendGrid sending IP)
474+
locals {
475+
null_spf_hosts = toset([
476+
"www",
477+
"api",
478+
"pybot",
479+
"resources",
480+
"api.staging",
481+
])
482+
}
483+
484+
resource "aws_route53_record" "null_spf" {
485+
for_each = local.null_spf_hosts
486+
zone_id = aws_route53_zone.operationcode.zone_id
487+
name = "${each.key}.operationcode.org"
488+
type = "TXT"
489+
ttl = 3600
490+
records = ["v=spf1 -all"]
491+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# DNSSEC signing and query logging for operationcode.org
2+
# Zone: aws_route53_zone.operationcode (dns_operationcode_org.tf)
3+
# Route53 requires the DNSSEC KMS key and the query log group in us-east-1.
4+
5+
# ======================================================================
6+
# DNSSEC signing (stage 1 of 2: sign the zone; no DS record at the registrar yet)
7+
# ======================================================================
8+
9+
# Key policy:
10+
# - account root: full admin, so IAM policies control key management
11+
# - Route53 DNSSEC service: read the public key and sign, only for hosted zones in this account
12+
# - Route53 DNSSEC service: create grants, only for AWS resources
13+
data "aws_iam_policy_document" "dnssec_kms" {
14+
statement {
15+
sid = "EnableIAMUserPermissions"
16+
effect = "Allow"
17+
actions = ["kms:*"]
18+
resources = ["*"]
19+
20+
principals {
21+
type = "AWS"
22+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
23+
}
24+
}
25+
26+
statement {
27+
sid = "AllowRoute53DNSSECService"
28+
effect = "Allow"
29+
actions = ["kms:DescribeKey", "kms:GetPublicKey", "kms:Sign"]
30+
resources = ["*"]
31+
32+
principals {
33+
type = "Service"
34+
identifiers = ["dnssec-route53.amazonaws.com"]
35+
}
36+
37+
condition {
38+
test = "StringEquals"
39+
variable = "aws:SourceAccount"
40+
values = [data.aws_caller_identity.current.account_id]
41+
}
42+
43+
condition {
44+
test = "ArnLike"
45+
variable = "aws:SourceArn"
46+
values = ["arn:aws:route53:::hostedzone/*"]
47+
}
48+
}
49+
50+
statement {
51+
sid = "AllowRoute53DNSSECToCreateGrant"
52+
effect = "Allow"
53+
actions = ["kms:CreateGrant"]
54+
resources = ["*"]
55+
56+
principals {
57+
type = "Service"
58+
identifiers = ["dnssec-route53.amazonaws.com"]
59+
}
60+
61+
condition {
62+
test = "Bool"
63+
variable = "kms:GrantIsForAWSResource"
64+
values = ["true"]
65+
}
66+
}
67+
}
68+
69+
# Asymmetric signing key for the KSK. Deleting it breaks resolution once the DS record is live; never destroy
70+
resource "aws_kms_key" "dnssec" {
71+
provider = aws.us_east_1
72+
description = "Route53 DNSSEC KSK for operationcode.org"
73+
customer_master_key_spec = "ECC_NIST_P256"
74+
key_usage = "SIGN_VERIFY"
75+
deletion_window_in_days = 7
76+
policy = data.aws_iam_policy_document.dnssec_kms.json
77+
78+
lifecycle {
79+
prevent_destroy = true
80+
}
81+
}
82+
83+
resource "aws_kms_alias" "dnssec" {
84+
provider = aws.us_east_1
85+
name = "alias/route53-dnssec-operationcode-org"
86+
target_key_id = aws_kms_key.dnssec.key_id
87+
}
88+
89+
resource "aws_route53_key_signing_key" "operationcode" {
90+
hosted_zone_id = aws_route53_zone.operationcode.id
91+
key_management_service_arn = aws_kms_key.dnssec.arn
92+
name = "operationcode_org_ksk"
93+
}
94+
95+
resource "aws_route53_hosted_zone_dnssec" "operationcode" {
96+
hosted_zone_id = aws_route53_key_signing_key.operationcode.hosted_zone_id
97+
98+
depends_on = [aws_route53_key_signing_key.operationcode]
99+
}
100+
101+
# Stage 2: DS record at the registrar (Route53 Domains, us-east-1). Keep commented out until:
102+
# 1. signing is verified: `aws route53 get-dnssec --hosted-zone-id <zone id>` shows SIGNING and the
103+
# KSK ACTIVE, and `dig +dnssec operationcode.org SOA` returns RRSIG records
104+
# 2. the max TTL in the zone has passed since signing started (NS: 172800s = 48h; other records: 3600s)
105+
# Rollback order: remove the DS record first, wait for the DS TTL at the .org parent (and the
106+
# registrar change) to pass, then disable signing (aws_route53_hosted_zone_dnssec signing_status = "NOT_SIGNING").
107+
# Disabling signing while the DS record is live makes the domain fail resolution for validating resolvers.
108+
#
109+
# resource "aws_route53domains_delegation_signer_record" "operationcode" {
110+
# provider = aws.us_east_1
111+
# domain_name = "operationcode.org"
112+
#
113+
# signing_attributes {
114+
# algorithm = aws_route53_key_signing_key.operationcode.signing_algorithm_type
115+
# flags = aws_route53_key_signing_key.operationcode.flag
116+
# public_key = aws_route53_key_signing_key.operationcode.public_key
117+
# }
118+
# }
119+
120+
# ======================================================================
121+
# Query logging
122+
# ======================================================================
123+
124+
resource "aws_cloudwatch_log_group" "route53_query_operationcode" {
125+
provider = aws.us_east_1
126+
name = "/aws/route53/operationcode.org"
127+
retention_in_days = 30
128+
}
129+
130+
# Account-wide policy (us-east-1 limit: 10 per region) that lets Route53 write any /aws/route53/* log group
131+
data "aws_iam_policy_document" "route53_query_logging" {
132+
statement {
133+
sid = "Route53QueryLogging"
134+
effect = "Allow"
135+
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
136+
resources = ["arn:aws:logs:us-east-1:${data.aws_caller_identity.current.account_id}:log-group:/aws/route53/*"]
137+
138+
principals {
139+
type = "Service"
140+
identifiers = ["route53.amazonaws.com"]
141+
}
142+
143+
condition {
144+
test = "StringEquals"
145+
variable = "aws:SourceAccount"
146+
values = [data.aws_caller_identity.current.account_id]
147+
}
148+
149+
condition {
150+
test = "ArnLike"
151+
variable = "aws:SourceArn"
152+
values = ["arn:aws:route53:::hostedzone/*"]
153+
}
154+
}
155+
}
156+
157+
resource "aws_cloudwatch_log_resource_policy" "route53_query_logging" {
158+
provider = aws.us_east_1
159+
policy_name = "route53-query-logging"
160+
policy_document = data.aws_iam_policy_document.route53_query_logging.json
161+
}
162+
163+
resource "aws_route53_query_log" "operationcode" {
164+
zone_id = aws_route53_zone.operationcode.zone_id
165+
cloudwatch_log_group_arn = aws_cloudwatch_log_group.route53_query_operationcode.arn
166+
167+
depends_on = [aws_cloudwatch_log_resource_policy.route53_query_logging]
168+
}

0 commit comments

Comments
 (0)