Commit 6ad6698f authored by Andreas Kempe's avatar Andreas Kempe
Browse files

Add basic templating for the bind configuration files.

This adds a basic generator that takes Python data structures and
outputs what should be functional bind configuration files.

TODO: Actually connect to an SQL server and fetch the data to put into
the templates.
parent cc8946d4
#!/usr/bin/env python3
from jinja2 import FileSystemLoader, Environment
# Represents the client key for updating a DNS record.
class Key:
def __init__(self, name, algorithm, secret):
self.name = name
self.algorithm = algorithm
self.secret = secret
# Represents a zone configuration in named.conf.
class Zone:
def __init__(self, name, keys, zonefile):
self.name = name
self.keys = keys
self.zonefile = zonefile
# Represents an update policy. Key is allowed to update domain.
class ZoneKey:
def __init__(self, key, domain):
self.key = key
self.domain = domain
# Represents a master zone file.
class ZoneFile:
def __init__(self, domain, primary_nameserver,
nameserver, hostmaster, recordlist):
self.domain = domain
self.primary_nameserver = primary_nameserver
self.nameserver = nameserver
self.hostmaster = hostmaster
self.recordlist = recordlist
# Represents DNS records to put in the master zone file.
class Record:
def __init__(self, subdomain, ttl, type, address):
self.subdomain = subdomain
self.ttl = ttl
self.type = type
self.address = address
if __name__ == '__main__':
env = Environment(loader = FileSystemLoader('.'))
key_template = env.get_template('key.template')
key = Key('kempe.se', 'hmac-md5',
'L+AwXRn7e+gVXDfsK9EelBrcKUCeygeKE3hywXHQzqvjh8zpXSVja+gp6X9iYgqODXgNYCvu2XExy4WZFlYzjw==')
rendered_key = key_template.render(keylist = [ key ])
zone_template = env.get_template('zone.template')
zone_key = ZoneKey(key.name, 'test.kempe.se')
zone_key1 = ZoneKey(key.name, 'test1.kempe.se')
zone = Zone('kempe.se', [ zone_key, zone_key1 ], '/usr/local/etc/namedb/master/kempe.se')
rendered_zone = zone_template.render(zonelist = [ zone ])
zonefile_template = env.get_template('zonefile.template')
record = Record('test', '300', 'A', '10.0.2.2')
record1 = Record('test1', '3000', 'A', '10.2.2.2')
zonefile = ZoneFile('kempe.se', 'dns.lysator.liu.se', 'dns.lysator.liu.se',
'hostmaster.kempe.se', [ record, record1 ])
rendered_zonefile = zonefile_template.render(domain = zonefile.domain,
primary_nameserver = zonefile.primary_nameserver,
nameserver = zonefile.nameserver,
hostmaster = zonefile.hostmaster,
recordlist = zonefile.recordlist)
print(rendered_key)
print(rendered_zone)
print(rendered_zonefile)
{% for key in keylist %}
key "{{ key.name }}" {
algorithm {{ key.algorithm }};
secret "{{ key.secret }}";
};
{% endfor %}
{% for zone in zonelist %}
zone "{{ zone.name }}" {
type master;
update-policy {
{% for key in zone.keys %} grant "{{ key.key }}" name "{{ key.domain }}.";
{% endfor %}
};
file "{{ zone.zonefile }}";
};
{% endfor %}
$ORIGIN .
$TTL 86400 ; 1 day
{{ domain }} IN SOA {{ primary_nameserver }}. {{ hostmaster }}. (
7 ; serial
21600 ; refresh (6 hours)
3600 ; retry (1 hour)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS {{ nameserver }}
$ORIGIN {{ domain }}.
$TTL 86400
{% for record in recordlist %}{{ record.subdomain }} {{ record.ttl }} {{ record.type }} {{ record.address }}
{% endfor %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment