Skip to content

Commit

Permalink
feat: add zabbix 6.4 header authentication
Browse files Browse the repository at this point in the history
In Zabbix 6.4, the 'auth' parameter to method calls is deprecated.
Use the 'Authorization: Bearer' header instead.

Co-authored-by: Christian Ullrich <[email protected]>
  • Loading branch information
jooola and chrullrich committed Apr 8, 2023
1 parent 821fefb commit 3d0d86e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 13 additions & 2 deletions pyzabbix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
logger.addHandler(logging.NullHandler())

ZABBIX_5_4_0 = Version("5.4.0")
ZABBIX_6_4_0 = Version("6.4.0")


class ZabbixAPIException(Exception):
Expand Down Expand Up @@ -196,18 +197,28 @@ def do_request(
"params": params or {},
"id": self.id,
}
headers = {}

# We don't have to pass the auth token if asking for
# the apiinfo.version or user.checkAuthentication
anonymous_methods = {
"apiinfo.version",
"user.checkAuthentication",
"user.login",
}
if self.auth and method not in anonymous_methods:
payload["auth"] = self.auth
if self.version and self.version >= ZABBIX_6_4_0:
headers["Authorization"] = f"Bearer {self.auth}"
else:
payload["auth"] = self.auth

logger.debug(f"Sending: {payload}")
resp = self.session.post(self.url, json=payload, timeout=self.timeout)
resp = self.session.post(
self.url,
json=payload,
headers=headers,
timeout=self.timeout,
)
logger.debug(f"Response Code: {resp.status_code}")

# NOTE: Getting a 412 response code means the headers are not in the
Expand Down
6 changes: 5 additions & 1 deletion tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def test_do_request(requests_mock, version):
"jsonrpc": "2.0",
"method": "host.get",
"params": {},
"auth": "some_auth_key",
"id": 0,
}
expect_headers = {
Expand All @@ -277,5 +276,10 @@ def test_do_request(requests_mock, version):
"User-Agent": "python/pyzabbix",
}

if zapi.version < Version("6.4.0"):
expect_json["auth"] = "some_auth_key"
else:
expect_headers["Authorization"] = "Bearer some_auth_key"

assert found.json() == expect_json
assert found.headers.items() >= expect_headers.items()

0 comments on commit 3d0d86e

Please sign in to comment.