From 906f6f8df2c883a1ee8642f63a4ae1c231120686 Mon Sep 17 00:00:00 2001 From: Angelos Kolaitis Date: Tue, 5 Dec 2023 12:26:48 +0200 Subject: [PATCH] Prevent find-resolv-conf.py from breaking with scoped IPv6 addresses (#4328) (#4329) --- scripts/find-resolv-conf.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/find-resolv-conf.py b/scripts/find-resolv-conf.py index e5249fc444..464f6829e9 100755 --- a/scripts/find-resolv-conf.py +++ b/scripts/find-resolv-conf.py @@ -23,7 +23,17 @@ def safe_is_non_loopback_address(address: str): try: return not ipaddress.ip_address(address).is_loopback except ValueError: - return False + # NOTE(neoaggelos): https://github.com/canonical/microk8s/issues/4327 + # Python 3.8 fails with scoped IPv6 address, e.g. "fe80::5054:ff:fe00:b61d%2" + # Try to remove the scope suffix, and accept if value is an IPv6 address + if "%" not in address: + return False + + try: + ip = ipaddress.ip_address(address[: address.find("%")]) + return ip.version == 6 and not ip.is_loopback + except (ValueError, IndexError): + return False def find_resolv_conf_with_non_loopback_address(resolv_confs: list):