Skip to content

Commit 0c31a48

Browse files
authored
add dns ALIAS record support (#277)
* Add ALIAS record support for custom domain * add tldextract for root domain detection that support multilevel TLDs
1 parent 2c115f3 commit 0c31a48

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ optional-dependencies.cosmos = [
5656
]
5757
optional-dependencies.dns = [
5858
"aiodns",
59+
"tldextract",
5960
]
6061
optional-dependencies.docs = [
6162
"sphinxcontrib-plantuml",

src/aleph/sdk/domain.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from urllib.parse import urlparse
66

77
import aiodns
8+
import tldextract
89
from pydantic import BaseModel, HttpUrl
910

1011
from .conf import settings
@@ -198,6 +199,13 @@ async def check_domain(
198199
record_type = dns_rule.dns["type"]
199200
record_value = dns_rule.dns["value"]
200201

202+
if record_type == "alias":
203+
# ALIAS records cannot be reliably validated via DNS since the
204+
# provider resolves them to A records asynchronously. Consider
205+
# the rule as valid and trust the user's configuration.
206+
status[dns_rule.name] = True
207+
continue
208+
201209
try:
202210
entries = await resolver.query(record_name, record_type.upper())
203211
except aiodns.error.DNSError:
@@ -249,19 +257,35 @@ def get_required_dns_rules(
249257
elif target == TargetType.INSTANCE:
250258
cname_value = f"{hostname}.{settings.DNS_INSTANCE_DOMAIN}"
251259

252-
# cname rule
253-
dns_rules.append(
254-
DNSRule(
255-
name="cname",
256-
dns={
257-
"type": "cname",
258-
"name": hostname,
259-
"value": cname_value,
260-
},
261-
info=f"Create a CNAME record for {hostname} with value {cname_value}",
262-
on_error=f"CNAME record not found: {hostname}",
260+
# cname or alias rule
261+
if self.is_root_domain(hostname):
262+
record_type = "alias"
263+
dns_rules.append(
264+
DNSRule(
265+
name=record_type,
266+
dns={
267+
"type": record_type,
268+
"name": hostname,
269+
"value": cname_value,
270+
},
271+
info=f"Create an ALIAS record for {hostname} with value {cname_value}",
272+
on_error=f"ALIAS record not found: {hostname}",
273+
)
274+
)
275+
else:
276+
record_type = "cname"
277+
dns_rules.append(
278+
DNSRule(
279+
name=record_type,
280+
dns={
281+
"type": record_type,
282+
"name": hostname,
283+
"value": cname_value,
284+
},
285+
info=f"Create a CNAME record for {hostname} with value {cname_value}",
286+
on_error=f"CNAME record not found: {hostname}",
287+
)
263288
)
264-
)
265289

266290
if target == TargetType.IPFS:
267291
# ipfs rule
@@ -294,3 +318,8 @@ def get_required_dns_rules(
294318
)
295319

296320
return dns_rules
321+
322+
@staticmethod
323+
def is_root_domain(hostname: Hostname) -> bool:
324+
extracted = tldextract.extract(hostname)
325+
return bool(extracted.domain) and not extracted.subdomain

0 commit comments

Comments
 (0)