usr_conf_data_handlers.py 16.9 KB
Newer Older
1
from colorlog import info, warning as warn, error
2
from typing import List, Dict, Union, Any
3 4 5

from yangson.instance import InstanceRoute, ObjectValue, EntryKeys, MemberName
from . import knot_api
6 7
from .data import BaseDataListener, SchemaNode, ChangeType, DataChange, ConfHandlerResult
from .helpers import PathFormat, ErrorHelpers, LogHelpers, DataHelpers
8
from .knot_api import RRecordBase, SOARecord, ARecord, AAAARecord, NSRecord, MXRecord
9

10
JsonNodeT = Union[Dict[str, Any], List]
11 12 13 14 15 16 17 18 19
epretty = ErrorHelpers.epretty
debug_confh = LogHelpers.create_module_dbg_logger(__name__)


class KnotConfServerListener(BaseDataListener):
    def process(self, sn: SchemaNode, ii: InstanceRoute, ch: DataChange):
        debug_confh(self.__class__.__name__ + " triggered")

        base_ii_str = self.schema_path
20
        base_ii = DataHelpers.parse_ii(base_ii_str, PathFormat.URL)
21
        base_nv = self.ds.get_data_root().goto(base_ii).value
22 23 24 25 26 27 28

        knot_api.KNOT.begin()

        knot_api.KNOT.set_item(section="server", item="comment", data=base_nv.get("description"))
        knot_api.KNOT.set_item(section="server", item="async-start", data=base_nv.get("knot-dns:async-start"))
        knot_api.KNOT.set_item(section="server", item="nsid", data=base_nv.get("nsid-identity", {}).get("nsid"))

29
        listen_endpoints = base_nv.get("listen-endpoint", [])
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        ep_str_list = []
        for ep in listen_endpoints:
            ep_str = ep["ip-address"]
            if ep.get("port"):
                ep_str += "@" + str(ep["port"])
            ep_str_list.append(ep_str)
        knot_api.KNOT.set_item_list(section="server", item="listen", data=ep_str_list)

        knot_api.KNOT.set_item(section="server", item="rundir", data=base_nv.get("filesystem-paths", {}).get("run-time-dir"))
        knot_api.KNOT.set_item(section="server", item="pidfile", data=base_nv.get("filesystem-paths", {}).get("pid-file"))
        knot_api.KNOT.set_item(section="server", item="tcp-workers", data=base_nv.get("resources", {}).get("knot-dns:tcp-workers"))
        knot_api.KNOT.set_item(section="server", item="udp-workers", data=base_nv.get("resources", {}).get("knot-dns:udp-workers"))
        knot_api.KNOT.set_item(section="server", item="rate-limit-table-size", data=base_nv.get("response-rate-limiting", {}).get("table-size"))

        knot_api.KNOT.commit()
        return ConfHandlerResult.OK


class KnotConfLogListener(BaseDataListener):
    def process(self, sn: SchemaNode, ii: InstanceRoute, ch: DataChange):
        debug_confh(self.__class__.__name__ + " triggered")

        base_ii_str = self.schema_path
54
        base_ii = DataHelpers.parse_ii(base_ii_str, PathFormat.URL)
55
        base_nv = self.ds.get_data_root().goto(base_ii).value
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

        knot_api.KNOT.begin()
        knot_api.KNOT.set_item(section="log", data=None)

        for logitem in base_nv:
            tgt = logitem.get("target")
            if tgt is None:
                continue

            knot_api.KNOT.set_item(section="log", item="target", data=tgt)
            knot_api.KNOT.set_item(section="log", identifier=tgt, item="comment", data=logitem.get("description"))
            knot_api.KNOT.set_item(section="log", identifier=tgt, item="server", data=logitem.get("server"))
            knot_api.KNOT.set_item(section="log", identifier=tgt, item="zone", data=logitem.get("zone"))
            knot_api.KNOT.set_item(section="log", identifier=tgt, item="any", data=logitem.get("any"))

        knot_api.KNOT.commit()
        return ConfHandlerResult.OK


class KnotConfZoneListener(BaseDataListener):
    def process(self, sn: SchemaNode, ii: InstanceRoute, ch: DataChange):
        debug_confh(self.__class__.__name__ + " triggered")

        # ii_str = "".join([str(seg) for seg in ii])
        base_ii_str = self.schema_path
