2023-09-19 16:07:25 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (c) 2017, Eike Frost <ei@kefro.st>
|
|
|
|
# Copyright (c) 2021, Christophe Gilles <christophe.gilles54@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or
|
|
|
|
# https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2024-12-26 12:42:44 +00:00
|
|
|
DOCUMENTATION = r"""
|
2023-09-19 16:07:25 +00:00
|
|
|
module: keycloak_authz_custom_policy
|
|
|
|
|
2024-12-26 12:42:44 +00:00
|
|
|
short_description: Allows administration of Keycloak client custom Javascript policies using Keycloak API
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
version_added: 7.5.0
|
|
|
|
|
|
|
|
description:
|
2025-01-06 20:31:59 +00:00
|
|
|
- This module allows the administration of Keycloak client custom Javascript using the Keycloak REST API. Custom Javascript
|
|
|
|
policies are only available if a client has Authorization enabled and if they have been deployed to the Keycloak server
|
|
|
|
as JAR files.
|
|
|
|
- This module requires access to the REST API using OpenID Connect; the user connecting and the realm being used must have
|
|
|
|
the requisite access rights. In a default Keycloak installation, admin-cli and an admin user would work, as would a separate
|
|
|
|
realm definition with the scope tailored to your needs and a user having the expected roles.
|
|
|
|
- The names of module options are snake_cased versions of the camelCase options used by Keycloak. The Authorization Services
|
|
|
|
paths and payloads have not officially been documented by the Keycloak project.
|
|
|
|
U(https://www.puppeteers.net/blog/keycloak-authorization-services-rest-api-paths-and-payload/).
|
2023-09-19 16:07:25 +00:00
|
|
|
attributes:
|
2024-12-26 12:42:44 +00:00
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
|
|
|
action_group:
|
|
|
|
version_added: 10.2.0
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
options:
|
2024-12-26 12:42:44 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the custom policy.
|
|
|
|
- On V(present), the custom policy will be created (or updated if it exists already).
|
|
|
|
- On V(absent), the custom policy will be removed if it exists.
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: 'present'
|
|
|
|
type: str
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the custom policy to create.
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
policy_type:
|
|
|
|
description:
|
|
|
|
- The type of the policy. This must match the name of the custom policy deployed to the server.
|
|
|
|
- Multiple policies pointing to the same policy type can be created, but their names have to differ.
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
client_id:
|
|
|
|
description:
|
|
|
|
- The V(clientId) of the Keycloak client that should have the custom policy attached to it.
|
|
|
|
- This is usually a human-readable name of the Keycloak client.
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
realm:
|
|
|
|
description:
|
|
|
|
- The name of the Keycloak realm the Keycloak client is in.
|
|
|
|
type: str
|
|
|
|
required: true
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
extends_documentation_fragment:
|
2024-12-26 12:42:44 +00:00
|
|
|
- community.general.keycloak
|
|
|
|
- community.general.keycloak.actiongroup_keycloak
|
|
|
|
- community.general.attributes
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
author:
|
2024-12-26 12:42:44 +00:00
|
|
|
- Samuli Seppänen (@mattock)
|
|
|
|
"""
|
2023-09-19 16:07:25 +00:00
|
|
|
|
2024-12-26 12:42:44 +00:00
|
|
|
EXAMPLES = r"""
|
2023-09-19 16:07:25 +00:00
|
|
|
- name: Manage Keycloak custom authorization policy
|
|
|
|
community.general.keycloak_authz_custom_policy:
|
|
|
|
name: OnlyOwner
|
|
|
|
state: present
|
|
|
|
policy_type: script-policy.js
|
|
|
|
client_id: myclient
|
|
|
|
realm: myrealm
|
|
|
|
auth_keycloak_url: http://localhost:8080/auth
|
|
|
|
auth_username: keycloak
|
|
|
|
auth_password: keycloak
|
|
|
|
auth_realm: master
|
2024-12-26 12:42:44 +00:00
|
|
|
"""
|
2023-09-19 16:07:25 +00:00
|
|
|
|
2024-12-26 12:42:44 +00:00
|
|
|
RETURN = r"""
|
2023-09-19 16:07:25 +00:00
|
|
|
msg:
|
2024-12-26 12:42:44 +00:00
|
|
|
description: Message as to what action was taken.
|
|
|
|
returned: always
|
|
|
|
type: str
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
end_state:
|
2024-12-26 12:42:44 +00:00
|
|
|
description: Representation of the custom policy after module execution.
|
|
|
|
returned: on success
|
|
|
|
type: dict
|
|
|
|
contains:
|
|
|
|
name:
|
|
|
|
description: Name of the custom policy.
|
|
|
|
type: str
|
|
|
|
returned: when I(state=present)
|
|
|
|
sample: file:delete
|
|
|
|
policy_type:
|
|
|
|
description: Type of custom policy.
|
|
|
|
type: str
|
|
|
|
returned: when I(state=present)
|
|
|
|
sample: File delete
|
|
|
|
"""
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak import KeycloakAPI, \
|
|
|
|
keycloak_argument_spec, get_token, KeycloakError
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Module execution
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
argument_spec = keycloak_argument_spec()
|
|
|
|
|
|
|
|
meta_args = dict(
|
|
|
|
state=dict(type='str', default='present',
|
|
|
|
choices=['present', 'absent']),
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
policy_type=dict(type='str', required=True),
|
|
|
|
client_id=dict(type='str', required=True),
|
|
|
|
realm=dict(type='str', required=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
argument_spec.update(meta_args)
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
required_one_of=(
|
|
|
|
[['token', 'auth_realm', 'auth_username', 'auth_password']]),
|
Keycloak modules retry request on authentication error, support refresh token parameter (#9494)
* feat: begin refactor to support refresh token in keycloak modules
* chore: add start of tests for shared token usage
* feat: progress towards supporting refresh token; token introspection not yet working [8857]
* chore: reset to main branch previous state; a different approach is needed [8857]
* feat: add request methods to keycloak class, which will be expanded with retry logic [8857]
* feat: all requests to keycloak use request methods instead of open_url [8857]
* fix: data argument is optional in keycloak request methods [8857]
* feat: add integration test for keycloak module authentication methods [8857]
* chore: refactor get token logic to separate logic using username/pass credentials [8857]
* chore: refactor token request logic further to isolate request logic [8857]
* chore: fix minor lint issues [8857]
* test: add (currently failing) test for request with invalid auth token, valid refresh token [8857]
* chore: allow realm to be provided to role module with refresh_token, without username/pass [8857]
* feat: add retry logic to requests in keycloak module utils [8857]
* chore: rename keycloak module fail_open_url method to fail_request [8857]
* chore: update all keycloak modules to support refresh token param [8857]
* chore: add refresh_token param to keycloak doc_fragments [8857]
* chore: restore dependency between auth_realm and auth_username,auth_password params [8857]
* chore: rearrange module param checks to reduce future pr size [8857]
* chore: remove extra comma [8857]
* chore: update version added for refresh token param [8857]
* chore: add changelog fragment [8857]
* chore: re-add fail_open_url to keycloak module utils for backward compatability [8857]
* fix: do not make a new request to keycloak without reauth when refresh token not provided (#8857)
* fix: only make final auth attempt if username/pass provided, and return exception on failure (#8857)
* fix: make re-auth and retry code more consistent, ensure final exceptions are thrown (#8857)
* test: fix arguments for invalid token, valid refresh token test (#8857)
* feat: catch invalid refresh token errors during re-auth attempt (#8857)
Add test to verify this behaviour works.
* test: improve test coverage, including some unhappy path tests for authentication failures (#8857)
* chore: store auth errors from token request in backwards compatible way (#8857)
* fix: ensure method is still specified for all requests (#8857)
* chore: simplify token request logic (#8857)
* chore: rename functions to request tokens using refresh token or username/password (#8857)
To emphasize their difference from the `get_token` function,
which either gets the token from the module params
*or* makes a request for it.
* doc: add docstrings for new or significantly modified functions (#8857)
* test: repair unit test following change to exception message upon key error during auth request (#8857)
2025-01-26 14:23:39 +00:00
|
|
|
required_together=([['auth_realm', 'auth_username', 'auth_password']]),
|
|
|
|
required_by={'refresh_token': 'auth_realm'},
|
|
|
|
)
|
2023-09-19 16:07:25 +00:00
|
|
|
|
|
|
|
result = dict(changed=False, msg='', end_state={})
|
|
|
|
|
|
|
|
# Obtain access token, initialize API
|
|
|
|
try:
|
|
|
|
connection_header = get_token(module.params)
|
|
|
|
except KeycloakError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
kc = KeycloakAPI(module, connection_header)
|
|
|
|
|
|
|
|
# Convenience variables
|
|
|
|
state = module.params.get('state')
|
|
|
|
name = module.params.get('name')
|
|
|
|
policy_type = module.params.get('policy_type')
|
|
|
|
client_id = module.params.get('client_id')
|
|
|
|
realm = module.params.get('realm')
|
|
|
|
|
|
|
|
cid = kc.get_client_id(client_id, realm=realm)
|
|
|
|
if not cid:
|
|
|
|
module.fail_json(msg='Invalid client %s for realm %s' %
|
|
|
|
(client_id, realm))
|
|
|
|
|
|
|
|
before_authz_custom_policy = kc.get_authz_policy_by_name(
|
|
|
|
name=name, client_id=cid, realm=realm)
|
|
|
|
|
|
|
|
desired_authz_custom_policy = {}
|
|
|
|
desired_authz_custom_policy['name'] = name
|
|
|
|
desired_authz_custom_policy['type'] = policy_type
|
|
|
|
|
|
|
|
# Modifying existing custom policies is not possible
|
|
|
|
if before_authz_custom_policy and state == 'present':
|
|
|
|
result['msg'] = "Custom policy %s already exists" % (name)
|
|
|
|
result['changed'] = False
|
|
|
|
result['end_state'] = desired_authz_custom_policy
|
|
|
|
elif not before_authz_custom_policy and state == 'present':
|
|
|
|
if module.check_mode:
|
|
|
|
result['msg'] = "Would create custom policy %s" % (name)
|
|
|
|
else:
|
|
|
|
kc.create_authz_custom_policy(
|
|
|
|
payload=desired_authz_custom_policy, policy_type=policy_type, client_id=cid, realm=realm)
|
|
|
|
result['msg'] = "Custom policy %s created" % (name)
|
|
|
|
|
|
|
|
result['changed'] = True
|
|
|
|
result['end_state'] = desired_authz_custom_policy
|
|
|
|
elif before_authz_custom_policy and state == 'absent':
|
|
|
|
if module.check_mode:
|
|
|
|
result['msg'] = "Would remove custom policy %s" % (name)
|
|
|
|
else:
|
|
|
|
kc.remove_authz_custom_policy(
|
|
|
|
policy_id=before_authz_custom_policy['id'], client_id=cid, realm=realm)
|
|
|
|
result['msg'] = "Custom policy %s removed" % (name)
|
|
|
|
|
|
|
|
result['changed'] = True
|
|
|
|
result['end_state'] = {}
|
|
|
|
elif not before_authz_custom_policy and state == 'absent':
|
|
|
|
result['msg'] = "Custom policy %s does not exist" % (name)
|
|
|
|
result['changed'] = False
|
|
|
|
result['end_state'] = {}
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|