diff --git a/CHANGELOG b/CHANGELOG index 9e92d1bd..93f3e7c5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Ordering of User commands in the help output. +- Auth token file being written when logging in with a token. +- Custom auth token file path not being used when writing auth token file. ## [3.4.0](https://github.com/unioslo/zabbix-cli/releases/tag/3.4.0) - 2024-11-28 diff --git a/zabbix_cli/auth.py b/zabbix_cli/auth.py index 8f2f1fe8..ebc69efe 100644 --- a/zabbix_cli/auth.py +++ b/zabbix_cli/auth.py @@ -130,8 +130,10 @@ def login(self) -> ZabbixAPI: self.config.api.url = self.get_zabbix_url() client = ZabbixAPI.from_config(self.config) info = self._do_login(client) - if self.config.app.use_auth_token_file: - write_auth_token_file(self.config.api.username, info.token) + if info.credentials.username and self.config.app.use_auth_token_file: + write_auth_token_file( + info.credentials.username, info.token, self.config.app.auth_token_file + ) if info.credentials.username: add_user(info.credentials.username) @@ -411,6 +413,14 @@ def write_auth_token_file( username: str, auth_token: str, file: Path = AUTH_TOKEN_FILE ) -> Path: """Write a username/auth token pair to the auth token file.""" + if not username or not auth_token: + logger.error( + "Cannot write auth token file without both a username (%s) and token (%s).", + username, + auth_token, + ) + return file + contents = f"{username}::{auth_token}" if not file.exists(): try: @@ -428,8 +438,11 @@ def write_auth_token_file( f"Unable to set secure permissions ({SECURE_PERMISSIONS_STR}) on {file} when saving auth token. " "Change permissions manually or delete the file." ) from e - file.write_text(contents) - logger.info(f"Wrote auth token file {file}") + try: + file.write_text(contents) + logger.info(f"Wrote auth token file {file}") + except OSError as e: + raise AuthTokenFileError(f"Unable to write auth token file {file}: {e}") from e return file