]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
location-importer: Introduce auxiliary function to sanitise ASNs
authorPeter Müller <peter.mueller@ipfire.org>
Mon, 1 Nov 2021 18:24:14 +0000 (19:24 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 2 Nov 2021 10:45:51 +0000 (10:45 +0000)
The third version of this patch does this in an even more Pythonic way.

Signed-off-by: Peter Müller <peter.mueller@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/location-importer.in

index da058d36319a7c71a200227fd04a05772bc39ee2..ede8a759e332991abcf0c0437bb2550fd32f1795 100644 (file)
@@ -37,6 +37,14 @@ from location.i18n import _
 log = logging.getLogger("location.importer")
 log.propagate = 1
 
+# Define constants
+VALID_ASN_RANGES = (
+       (1, 23455),
+       (23457, 64495),
+       (131072, 4199999999),
+)
+
+
 class CLI(object):
        def parse_cli(self):
                parser = argparse.ArgumentParser(
@@ -574,6 +582,19 @@ class CLI(object):
                # be suitable for libloc consumption...
                return True
 
+       def _check_parsed_asn(self, asn):
+               """
+                       Assistive function to filter Autonomous System Numbers not being suitable
+                       for adding to our database. Returns False in such cases, and True otherwise.
+               """
+
+               for start, end in VALID_ASN_RANGES:
+                       if start <= asn and end >= asn:
+                               return True
+
+               log.info("Supplied ASN %s out of publicly routable ASN ranges" % asn)
+               return False
+
        def _parse_block(self, block, source_key, validcountries = None):
                # Get first line to find out what type of block this is
                line = block[0]
@@ -829,8 +850,8 @@ class CLI(object):
                                        log.debug("Skipping ARIN AS names line not containing an integer for ASN")
                                        continue
 
-                               if not ((1 <= asn and asn <= 23455) or (23457 <= asn and asn <= 64495) or (131072 <= asn and asn <= 4199999999)):
-                                       log.debug("Skipping ARIN AS names line not containing a valid ASN: %s" % asn)
+                               # Filter invalid ASNs...
+                               if not self._check_parsed_asn(asn):
                                        continue
 
                                # Skip any AS name that appears to be a placeholder for a different RIR or entity...