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

ZoneFileProvider, read and write support added #42

Merged
merged 7 commits into from
Sep 13, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v0.0.5 - 2023-??-?? - ???

- ZoneFileProvider, full support writing zone files out to disk
- ZoneFileSource.list_zones added to support dynamic zone config

## v0.0.4 - 2023-05-23 - First Stop /etc/hosts
Expand Down
231 changes: 210 additions & 21 deletions octodns_bind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#

import socket
from datetime import datetime
from logging import getLogger
from os import listdir
from os.path import join
from os import listdir, makedirs
from os.path import exists, isdir, join
from string import Template

import dns.name
import dns.query
Expand All @@ -24,8 +26,11 @@


class RfcPopulate:
SUPPORTS_GEO = False
SUPPORTS_DYNAMIC = False
SUPPORTS_GEO = False
SUPPORTS_MULTIVALUE_PTR = True
SUPPORTS_ROOT_NS = True

SUPPORTS = set(
(
'A',
Expand Down Expand Up @@ -53,16 +58,15 @@ def populate(self, zone, target=False, lenient=False):
)

before = len(zone.records)
rrs = self.zone_records(zone)
rrs = self.zone_records(zone, target=target)
for record in Record.from_rrs(zone, rrs, lenient=lenient):
zone.add_record(record, lenient=lenient)

self.log.info(
'populate: found %s records', len(zone.records) - before
)

# TODO: how do we do exists
return True
return self.zone_exists(zone)


class ZoneFileSourceException(Exception):
Expand All @@ -79,21 +83,87 @@ def __init__(self, error):
super().__init__(str(error))


class ZoneFileSource(RfcPopulate, BaseSource):
def __init__(self, id, directory, file_extension='.', check_origin=True):
self.log = getLogger(f'ZoneFileSource[{id}]')
class ZoneFileProvider(RfcPopulate, BaseProvider):
'''
Provider that reads and writes BIND style zone files

config:
class: octodns_bind.ZoneFileProvider

# The location of zone files. Records are defined in a
# file named for the zone in this directory, e.g.
# something.com., including the trailing ., see `file_extension` below
# (required)
directory: ./config

# The extension to use when working with zone files. It is appended onto
# the zone name to determine the file when reading or writing
# records. Some operating systems do not allow filenames ending with a .
# and this value may need to be changed when working on them, e.g. to
# .zone. The leading . should be included.
# (default: .)
file_extension: .

# Wether the provider should check for the existence a root NS record
# when loading a zone
# (default: true)
check_origin: true

# The email username or full address to be used when creating zonefiles.
# If this is just a username, no @[domain.com.], the current zone name
# will be appended to this value. If the value is a complete email
# address it will be used as-is. Note that the actual email address with
# a @ should be used and not the zone file format with the value
# replaced with a `.`.
# (default: webmaster)
hostmaster_email: webmaster

# The details of the SOA record can be customized when creating
# zonefiles with the following options.
default_ttl: 3600
refresh: 3600
retry: 600
expire: 604800
nxdomain: 3600
'''

def __init__(
self,
id,
directory,
file_extension='.',
check_origin=True,
hostmaster_email='webmaster',
default_ttl=3600,
refresh=3600,
retry=600,
expire=604800,
nxdomain=3600,
):
self.log = getLogger(f'ZoneFileProvider[{id}]')
self.log.debug(
'__init__: id=%s, directory=%s, file_extension=%s, '
'check_origin=%s',
'__init__: id=%s, directory=%s, file_extension=%s, check_origin=%s, hostmaster_email=%s, default_ttl=%d, refresh=%d, retry=%d, expire=%d, nxdomain=%d',
id,
directory,
file_extension,
check_origin,
hostmaster_email,
default_ttl,
refresh,
retry,
expire,
nxdomain,
)
super().__init__(id)
self.directory = directory
self.file_extension = file_extension
self.check_origin = check_origin
self.hostmaster_email = hostmaster_email
self.default_ttl = default_ttl
self.refresh = refresh
self.retry = retry
self.expire = expire
self.nxdomain = nxdomain

self._zone_records = {}

Expand All @@ -105,7 +175,12 @@ def list_zones(self):
filename = filename[:-n]
yield filename

def _load_zone_file(self, zone_name):
def _load_zone_file(self, zone_name, target):
if target:
# if we're in target mode we assume nothing exists b/c we recreate
# everything every time, similar to YamlProvider
return None

zone_filename = f'{zone_name[:-1]}{self.file_extension}'
zonefiles = listdir(self.directory)
path = join(self.directory, zone_filename)
Expand All @@ -124,22 +199,132 @@ def _load_zone_file(self, zone_name):

return z

def zone_records(self, zone):
def zone_exists(self, zone):
zone_filename = f'{zone.name[:-1]}{self.file_extension}'
return exists(join(self.directory, zone_filename))

def zone_records(self, zone, target):
if zone.name not in self._zone_records:
z = self._load_zone_file(zone.name)
z = self._load_zone_file(zone.name, target)

records = []
for name, ttl, rdata in z.iterate_rdatas():
rdtype = dns.rdatatype.to_text(rdata.rdtype)
if rdtype in self.SUPPORTS:
records.append(
Rr(name.to_text(), rdtype, ttl, rdata.to_text())
)
if z:
for name, ttl, rdata in z.iterate_rdatas():
rdtype = dns.rdatatype.to_text(rdata.rdtype)
if rdtype in self.SUPPORTS:
records.append(
Rr(name.to_text(), rdtype, ttl, rdata.to_text())
)

self._zone_records[zone.name] = records

return self._zone_records[zone.name]

def _primary_nameserver(self, decoded_name, records):
for record in records:
if record.name == '' and record._type == 'NS':
return record.values[0]
self.log.warning(
'_primary_nameserver: unable to find a primary_nameserver for %s, using placeholder',
decoded_name,
)
return f'ns.{decoded_name}'

def _hostmaster_email(self, decoded_name):
pieces = self.hostmaster_email.split('@')
# escape any .'s in the email username
pieces[0] = pieces[0].replace('.', '\\.')
if len(pieces) == 2:
return '.'.join(pieces)

return f'{pieces[0]}.{decoded_name}'

def _longest_name(self, records):
try:
return sorted(len(r.name) for r in records)[-1]
except IndexError:
return 0

def _now(self):
return datetime.utcnow()

def _serial(self):
# things wrap/reset at max int
return int(self._now().timestamp()) % 2147483647

def _apply(self, plan):
desired = plan.desired
name = desired.decoded_name

if not isdir(self.directory):
makedirs(self.directory)

records = sorted(c.record for c in plan.changes)
longest_name = self._longest_name(records)

filename = join(self.directory, f'{name[:-1]}{self.file_extension}')
with open(filename, 'w') as fh:
template = Template(
'''$$ORIGIN $zone_name

@ $default_ttl IN SOA $primary_nameserver $hostmaster_email (
$serial ; Serial
$refresh ; Refresh
$retry ; Retry
$expire ; Expire
$nxdomain ; NXDOMAIN ttl
)

'''
)

primary_nameserver = self._primary_nameserver(name, records)
fh.write(
template.substitute(
{
'hostmaster_email': self._hostmaster_email(name),
'serial': self._serial(),
'zone_name': name,
'default_ttl': self.default_ttl,
'primary_nameserver': primary_nameserver,
'refresh': self.refresh,
'retry': self.retry,
'expire': self.expire,
'nxdomain': self.nxdomain,
}
)
)

prev_name = None
for record in records:
try:
values = record.values
except AttributeError:
values = [record.value]
for value in values:
value = value.rdata_text
if record._type in ('SPF', 'TXT'):
# TXT values need to be quoted
value = value.replace('"', '\\"')
value = f'"{value}"'
name = '@' if record.name == '' else record.name
if name == prev_name:
name = ''
else:
prev_name = name
fh.write(
f'{name:<{longest_name}} {record.ttl:8d} IN {record._type:<8} {value}\n'
)

self.log.debug(
'_apply: zone=%s, num_records=%d', name, len(plan.changes)
)

return True


ZoneFileSource = ZoneFileProvider


class AxfrSourceException(Exception):
pass
Expand Down Expand Up @@ -210,7 +395,11 @@ def _auth_params(self):
params['keyalgorithm'] = self.key_algorithm
return params

def zone_records(self, zone):
def zone_exists(self, zone):
# We can't create them so they have to already exist
return True

def zone_records(self, zone, target):
auth_params = self._auth_params()
try:
z = dns.zone.from_xfr(
Expand Down
Loading
Loading