From c1ad072f8e125325e9bdbf34af907e3f9f7206dd Mon Sep 17 00:00:00 2001 From: mwiegand Date: Sun, 20 Jun 2021 12:57:16 +0200 Subject: [PATCH] wip --- bundles/bind/files/db | 8 +++-- bundles/bind/files/named.conf.local | 8 +++++ bundles/bind/items.py | 45 ++++++++++++++++++++++++++++- bundles/bind/metadata.py | 42 +++++++++++++++++++++++++-- bundles/mailserver/metadata.py | 2 +- nodes/htz.mails.py | 1 + 6 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 bundles/bind/files/named.conf.local diff --git a/bundles/bind/files/db b/bundles/bind/files/db index 519c01e..3b01c26 100644 --- a/bundles/bind/files/db +++ b/bundles/bind/files/db @@ -13,9 +13,13 @@ $TTL 600 % for record in sorted(records, key=lambda r: (r['name'], r['type'], r['value'])): % for part in (record['value'][i:i+255] for i in range(0, len(record['value']), 255)): -${record['name'].rjust(column_width('name', records))} \ +${record['name'].ljust(column_width('name', records))} \ IN \ ${record['type'].ljust(column_width('type', records))} \ -"${part}" + % if record['type'] == 'TXT': + "${part}" + % else: + ${part} + % endif % endfor % endfor diff --git a/bundles/bind/files/named.conf.local b/bundles/bind/files/named.conf.local new file mode 100644 index 0000000..b81b6d8 --- /dev/null +++ b/bundles/bind/files/named.conf.local @@ -0,0 +1,8 @@ +% for zone in zones: +zone "${zone}" { + type master; + file "/var/lib/bind/db.${zone}"; +}; +% endfor + +// include "/etc/bind/zones.rfc1918"; diff --git a/bundles/bind/items.py b/bundles/bind/items.py index d5da4d3..4414cbc 100644 --- a/bundles/bind/items.py +++ b/bundles/bind/items.py @@ -1,16 +1,59 @@ directories['/var/lib/bind'] = { 'purge': True, + 'needed_by': [ + 'svc_systemd:bind9', + ], + 'triggers': [ + 'svc_systemd:bind9:restart', + ], } files['/etc/default/bind9'] = { 'source': 'defaults', + 'needed_by': [ + 'svc_systemd:bind9', + ], + 'triggers': [ + 'svc_systemd:bind9:restart', + ], +} + +files['/etc/bind/named.conf.local'] = { + 'content_type': 'mako', + 'context': { + 'zones': sorted(node.metadata.get('bind/zones')), + }, + 'owner': 'root', + 'group': 'bind', + 'needed_by': [ + 'svc_systemd:bind9', + ], + 'triggers': [ + 'svc_systemd:bind9:restart', + ], } for zone, records in node.metadata.get('bind/zones').items(): files[f'/var/lib/bind/db.{zone}'] = { + 'group': 'bind', 'source': 'db', 'content_type': 'mako', 'context': { 'records': records, - } + }, + 'needed_by': [ + 'svc_systemd:bind9', + ], + 'triggers': [ + 'svc_systemd:bind9:restart', + ], } + +svc_systemd['bind9'] = {} + +actions['named-checkconf'] = { + 'command': 'named-checkconf -z', + 'needs': [ + 'svc_systemd:bind9', + ] +} diff --git a/bundles/bind/metadata.py b/bundles/bind/metadata.py index 37b3f6f..b5845c6 100644 --- a/bundles/bind/metadata.py +++ b/bundles/bind/metadata.py @@ -1,3 +1,6 @@ +from ipaddress import ip_interface + + defaults = { 'apt': { 'packages': { @@ -10,22 +13,40 @@ defaults = { } +@metadata_reactor.provides( + 'bind/zones', +) +def dns(metadata): + return { + 'dns': { + 'ns.sublimity.de': { + 'A': [ + str(ip_interface(metadata.get('network/ipv4')).ip) + ], + 'AAAA': [ + str(ip_interface(metadata.get('network/ipv6')).ip) + ] + }, + }, + } + + @metadata_reactor.provides( 'bind/zones', ) def collect_records(metadata): - zones = metadata.get('bind/zones') + zones = {} for other_node in repo.nodes: for fqdn, records in other_node.metadata.get('dns').items(): matching_zones = sorted( filter( lambda potential_zone: fqdn.endswith(potential_zone), - zones + metadata.get('bind/zones').keys() ), key=len, ) - + if matching_zones: zone = matching_zones[0] else: @@ -46,3 +67,18 @@ def collect_records(metadata): 'zones': zones, }, } + + +@metadata_reactor.provides( + 'bind/zones', +) +def ns_records(metadata): + return { + 'bind': { + 'zones': { + zone: [ + {'name': '', 'type': 'NS', 'value': f"{metadata.get('bind/domain')}."}, + ] for zone in metadata.get('bind/zones').keys() + }, + }, + } diff --git a/bundles/mailserver/metadata.py b/bundles/mailserver/metadata.py index 23d5416..1204186 100644 --- a/bundles/mailserver/metadata.py +++ b/bundles/mailserver/metadata.py @@ -45,7 +45,7 @@ def dns(metadata): for domain in metadata.get('mailserver/domains'): dns[domain] = { - 'MX': [domain], + 'MX': [f'5 {domain}'], 'TXT': ['v=spf1 a mx -all'], } diff --git a/nodes/htz.mails.py b/nodes/htz.mails.py index 4bf6164..009119e 100644 --- a/nodes/htz.mails.py +++ b/nodes/htz.mails.py @@ -15,6 +15,7 @@ ], 'metadata': { 'bind': { + 'domain': 'ns.sublimity.de', 'zones': { 'mail2.sublimity.de': [], 'sublimity.de': [],