location-importer.in: emit warnings due to unknown country code for valid networks only

Message ID 831d1a84-1489-2a55-942d-1bad147497cb@ipfire.org
State Accepted
Commit 7138b4ac9ff238e46d23f06c5a749840c5575946
Headers
Series location-importer.in: emit warnings due to unknown country code for valid networks only |

Commit Message

Peter Müller May 3, 2021, 5:14 p.m. UTC
  This reduces log spam in case of processing RIR database, checking for
networks with unknown country codes assigned. If we would not have
written into the database, there is no need to warn about them.

Signed-off-by: Peter Müller <peter.mueller@ipfire.org>
---
 src/python/location-importer.in | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
  

Patch

diff --git a/src/python/location-importer.in b/src/python/location-importer.in
index 1e08458..e5f55af 100644
--- a/src/python/location-importer.in
+++ b/src/python/location-importer.in
@@ -623,16 +623,19 @@  class CLI(object):
 		if not inetnum or not "country" in inetnum:
 			return
 
-		# Skip objects with unknown country codes
-		if validcountries and inetnum.get("country") not in validcountries:
-			log.warning("Skipping network with bogus country '%s': %s" % \
-				(inetnum.get("country"), inetnum.get("inet6num") or inetnum.get("inetnum")))
-			return
-
 		# Iterate through all networks enumerated from above, check them for plausibility and insert
 		# them into the database, if _check_parsed_network() succeeded
 		for single_network in inetnum.get("inet6num") or inetnum.get("inetnum"):
 			if self._check_parsed_network(single_network):
+
+				# Skip objects with unknown country codes - to avoid log spam for invalid or too small
+				# networks, this check is - kinda ugly - done at this point
+				if validcountries and inetnum.get("country") not in validcountries:
+					log.warning("Skipping network with bogus country '%s': %s" % \
+						(inetnum.get("country"), inetnum.get("inet6num") or inetnum.get("inetnum")))
+					break
+
+				# Everything is fine here, run INSERT statement...
 				self.db.execute("INSERT INTO _rirdata(network, country) \
 					VALUES(%s, %s) ON CONFLICT (network) DO UPDATE SET country = excluded.country",
 					"%s" % single_network, inetnum.get("country"),