--- # 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)