Skip to content

Commit 81b9e2e

Browse files
authored
fix: Writing auth token file when logging in with token (#268)
* fix: Writing auth token file when logging in with token * Pass in config, guard write in try/except * Fix changelog
1 parent cbe83c2 commit 81b9e2e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
### Fixed
2424

2525
- Ordering of User commands in the help output.
26+
- Auth token file being written when logging in with a token.
27+
- Custom auth token file path not being used when writing auth token file.
2628

2729
## [3.4.0](https://github.com/unioslo/zabbix-cli/releases/tag/3.4.0) - 2024-11-28
2830

zabbix_cli/auth.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ def login(self) -> ZabbixAPI:
130130
self.config.api.url = self.get_zabbix_url()
131131
client = ZabbixAPI.from_config(self.config)
132132
info = self._do_login(client)
133-
if self.config.app.use_auth_token_file:
134-
write_auth_token_file(self.config.api.username, info.token)
133+
if info.credentials.username and self.config.app.use_auth_token_file:
134+
write_auth_token_file(
135+
info.credentials.username, info.token, self.config.app.auth_token_file
136+
)
135137

136138
if info.credentials.username:
137139
add_user(info.credentials.username)
@@ -411,6 +413,14 @@ def write_auth_token_file(
411413
username: str, auth_token: str, file: Path = AUTH_TOKEN_FILE
412414
) -> Path:
413415
"""Write a username/auth token pair to the auth token file."""
416+
if not username or not auth_token:
417+
logger.error(
418+
"Cannot write auth token file without both a username (%s) and token (%s).",
419+
username,
420+
auth_token,
421+
)
422+
return file
423+
414424
contents = f"{username}::{auth_token}"
415425
if not file.exists():
416426
try:
@@ -428,8 +438,11 @@ def write_auth_token_file(
428438
f"Unable to set secure permissions ({SECURE_PERMISSIONS_STR}) on {file} when saving auth token. "
429439
"Change permissions manually or delete the file."
430440
) from e
431-
file.write_text(contents)
432-
logger.info(f"Wrote auth token file {file}")
441+
try:
442+
file.write_text(contents)
443+
logger.info(f"Wrote auth token file {file}")
444+
except OSError as e:
445+
raise AuthTokenFileError(f"Unable to write auth token file {file}: {e}") from e
433446
return file
434447

435448

0 commit comments

Comments
 (0)