Skip to content

Commit 3d0d86e

Browse files
jooolachrullrich
andcommitted
feat: add zabbix 6.4 header authentication
In Zabbix 6.4, the 'auth' parameter to method calls is deprecated. Use the 'Authorization: Bearer' header instead. Co-authored-by: Christian Ullrich <chris@chrullrich.net>
1 parent 821fefb commit 3d0d86e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pyzabbix/api.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
logger.addHandler(logging.NullHandler())
2020

2121
ZABBIX_5_4_0 = Version("5.4.0")
22+
ZABBIX_6_4_0 = Version("6.4.0")
2223

2324

2425
class ZabbixAPIException(Exception):
@@ -196,18 +197,28 @@ def do_request(
196197
"params": params or {},
197198
"id": self.id,
198199
}
200+
headers = {}
199201

200202
# We don't have to pass the auth token if asking for
201203
# the apiinfo.version or user.checkAuthentication
202204
anonymous_methods = {
203205
"apiinfo.version",
204206
"user.checkAuthentication",
207+
"user.login",
205208
}
206209
if self.auth and method not in anonymous_methods:
207-
payload["auth"] = self.auth
210+
if self.version and self.version >= ZABBIX_6_4_0:
211+
headers["Authorization"] = f"Bearer {self.auth}"
212+
else:
213+
payload["auth"] = self.auth
208214

209215
logger.debug(f"Sending: {payload}")
210-
resp = self.session.post(self.url, json=payload, timeout=self.timeout)
216+
resp = self.session.post(
217+
self.url,
218+
json=payload,
219+
headers=headers,
220+
timeout=self.timeout,
221+
)
211222
logger.debug(f"Response Code: {resp.status_code}")
212223

213224
# NOTE: Getting a 412 response code means the headers are not in the

tests/api_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ def test_do_request(requests_mock, version):
268268
"jsonrpc": "2.0",
269269
"method": "host.get",
270270
"params": {},
271-
"auth": "some_auth_key",
272271
"id": 0,
273272
}
274273
expect_headers = {
@@ -277,5 +276,10 @@ def test_do_request(requests_mock, version):
277276
"User-Agent": "python/pyzabbix",
278277
}
279278

279+
if zapi.version < Version("6.4.0"):
280+
expect_json["auth"] = "some_auth_key"
281+
else:
282+
expect_headers["Authorization"] = "Bearer some_auth_key"
283+
280284
assert found.json() == expect_json
281285
assert found.headers.items() >= expect_headers.items()

0 commit comments

Comments
 (0)