diff --git a/lib/ansible/module_utils/identity/keycloak/keycloak.py b/lib/ansible/module_utils/identity/keycloak/keycloak.py index 5fb9b92852e..5cab048dc85 100644 --- a/lib/ansible/module_utils/identity/keycloak/keycloak.py +++ b/lib/ansible/module_utils/identity/keycloak/keycloak.py @@ -34,6 +34,7 @@ import json from ansible.module_utils.urls import open_url from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.six.moves.urllib.error import HTTPError +from ansible.module_utils._text import to_native URL_TOKEN = "{url}/realms/{realm}/protocol/openid-connect/token" URL_CLIENT = "{url}/admin/realms/{realm}/clients/{id}" @@ -86,9 +87,9 @@ def get_token(base_url, validate_certs, auth_realm, client_id, payload = dict( (k, v) for k, v in temp_payload.items() if v is not None) try: - r = json.load(open_url(auth_url, method='POST', - validate_certs=validate_certs, - data=urlencode(payload))) + r = json.loads(to_native(open_url(auth_url, method='POST', + validate_certs=validate_certs, + data=urlencode(payload)).read())) except ValueError as e: raise KeycloakError( 'API returned invalid JSON when trying to obtain access token from %s: %s' @@ -129,8 +130,8 @@ class KeycloakAPI(object): clientlist_url += '?clientId=%s' % filter try: - return json.load(open_url(clientlist_url, method='GET', headers=self.restheaders, - validate_certs=self.validate_certs)) + return json.loads(to_native(open_url(clientlist_url, method='GET', headers=self.restheaders, + validate_certs=self.validate_certs).read())) except ValueError as e: self.module.fail_json(msg='API returned incorrect JSON when trying to obtain list of clients for realm %s: %s' % (realm, str(e))) @@ -160,8 +161,8 @@ class KeycloakAPI(object): client_url = URL_CLIENT.format(url=self.baseurl, realm=realm, id=id) try: - return json.load(open_url(client_url, method='GET', headers=self.restheaders, - validate_certs=self.validate_certs)) + return json.loads(to_native(open_url(client_url, method='GET', headers=self.restheaders, + validate_certs=self.validate_certs).read())) except HTTPError as e: if e.code == 404: @@ -245,8 +246,8 @@ class KeycloakAPI(object): url = URL_CLIENTTEMPLATES.format(url=self.baseurl, realm=realm) try: - return json.load(open_url(url, method='GET', headers=self.restheaders, - validate_certs=self.validate_certs)) + return json.loads(to_native(open_url(url, method='GET', headers=self.restheaders, + validate_certs=self.validate_certs).read())) except ValueError as e: self.module.fail_json(msg='API returned incorrect JSON when trying to obtain list of client templates for realm %s: %s' % (realm, str(e))) @@ -264,8 +265,8 @@ class KeycloakAPI(object): url = URL_CLIENTTEMPLATE.format(url=self.baseurl, id=id, realm=realm) try: - return json.load(open_url(url, method='GET', headers=self.restheaders, - validate_certs=self.validate_certs)) + return json.loads(to_native(open_url(url, method='GET', headers=self.restheaders, + validate_certs=self.validate_certs).read())) except ValueError as e: self.module.fail_json(msg='API returned incorrect JSON when trying to obtain client templates %s for realm %s: %s' % (id, realm, str(e))) @@ -357,8 +358,8 @@ class KeycloakAPI(object): """ groups_url = URL_GROUPS.format(url=self.baseurl, realm=realm) try: - return json.load(open_url(groups_url, method="GET", headers=self.restheaders, - validate_certs=self.validate_certs)) + return json.loads(to_native(open_url(groups_url, method="GET", headers=self.restheaders, + validate_certs=self.validate_certs).read())) except Exception as e: self.module.fail_json(msg="Could not fetch list of groups in realm %s: %s" % (realm, str(e))) @@ -374,8 +375,8 @@ class KeycloakAPI(object): """ groups_url = URL_GROUP.format(url=self.baseurl, realm=realm, groupid=gid) try: - return json.load(open_url(groups_url, method="GET", headers=self.restheaders, - validate_certs=self.validate_certs)) + return json.loads(to_native(open_url(groups_url, method="GET", headers=self.restheaders, + validate_certs=self.validate_certs).read())) except HTTPError as e: if e.code == 404: