@@ -130,8 +130,10 @@ def login(self) -> ZabbixAPI:
130
130
self .config .api .url = self .get_zabbix_url ()
131
131
client = ZabbixAPI .from_config (self .config )
132
132
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
+ )
135
137
136
138
if info .credentials .username :
137
139
add_user (info .credentials .username )
@@ -411,6 +413,14 @@ def write_auth_token_file(
411
413
username : str , auth_token : str , file : Path = AUTH_TOKEN_FILE
412
414
) -> Path :
413
415
"""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
+
414
424
contents = f"{ username } ::{ auth_token } "
415
425
if not file .exists ():
416
426
try :
@@ -428,8 +438,11 @@ def write_auth_token_file(
428
438
f"Unable to set secure permissions ({ SECURE_PERMISSIONS_STR } ) on { file } when saving auth token. "
429
439
"Change permissions manually or delete the file."
430
440
) 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
433
446
return file
434
447
435
448
0 commit comments