Skip to content

Commit 82ccf29

Browse files
committed
Fix connection refused error when WAZUH_HOST includes protocol
Fixes #63 - Users following documentation that shows WAZUH_HOST=https://... would get "connection refused" because the code was generating malformed URLs like https://https://192.168.1.100:55000 Changes: - Add normalize_host() to strip protocol prefix (https://, http://) - Apply normalization to WAZUH_HOST and WAZUH_INDEXER_HOST - Add defensive normalization in WazuhIndexerClient - Handles both formats: hostname only OR with protocol
1 parent 3c80f90 commit 82ccf29

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/wazuh_mcp_server/api/wazuh_indexer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def __init__(
3535
verify_ssl: bool = True,
3636
timeout: int = 30
3737
):
38-
self.host = host
38+
# Normalize host (strip protocol if user included it)
39+
self.host = self._normalize_host(host)
3940
self.port = port
4041
self.username = username
4142
self.password = password
@@ -44,6 +45,17 @@ def __init__(
4445
self.client: Optional[httpx.AsyncClient] = None
4546
self._initialized = False
4647

48+
@staticmethod
49+
def _normalize_host(host: str) -> str:
50+
"""Strip protocol prefix from host if present."""
51+
if not host:
52+
return host
53+
for prefix in ('https://', 'http://'):
54+
if host.lower().startswith(prefix):
55+
host = host[len(prefix):]
56+
break
57+
return host.rstrip('/')
58+
4759
@property
4860
def base_url(self) -> str:
4961
"""Get the base URL for the Wazuh Indexer."""

src/wazuh_mcp_server/config.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ def validate_positive_int(value: str, name: str, max_val: Optional[int] = None)
3737
raise ConfigurationError(f"{name} must be a valid integer, got '{value}'")
3838

3939

40+
def normalize_host(host: str) -> str:
41+
"""
42+
Normalize hostname by stripping protocol prefix if present.
43+
44+
Handles common user mistakes like including https:// in WAZUH_HOST.
45+
Examples:
46+
'https://192.168.1.100' -> '192.168.1.100'
47+
'http://wazuh.local' -> 'wazuh.local'
48+
'192.168.1.100' -> '192.168.1.100'
49+
"""
50+
if not host:
51+
return host
52+
# Strip protocol prefixes
53+
for prefix in ('https://', 'http://'):
54+
if host.lower().startswith(prefix):
55+
host = host[len(prefix):]
56+
break
57+
# Strip trailing slashes
58+
return host.rstrip('/')
59+
60+
4061
@dataclass
4162
class WazuhConfig:
4263
"""Wazuh configuration settings."""
@@ -104,14 +125,19 @@ def safe_int_env(key: str, default: str, min_val: int = 1, max_val: int = None)
104125
port = int(os.getenv("WAZUH_PORT", "55000"))
105126
verify_ssl = os.getenv("VERIFY_SSL", "true").lower() == "true"
106127

128+
# Normalize host values (strip protocol if user included it)
129+
normalized_host = normalize_host(host)
130+
indexer_host = os.getenv("WAZUH_INDEXER_HOST")
131+
normalized_indexer_host = normalize_host(indexer_host) if indexer_host else None
132+
107133
# Create config with defaults for most settings
108134
config = cls(
109-
wazuh_host=host,
135+
wazuh_host=normalized_host,
110136
wazuh_user=user,
111137
wazuh_pass=password,
112138
wazuh_port=port,
113139
verify_ssl=verify_ssl,
114-
wazuh_indexer_host=os.getenv("WAZUH_INDEXER_HOST"),
140+
wazuh_indexer_host=normalized_indexer_host,
115141
wazuh_indexer_port=int(os.getenv("WAZUH_INDEXER_PORT", "9200")),
116142
wazuh_indexer_user=os.getenv("WAZUH_INDEXER_USER"),
117143
wazuh_indexer_pass=os.getenv("WAZUH_INDEXER_PASS"),
@@ -213,14 +239,14 @@ def from_env(cls) -> 'ServerConfig':
213239
os.getenv("OAUTH_AUTHORIZATION_CODE_TTL", "600"), "OAUTH_AUTHORIZATION_CODE_TTL"
214240
),
215241
ALLOWED_ORIGINS=os.getenv("ALLOWED_ORIGINS", "https://claude.ai,http://localhost:*"),
216-
WAZUH_HOST=os.getenv("WAZUH_HOST", ""),
242+
WAZUH_HOST=normalize_host(os.getenv("WAZUH_HOST", "")),
217243
WAZUH_USER=os.getenv("WAZUH_USER", ""),
218244
WAZUH_PASS=os.getenv("WAZUH_PASS", ""),
219245
WAZUH_PORT=validate_port(os.getenv("WAZUH_PORT", "55000"), "WAZUH_PORT"),
220246
WAZUH_VERIFY_SSL=os.getenv("WAZUH_VERIFY_SSL", "false").lower() == "true",
221247
WAZUH_ALLOW_SELF_SIGNED=os.getenv("WAZUH_ALLOW_SELF_SIGNED", "true").lower() == "true",
222248
# Wazuh Indexer settings (for vulnerability tools in Wazuh 4.8.0+)
223-
WAZUH_INDEXER_HOST=os.getenv("WAZUH_INDEXER_HOST", ""),
249+
WAZUH_INDEXER_HOST=normalize_host(os.getenv("WAZUH_INDEXER_HOST", "")),
224250
WAZUH_INDEXER_PORT=validate_port(
225251
os.getenv("WAZUH_INDEXER_PORT", "9200"), "WAZUH_INDEXER_PORT"
226252
),

0 commit comments

Comments
 (0)