Skip to content

Commit 117aba8

Browse files
committed
discontinued support for HTTP authentication
1 parent ff24a23 commit 117aba8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

zabbix_utils/aioapi.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def __init__(self, url: Optional[str] = None,
137137
client_params["connector"] = aiohttp.TCPConnector(
138138
ssl=self.validate_certs
139139
)
140+
# HTTP Auth unsupported since Zabbix 7.2
140141
if http_user and http_password:
141142
client_params["auth"] = aiohttp.BasicAuth(
142143
login=http_user,
@@ -153,6 +154,10 @@ def __init__(self, url: Optional[str] = None,
153154

154155
self.__check_version(skip_version_check)
155156

157+
if self.version > 7.0 and http_user and http_password:
158+
self.__close_session()
159+
raise APINotSupported("HTTP authentication unsupported since Zabbix 7.2.")
160+
156161
def __getattr__(self, name: str) -> Callable:
157162
"""Dynamic creation of an API object.
158163
@@ -171,14 +176,18 @@ async def __aenter__(self) -> Callable:
171176
async def __aexit__(self, *args) -> None:
172177
await self.logout()
173178

174-
async def __close_session(self) -> None:
179+
async def __aclose_session(self) -> None:
175180
if self.__internal_client:
176181
await self.__internal_client.close()
177182

178183
async def __exception(self, exc) -> None:
179-
await self.__close_session()
184+
await self.__aclose_session()
180185
raise exc from exc
181186

187+
def __close_session(self) -> None:
188+
if self.__internal_client:
189+
self.__internal_client._connector.close()
190+
182191
def api_version(self) -> APIVersion:
183192
"""Return object of Zabbix API version.
184193
@@ -261,13 +270,13 @@ async def logout(self) -> None:
261270
if self.__use_token:
262271
self.__session_id = None
263272
self.__use_token = False
264-
await self.__close_session()
273+
await self.__aclose_session()
265274
return
266275

267276
log.debug("Logout from Zabbix API")
268277
await self.user.logout()
269278
self.__session_id = None
270-
await self.__close_session()
279+
await self.__aclose_session()
271280
else:
272281
log.debug("You're not logged in Zabbix API")
273282

@@ -309,7 +318,9 @@ def __prepare_request(self, method: str, params: Optional[dict] = None,
309318
if need_auth:
310319
if not self.__session_id:
311320
raise ProcessingError("You're not logged in Zabbix API")
312-
if self.version < 6.4 or self.client_session._default_auth is not None:
321+
if self.version < 6.4:
322+
request['auth'] = self.__session_id
323+
elif self.version <= 7.0 and self.client_session._default_auth is not None:
313324
request['auth'] = self.__session_id
314325
else:
315326
headers["Authorization"] = f"Bearer {self.__session_id}"
@@ -405,6 +416,7 @@ def send_sync_request(self, method: str, params: Optional[dict] = None,
405416

406417
request_json, headers = self.__prepare_request(method, params, need_auth)
407418

419+
# HTTP Auth unsupported since Zabbix 7.2
408420
basic_auth = self.client_session._default_auth
409421
if basic_auth is not None:
410422
headers["Authorization"] = "Basic " + base64.b64encode(
@@ -433,9 +445,16 @@ def send_sync_request(self, method: str, params: Optional[dict] = None,
433445
resp = ul.urlopen(req, context=ctx)
434446
resp_json = json.loads(resp.read().decode('utf-8'))
435447
except URLError as err:
448+
if basic_auth is not None:
449+
log.warning("HTTP authentication unsupported since Zabbix 7.2.")
450+
self.__close_session()
436451
raise ProcessingError(f"Unable to connect to {self.url}:", err) from None
437452
except ValueError as err:
453+
self.__close_session()
438454
raise ProcessingError("Unable to parse json:", err) from None
455+
except Exception as err:
456+
self.__close_session()
457+
raise ProcessingError(err) from None
439458

440459
return self.__check_response(method, resp_json)
441460

zabbix_utils/api.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,21 @@ def __init__(self, url: Optional[str] = None, token: Optional[str] = None,
135135
self.validate_certs = validate_certs
136136
self.timeout = timeout
137137

138+
# HTTP Auth unsupported since Zabbix 7.2
138139
if http_user and http_password:
139140
self.__basic_auth(http_user, http_password)
140141

141142
if ssl_context is not None:
142143
if not isinstance(ssl_context, ssl.SSLContext):
143-
raise TypeError('Parameter "ssl_context" must be an "ssl.SSLContext".') from None
144+
raise TypeError(
145+
'Parameter "ssl_context" must be an "ssl.SSLContext".') from None
144146
self.ssl_context = ssl_context
145147

146148
self.__check_version(skip_version_check)
147149

150+
if self.version > 7.0 and http_user and http_password:
151+
raise APINotSupported("HTTP authentication unsupported since Zabbix 7.2.")
152+
148153
if token or user or password:
149154
self.login(token, user, password)
150155

@@ -320,7 +325,9 @@ def send_api_request(self, method: str, params: Optional[dict] = None,
320325
if need_auth:
321326
if not self.__session_id:
322327
raise ProcessingError("You're not logged in Zabbix API")
323-
if self.version < 6.4 or self.__basic_cred is not None:
328+
if self.version < 6.4:
329+
request_json['auth'] = self.__session_id
330+
elif self.version <= 7.0 and self.__basic_cred is not None:
324331
request_json['auth'] = self.__session_id
325332
else:
326333
headers["Authorization"] = f"Bearer {self.__session_id}"

0 commit comments

Comments
 (0)