Vendor middleware_automation.keycloak collection
Needed for keycloak_authentication_v2, to manage Keycloak authentication flows (e.g. the CCCHH browser flow) as YAML. Chosen over community.general's own copy of the module (deprecated in favor of this collection, and limited to 4 sub-flow levels there vs. 10 here).
This commit is contained in:
parent
4540dc5516
commit
4832e5da5d
159 changed files with 34399 additions and 0 deletions
|
|
@ -0,0 +1,196 @@
|
|||
keycloak_realm
|
||||
==============
|
||||
<!--start description_realm -->
|
||||
|
||||
Create realms and clients in [keycloak](https://keycloak.org/) or [Red Hat Single Sign-On](https://access.redhat.com/products/red-hat-single-sign-on) services.
|
||||
<!--end description_realm -->
|
||||
|
||||
Role Defaults
|
||||
-------------
|
||||
|
||||
| Variable | Description | Default |
|
||||
|:---------|:------------|:--------|
|
||||
|`keycloak_realm_admin_user`| Administration console user account | `admin` |
|
||||
|`keycloak_realm_host`| hostname | `localhost` |
|
||||
|`keycloak_realm_context`| Context path for rest calls (set to `/auth` for legacy WildFly-based Keycloak) | `` |
|
||||
|`keycloak_realm_http_port`| HTTP port | `8080` |
|
||||
|`keycloak_realm_https_port`| TLS HTTP port | `8443` |
|
||||
|`keycloak_realm_auth_realm`| Name of the main authentication realm | `master` |
|
||||
|`keycloak_realm_management_http_port`| Management port | `9990` |
|
||||
|`keycloak_realm_auth_client`| Authentication client for configuration REST calls | `admin-cli` |
|
||||
|`keycloak_realm_client_public`| Configure a public realm client | `True` |
|
||||
|`keycloak_realm_client_web_origins`| Web origins for realm client | `/*` |
|
||||
|`keycloak_realm_url`| URL for configuration rest calls | `http://{{ keycloak_realm_host }}:{{ keycloak_realm_http_port }}` |
|
||||
|`keycloak_realm_management_url`| URL for management console rest calls | `http://{{ keycloak_realm_host }}:{{ keycloak_realm_management_http_port }}` |
|
||||
|
||||
|
||||
Role Variables
|
||||
--------------
|
||||
|
||||
The following are a set of _required_ variables for the role:
|
||||
|
||||
| Variable | Description |
|
||||
|:---------|:------------|
|
||||
|`keycloak_realm_realm` | Name of the realm to be created |
|
||||
|`keycloak_realm_admin_password`| Password for the administration console user account |
|
||||
|
||||
|
||||
The following variables are available for creating clients:
|
||||
|
||||
| Variable | Description | Default |
|
||||
|:---------|:------------|:---------|
|
||||
|`keycloak_realm_clients` | List of _client_ declarations for the realm | `[]` |
|
||||
|`keycloak_realm_client_default_roles` | List of default role name for clients | `[]` |
|
||||
|`keycloak_realm_client_users` | List of user/role mappings for a client | `[]` |
|
||||
|
||||
|
||||
The following variables are available for creating user federation:
|
||||
|
||||
| Variable | Description | Default |
|
||||
|:---------|:------------|:---------|
|
||||
|`keycloak_realm_user_federation` | List of _keycloak_user_federation_ for the realm | `[]` |
|
||||
|
||||
|
||||
Variable formats
|
||||
----------------
|
||||
|
||||
* `keycloak_realm_user_federation`, a list of:
|
||||
|
||||
```yaml
|
||||
- realm: <name of the realm in which user federation should be configured, required>
|
||||
name: <name of the user federation provider, required>
|
||||
provider_id: <type of the user federation provider, required>
|
||||
provider_type: <Provider Type, default is set to org.keycloak.storage.UserStorageProvider>
|
||||
config: <dictionary of supported configuration values, required>
|
||||
mappers: <list of supported configuration values, required>
|
||||
```
|
||||
|
||||
Refer to [docs](https://docs.ansible.com/ansible/latest/collections/community/general/keycloak_user_federation_module.html) for information on supported variables.
|
||||
|
||||
|
||||
* `keycloak_realm_clients`, a list of:
|
||||
|
||||
```yaml
|
||||
- name: <name of the client>
|
||||
id: <id of the client>
|
||||
client_id: <id of the client>
|
||||
secret: <secret of the client (Optional)>
|
||||
roles: <keycloak_realm_client_default_roles>
|
||||
realm: <name of the realm that contains the client>
|
||||
public_client: <true for public, false for confidential>
|
||||
web_origins: <list of allowed we origins for the client>
|
||||
users: <keycloak_realm_client_users>
|
||||
```
|
||||
|
||||
`name` and either `id` or `client_id` are required.
|
||||
|
||||
|
||||
* `keycloak_realm_client_users`, a list of:
|
||||
|
||||
```yaml
|
||||
- username: <username, required>
|
||||
password: <password, required>
|
||||
firstName: <firstName, optional>
|
||||
lastName: <lastName, optional>
|
||||
email: <email, optional>
|
||||
client_roles: <list of client user/role mappings>
|
||||
```
|
||||
|
||||
* Client user/role mappings, a list of:
|
||||
|
||||
```yaml
|
||||
- client: <name of the client>
|
||||
role: <name of the role>
|
||||
realm: <name of the realm>
|
||||
```
|
||||
|
||||
For a comprehensive example, refer to the [playbook](../../playbooks/keycloak_realm.yml).
|
||||
|
||||
|
||||
Related Modules
|
||||
---------------
|
||||
|
||||
For features not covered by this role, the collection provides dedicated modules:
|
||||
|
||||
| Module | What It Manages |
|
||||
|:-------|:----------------|
|
||||
| `keycloak_client_scope` | Client scopes and protocol mappers — see [example playbook](../../playbooks/keycloak_client_scope.yml) |
|
||||
| `keycloak_authentication_flow` | Authentication flows and execution steps — see [example playbook](../../playbooks/keycloak_authentication_flow.yml) |
|
||||
| `keycloak_client` | Clients (also used internally by this role) |
|
||||
| `keycloak_role` | Realm and client roles |
|
||||
| `keycloak_realm_user_federation` | User federations such as LDAP (also used internally by this role) |
|
||||
|
||||
|
||||
Example Playbook
|
||||
----------------
|
||||
|
||||
The following is an example playbook that makes use of the role to create a realm in keycloak.
|
||||
|
||||
```yaml
|
||||
---
|
||||
- hosts: ...
|
||||
collections:
|
||||
- middleware_automation.keycloak
|
||||
tasks:
|
||||
- name: Include keycloak role
|
||||
include_role:
|
||||
name: keycloak_realm
|
||||
vars:
|
||||
keycloak_admin_password: "changeme"
|
||||
keycloak_realm: TestRealm
|
||||
keycloak_clients: [...]
|
||||
```
|
||||
|
||||
The following example uses the `keycloak_client_scope` module to create a client scope with protocol mappers:
|
||||
|
||||
```yaml
|
||||
- name: Create client scope
|
||||
middleware_automation.keycloak.keycloak_client_scope:
|
||||
auth_keycloak_url: http://localhost:8080
|
||||
auth_realm: master
|
||||
auth_username: admin
|
||||
auth_password: changeme
|
||||
realm: TestRealm
|
||||
name: my-scope
|
||||
protocol_mappers:
|
||||
- name: email
|
||||
protocolMapper: oidc-usermodel-attribute-mapper
|
||||
config:
|
||||
user.attribute: email
|
||||
claim.name: email
|
||||
id.token.claim: "true"
|
||||
access.token.claim: "true"
|
||||
state: present
|
||||
```
|
||||
|
||||
The following example uses the `keycloak_authentication_flow` module to create a custom authentication flow:
|
||||
|
||||
```yaml
|
||||
- name: Create authentication flow
|
||||
middleware_automation.keycloak.keycloak_authentication_flow:
|
||||
auth_keycloak_url: http://localhost:8080
|
||||
auth_realm: master
|
||||
auth_username: admin
|
||||
auth_password: changeme
|
||||
realm: TestRealm
|
||||
alias: my-browser-flow
|
||||
executions:
|
||||
- provider_id: auth-cookie
|
||||
requirement: ALTERNATIVE
|
||||
- provider_id: auth-password
|
||||
requirement: REQUIRED
|
||||
state: present
|
||||
```
|
||||
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
Apache License 2.0
|
||||
|
||||
|
||||
Author Information
|
||||
------------------
|
||||
|
||||
* [Guido Grazioli](https://github.com/guidograzioli)
|
||||
* [Romain Pelisse](https://github.com/rpelisse)
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
---
|
||||
### Keycloak configuration settings
|
||||
keycloak_realm_host: localhost
|
||||
keycloak_realm_http_port: 8080
|
||||
keycloak_realm_https_port: 8443
|
||||
keycloak_realm_management_http_port: 9990
|
||||
|
||||
### Keycloak administration console user
|
||||
keycloak_realm_admin_user: admin
|
||||
keycloak_realm_auth_realm: master
|
||||
keycloak_realm_auth_client: admin-cli
|
||||
keycloak_realm_context: ''
|
||||
|
||||
# administrator console password, this is a required variable
|
||||
keycloak_realm_admin_password: ''
|
||||
|
||||
### Keycloak realms, clients, roles, federation
|
||||
# list of clients to create in the realm
|
||||
#
|
||||
# Refer to the playbook for a comprehensive example.
|
||||
# Also refer to meta/argument_specs.yml for specifications.
|
||||
#
|
||||
# Each client has the form:
|
||||
# { name: '', roles: [], realm: '', public_client: bool, web_origins: '', users: [] }
|
||||
# where roles is a list of default role names for the client
|
||||
# and users is a list of account, see below for the format definition
|
||||
# an empty name will skip the creation of the client
|
||||
#
|
||||
# keycloak_realm_clients:
|
||||
# - name: ''
|
||||
# roles: "{{ keycloak_realm_client_default_roles }}"
|
||||
# realm: "{{ keycloak_realm_realm }}"
|
||||
# public_client: "{{ keycloak_realm_client_public }}"
|
||||
# web_origins: "{{ keycloak_realm_client_web_origins }}"
|
||||
# redirect_uris: "{{ keycloak_realm_client_redirect_uris }}"
|
||||
# users: "{{ keycloak_realm_client_users }}"
|
||||
keycloak_realm_clients: []
|
||||
|
||||
# list of roles to create in the client
|
||||
keycloak_realm_client_default_roles: []
|
||||
|
||||
# if True, create a public client; otherwise, a confidetial client
|
||||
keycloak_realm_client_public: true
|
||||
|
||||
# allowed web origins for the client
|
||||
keycloak_realm_client_web_origins: '/*'
|
||||
|
||||
# list of user and role mappings to create in the client
|
||||
# Each user has the form:
|
||||
# { username: '', password: '', email: '', firstName: '', lastName: '', client_roles: [] }
|
||||
# where each client_role has the form:
|
||||
# { client: '', role: '', realm: '' }
|
||||
keycloak_realm_client_users: []
|
||||
|
||||
### List of Keycloak User Federation
|
||||
keycloak_realm_user_federation: []
|
||||
|
||||
# other settings
|
||||
keycloak_realm_url: "http://{{ keycloak_realm_host }}:{{ keycloak_realm_http_port + (keycloak_realm_jboss_port_offset | default(0)) }}"
|
||||
keycloak_realm_management_url: "http://{{ keycloak_realm_host }}:{{ keycloak_realm_management_http_port + (keycloak_realm_jboss_port_offset | default(0)) }}"
|
||||
keycloak_realm_no_log: true
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
argument_specs:
|
||||
main:
|
||||
options:
|
||||
keycloak_realm_host:
|
||||
# line 3 of keycloak_realm/defaults/main.yml
|
||||
default: "localhost"
|
||||
description: "Hostname for rest calls"
|
||||
type: "str"
|
||||
keycloak_realm_context:
|
||||
# line 5 of keycloak_realm/defaults/main.yml
|
||||
default: ""
|
||||
description: "Context path for rest calls (was /auth for legacy WildFly-based Keycloak, empty for Quarkus-based Keycloak/RHBK)"
|
||||
type: "str"
|
||||
keycloak_realm_http_port:
|
||||
# line 4 of keycloak_realm/defaults/main.yml
|
||||
default: 8080
|
||||
description: "HTTP port"
|
||||
type: "int"
|
||||
keycloak_realm_https_port:
|
||||
# line 5 of keycloak_realm/defaults/main.yml
|
||||
default: 8443
|
||||
description: "HTTPS port"
|
||||
type: "int"
|
||||
keycloak_realm_management_http_port:
|
||||
# line 6 of keycloak_realm/defaults/main.yml
|
||||
default: 9990
|
||||
description: "Management port"
|
||||
type: "int"
|
||||
keycloak_realm_admin_user:
|
||||
# line 10 of keycloak_realm/defaults/main.yml
|
||||
default: "admin"
|
||||
description: "Administration console user account"
|
||||
type: "str"
|
||||
keycloak_realm_auth_realm:
|
||||
# line 11 of keycloak_realm/defaults/main.yml
|
||||
default: "master"
|
||||
description: "Name of the main authentication realm"
|
||||
type: "str"
|
||||
keycloak_realm_auth_client:
|
||||
# line 12 of keycloak_realm/defaults/main.yml
|
||||
default: "admin-cli"
|
||||
description: "Authentication client for configuration REST calls"
|
||||
type: "str"
|
||||
keycloak_realm_client_default_roles:
|
||||
# line 36 of keycloak_realm/defaults/main.yml
|
||||
default: "[]"
|
||||
description: "List of roles to configure as client default"
|
||||
type: "list"
|
||||
keycloak_realm_client_public:
|
||||
# line 39 of keycloak_realm/defaults/main.yml
|
||||
default: true
|
||||
description: "Configure a public realm client"
|
||||
type: "bool"
|
||||
keycloak_realm_client_web_origins:
|
||||
# line 42 of keycloak_realm/defaults/main.yml
|
||||
default: "/*"
|
||||
description: "Web origins for realm client"
|
||||
type: "str"
|
||||
keycloak_realm_client_users:
|
||||
# line 49 of keycloak_realm/defaults/main.yml
|
||||
default: "[]"
|
||||
description: "List of users to configure in the realm client"
|
||||
type: "list"
|
||||
keycloak_realm_user_federation:
|
||||
# line 52 of keycloak_realm/defaults/main.yml
|
||||
default: "[]"
|
||||
description: "List of user federations to configure in the realm"
|
||||
type: "list"
|
||||
keycloak_realm_admin_password:
|
||||
# line 5 of keycloak_realm/vars/main.yml
|
||||
required: true
|
||||
description: "Password for the administration console user account"
|
||||
type: "str"
|
||||
keycloak_realm_realm:
|
||||
# line 8 of keycloak_realm/vars/main.yml
|
||||
required: true
|
||||
description: "Name of the realm to be configured"
|
||||
type: "str"
|
||||
keycloak_realm_clients:
|
||||
# line 11 of keycloak_realm/vars/main.yml
|
||||
default: "[]"
|
||||
description: "List of client declarations for the realm"
|
||||
type: "list"
|
||||
keycloak_realm_url:
|
||||
# line 14 of keycloak_realm/vars/main.yml
|
||||
default: "http://{{ keycloak_realm_host }}:{{ keycloak_realm_http_port + ( keycloak_realm_jboss_port_offset | default(0) ) }}"
|
||||
description: "URL for configuration rest calls"
|
||||
type: "str"
|
||||
keycloak_realm_management_url:
|
||||
# line 15 of keycloak_realm/vars/main.yml
|
||||
default: "http://{{ keycloak_realm_host }}:{{ keycloak_realm_management_http_port + ( keycloak_realm_jboss_port_offset | default(0) ) }}"
|
||||
description: "URL for management console rest calls"
|
||||
type: "str"
|
||||
downstream:
|
||||
options:
|
||||
sso_version:
|
||||
default: "7.6.0"
|
||||
description: "Red Hat Single Sign-On version"
|
||||
type: "str"
|
||||
sso_dest:
|
||||
default: "/opt/sso"
|
||||
description: "Root installation directory"
|
||||
type: "str"
|
||||
sso_installdir:
|
||||
default: "{{ sso_dest }}/rh-sso-{{ sso_version.split('.')[0] }}.{{ sso_version.split('.')[1] }}"
|
||||
description: "Installation path for Red Hat SSO"
|
||||
type: "str"
|
||||
sso_apply_patches:
|
||||
default: false
|
||||
description: "Install Red Hat SSO most recent cumulative patch"
|
||||
type: "bool"
|
||||
sso_enable:
|
||||
default: true
|
||||
description: "Enable Red Hat Single Sign-on installation"
|
||||
type: "bool"
|
||||
rhbk_version:
|
||||
default: "22.0.6"
|
||||
description: "Red Hat Build of Keycloak version"
|
||||
type: "str"
|
||||
rhbk_archive:
|
||||
default: "rhbk-{{ rhbk_version }}.zip"
|
||||
description: "Red Hat Build of Keycloak install archive filename"
|
||||
type: "str"
|
||||
rhbk_dest:
|
||||
default: "/opt/rhbk"
|
||||
description: "Root installation directory"
|
||||
type: "str"
|
||||
rhbk_installdir:
|
||||
default: "{{ rhbk_dest }}/rhbk-{{ rhbk_version.split('.')[0] }}.{{ rhbk_version.split('.')[1] }}"
|
||||
description: "Installation path for Red Hat Build of Keycloak"
|
||||
type: "str"
|
||||
rhbk_enable:
|
||||
default: true
|
||||
description: "Enable Red Hat Build of Keycloak installation"
|
||||
type: "bool"
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
---
|
||||
galaxy_info:
|
||||
role_name: keycloak_realm
|
||||
namespace: middleware_automation
|
||||
author: Romain Pelisse, Guido Grazioli
|
||||
description: Create realms and clients in keycloak or Red Hat Single Sign-On
|
||||
company: Red Hat, Inc.
|
||||
|
||||
license: Apache License 2.0
|
||||
|
||||
min_ansible_version: "2.16"
|
||||
|
||||
platforms:
|
||||
- name: EL
|
||||
versions:
|
||||
- "8"
|
||||
|
||||
galaxy_tags:
|
||||
- keycloak
|
||||
- redhat
|
||||
- rhel
|
||||
- sso
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
---
|
||||
- name: Generate keycloak auth token
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/realms/master/protocol/openid-connect/token"
|
||||
method: POST
|
||||
body: "client_id={{ keycloak_realm_auth_client }}&username={{ keycloak_realm_admin_user }}&password={{ keycloak_realm_admin_password }}&grant_type=password"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
no_log: "{{ keycloak_realm_no_log | default('True') }}"
|
||||
register: keycloak_realm_auth_response
|
||||
until: keycloak_realm_auth_response.status == 200
|
||||
retries: 5
|
||||
delay: 2
|
||||
|
||||
- name: "Determine if realm exists"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}"
|
||||
method: GET
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
status_code:
|
||||
- 200
|
||||
- 404
|
||||
headers:
|
||||
Accept: "application/json"
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_realm_exists
|
||||
|
||||
- name: Create Realm
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms"
|
||||
method: POST
|
||||
body: "{{ lookup('template', 'realm.json.j2') }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
body_format: json
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
status_code: 201
|
||||
when: keycloak_realm_realm_exists.status == 404
|
||||
|
||||
- name: Create user federation
|
||||
middleware_automation.keycloak.keycloak_user_federation:
|
||||
auth_keycloak_url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}"
|
||||
auth_realm: "{{ keycloak_realm_auth_realm }}"
|
||||
auth_username: "{{ keycloak_realm_admin_user }}"
|
||||
auth_password: "{{ keycloak_realm_admin_password }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
realm: "{{ item.realm | default(keycloak_realm_realm) }}"
|
||||
name: "{{ item.name }}"
|
||||
state: present
|
||||
provider_id: "{{ item.provider_id }}"
|
||||
provider_type: "{{ item.provider_type | default('org.keycloak.storage.UserStorageProvider') }}"
|
||||
config: "{{ item.config }}"
|
||||
mappers: "{{ item.mappers | default(omit) }}"
|
||||
no_log: "{{ keycloak_realm_no_log | default('True') }}"
|
||||
register: keycloak_realm_create_user_federation_result
|
||||
loop: "{{ keycloak_realm_user_federation | flatten }}"
|
||||
when: keycloak_realm_user_federation is defined
|
||||
|
||||
- name: Validate Keycloak clients
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- item.name is defined and item.name | length > 0
|
||||
- (item.client_id is defined and item.client_id | length > 0) or (item.id is defined and item.id | length > 0)
|
||||
fail_msg: "For each keycloak client, attributes `name` and either `id` or `client_id` is required"
|
||||
quiet: true
|
||||
loop: "{{ keycloak_realm_clients | flatten }}"
|
||||
loop_control:
|
||||
label: "{{ item.name | default('unnamed client') }}"
|
||||
|
||||
- name: Create or update a Keycloak client
|
||||
middleware_automation.keycloak.keycloak_client:
|
||||
auth_client_id: "{{ keycloak_realm_auth_client }}"
|
||||
auth_keycloak_url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}"
|
||||
auth_realm: "{{ keycloak_realm_auth_realm }}"
|
||||
auth_username: "{{ keycloak_realm_admin_user }}"
|
||||
auth_password: "{{ keycloak_realm_admin_password }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
realm: "{{ item.realm | default(keycloak_realm_realm) }}"
|
||||
default_roles: "{{ item.roles | default(omit) }}"
|
||||
client_id: "{{ item.client_id | default(omit) }}"
|
||||
id: "{{ item.id | default(omit) }}"
|
||||
secret: "{{ item.secret | default(omit) }}"
|
||||
name: "{{ item.name | default(omit) }}"
|
||||
description: "{{ item.description | default(omit) }}"
|
||||
root_url: "{{ item.root_url | default('') }}"
|
||||
admin_url: "{{ item.admin_url | default('') }}"
|
||||
base_url: "{{ item.base_url | default('') }}"
|
||||
enabled: "{{ item.enabled | default(True) }}"
|
||||
redirect_uris: "{{ item.redirect_uris | default(omit) }}"
|
||||
web_origins: "{{ item.web_origins | default(omit) }}"
|
||||
bearer_only: "{{ item.bearer_only | default(omit) }}"
|
||||
standard_flow_enabled: "{{ item.standard_flow_enabled | default(omit) }}"
|
||||
implicit_flow_enabled: "{{ item.implicit_flow_enabled | default(omit) }}"
|
||||
direct_access_grants_enabled: "{{ item.direct_access_grants_enabled | default(omit) }}"
|
||||
service_accounts_enabled: "{{ item.service_accounts_enabled | default(omit) }}"
|
||||
public_client: "{{ item.public_client | default(False) }}"
|
||||
protocol: "{{ item.protocol | default(omit) }}"
|
||||
attributes: "{{ item.attributes | default(omit) }}"
|
||||
state: present
|
||||
no_log: "{{ keycloak_realm_no_log | default('false') }}"
|
||||
register: keycloak_realm_create_client_result
|
||||
loop: "{{ keycloak_realm_clients | flatten }}"
|
||||
when: (item.name is defined and item.client_id is defined) or (item.name is defined and item.id is defined)
|
||||
|
||||
- name: Create client roles
|
||||
ansible.builtin.include_tasks: manage_client_roles.yml
|
||||
loop: "{{ keycloak_realm_clients | flatten }}"
|
||||
loop_control:
|
||||
loop_var: client
|
||||
when: "'roles' in client"
|
||||
|
||||
- name: Create client users
|
||||
ansible.builtin.include_tasks: manage_client_users.yml
|
||||
loop: "{{ keycloak_realm_clients | flatten }}"
|
||||
loop_control:
|
||||
loop_var: client
|
||||
when: "'users' in client"
|
||||
|
||||
- name: Provide Access token lifespan
|
||||
ansible.builtin.include_tasks: manage_token_lifespan.yml
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
- name: Create client roles
|
||||
middleware_automation.keycloak.keycloak_role:
|
||||
name: "{{ item }}"
|
||||
realm: "{{ client.realm | default(keycloak_realm_realm) }}"
|
||||
client_id: "{{ client.client_id }}"
|
||||
auth_client_id: "{{ keycloak_realm_auth_client }}"
|
||||
auth_keycloak_url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}"
|
||||
auth_realm: "{{ keycloak_realm_auth_realm }}"
|
||||
auth_username: "{{ keycloak_realm_admin_user }}"
|
||||
auth_password: "{{ keycloak_realm_admin_password }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
state: present
|
||||
loop: "{{ client.roles | flatten }}"
|
||||
no_log: "{{ keycloak_realm_no_log | default('True') }}"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
- name: Manage Users
|
||||
ansible.builtin.include_tasks: manage_user.yml
|
||||
loop: "{{ client.users | flatten }}"
|
||||
loop_control:
|
||||
loop_var: user
|
||||
|
||||
- name: Manage User Roles
|
||||
ansible.builtin.include_tasks: manage_user_roles.yml
|
||||
loop: "{{ client.users | flatten }}"
|
||||
loop_control:
|
||||
loop_var: user
|
||||
when: "'client_roles' in user"
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
---
|
||||
- name: "Update Access token lifespan"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}"
|
||||
method: PUT
|
||||
body:
|
||||
accessTokenLifespan: 300
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
body_format: json
|
||||
status_code:
|
||||
- 200
|
||||
- 204
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
---
|
||||
- name: "Check if User Already Exists"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}/users?username={{ user.username }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_user_search_result
|
||||
|
||||
- name: "Create User"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}/users"
|
||||
method: POST
|
||||
body:
|
||||
enabled: true
|
||||
attributes: "{{ user.attributes | default(omit) }}"
|
||||
username: "{{ user.username }}"
|
||||
email: "{{ user.email | default(omit) }}"
|
||||
firstName: "{{ user.firstName | default(omit) }}"
|
||||
lastName: "{{ user.lastName | default(omit) }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
body_format: json
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
status_code: 201
|
||||
when: keycloak_realm_user_search_result.json | length == 0
|
||||
|
||||
- name: "Get User"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}/users?username={{ user.username }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_user
|
||||
|
||||
- name: "Update User Password"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}/users/{{ (keycloak_realm_user.json | first).id }}/reset-password"
|
||||
method: PUT
|
||||
body:
|
||||
type: password
|
||||
temporary: false
|
||||
value: "{{ user.password }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
body_format: json
|
||||
status_code:
|
||||
- 200
|
||||
- 204
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_user
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
---
|
||||
- name: "Get Realm for role"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ client_role.realm | default(keycloak_realm_realm) }}"
|
||||
method: GET
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
status_code:
|
||||
- 200
|
||||
headers:
|
||||
Accept: "application/json"
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_client_role_realm
|
||||
|
||||
- name: Check if Mapping is available
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ client_role.realm | \
|
||||
default(keycloak_realm_realm) }}/users/{{ (keycloak_realm_user.json | first).id }}/role-mappings/clients/{{ (keycloak_realm_create_client_result.results | \
|
||||
selectattr('end_state.clientId', 'equalto', client_role.client) | list | first).end_state.id }}/available"
|
||||
method: GET
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
status_code:
|
||||
- 200
|
||||
headers:
|
||||
Accept: "application/json"
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_client_role_user_available
|
||||
|
||||
- name: "Create Role Mapping"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ client_role.realm | \
|
||||
default(keycloak_realm_realm) }}/users/{{ (keycloak_realm_user.json | first).id }}/role-mappings/clients/{{ (keycloak_realm_create_client_result.results | \
|
||||
selectattr('end_state.clientId', 'equalto', client_role.client) | list | first).end_state.id }}"
|
||||
method: POST
|
||||
body:
|
||||
- id: "{{ item.id }}"
|
||||
clientRole: "{{ item.clientRole }}"
|
||||
containerId: "{{ item.containerId }}"
|
||||
name: "{{ item.name }}"
|
||||
composite: "{{ item.composite }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
body_format: json
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
status_code: 204
|
||||
loop: "{{ keycloak_realm_client_role_user_available.json | flatten }}"
|
||||
when: item.name == client_role.role
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
- name: "Get User {{ user.username }}"
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/admin/realms/{{ keycloak_realm_realm }}/users?username={{ user.username }}"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
headers:
|
||||
Authorization: "Bearer {{ keycloak_realm_auth_response.json.access_token }}"
|
||||
register: keycloak_realm_user
|
||||
|
||||
- name: Refresh keycloak auth token
|
||||
ansible.builtin.uri:
|
||||
url: "{{ keycloak_realm_url }}{{ keycloak_realm_context }}/realms/master/protocol/openid-connect/token"
|
||||
method: POST
|
||||
body: "client_id={{ keycloak_realm_auth_client }}&username={{ keycloak_realm_admin_user }}&password={{ keycloak_realm_admin_password }}&grant_type=password"
|
||||
validate_certs: "{{ keycloak_realm_validate_certs | default(omit) }}"
|
||||
register: keycloak_realm_auth_response
|
||||
no_log: "{{ keycloak_realm_no_log | default('True') }}"
|
||||
until: keycloak_realm_auth_response.status == 200
|
||||
retries: 5
|
||||
delay: 2
|
||||
|
||||
- name: "Manage Client Role Mapping for {{ user.username }}"
|
||||
ansible.builtin.include_tasks: manage_user_client_roles.yml
|
||||
loop: "{{ user.client_roles | flatten }}"
|
||||
loop_control:
|
||||
loop_var: client_role
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"id": "{{ keycloak_realm_realm }}",
|
||||
"realm": "{{ keycloak_realm_realm }}",
|
||||
"enabled": true,
|
||||
"eventsEnabled": true,
|
||||
"eventsExpiration": 7200
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
# vars file for keycloak_realm
|
||||
|
||||
# name of the realm to create, this is a required variable
|
||||
keycloak_realm_realm:
|
||||
Loading…
Reference in a new issue