]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding methods get_api_zone() and is_local() to pp_lib/pdns_app.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jan 2018 14:45:11 +0000 (15:45 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jan 2018 14:45:11 +0000 (15:45 +0100)
pp_lib/pdns_app.py

index 5988e8752542049a31c5cf0980fd1c475728b7ca..1e6f2405badd0048486d0a5a3debb8d7f986a58d 100644 (file)
@@ -29,7 +29,7 @@ from .common import pp, to_bool
 from .cfg_app import PpCfgAppError, PpConfigApplication
 from .pdns_zone import PdnsApiZone
 
-__version__ = '0.4.2'
+__version__ = '0.5.1'
 LOG = logging.getLogger(__name__)
 _LIBRARY_NAME = "pp-pdns-api-client"
 
@@ -576,6 +576,94 @@ class PpPDNSApplication(PpConfigApplication):
 
         return zone_list
 
+    # -------------------------------------------------------------------------
+    def get_api_zone(self, zone_name):
+
+        zone_unicode = zone_name
+        json_response = None
+        zout = "{!r}".format(zone_name)
+        if 'xn--' in zone_name:
+            zone_unicode = zone_name.encode('idna').decode('idna')
+            zout = "{!r} ({})".format(zone_name, zone_unicode)
+        LOG.debug("Trying to get complete information about zone {!r} ...".format(zone_name))
+
+        path = "/servers/{}/zones/{}".format(self.api_servername, zone_name)
+        try:
+            json_response = self.perform_request(path)
+        except (PDNSApiNotFoundError, PDNSApiValidationError) as e:
+            LOG.error("The given zone {} was not found.".format(zout))
+            return None
+        if self.verbose > 2:
+            LOG.debug("Got a response:\n{}".format(pp(json_response)))
+
+        zone = PdnsApiZone.init_from_dict(
+            json_response, appname=self.appname, verbose=self.verbose,  base_dir=self. base_dir)
+        if self.verbose > 2:
+            LOG.debug("Zone object:\n{}".format(pp(zone.as_dict())))
+
+        return zone
+
+    # -------------------------------------------------------------------------
+    def is_local(self, domain):
+
+        if self.verbose > 1:
+            LOG.debug("Checking, whether {!r} is a not public zone.".format(domain))
+
+        tld = domain.split('.')[-1]
+        if tld in ('intern', 'internal', 'local', 'localdomain', 'lokal'):
+            LOG.debug("Zone {!r} has a local TLD {!r}.".format(domain, tld))
+            return True
+
+        zone_base = domain.split('.')[0]
+        if zone_base in ('intern', 'internal', 'local', 'localdomain', 'lokal'):
+            LOG.debug("Zone {!r} has a local base {!r}.".format(domain, tld))
+            return True
+
+        if tld != 'arpa':
+            if self.verbose > 2:
+                LOG.debug("Zone {!r} has a public TLD {!r}.".format(domain, tld))
+                return False
+
+        if domain.endswith('.in-addr.arpa'):
+            tupels = []
+            for tupel in reversed(domain.replace('.in-addr.arpa', '').split('.')):
+                tupels.append(tupel)
+            if self.verbose > 2:
+                LOG.debug("Got IPv4 tupels from zone {!r}: {}".format(domain, pp(tupels)))
+            bitmask = None
+            if len(tupels) == 1:
+                bitmask = 8
+                tupels.append('0')
+                tupels.append('0')
+                tupels.append('0')
+            elif len(tupels) == 2:
+                tupels.append('0')
+                tupels.append('0')
+                bitmask = 16
+            elif len(tupels) == 3:
+                bitmask = 24
+                tupels.append('0')
+            else:
+                LOG.warn("Could not interprete reverse IPv4 zone {!r}.".format(domain))
+                return False
+            net_address = '.'.join(tupels) + '/{}'.format(bitmask)
+            if self.verbose > 2:
+                LOG.debug("Got IPv4 network address of zone {!r}: {!r}.".format(domain, net_address))
+            network = ipaddress.ip_network(net_address)
+            if network.is_global:
+                if self.verbose > 1:
+                    LOG.debug("The network {!r} of zone {!r} is allocated for public networks.".format(
+                        net_address, domain))
+                return False
+            LOG.debug("The network {!r} of zone {!r} is allocated for local networks.".format(
+                net_address, domain))
+            return True
+
+        if self.verbose > 2:
+            LOG.debug("Zone {!r} seems to be a reverse zone for a public network.".format(domain))
+        return False
+
+
 # =============================================================================
 
 if __name__ == "__main__":