Skip to content

Commit 66b6c48

Browse files
committed
dstack-ingress: Fixes incorrect public suffixes handling
1 parent 8c1f88b commit 66b6c48

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

custom-domain/dstack-ingress/scripts/cloudflare_dns.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,43 @@ def _make_request(self, method: str, endpoint: str, data: Optional[Dict] = None)
6161

6262
def get_zone_id(self, domain: str) -> Optional[str]:
6363
"""Get the zone ID for a domain."""
64-
# Extract the root domain (e.g., example.com from sub.example.com)
65-
parts = domain.split('.')
66-
if len(parts) > 2:
67-
root_domain = '.'.join(parts[-2:])
68-
else:
69-
root_domain = domain
64+
# Find the zone with the longest matching suffix for the domain
65+
zone_name_len = 0
66+
zone_id = None
7067

71-
print(f"Fetching zone ID for domain: {root_domain}")
72-
result = self._make_request("GET", f"zones?name={root_domain}")
68+
page = 1
69+
total_pages = 1
7370

74-
if not result.get("success", False):
75-
return None
71+
while page <= total_pages:
72+
result = self._make_request("GET", f"zones?page={page}")
7673

77-
zones = result.get("result", [])
78-
if not zones:
79-
print(f"No zones found for domain: {root_domain}", file=sys.stderr)
80-
return None
74+
if not result.get("success", False):
75+
return None
76+
77+
zones = result.get("result", [])
78+
if not zones and page == 1:
79+
print(f"No zones found for any domain", file=sys.stderr)
80+
return None
81+
82+
result_info = result.get("result_info", {})
83+
if result_info:
84+
total_pages = result_info.get("total_pages", total_pages)
85+
86+
for zone in zones:
87+
zone_name = zone.get("name", "")
88+
if domain == zone_name:
89+
return zone.get("id")
90+
if domain.endswith(f".{zone_name}") and len(zone_name) > zone_name_len:
91+
zone_name_len = len(zone_name)
92+
zone_id = zone.get("id")
93+
94+
page += 1
8195

82-
zone_id = zones[0].get("id")
8396
if zone_id:
84-
print(f"Successfully retrieved zone ID: {zone_id} for domain {root_domain}")
85-
# Store the zone ID separately from any print output
8697
self.zone_id = zone_id
8798
return zone_id
8899
else:
89-
print(f"Zone ID not found in response for domain: {root_domain}", file=sys.stderr)
100+
print(f"Zone ID not found in response for domain: {domain}", file=sys.stderr)
90101
return None
91102

92103
def get_dns_records(self, name: str, record_type: Optional[str] = None) -> List[Dict]:

0 commit comments

Comments
 (0)