Fix IPv6 host IP escaping

Change-Id: I9f7e55e02023eb492080efe95153a63dc4607b3f
This commit is contained in:
Jaromir Wysoglad
2025-06-27 08:36:18 -04:00
parent c3fa8c0543
commit 2937871577
3 changed files with 52 additions and 1 deletions

View File

@@ -157,6 +157,51 @@ class GetPrometheusClientTest(testtools.TestCase):
)
ca_m.assert_called_with("ca/path")
def test_get_prometheus_client_from_env_vars_ipv6(self):
patched_env = {'PROMETHEUS_HOST': '2607:5300:201:2000::654',
'PROMETHEUS_PORT': '1234'}
with mock.patch.dict(os.environ, patched_env), \
mock.patch.object(metric_utils, 'get_config_file',
return_value=None), \
mock.patch.object(prometheus_client.PrometheusAPIClient,
"__init__", return_value=None) as m:
metric_utils.get_prometheus_client()
m.assert_called_with("[2607:5300:201:2000::654]:1234", None, "")
def test_get_prometheus_client_from_conf_file_ipv6(self):
config_data = '''
host: "2607:5300:201:2000::654"
port: "80"
'''
config_file = mock.mock_open(read_data=config_data)("name", 'r')
with mock.patch.dict(os.environ, {}), \
mock.patch.object(metric_utils, 'get_config_file',
return_value=config_file), \
mock.patch.object(prometheus_client.PrometheusAPIClient,
"__init__", return_value=None) as init_m:
metric_utils.get_prometheus_client()
init_m.assert_called_with(
"[2607:5300:201:2000::654]:80", None, ""
)
def test_get_prometheus_client_from_keystone_ipv6(self):
prometheus_endpoint = "http://[2607:5300:201:2000::654]:80/prometheus"
keystone_session = session.Session()
with mock.patch.dict(os.environ, {}), \
mock.patch.object(metric_utils, 'get_config_file',
return_value=None), \
mock.patch.object(adapter.Adapter, 'get_endpoint',
return_value=prometheus_endpoint), \
mock.patch.object(prometheus_client.PrometheusAPIClient,
"__init__", return_value=None) as init_m, \
mock.patch.object(prometheus_client.PrometheusAPIClient,
"set_ca_cert") as ca_m:
metric_utils.get_prometheus_client(keystone_session)
init_m.assert_called_with(
"[2607:5300:201:2000::654]:80", keystone_session, "prometheus"
)
ca_m.assert_not_called()
class FormatLabelsTest(testtools.TestCase):
def setUp(self):

View File

@@ -18,6 +18,7 @@ from urllib import parse
from keystoneauth1 import adapter
from keystoneauth1.exceptions import catalog as keystone_exception
from oslo_utils import netutils
import yaml
from observabilityclient.prometheus_client import PrometheusAPIClient
@@ -102,7 +103,8 @@ def get_prometheus_client(session=None, adapter_options={}):
raise ConfigurationError("Can't find prometheus host and "
"port configuration and endpoint for service"
"prometheus not found.")
client = PrometheusAPIClient(f"{host}:{port}", session, root_path)
escaped_host = netutils.escape_ipv6(host)
client = PrometheusAPIClient(f"{escaped_host}:{port}", session, root_path)
if ca_cert is not None:
client.set_ca_cert(ca_cert)
return client

View File

@@ -0,0 +1,4 @@
---
fixes:
- |
Fixed broken handling of IPv6 address host, caused by missing host escape.