From patchwork Sun May 30 08:50:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Peter_M=C3=BCller?= X-Patchwork-Id: 4381 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4FtBvP6242z3wc6 for ; Sun, 30 May 2021 08:50:09 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4FtBvP2rw4zyY; Sun, 30 May 2021 08:50:09 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4FtBvP08DJz2xPx; Sun, 30 May 2021 08:50:09 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4FtBvM4MBkz2x9h for ; Sun, 30 May 2021 08:50:07 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4FtBvL0fj5zs2; Sun, 30 May 2021 08:50:05 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1622364606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=boCleZBlRU/RG37+TCc36Wwp7Urj3OpcOpsuyt05LkM=; b=9lW76dCGC8PEUs21mE5+hfr9WR4tFfk5mu0NPsIhyv5Gn4BuxILeu4IGCrjb2qL7PzGJ3J D+O5i7pPtzFKlDAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1622364606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=boCleZBlRU/RG37+TCc36Wwp7Urj3OpcOpsuyt05LkM=; b=HupslWiT+7HX1DceJa7wp6tHscD1teqHFipCn1zHRz570fsPkA9MoIH5ejMyJAvmrZimv8 av1yvIdqc4y1U9fkobrEnhzzNPn0TESvb0UaatQpkX6NOjiRnuSBFvXOAKfkZ/JRLw2g01 eXSgx41iftr4lVVFP412T9Q882sb7L30ulBpTIQl0sSO7uw30J5yqh7iMFNvlfr8OKZbZo r0Ym4uN9m6Z4d1c87D2CkpuVgZIuEjAmbLKgVDWyt6Z5Vpwgp7Uxzw92ZhU4iHXeFuhHxI F1LpM2asogJO7KbLbF6nHlDp+rcgEfoVHGGAmsWnLS2c3wpzO7WrWONkHqrIjQ== Subject: [PATCH v3] location-importer.in: track original countries as well To: Michael Tremer References: <20210522125758.28770-1-peter.mueller@ipfire.org> From: =?utf-8?q?Peter_M=C3=BCller?= Message-ID: Date: Sun, 30 May 2021 10:50:04 +0200 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-BeenThere: location@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "IPFire: Location" Errors-To: location-bounces@lists.ipfire.org Sender: "Location" This helps us to determine how many network objects have more than one country set, and what their original country code set looked like. The third version of this patch uses ALTER TABLE to add the column for original countries, preventing existing SQL setups from breaking, and is correctly based against the current "master" branch. Signed-off-by: Peter Müller --- src/python/location-importer.in | 47 +++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/python/location-importer.in b/src/python/location-importer.in index f796652..a3c16bc 100644 --- a/src/python/location-importer.in +++ b/src/python/location-importer.in @@ -166,6 +166,7 @@ class CLI(object): -- networks CREATE TABLE IF NOT EXISTS networks(network inet, country text); + ALTER TABLE networks ADD COLUMN IF NOT EXISTS original_countries text[]; ALTER TABLE networks ADD COLUMN IF NOT EXISTS source text NOT NULL; CREATE UNIQUE INDEX IF NOT EXISTS networks_network ON networks(network); CREATE INDEX IF NOT EXISTS networks_family ON networks USING BTREE(family(network)); @@ -379,7 +380,7 @@ class CLI(object): ON COMMIT DROP; CREATE UNIQUE INDEX _organizations_handle ON _organizations(handle); - CREATE TEMPORARY TABLE _rirdata(network inet NOT NULL, country text NOT NULL, source text NOT NULL) + CREATE TEMPORARY TABLE _rirdata(network inet NOT NULL, country text NOT NULL, original_countries text[] NOT NULL, source text NOT NULL) ON COMMIT DROP; CREATE INDEX _rirdata_search ON _rirdata USING BTREE(family(network), masklen(network)); CREATE UNIQUE INDEX _rirdata_network ON _rirdata(network); @@ -410,8 +411,8 @@ class CLI(object): for family in (row.family for row in families): smallest = self.db.get("SELECT MIN(masklen(network)) AS prefix FROM _rirdata WHERE family(network) = %s", family) - self.db.execute("INSERT INTO networks(network, country, source) \ - SELECT network, country, source FROM _rirdata WHERE masklen(network) = %s AND family(network) = %s", smallest.prefix, family) + self.db.execute("INSERT INTO networks(network, country, original_countries, source) \ + SELECT network, country, original_countries, source FROM _rirdata WHERE masklen(network) = %s AND family(network) = %s", smallest.prefix, family) # ... determine any other prefixes for this network family, ... prefixes = self.db.query("SELECT DISTINCT masklen(network) AS prefix FROM _rirdata \ @@ -425,6 +426,7 @@ class CLI(object): SELECT _rirdata.network, _rirdata.country, + _rirdata.original_countries, _rirdata.source FROM _rirdata @@ -438,6 +440,7 @@ class CLI(object): DISTINCT ON (c.network) c.network, c.country, + c.original_countries, c.source, masklen(networks.network), networks.country AS parent_country @@ -452,10 +455,11 @@ class CLI(object): masklen(networks.network) DESC NULLS LAST ) INSERT INTO - networks(network, country, source) + networks(network, country, original_countries, source) SELECT network, country, + original_countries, source FROM filtered @@ -624,28 +628,37 @@ class CLI(object): inetnum[key] = [ipaddress.ip_network(val, strict=False)] elif key == "country": - inetnum[key] = val.upper() + # Catch RIR data objects with more than one country code... + if not key in inetnum.keys(): + inetnum[key] = [] + else: + if val.upper() in inetnum.get("country"): + # ... but keep this list distinct... + continue + + inetnum[key].append(val.upper()) # Skip empty objects if not inetnum or not "country" in inetnum: return + # Prepare skipping objects with unknown country codes... + invalidcountries = [singlecountry for singlecountry in inetnum.get("country") if singlecountry not in validcountries] + # 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 + # Skip objects with unknown country codes if they are valid to avoid log spam... + if validcountries and invalidcountries: + log.warning("Skipping network with bogus countr(y|ies) %s (original countries: %s): %s" % \ + (invalidcountries, inetnum.get("country"), inetnum.get("inet6num") or inetnum.get("inetnum"))) # Everything is fine here, run INSERT statement... - self.db.execute("INSERT INTO _rirdata(network, country, source) \ - VALUES(%s, %s, %s) ON CONFLICT (network) DO UPDATE SET country = excluded.country", - "%s" % single_network, inetnum.get("country"), source_key, + self.db.execute("INSERT INTO _rirdata(network, country, original_countries, source) \ + VALUES(%s, %s, %s, %s) ON CONFLICT (network) DO UPDATE SET country = excluded.country", + "%s" % single_network, inetnum.get("country")[0], inetnum.get("country"), source_key, ) def _parse_org_block(self, block, source_key): @@ -736,10 +749,10 @@ class CLI(object): if not self._check_parsed_network(network): return - self.db.execute("INSERT INTO networks(network, country, source) \ - VALUES(%s, %s, %s) ON CONFLICT (network) DO \ + self.db.execute("INSERT INTO networks(network, country, original_countries, source) \ + VALUES(%s, %s, %s, %s) ON CONFLICT (network) DO \ UPDATE SET country = excluded.country", - "%s" % network, country, source_key, + "%s" % network, country, [country], source_key, ) def handle_update_announcements(self, ns):