mirror of
https://github.com/oskar456/dzonegit.git
synced 2024-05-11 05:55:41 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5988fd005e | ||
|
|
7e6376ffb2 | ||
|
|
c3a181be14 | ||
|
|
6f23c066bc |
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
before_install:
|
||||
- sudo apt-get install -y bind9utils
|
||||
language: python
|
||||
python:
|
||||
- "3.5"
|
||||
- "3.6"
|
||||
- "nightly"
|
||||
install:
|
||||
- pip install -e .
|
||||
- pip install pytest
|
||||
script:
|
||||
- pytest
|
||||
sudo: false
|
||||
28
README.rst
28
README.rst
@@ -24,7 +24,7 @@ Requirements
|
||||
------------
|
||||
|
||||
- Python 3.5+
|
||||
- `named-compilezone(8)`_ (part of BIND9 package)
|
||||
- `named-compilezone(8)`_ (part of `bind9utils` package)
|
||||
- git
|
||||
|
||||
|
||||
@@ -95,9 +95,14 @@ JSON template
|
||||
-------------
|
||||
|
||||
The DNS server configuration snippets are generated using a simple JSON-based
|
||||
template. All keys are optional but please make sure the file is a valid
|
||||
JSON file. It is possible to define a zone-specific options, for instance for
|
||||
changing DNSSEC parameters per zone.
|
||||
template. All keys are optional but please make sure the file is a valid JSON
|
||||
file. It is possible to define a zone-specific options, for instance for
|
||||
changing DNSSEC parameters per zone. Those zone-specific options allow usage of
|
||||
wildcards; if exact match of zone name is not found, the leftmost label is
|
||||
substituted with `*`. If still no match is found, the leftmost label is dropped
|
||||
and the second one is again substituted with `*`. In the end, a single `*` is
|
||||
checked. Only if even this key is not found, the value of *defaultvar* is used
|
||||
as the zone-specific option.
|
||||
|
||||
Valid keys are:
|
||||
|
||||
@@ -112,11 +117,14 @@ Valid keys are:
|
||||
|
||||
*defaultvar*
|
||||
A string that would template variable ``$zonevar`` expand to if there is not
|
||||
a zone-specific variable defined.
|
||||
a zone-specific variable defined, nor any wildcard matched.
|
||||
|
||||
*zonevars*
|
||||
An object mapping zone names (without the final dot) to a zone-specific
|
||||
variable to which template variable ``$zonevar`` would expand to.
|
||||
variable to which template variable ``$zonevar`` would expand to. Using
|
||||
wildcards is possible by replacing the leftmost label with `*`. Ultimately,
|
||||
a key with label `*` will match every single zone (making *defaultvar*
|
||||
option litte bit pointless)
|
||||
|
||||
In the template strings, these placeholders are supported:
|
||||
|
||||
@@ -138,12 +146,14 @@ Example JSON template for Knot DNS
|
||||
.. code-block:: json
|
||||
|
||||
{
|
||||
"header": "# Managed by dzonegit, do not edit.\n",
|
||||
"header": "# Managed by dzonegit, do not edit.\nzone:",
|
||||
"footer": "",
|
||||
"item": " - zone: \"$zonename\"\n file: \"$zonefile\"\n $zonevar\n",
|
||||
"item": " - domain: \"$zonename\"\n file: \"$zonefile\"\n $zonevar\n",
|
||||
"defaultvar": "template: default",
|
||||
"zonevars": {
|
||||
"example.com": "template: signed"
|
||||
"example.com": "template: signed",
|
||||
"*.cz": "template: czdomains",
|
||||
"*.in-addr.arpa": "template: ipv4reverse"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
dzonegit.py
28
dzonegit.py
@@ -268,6 +268,23 @@ def replace_serial(path, oldserial, newserial):
|
||||
path.write_text(updated)
|
||||
|
||||
|
||||
def get_zone_wildcards(name):
|
||||
""" A generator of wildcards out of a zone name.
|
||||
For a DNS name, returns series of:
|
||||
- the name itself
|
||||
- the name with first label substitued as *
|
||||
- the name with first label dropped and second substittuted as *
|
||||
- ...
|
||||
- single *
|
||||
"""
|
||||
yield name
|
||||
labels = name.split(".")
|
||||
while labels:
|
||||
labels[0] = "*"
|
||||
yield ".".join(labels)
|
||||
labels.pop(0)
|
||||
|
||||
|
||||
def template_config(checkoutpath, template, blacklist=set(), whitelist=set()):
|
||||
""" Recursively find all *.zone files and template config file using
|
||||
a simple JSON based template like this:
|
||||
@@ -278,7 +295,9 @@ def template_config(checkoutpath, template, blacklist=set(), whitelist=set()):
|
||||
"item": " - zone: \"$zonename\"\n file: \"$zonefile\"\n $zonevar\n",
|
||||
"defaultvar": "template: default",
|
||||
"zonevars": {
|
||||
"example.com": "template: signed"
|
||||
"example.com": "template: signed",
|
||||
"*.com": "template: dotcom",
|
||||
"*": "template: uberdefault"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,7 +342,12 @@ def template_config(checkoutpath, template, blacklist=set(), whitelist=set()):
|
||||
)
|
||||
continue
|
||||
zones[zonename] = f.relative_to(checkoutpath)
|
||||
zonevar = zonevars[zonename] if zonename in zonevars else defaultvar
|
||||
for name in get_zone_wildcards(zonename):
|
||||
if name in zonevars:
|
||||
zonevar = zonevars[name]
|
||||
break
|
||||
else:
|
||||
zonevar = defaultvar
|
||||
out.append(itemtpl.substitute(
|
||||
mapping, zonename=zonename,
|
||||
zonefile=str(f), zonevar=zonevar,
|
||||
|
||||
2
setup.py
2
setup.py
@@ -5,7 +5,7 @@ readme = Path(__file__).with_name("README.rst").read_text()
|
||||
|
||||
setup(
|
||||
name="dzonegit",
|
||||
version="0.1",
|
||||
version="0.2",
|
||||
description="Git hooks to manage a repository of DNS zones",
|
||||
long_description=readme,
|
||||
long_description_content_type="text/x-rst",
|
||||
|
||||
@@ -298,12 +298,14 @@ def test_template_config(git_dir):
|
||||
"item": " - zone: \"$zonename\"\n file: \"$zonefile\"\n $zonevar\n",
|
||||
"defaultvar": "template: default",
|
||||
"zonevars": {
|
||||
"example.com": "template: signed"
|
||||
"example.com": "template: signed",
|
||||
"*": "template: dummy"
|
||||
}
|
||||
}"""
|
||||
output = dzonegit.template_config(str(git_dir), template)
|
||||
assert output.startswith("# Managed by dzonegit")
|
||||
assert " - zone: \"dummy\"\n file: \"" in output
|
||||
assert " template: dummy" in output
|
||||
assert output.endswith("# This is the end")
|
||||
output = dzonegit.template_config(
|
||||
str(git_dir),
|
||||
@@ -317,3 +319,10 @@ def test_load_set_file(git_dir):
|
||||
git_dir.join("dummy").write("dummy\n\n # Comment")
|
||||
s = dzonegit.load_set_file("dummy")
|
||||
assert s == {"dummy"}
|
||||
|
||||
|
||||
def test_get_zone_wildcards():
|
||||
assert list(dzonegit.get_zone_wildcards("a.long.zone.name")) == [
|
||||
"a.long.zone.name", "*.long.zone.name",
|
||||
"*.zone.name", "*.name", "*",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user