Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encode zone names in url paths #74

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.0.? - 2024-??-?? - ???

* Support for fully managing zones with special characters in their names, e.g.
128/26.2.0.192.in-addr.arpa. added.

## v0.0.6 - 2024-03-08 - Get port type straight

* DS Record support added
Expand Down
19 changes: 15 additions & 4 deletions octodns_powerdns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
from operator import itemgetter
from urllib.parse import quote_plus

from requests import HTTPError, Session

Expand All @@ -27,6 +28,15 @@
__version__ = __VERSION__ = '0.0.6'


def _encode_zone_name(name):
# Powerdns uses a special encoding for URLs. Instead of "%2F" for a slash,
# the slash must be encoded with "=2F". (This must be done in version 4.7.3
# from Debian, from version >= 4.8 Powerdns accepts “%2F” and “=2F” as path
# argument. The output of "/api/v1/servers/localhost/zones" still shows the
# zone URL with "=2F")
return quote_plus(name).replace('%', '=')


def _escape_unescaped_semicolons(value):
pieces = value.split(';')
if len(pieces) == 1:
Expand Down Expand Up @@ -453,10 +463,10 @@ def populate(self, zone, target=False, lenient=False):
target,
lenient,
)

encoded_name = _encode_zone_name(zone.name)
resp = None
try:
resp = self._get(f'zones/{zone.name}')
resp = self._get(f'zones/{encoded_name}')
self.log.debug('populate: loaded')
except HTTPError as e:
error = self._get_error(e)
Expand Down Expand Up @@ -674,6 +684,7 @@ def _get_error(self, http_error):
def _apply(self, plan):
desired = plan.desired
changes = plan.changes
encoded_name = _encode_zone_name(desired.name)
self.log.debug(
'_apply: zone=%s, len(changes)=%d', desired.name, len(changes)
)
Expand All @@ -691,7 +702,7 @@ def _apply(self, plan):
self.log.debug('_apply: sending change request')

try:
self._patch(f'zones/{desired.name}', data={'rrsets': mods})
self._patch(f'zones/{encoded_name}', data={'rrsets': mods})
self.log.debug('_apply: patched')
except HTTPError as e:
error = self._get_error(e)
Expand Down Expand Up @@ -736,7 +747,7 @@ def _apply(self, plan):
self.log.debug('_apply: created')

if self.notify:
self._request_notify(desired.name)
self._request_notify(encoded_name)

self.log.debug('_apply: complete')

Expand Down
9 changes: 9 additions & 0 deletions tests/test_octodns_provider_powerdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from octodns_powerdns import (
PowerDnsBaseProvider,
PowerDnsProvider,
_encode_zone_name,
_escape_unescaped_semicolons,
)
from octodns_powerdns.record import PowerDnsLuaRecord, _PowerDnsLuaValue
Expand Down Expand Up @@ -792,3 +793,11 @@ def test_lua_validate(self):
# list w/a bad value
got = _PowerDnsLuaValue.validate([val, {}], PowerDnsLuaRecord._type)
self.assertEqual(['missing type', 'missing script'], got)

def test_encode_zone_name(self):
for expected, value in (
('unit.tests.', 'unit.tests.'),
('another_one.unit.tests.', 'another_one.unit.tests.'),
('128=2F26.2.0.192.in-addr.arpa.', '128/26.2.0.192.in-addr.arpa.'),
):
self.assertEqual(expected, _encode_zone_name(value))
Loading