Skip to content

Commit

Permalink
Switch username/password during playbook execution (#1386)
Browse files Browse the repository at this point in the history
* During playbook execution it is possible to switch to other username/password.

* CHANGELOG fragment added.

* Do not cach creds but take them from every task vars.
  • Loading branch information
BGmot authored Sep 4, 2024
1 parent b5c237f commit e48220d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/relogin-when-username-changes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- httpapi - added ability to switch username/password during playbook execution.
41 changes: 41 additions & 0 deletions plugins/httpapi/zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,47 @@ def send_request(self, data=None, request_method="POST", path="/api_jsonrpc.php"
raise ConnectionError("Invalid JSON response: %s" % value)

if "error" in json_data:
if "re-login" in json_data["error"]["data"]:
# Get this response from Zabbix when we switch username to execute REST API
if not self.auth_key:
# Provide "fake" auth so netcommon.connection does not replace our headers
self.connection._auth = {'auth': 'fake'}
# Need to login with new username/password
self.login(self.connection.get_option('remote_user'), self.connection.get_option('password'))
# Replace 'auth' field in payload with new one (we got from login process)
data = json.loads(data)
data['auth'] = self.connection._auth['auth']
data = json.dumps(data)
# Re-send the request we initially were trying to execute
response, response_data = self.connection.send(
path,
data,
method=request_method,
headers=hdrs
)
value = to_text(response_data.getvalue())

try:
json_data = json.loads(value) if value else {}
# JSONDecodeError only available on Python 3.5+
except ValueError:
raise ConnectionError("Invalid JSON response: %s" % value)

if "error" in json_data:
raise ConnectionError("REST API returned %s when sending %s" % (json_data["error"], data))

if "result" in json_data:
json_data = json_data["result"]

try:
# Some methods return bool not a dict in "result"
iter(json_data)
except TypeError:
# Do not try to find "error" if it is not a dict
return response.getcode(), json_data

return response.getcode(), json_data

raise ConnectionError("REST API returned %s when sending %s" % (json_data["error"], data))

if "result" in json_data:
Expand Down

0 comments on commit e48220d

Please sign in to comment.