lukecyca/pyzabbix

Detecting bad creditentials

bilbolodz opened this issue · 2 comments

How to detect that provided credentials used to login to API are wrong?
It looks that:

zapi.login(api_token=zabbix_api_token)

always return None regardless credentials are good or wrong

jooola commented

I think the current api of pyzabbix is a bit misleading, there a a few methods/properties to check whether you are authenticated, but they always return True if you use an api token, which I think can be misleading.

You can try to call this endpoint with your token to check whether your token is valid: https://www.zabbix.com/documentation/current/en/manual/api/reference/user/checkauthentication

As an (ugly) workaround, I did my implementations like this:

success = False
try:
    
    with ZabbixAPI(zabbixurl) as zapi:
        zapi.login(zabbixuser, zabbixpw)
        print("Connected and logged in to Zabbix API Version %s" % zapi.api_version())
        ...
        success = True
except Exception as e:
    sys.stderr.write("%s\n" % e)
    ...
finally:
    if not success:
        sys.stderr.write("Errror (probably Zabbiy auth error)\n")
        ...