81
        base_ii = DataHelpers.parse_ii(base_ii_str, PathFormat.URL)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        knot_api.KNOT.begin()

        # Create new zone
        if (ii == base_ii) and (ch.change_type == ChangeType.CREATE):
            domain = ch.data["zone"]["domain"]
            debug_confh("Creating new zone \"{}\"".format(domain))
            knot_api.KNOT.zone_new(domain)
        # Delete zone
        elif (len(ii) == (len(base_ii) + 2)) and isinstance(ii[len(base_ii) + 1], EntryKeys) and (ch.change_type == ChangeType.DELETE):
            domain = ii[len(base_ii) + 1].keys["domain"]
            debug_confh("Deleting zone \"{}\"".format(domain))
            knot_api.KNOT.zone_remove(domain)
        # Edit particular zone
        elif (len(ii) >= (len(base_ii) + 2)) and isinstance(ii[len(base_ii)], MemberName) and isinstance(ii[len(base_ii) + 1], EntryKeys):
            domain = ii[len(base_ii) + 1].keys["domain"]
            debug_confh("Editing config of zone \"{}\"".format(domain))

            # Write whole zone config to Knot
101
            zone_nv = self.ds.get_data_root().goto(ii[0:(len(base_ii) + 1)]).value
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            knot_api.KNOT.set_item(section="zone", zone=domain, item="comment", data=zone_nv.get("description"))
            knot_api.KNOT.set_item(section="zone", zone=domain, item="file", data=zone_nv.get("file"))
            knot_api.KNOT.set_item_list(section="zone", zone=domain, item="master", data=zone_nv.get("master"))
            knot_api.KNOT.set_item_list(section="zone", zone=domain, item="notify", data=zone_nv.get("notify", {}).get("recipient"))
            knot_api.KNOT.set_item_list(section="zone", zone=domain, item="acl", data=zone_nv.get("access-control-list"))
            knot_api.KNOT.set_item(section="zone", zone=domain, item="serial-policy", data=zone_nv.get("serial-update-method"))

            anytotcp = zone_nv.get("any-to-tcp")
            disable_any_str = str(not anytotcp) if isinstance(anytotcp, bool) else None
            knot_api.KNOT.set_item(section="zone", zone=domain, item="disable-any", data=disable_any_str)

            knot_api.KNOT.set_item(section="zone", zone=domain, item="max-journal-size", data=zone_nv.get("journal", {}).get("maximum-journal-size"))
            knot_api.KNOT.set_item(section="zone", zone=domain, item="zonefile-sync", data=zone_nv.get("journal", {}).get("zone-file-sync-delay"))
            knot_api.KNOT.set_item(section="zone", zone=domain, item="ixfr-from-differences", data=zone_nv.get("journal", {}).get("from-differences"))

            qms = zone_nv.get("query-module")
            if qms is not None:
                qm_str_list = list(map(lambda n: n["name"] + "/" + n["type"], qms))
            else:
                qm_str_list = None
            knot_api.KNOT.set_item_list(section="zone", zone=domain, item="module", data=qm_str_list)
            knot_api.KNOT.set_item(section="zone", zone=domain, item="semantic-checks", data=zone_nv.get("knot-dns:semantic-checks"))

        knot_api.KNOT.commit()
        return ConfHandlerResult.OK


class KnotConfControlListener(BaseDataListener):
    def process(self, sn: SchemaNode, ii: InstanceRoute, ch: DataChange):
        debug_confh(self.__class__.__name__ + " triggered")

        base_ii_str = self.schema_path
134
        base_ii = DataHelpers.parse_ii(base_ii_str, PathFormat.URL)
135
        base_nv = self.ds.get_data_root().goto(base_ii).value
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

        knot_api.KNOT.begin()
        knot_api.KNOT.set_item(section="control", item="listen", data=base_nv.get("unix"))
        knot_api.KNOT.commit()
        return ConfHandlerResult.OK


class KnotConfAclListener(BaseDataListener):
    def _process_list_item(self, acl_nv: ObjectValue):
        name = acl_nv.get("name")
        debug_confh("ACL name={}".format(name))
        knot_api.KNOT.set_item(section="acl", identifier=name, data=None)
        knot_api.KNOT.set_item(section="acl", item="id", data=name)
        knot_api.KNOT.set_item(section="acl", identifier=name, item="comment", data=acl_nv.get("description"))
        knot_api.KNOT.set_item_list(section="acl", identifier=name, item="key", data=acl_nv.get("key"))
        knot_api.KNOT.set_item_list(section="acl", identifier=name, item="action", data=acl_nv.get("operation"))

        netws = acl_nv.get("network")
        if netws is not None:
            addrs = list(map(lambda n: n["ip-prefix"], netws))
            knot_api.KNOT.set_item_list(section="acl", identifier=name, item="address", data=addrs)

        action = acl_nv.get("action")
        deny = "true" if action == "deny" else "false"
        knot_api.KNOT.set_item(section="acl", identifier=name, item="deny", data=deny)

    def process(self, sn: SchemaNode, ii: InstanceRoute, ch: DataChange):
        debug_confh(self.__class__.__name__ + " triggered")

        base_ii_str = self.schema_path
166
        base_ii = DataHelpers.parse_ii(base_ii_str, PathFormat.URL)
