Skip to content

Commit 28dbc3d

Browse files
committed
fix(ingress): replace a Namecheap CAA in one zone write
The base set_caa_record deletes the stale same-tag record and then creates the new one, which on Namecheap is two setHosts calls with no CAA of that tag in between. Do the replacement in a single setHosts, and stop saying the API cannot manage CAA. Signed-off-by: Kevin Wang <wy721@qq.com>
1 parent 436a855 commit 28dbc3d

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎custom-domain/dstack-ingress/DNS_PROVIDERS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ NAMECHEAP_CLIENT_IP=your-client-ip
7474

7575
**Important Notes for Namecheap:**
7676
- Namecheap API requires node IP address for authentication, and you need add it to whitelist IP first.
77-
- Namecheap doesn't support CAA records through their API currently
77+
- CAA records are written through `setHosts`, which replaces the whole zone. The rest of the zone is read first and written back unchanged.
7878
- The certbot plugin uses the format `certbot-dns-namecheap` package
7979

8080
### Route53

‎custom-domain/dstack-ingress/scripts/dns_providers/namecheap.py‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,52 @@ def delete_dns_record(self, record_id: str, domain: str) -> bool:
281281

282282
return self._set_dns_records(sld, tld, all_records, email_type)
283283

284+
def set_caa_record(
285+
self,
286+
name: str,
287+
tag: str,
288+
value: str,
289+
flags: int = 0,
290+
ttl: int = 60,
291+
) -> bool:
292+
"""Replace the name's CAA records of this tag in a single setHosts.
293+
294+
The base implementation deletes first and creates after, which on
295+
Namecheap is two zone writes with no CAA of this tag in between.
296+
"""
297+
values = [
298+
r.data["value"]
299+
for r in self.get_dns_records(name, RecordType.CAA)
300+
if r.data and r.data.get("tag") == tag
301+
]
302+
if values == [value.strip('"')]:
303+
print("CAA record with the same content already exists")
304+
return True
305+
return self.create_caa_record(
306+
CAARecord(name=name, flags=flags, tag=tag, value=value, ttl=ttl)
307+
)
308+
284309
def create_caa_record(self, caa_record: CAARecord) -> bool:
285-
"""Add a CAA record, keeping the name's other CAA records (e.g. iodef)."""
310+
"""Replace the name's CAA records of this tag, keeping other tags (e.g. iodef)."""
286311
domain_info = self._get_domain_info(caa_record.name)
287312
if not domain_info:
288313
print(f"Could not determine domain info from {caa_record.name}")
289314
return False
290315

291316
sld, tld = domain_info
292-
hostname = self._host_label(caa_record.name.rstrip("."), sld, tld)
317+
name = caa_record.name.rstrip(".")
318+
hostname = self._host_label(name, sld, tld)
293319
value = caa_record.value.strip('"')
294320

295-
hosts, email_type = self._get_hosts(sld, tld)
321+
all_hosts, email_type = self._get_hosts(sld, tld)
322+
hosts = [
323+
h for h in all_hosts
324+
if not (
325+
h.get("Type") == "CAA"
326+
and self._host_fqdn(h.get("Name", "@"), sld, tld).lower() == name.lower()
327+
and h.get("Address", "").split(None, 2)[1:2] == [caa_record.tag]
328+
)
329+
]
296330
hosts.append({
297331
"Name": hostname,
298332
"Type": "CAA",

0 commit comments

Comments
 (0)