Skip to content

Commit add0429

Browse files
author
Simon Stier
authored
Merge pull request #7 from stackitcloud/bugfix/find-best-matching-zone
find best matching zone
2 parents 4292060 + f710fb6 commit add0429

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

certbot_dns_stackit/stackit.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def add_txt_record(self, domain: str, validation_name: str, validation: str):
5353
"""
5454
Add a TXT record using the supplied information.
5555
56-
:param domain: The zone dnsName.
57-
:param validation_name: The record name.
58-
:param validation: The record content.
56+
:param domain: The domain one level above the validation name.
57+
:param validation_name: The acme challenge record name.
58+
:param validation: The acme challenge record content.
5959
"""
6060
zone_id = self._get_zone_id(domain)
6161
rrset = self._get_rrset(zone_id, validation_name)
@@ -137,16 +137,23 @@ def _get_zone_id(self, domain: str) -> str:
137137
:param domain: The domain (zone dnsName) for which the zone ID is needed.
138138
:return: The ID of the zone.
139139
"""
140-
res = requests.get(
141-
f"{self.base_url}/v1/projects/{self.project_id}/zones?dnsName[eq]={domain}&active[eq]=true",
142-
headers=self.headers,
143-
)
144-
if res.status_code != 200 or len(res.json()["zones"]) == 0:
145-
raise errors.PluginError(
146-
f"Could not find zone id for domain {domain}, Response: {res.text}"
140+
parts = domain.split('.')
141+
142+
# we are searching for the best matching zone. We can do that by iterating over the parts of the domain
143+
# from left to right.
144+
for i in range(len(parts)):
145+
subdomain = '.'.join(parts[i:])
146+
res = requests.get(
147+
f"{self.base_url}/v1/projects/{self.project_id}/zones?dnsName[eq]={subdomain}&active[eq]=true",
148+
headers=self.headers,
147149
)
148150

149-
return res.json()["zones"][0]["id"]
151+
if res.status_code == 200 and len(res.json()["zones"]) > 0:
152+
return res.json()["zones"][0]["id"]
153+
154+
raise errors.PluginError(
155+
f"Could not find zone id for domain {domain}, Response: {res.text}"
156+
)
150157

151158
def _get_rrset(self, zone_id: str, validation_name: str) -> Optional[RRSet]:
152159
"""
@@ -256,7 +263,7 @@ def _perform(self, domain: str, validation_name: str, validation: str):
256263
"""
257264
Carry out a DNS update.
258265
259-
:param domain: The domain where the DNS record will be added.
266+
:param domain: The domain where the DNS record will be added. Does not need to be the zone dns name but any domain.
260267
:param validation_name: The name of the DNS record.
261268
:param validation: The validation content to be added to the DNS record.
262269
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from setuptools import find_packages
33
import os
44

5-
version = os.environ.get("PACKAGE_VERSION", "v0.1.0")
5+
version = os.environ.get("PACKAGE_VERSION", "v0.1.1")
66

77
install_requires = [
88
"acme>=2.6.0",

0 commit comments

Comments
 (0)