Skip to content

Commit 4097749

Browse files
committed
Keep session transport options
currently there's no way to use observabilityclient against Keystone endpoint (Aetos) that uses self-signed TLS cert, as verify/insecure option set by clouds.yaml/openstackclient is effectively overridden based solely on endpoint URL scheme. Instead, presume that all transport options are already properly set, and do not touch them at all when working with pre-created session. Assisted-By: Cursor + Grok 4.5 High Change-Id: I423f1f444c4d5ac4357db6e3c57c0e486c61c79d Signed-off-by: Pavlo Shchelokovskyy <shchelokovskyy@gmail.com>
1 parent d0614a7 commit 4097749

5 files changed

Lines changed: 52 additions & 13 deletions

File tree

‎observabilityclient/prometheus_client.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ def __init__(self, input):
5353

5454

5555
class PrometheusAPIClient:
56-
def __init__(self, host, session=None, root_path=""):
56+
def __init__(self, host, session=None, root_path="", scheme=None):
57+
self._scheme = scheme
5758
self._host = host
5859
if not self._host.endswith('/'):
5960
self._host += '/'
6061
if session is None:
6162
self._session = requests.Session()
63+
self._session.verify = False
6264
else:
6365
self._session = session
64-
self._session.verify = False
6566
self._root_path = root_path
6667
if root_path != "" and not self._root_path.endswith('/'):
6768
self._root_path += '/'
@@ -76,7 +77,10 @@ def set_basic_auth(self, auth_user, auth_password):
7677
self._session.auth = (auth_user, auth_password)
7778

7879
def _get_url(self, endpoint):
79-
scheme = 'https' if self._session.verify else 'http'
80+
if self._scheme is None:
81+
scheme = 'https' if self._session.verify else 'http'
82+
else:
83+
scheme = self._scheme
8084
return f"{scheme}://{self._host}{self._root_path}api/v1/{endpoint}"
8185

8286
def _get(self, endpoint, params=None):

‎observabilityclient/tests/unit/test_prometheus_client.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ def json(self):
8383

8484

8585
class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
86+
def test_init_scheme_default(self):
87+
c = client.PrometheusAPIClient("localhost:9090")
88+
self.assertIsNone(c._scheme)
89+
90+
def test_init_scheme_passed(self):
91+
c = client.PrometheusAPIClient("localhost:9090", scheme="https")
92+
self.assertEqual("https", c._scheme)
93+
94+
def test_get_url_without_scheme_uses_verify(self):
95+
c = client.PrometheusAPIClient("localhost:9090")
96+
c._session.verify = False
97+
self.assertEqual("http://localhost:9090/api/v1/query",
98+
c._get_url("query"))
99+
c._session.verify = True
100+
self.assertEqual("https://localhost:9090/api/v1/query",
101+
c._get_url("query"))
102+
103+
def test_get_url_with_scheme_ignores_verify(self):
104+
c = client.PrometheusAPIClient("localhost:9090", scheme="https")
105+
c._session.verify = False
106+
self.assertEqual("https://localhost:9090/api/v1/query",
107+
c._get_url("query"))
108+
109+
c = client.PrometheusAPIClient("localhost:9090", scheme="http")
110+
c._session.verify = True
111+
self.assertEqual("http://localhost:9090/api/v1/query",
112+
c._get_url("query"))
113+
86114
def test_get(self):
87115
url = "test"
88116
root_path = "root_path"

‎observabilityclient/tests/unit/test_utils.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_get_prometheus_client_from_keystone_http(self):
115115
"set_ca_cert") as ca_m:
116116
metric_utils.get_prometheus_client(keystone_session)
117117
init_m.assert_called_with(
118-
"localhost:1234", keystone_session, "prometheus"
118+
"localhost:1234", keystone_session, "prometheus", scheme="http"
119119
)
120120
ca_m.assert_not_called()
121121

@@ -133,9 +133,11 @@ def test_get_prometheus_client_from_keystone_https(self):
133133
"set_ca_cert") as ca_m:
134134
metric_utils.get_prometheus_client(keystone_session)
135135
init_m.assert_called_with(
136-
"localhost:1234", keystone_session, "prometheus"
136+
"localhost:1234", keystone_session, "prometheus", scheme="https"
137137
)
138-
ca_m.assert_called_with(True)
138+
# Session transport options (verify/insecure) must be preserved;
139+
# do not override them based on the endpoint URL scheme.
140+
ca_m.assert_not_called()
139141

140142
def test_get_prometheus_client_from_env_vars_ipv6(self):
141143
patched_env = {'PROMETHEUS_HOST': '2607:5300:201:2000::654',
@@ -178,7 +180,8 @@ def test_get_prometheus_client_from_keystone_ipv6(self):
178180
"set_ca_cert") as ca_m:
179181
metric_utils.get_prometheus_client(keystone_session)
180182
init_m.assert_called_with(
181-
"[2607:5300:201:2000::654]:80", keystone_session, "prometheus"
183+
"[2607:5300:201:2000::654]:80", keystone_session, "prometheus",
184+
scheme="http"
182185
)
183186
ca_m.assert_not_called()
184187

‎observabilityclient/utils/metric_utils.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,14 @@ def get_prom_client_from_keystone(session, adapter_options=None):
5959

6060
escaped_host = netutils.escape_ipv6(parsed_url.hostname)
6161
root_path = parsed_url.path.strip('/')
62-
tls = parsed_url.scheme == "https"
6362

6463
if parsed_url.port is not None:
6564
url = f'{escaped_host}:{parsed_url.port}'
6665
else:
6766
url = escaped_host
6867

69-
client = PrometheusAPIClient(url, session, root_path)
70-
71-
if tls:
72-
client.set_ca_cert(True)
73-
return client
68+
return PrometheusAPIClient(
69+
url, session, root_path, scheme=parsed_url.scheme)
7470

7571

7672
def get_prom_client_from_file_or_env():
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
fixes:
3+
- |
4+
Preserve transport options (such as ``verify`` / ``insecure``) on a
5+
pre-created Keystone session when talking to Aetos. Previously, TLS
6+
verification was forced on solely from the endpoint URL scheme, which
7+
prevented using observabilityclient against endpoints with self-signed
8+
certificates configured via ``clouds.yaml`` / openstackclient.

0 commit comments

Comments
 (0)