From b35e7be391547c59b3952f83c46eec35b48af989 Mon Sep 17 00:00:00 2001 From: 1yam Date: Thu, 13 Jul 2023 13:29:42 +0200 Subject: [PATCH 1/7] Fix: Ipv6 filtering --- vm_supervisor/conf.py | 2 +- vm_supervisor/vm/firecracker/program.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/vm_supervisor/conf.py b/vm_supervisor/conf.py index 2fc00b091..5aa696e93 100644 --- a/vm_supervisor/conf.py +++ b/vm_supervisor/conf.py @@ -263,7 +263,7 @@ def setup(self): elif self.DNS_RESOLUTION == DnsResolver.resolvectl: self.DNS_NAMESERVERS = list( - resolvectl_dns_servers_ipv4(interface=self.NETWORK_INTERFACE) + resolvectl_dns_servers(interface=self.NETWORK_INTERFACE) ) else: assert "This should never happen" diff --git a/vm_supervisor/vm/firecracker/program.py b/vm_supervisor/vm/firecracker/program.py index 61e1b2ade..c5ca89c8b 100644 --- a/vm_supervisor/vm/firecracker/program.py +++ b/vm_supervisor/vm/firecracker/program.py @@ -8,6 +8,7 @@ from enum import Enum from pathlib import Path from typing import Dict, List, Optional, Tuple +import ipaddress import msgpack from aiohttp import ClientResponseError @@ -38,6 +39,7 @@ Volume, ) + logger = logging.getLogger(__name__) @@ -388,9 +390,13 @@ async def _setup_configuration( ipv6 = self.get_vm_ipv6() ipv6_gateway = self.get_vm_ipv6_gateway() - if not settings.DNS_NAMESERVERS: + dns_servers = settings.DNS_NAMESERVERS + if not dns_servers: raise ValueError("Invalid configuration: DNS nameservers missing") + # Apply DNS IPv6 filtering here if needed + dns_servers = [server for server in dns_servers if not ipaddress.ip_address(server).version == 6] + runtime_config = self.fvm.runtime_config assert runtime_config @@ -405,7 +411,7 @@ async def _setup_configuration( ipv6=ipv6, route=route, ipv6_gateway=ipv6_gateway, - dns_servers=settings.DNS_NAMESERVERS, + dns_servers=dns_servers, code=code, encoding=self.resources.code_encoding, entrypoint=self.resources.code_entrypoint, From e5f17a11acb51b6eb5ec6dc8d2dec796df3bd028 Mon Sep 17 00:00:00 2001 From: 1yam Date: Thu, 13 Jul 2023 13:51:32 +0200 Subject: [PATCH 2/7] Fix: use method for filtering ipv6 --- vm_supervisor/conf.py | 9 +++++++++ vm_supervisor/vm/firecracker/program.py | 8 +------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/vm_supervisor/conf.py b/vm_supervisor/conf.py index 5aa696e93..6ff7b7df8 100644 --- a/vm_supervisor/conf.py +++ b/vm_supervisor/conf.py @@ -284,6 +284,15 @@ def display(self) -> str: return "\n".join( f"{attribute:<27} = {value}" for attribute, value in attributes.items() ) + + def dns_servers(self, ipv4_only: bool) -> Optional[List[str]]: + dns_servers = self.DNS_NAMESERVERS + if not dns_servers: + raise ValueError("Invalid configuration: DNS nameservers missing") + + # Apply DNS IPv6 filtering here if needed + dns_servers = [server for server in dns_servers if not ipaddress.ip_address(server).version == 6 or ipv4_only] + return dns_servers class Config: env_prefix = "ALEPH_VM_" diff --git a/vm_supervisor/vm/firecracker/program.py b/vm_supervisor/vm/firecracker/program.py index c5ca89c8b..fb3143967 100644 --- a/vm_supervisor/vm/firecracker/program.py +++ b/vm_supervisor/vm/firecracker/program.py @@ -389,13 +389,7 @@ async def _setup_configuration( route = self.get_vm_route() ipv6 = self.get_vm_ipv6() ipv6_gateway = self.get_vm_ipv6_gateway() - - dns_servers = settings.DNS_NAMESERVERS - if not dns_servers: - raise ValueError("Invalid configuration: DNS nameservers missing") - - # Apply DNS IPv6 filtering here if needed - dns_servers = [server for server in dns_servers if not ipaddress.ip_address(server).version == 6] + dns_servers = settings.dns_servers(False) runtime_config = self.fvm.runtime_config assert runtime_config From 29223022fe1ea2ad8074c840a0ecb35716a242ee Mon Sep 17 00:00:00 2001 From: 1yam Date: Thu, 13 Jul 2023 16:19:40 +0200 Subject: [PATCH 3/7] Fix: black --- vm_supervisor/conf.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vm_supervisor/conf.py b/vm_supervisor/conf.py index 6ff7b7df8..933a5865b 100644 --- a/vm_supervisor/conf.py +++ b/vm_supervisor/conf.py @@ -284,14 +284,18 @@ def display(self) -> str: return "\n".join( f"{attribute:<27} = {value}" for attribute, value in attributes.items() ) - + def dns_servers(self, ipv4_only: bool) -> Optional[List[str]]: dns_servers = self.DNS_NAMESERVERS if not dns_servers: raise ValueError("Invalid configuration: DNS nameservers missing") # Apply DNS IPv6 filtering here if needed - dns_servers = [server for server in dns_servers if not ipaddress.ip_address(server).version == 6 or ipv4_only] + dns_servers = [ + server + for server in dns_servers + if not ipaddress.ip_address(server).version == 6 or ipv4_only + ] return dns_servers class Config: From 9eaf00d578b1b679ac58a1e6a721fbf0bc4afd47 Mon Sep 17 00:00:00 2001 From: 1yam Date: Thu, 13 Jul 2023 16:29:47 +0200 Subject: [PATCH 4/7] Fix: isort --- vm_supervisor/vm/firecracker/program.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vm_supervisor/vm/firecracker/program.py b/vm_supervisor/vm/firecracker/program.py index fb3143967..75b8d2729 100644 --- a/vm_supervisor/vm/firecracker/program.py +++ b/vm_supervisor/vm/firecracker/program.py @@ -2,13 +2,13 @@ import asyncio import dataclasses +import ipaddress import logging import os.path from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import Dict, List, Optional, Tuple -import ipaddress import msgpack from aiohttp import ClientResponseError @@ -39,7 +39,6 @@ Volume, ) - logger = logging.getLogger(__name__) From f80c4f1014f97c8ef17f76e5a110ca208e037304 Mon Sep 17 00:00:00 2001 From: 1yam Date: Tue, 3 Oct 2023 13:29:25 +0200 Subject: [PATCH 5/7] Fix: add docstring / comment --- vm_supervisor/conf.py | 17 ++++++++++++++++- vm_supervisor/vm/firecracker/program.py | 3 ++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/vm_supervisor/conf.py b/vm_supervisor/conf.py index 933a5865b..3d6782fc2 100644 --- a/vm_supervisor/conf.py +++ b/vm_supervisor/conf.py @@ -286,11 +286,26 @@ def display(self) -> str: ) def dns_servers(self, ipv4_only: bool) -> Optional[List[str]]: + """ + Retrieve a list of DNS nameservers, optionally filtering for IPv4. + + Parameters: + ipv4_only (bool): If True, return only IPv4 DNS servers. + + Returns: + Optional[List[str]]: A list of DNS nameservers, or None if none are configured. + + Raises: + ValueError: If there are no DNS nameservers in the configuration. + + This method returns a list of DNS nameservers. If the `ipv4_only` parameter is True, it filters + out IPv6 nameservers. If no nameservers are configured, a ValueError is raised. + """ dns_servers = self.DNS_NAMESERVERS if not dns_servers: raise ValueError("Invalid configuration: DNS nameservers missing") - # Apply DNS IPv6 filtering here if needed + # Apply DNS IPv6 here if user configurations allow it dns_servers = [ server for server in dns_servers diff --git a/vm_supervisor/vm/firecracker/program.py b/vm_supervisor/vm/firecracker/program.py index 75b8d2729..6cd8af10d 100644 --- a/vm_supervisor/vm/firecracker/program.py +++ b/vm_supervisor/vm/firecracker/program.py @@ -388,7 +388,8 @@ async def _setup_configuration( route = self.get_vm_route() ipv6 = self.get_vm_ipv6() ipv6_gateway = self.get_vm_ipv6_gateway() - dns_servers = settings.dns_servers(False) + + dns_servers = settings.dns_servers(ipv4_only=False) # True to Disable Ipv6 Dns runtime_config = self.fvm.runtime_config assert runtime_config From a2d8c15b6e8ef27f96726c5f4ea0289ba855e23e Mon Sep 17 00:00:00 2001 From: 1yam <40899431+1yam@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:57:03 +0200 Subject: [PATCH 6/7] Fix: program.py --- vm_supervisor/vm/firecracker/program.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm_supervisor/vm/firecracker/program.py b/vm_supervisor/vm/firecracker/program.py index 6cd8af10d..a2d17f98b 100644 --- a/vm_supervisor/vm/firecracker/program.py +++ b/vm_supervisor/vm/firecracker/program.py @@ -389,7 +389,7 @@ async def _setup_configuration( ipv6 = self.get_vm_ipv6() ipv6_gateway = self.get_vm_ipv6_gateway() - dns_servers = settings.dns_servers(ipv4_only=False) # True to Disable Ipv6 Dns + dns_servers = settings.filter_dns_servers(ipv4_only=False) # True to Disable Ipv6 Dns runtime_config = self.fvm.runtime_config assert runtime_config From 577e1f5033bf2daf64689018aa46a94cf9c72c82 Mon Sep 17 00:00:00 2001 From: 1yam <40899431+1yam@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:01:19 +0200 Subject: [PATCH 7/7] Fix: conf.py --- vm_supervisor/conf.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/vm_supervisor/conf.py b/vm_supervisor/conf.py index 3d6782fc2..f43be04b1 100644 --- a/vm_supervisor/conf.py +++ b/vm_supervisor/conf.py @@ -285,21 +285,15 @@ def display(self) -> str: f"{attribute:<27} = {value}" for attribute, value in attributes.items() ) - def dns_servers(self, ipv4_only: bool) -> Optional[List[str]]: + def filter_dns_servers(self, ipv4_only: bool) -> Optional[List[str]]: """ - Retrieve a list of DNS nameservers, optionally filtering for IPv4. + Filter Dns (ipv4 + ipv6 or only ipv4) - Parameters: - ipv4_only (bool): If True, return only IPv4 DNS servers. + Parameter: + ipv4_only (bool) + Return: + Optional[List[str]]: list of dns filtered - Returns: - Optional[List[str]]: A list of DNS nameservers, or None if none are configured. - - Raises: - ValueError: If there are no DNS nameservers in the configuration. - - This method returns a list of DNS nameservers. If the `ipv4_only` parameter is True, it filters - out IPv6 nameservers. If no nameservers are configured, a ValueError is raised. """ dns_servers = self.DNS_NAMESERVERS if not dns_servers: