Files
ansible-middleware.keycloak/molecule/keycloak_modules/keycloak_modules_authentication/verify.yml
T
Martin Cihlar c51f290d64 AMW-616 Port 24 community.general keycloak integration tests to molecule
Port integration tests from community.general keycloak modules into
the keycloak collection molecule test framework (keycloak_modules
scenario), adapting for KC 26.x behavioral changes and middleware
module differences.

Ported modules: keycloak_authentication, keycloak_authz_authorization_scope,
keycloak_authz_permission, keycloak_client, keycloak_client_rolescope,
keycloak_client_scope_rolemappings, keycloak_client_scope_type,
keycloak_clientsecret_info, keycloak_clientsecret_regenerate,
keycloak_component_info, keycloak_group, keycloak_group_rolemapping,
keycloak_identity_provider, keycloak_modules_authentication,
keycloak_realm, keycloak_realm_key, keycloak_realm_users_info,
keycloak_role, keycloak_user, keycloak_user_federation,
keycloak_user_rolemapping, keycloak_userprofile.

KC 26.x adaptations:
- serviceAccountsEnabled defaults to true on new clients
- Default Policy/Resource no longer auto-created for authz clients
- Built-in admin role rejects PUT updates (403)
- Invalid credentials return HTTP 400 instead of 401
- LDAP federation no longer auto-creates mappers

Other fixes:
- Variable collision avoidance (role/group/client register overwriting
  play-level vars via include_vars)
- Ansible 2.19+ native Jinja dict handling (endpoint combine filter)
- Jinja embedded template warnings in assert expressions
- Auth fallback prevention in invalid-token tests
2026-10-08 20:48:09 +02:00

282 lines
9.2 KiB
YAML

---
# Ported from community.general tests/integration/targets/keycloak_modules_authentication
# Tests the authentication layer shared by all keycloak modules:
# token auth, refresh token fallback, auth fallback chains, client_credentials, and 401 errors.
- name: Load authentication test variables
ansible.builtin.include_vars:
file: vars.yml
- name: Reset public login in master admin-cli (if potentially previous test failed)
middleware_automation.keycloak.keycloak_client:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
auth_client_id: admin-cli
auth_client_secret: "{{ client_secret }}"
client_id: admin-cli
secret: "{{ client_secret }}"
public_client: true
state: present
- name: Create new realm role with username/password authentication
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
- name: Assert username/password authentication succeeded
ansible.builtin.assert:
that:
- result is changed
- result is not failed
- name: Remove created realm role
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
state: absent
register: result
- name: Get Keycloak token
ansible.builtin.uri:
url: "{{ auth_keycloak_url }}/realms/master/protocol/openid-connect/token"
method: POST
return_content: true
status_code: 200
body_format: form-urlencoded
body:
grant_type: "password"
client_id: "admin-cli"
username: "{{ auth_username }}"
password: "{{ auth_password }}"
register: token_response
- name: Extract tokens
ansible.builtin.set_fact:
access_token: "{{ token_response.json.access_token }}"
refresh_token: "{{ token_response.json.refresh_token }}"
- name: Create new realm role with provided token authentication
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
token: "{{ access_token }}"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
- name: Assert token authentication succeeded
ansible.builtin.assert:
that:
- result is changed
- result is not failed
- name: Remove created realm role
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
state: absent
register: result
- name: Create new realm role with invalid auth token and valid refresh token
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
token: "invalidtoken!!!"
refresh_token: "{{ refresh_token }}"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
- name: Assert refresh token fallback authentication succeeded
ansible.builtin.assert:
that:
- result is changed
- result is not failed
- name: Remove created realm role
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
state: absent
register: result
- name: Create new realm role with invalid auth token and valid username/password
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
token: "invalidtoken!!!"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
- name: Assert username/password fallback authentication succeeded
ansible.builtin.assert:
that:
- result is changed
- result is not failed
- name: Remove created realm role
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
state: absent
register: result
- name: Create new realm role with invalid auth token, invalid refresh token, and valid username/password
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
token: "invalidtoken!!!"
refresh_token: "invalidrefreshtoken!!!"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
- name: Assert full fallback chain authentication succeeded
ansible.builtin.assert:
that:
- result is changed
- result is not failed
- name: Remove created realm role
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
state: absent
register: result
- name: Test client_credentials authentication
block:
- name: PREPARE - Temporarily disable public login in master admin-cli
middleware_automation.keycloak.keycloak_client:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
auth_client_id: admin-cli
auth_client_secret: "{{ client_secret }}"
client_id: admin-cli
secret: "{{ client_secret }}"
public_client: false
service_accounts_enabled: true
client_authenticator_type: client-secret
state: present
- name: PREPARE - Assign admin role to admin-cli in master
middleware_automation.keycloak.keycloak_user_rolemapping:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
auth_client_id: admin-cli
auth_client_secret: "{{ client_secret }}"
realm: master
roles:
- name: admin
service_account_user_client_id: admin-cli
- name: Create new realm role with valid client_id and client_secret
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_client_id: admin-cli
auth_client_secret: "{{ client_secret }}"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
- name: Assert client_credentials authentication succeeded
ansible.builtin.assert:
that:
- result is changed
- result is not failed
always:
- name: Reset temporarily disabled public login in master admin-cli
middleware_automation.keycloak.keycloak_client:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
auth_client_id: admin-cli
auth_client_secret: "{{ client_secret }}"
client_id: admin-cli
secret: "{{ client_secret }}"
public_client: true
state: present
- name: Remove created realm role
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
state: absent
register: result
### Unhappy path tests
- name: Fail to create new realm role with invalid username/password
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "invalid_password"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
failed_when: >
("HTTP Error 401: Unauthorized" not in result.msg) and
("HTTP Error 400: Bad Request" not in result.msg)
- name: Fail to create new realm role with invalid auth token
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_username: ""
auth_password: ""
token: "invalidtoken!!!"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
failed_when: >
("HTTP Error 401: Unauthorized" not in result.msg) and
("HTTP Error 400: Bad Request" not in result.msg)
- name: Fail to create new realm role with invalid auth and refresh tokens, and invalid username/password
middleware_automation.keycloak.keycloak_role:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: master
auth_username: "{{ auth_username }}"
auth_password: "invalid_password"
token: "invalidtoken!!!"
refresh_token: "invalidtoken!!!"
realm: "{{ ephemeral_realm }}"
name: "{{ auth_test_role }}"
description: "{{ keycloak_role_description }}"
state: present
register: result
failed_when: >
("HTTP Error 401: Unauthorized" not in result.msg) and
("HTTP Error 400: Bad Request" not in result.msg)