Skip to content

python: validate data it reads from nodes #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions python/alternator_lb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ipaddress
import threading
import time
import logging
Expand All @@ -9,6 +10,7 @@
from concurrent.futures import ThreadPoolExecutor



class ExecutorPool:
def __init__(self):
self._executor = None
Expand Down Expand Up @@ -45,6 +47,7 @@ class Config:
port: int = 8080
datacenter: str = None
rack: str = None
client_cert: str = None
aws_region_name: str = "fake-alternator-lb-region"
aws_access_key_id: str = "fake-alternator-lb-access-key-id"
aws_secret_access_key: str = "fake-alternator-lb-secret-access-key"
Expand Down Expand Up @@ -105,10 +108,12 @@ def __init__(self, config: Config):
self._next_update_time = 0

@staticmethod
def _validate_uri(uri: str):
parsed_uri = urlparse(uri)
if not parsed_uri.scheme or not parsed_uri.netloc:
raise ValueError(f"Invalid URI: {uri}")
def _validate_node(node: str) -> bool:
try:
ipaddress.ip_address(node)
return True
except ValueError:
return False

def _update_nodes_if_needed(self):
if self._updating:
Expand Down Expand Up @@ -162,7 +167,7 @@ def _get_nodes(self, uri: str) -> List[str]:
return []

nodes = response.json()
return [self._host_to_uri(host) for host in nodes if host]
return [self._host_to_uri(host) for host in nodes if host and self._validate_node(host)]
except Exception as e:
self._logger.warning(f"Failed to fetch nodes from {uri}: {e}")
return []
Expand Down