167
        base_nv = self.ds.get_data_root().goto(base_ii).value
168 169 170 171 172 173

        knot_api.KNOT.begin()
        knot_api.KNOT.set_item(section="acl", data=None)

        if (len(ii) > len(base_ii)) and isinstance(ii[len(base_ii)], EntryKeys):
            # Write only changed list item
174
            acl_nv = self.ds.get_data_root().goto(ii[0:(len(base_ii) + 1)]).value
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
            print("acl nv={}".format(acl_nv))
            self._process_list_item(acl_nv)
        else:
            # Delete all list items from KNOT
            knot_api.KNOT.set_item(section="acl", data=None)
            # Write whole list
            for acl_nv in base_nv:
                print("acl nv={}".format(acl_nv))
                self._process_list_item(acl_nv)

        knot_api.KNOT.commit()
        return ConfHandlerResult.OK


class KnotZoneDataListener(BaseDataListener):
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    # Create RR object from "rdata" json node
    @staticmethod
    def _rr_from_rdata_item(domain_name: str, rr_owner: str, rr_ttl: int, rr_type: str, rdata_item: JsonNodeT) -> RRecordBase:
        try:
            if rr_type == "A":
                new_rr = ARecord(domain_name, rr_ttl)
                new_rr.owner = rr_owner
                new_rr.address = rdata_item["A"]["address"]
            elif rr_type == "AAAA":
                new_rr = AAAARecord(domain_name, rr_ttl)
                new_rr.owner = rr_owner
                new_rr.address = rdata_item["AAAA"]["address"]
            elif rr_type == "NS":
                new_rr = NSRecord(domain_name, rr_ttl)
                new_rr.owner = rr_owner
                new_rr.nsdname = rdata_item["NS"]["nsdname"]
            elif rr_type == "MX":
                new_rr = MXRecord(domain_name, rr_ttl)
                new_rr.owner = rr_owner
                new_rr.exchange = rdata_item["MX"]["exchange"]
            else:
                new_rr = None
        except KeyError:
            new_rr = None

        return new_rr

217 218 219 220
    def process(self, sn: SchemaNode, ii: InstanceRoute, ch: DataChange):
        debug_confh(self.__class__.__name__ + " triggered")

        base_ii_str = "/dns-zones:zone-data"
221
        base_ii = DataHelpers.parse_ii(base_ii_str, PathFormat.URL)
222 223 224 225 226 227 228 229
        base_ii_len = len(base_ii)
        ii_str = "".join([str(seg) for seg in ii])

        debug_confh("ZoneDataHandler: Change at sn \"{}\", dn \"{}\"".format(sn.name, ii_str))

        # Create new zone with SOA in zone data section
        if (ii == base_ii) and (ch.change_type == ChangeType.CREATE):
            domain_name = ch.data["zone"]["name"]
230
            def_ttl = ch.data["zone"]["default-ttl"]
231 232 233 234 235 236

            soa = ch.data.get("zone", {}).get("SOA")
            if soa is None:
                return ConfHandlerResult.ERROR

            soarr = SOARecord()
237
            soarr.ttl = def_ttl
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
            soarr.mname = soa["mname"]
            soarr.rname = soa["rname"]
            soarr.serial = soa["serial"]
            soarr.refresh = soa["refresh"]
            soarr.retry = soa["retry"]
            soarr.expire = soa["expire"]
            soarr.minimum = soa["minimum"]

            debug_confh("KnotApi: adding new SOA RR to zone \"{}\"".format(domain_name))
            knot_api.KNOT.begin_zone()
            knot_api.KNOT.zone_add_record(domain_name, soarr)
            knot_api.KNOT.commit_zone()

        # Add resource record to particular zone
        elif (
                len(ii) == (base_ii_len + 2)) \
254
                and isinstance(ii[base_ii_len], MemberName) and (ii[base_ii_len].key == "zone") \
255
                and isinstance(ii[base_ii_len + 1], EntryKeys) \
256 257
                and (ch.change_type == ChangeType.CREATE) \
                and (ch.data.get("rrset") is not None):
258 259
            domain_name = ii[base_ii_len + 1].keys["name"]
            rr = ch.data.get("rrset", {})
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
            rr_owner = rr["owner"]
            rr_type = rr.get("type").split(":")[-1]
            rr_ttl = rr.get("ttl")
            if rr_ttl is None:
                # Obtain default ttl from datastore if not specified explicitly
                rr_ttl = self.ds.get_data_root().goto(ii[0:3]).value["default-ttl"]

            for rdata_item in rr["rdata"]:
                new_rr = self._rr_from_rdata_item(domain_name, rr_owner, rr_ttl, rr_type, rdata_item)
                if new_rr is not None:
                    debug_confh("KnotApi: adding new {} RR to zone \"{}\"".format(rr_type, domain_name))
                    knot_api.KNOT.begin_zone()
                    knot_api.KNOT.zone_add_record(domain_name, new_rr)
                    knot_api.KNOT.commit_zone()

        # Add resource record to particular zone (only specific "rdata" item)
        elif (
                len(ii) == (base_ii_len + 4)) \
278
                and isinstance(ii[base_ii_len], MemberName) and (ii[base_ii_len].key == "zone") \
279
                and isinstance(ii[base_ii_len + 1], EntryKeys) \
280
                and isinstance(ii[base_ii_len + 2], MemberName) and (ii[base_ii_len + 2].key == "rrset") \
281 282 283 284 285 286 287 288
                and isinstance(ii[base_ii_len + 3], EntryKeys) \
                and (ch.change_type == ChangeType.CREATE) \
                and (ch.data.get("rdata") is not None):
            domain_name = ii[base_ii_len + 1].keys["name"]
            rdata_item = ch.data.get("rdata", {})
            keys_ii_seg = ii[len(base_ii) + 3]
            rr_owner = keys_ii_seg.keys["owner"]
            rr_type = keys_ii_seg.keys["type"][0].split(":")[-1]
289

290 291 292 293 294 295 296
            # Try to use record-specific ttl first
            rr_ttl = self.ds.get_data_root().goto(ii).value.get("ttl")
            if rr_ttl is None:
                # Obtain default ttl from datastore if not specified explicitly
                rr_ttl = self.ds.get_data_root().goto(ii[0:3]).value["default-ttl"]

            new_rr = self._rr_from_rdata_item(domain_name, rr_owner, rr_ttl, rr_type, rdata_item)
297 298

            if new_rr is not None:
299
                debug_confh("KnotApi: adding new {} RR to zone \"{}\"".format(rr_type, domain_name))
300 301 302 303 304 305 306
                knot_api.KNOT.begin_zone()
                knot_api.KNOT.zone_add_record(domain_name, new_rr)
                knot_api.KNOT.commit_zone()

        # Delete resource record from particular zone
        elif (
                len(ii) == (base_ii_len + 4)) \
307
                and isinstance(ii[base_ii_len], MemberName) and (ii[base_ii_len].key == "zone") \
308
                and isinstance(ii[base_ii_len + 1], EntryKeys) \
309
                and isinstance(ii[base_ii_len + 2], MemberName) and (ii[base_ii_len + 2].key == "rrset") \
310 311 312 313 314 315 316
                and isinstance(ii[base_ii_len + 3], EntryKeys) \
                and (ch.change_type == ChangeType.DELETE):
            domain_name = ii[base_ii_len + 1].keys["name"]
            keys_ii_seg = ii[len(base_ii) + 3]
            rr_owner = keys_ii_seg.keys["owner"]
            rr_type = keys_ii_seg.keys["type"][0]

317
            debug_confh("KnotApi: deleting {} RR from zone \"{}\"".format(rr_type, domain_name))
318 319 320 321
            knot_api.KNOT.begin_zone()
            knot_api.KNOT.zone_del_record(domain_name, rr_owner, rr_type)
            knot_api.KNOT.commit_zone()

322 323 324
        # Delete resource record from particular zone (only specific "rdata" item)
        elif (
                len(ii) == (base_ii_len + 6)) \
325
                and isinstance(ii[base_ii_len], MemberName) and (ii[base_ii_len].key == "zone") \
326
                and isinstance(ii[base_ii_len + 1], EntryKeys) \
327
                and isinstance(ii[base_ii_len + 2], MemberName) and (ii[base_ii_len + 2].key == "rrset") \
328
                and isinstance(ii[base_ii_len + 3], EntryKeys) \
329
                and isinstance(ii[base_ii_len + 4], MemberName) and (ii[base_ii_len + 4].key == "rdata") \
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
                and isinstance(ii[base_ii_len + 5], EntryKeys) \
                and (ch.change_type == ChangeType.DELETE):
            domain_name = ii[base_ii_len + 1].keys["name"]
            keys_ii_seg = ii[len(base_ii) + 3]
            rr_owner = keys_ii_seg.keys["owner"]
            rr_type = keys_ii_seg.keys["type"][0]
            rdata_item = self.ds.get_data_root(previous_version=1).goto(ii).value

            rr_sel = self._rr_from_rdata_item(domain_name, rr_owner, 0, rr_type, rdata_item)

            debug_confh("KnotApi: deleting {} RR from zone \"{}\"".format(rr_type, domain_name))
            knot_api.KNOT.begin_zone()
            knot_api.KNOT.zone_del_record(domain_name, rr_owner, rr_type, selector=rr_sel.rrdata_format())
            knot_api.KNOT.commit_zone()

345 346 347 348
        else:
            return ConfHandlerResult.ERROR

        return ConfHandlerResult.OK