| 1 |
# Copyright (c) 2022, Dušan Marković (@bratwurzt) |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
- name: Create realm |
| 6 |
community.general.keycloak_realm: |
| 7 |
auth_keycloak_url: "{{ url }}" |
| 8 |
auth_realm: "{{ admin_realm }}" |
| 9 |
auth_username: "{{ admin_user }}" |
| 10 |
auth_password: "{{ admin_password }}" |
| 11 |
id: "{{ realm }}" |
| 12 |
realm: "{{ realm }}" |
| 13 |
state: present |
| 14 |
|
| 15 |
- name: Create client |
| 16 |
community.general.keycloak_client: |
| 17 |
auth_keycloak_url: "{{ url }}" |
| 18 |
auth_realm: "{{ admin_realm }}" |
| 19 |
auth_username: "{{ admin_user }}" |
| 20 |
auth_password: "{{ admin_password }}" |
| 21 |
realm: "{{ realm }}" |
| 22 |
client_id: "{{ client_id }}" |
| 23 |
service_accounts_enabled: true |
| 24 |
state: present |
| 25 |
register: client |
| 26 |
|
| 27 |
- name: Create new realm role |
| 28 |
community.general.keycloak_role: |
| 29 |
auth_keycloak_url: "{{ url }}" |
| 30 |
auth_realm: "{{ admin_realm }}" |
| 31 |
auth_username: "{{ admin_user }}" |
| 32 |
auth_password: "{{ admin_password }}" |
| 33 |
realm: "{{ realm }}" |
| 34 |
name: "{{ role }}" |
| 35 |
description: "{{ description_1 }}" |
| 36 |
state: present |
| 37 |
|
| 38 |
- name: Map a realm role to client service account |
| 39 |
vars: |
| 40 |
roles: |
| 41 |
- name: '{{ role }}' |
| 42 |
community.general.keycloak_user_rolemapping: |
| 43 |
auth_keycloak_url: "{{ url }}" |
| 44 |
auth_realm: "{{ admin_realm }}" |
| 45 |
auth_username: "{{ admin_user }}" |
| 46 |
auth_password: "{{ admin_password }}" |
| 47 |
realm: "{{ realm }}" |
| 48 |
service_account_user_client_id: "{{ client_id }}" |
| 49 |
roles: "{{ roles }}" |
| 50 |
state: present |
| 51 |
register: result |
| 52 |
|
| 53 |
- name: Assert realm role is assigned |
| 54 |
assert: |
| 55 |
that: |
| 56 |
- result is changed |
| 57 |
- result.end_state | selectattr("clientRole", "eq", false) | selectattr("name", "eq", role) | list | count > 0 |
| 58 |
|
| 59 |
- name: Unmap a realm role from client service account |
| 60 |
vars: |
| 61 |
roles: |
| 62 |
- name: '{{ role }}' |
| 63 |
community.general.keycloak_user_rolemapping: |
| 64 |
auth_keycloak_url: "{{ url }}" |
| 65 |
auth_realm: "{{ admin_realm }}" |
| 66 |
auth_username: "{{ admin_user }}" |
| 67 |
auth_password: "{{ admin_password }}" |
| 68 |
realm: "{{ realm }}" |
| 69 |
service_account_user_client_id: "{{ client_id }}" |
| 70 |
roles: "{{ roles }}" |
| 71 |
state: absent |
| 72 |
register: result |
| 73 |
|
| 74 |
- name: Assert realm role is unassigned |
| 75 |
assert: |
| 76 |
that: |
| 77 |
- result is changed |
| 78 |
- (result.end_state | length) == (result.existing | length) - 1 |
| 79 |
- result.existing | selectattr("clientRole", "eq", false) | selectattr("name", "eq", role) | list | count > 0 |
| 80 |
- result.end_state | selectattr("clientRole", "eq", false) | selectattr("name", "eq", role) | list | count == 0 |
| 81 |
|
| 82 |
- name: Delete existing realm role |
| 83 |
community.general.keycloak_role: |
| 84 |
auth_keycloak_url: "{{ url }}" |
| 85 |
auth_realm: "{{ admin_realm }}" |
| 86 |
auth_username: "{{ admin_user }}" |
| 87 |
auth_password: "{{ admin_password }}" |
| 88 |
realm: "{{ realm }}" |
| 89 |
name: "{{ role }}" |
| 90 |
state: absent |
| 91 |
|
| 92 |
- name: Create second client (for cross-client role mapping test) |
| 93 |
community.general.keycloak_client: |
| 94 |
auth_keycloak_url: "{{ url }}" |
| 95 |
auth_realm: "{{ admin_realm }}" |
| 96 |
auth_username: "{{ admin_user }}" |
| 97 |
auth_password: "{{ admin_password }}" |
| 98 |
realm: "{{ realm }}" |
| 99 |
client_id: "{{ client_id_2 }}" |
| 100 |
service_accounts_enabled: true |
| 101 |
state: present |
| 102 |
register: client_2 |
| 103 |
|
| 104 |
- name: Create new client role |
| 105 |
community.general.keycloak_role: |
| 106 |
auth_keycloak_url: "{{ url }}" |
| 107 |
auth_realm: "{{ admin_realm }}" |
| 108 |
auth_username: "{{ admin_user }}" |
| 109 |
auth_password: "{{ admin_password }}" |
| 110 |
realm: "{{ realm }}" |
| 111 |
client_id: "{{ client_id }}" |
| 112 |
name: "{{ role }}" |
| 113 |
description: "{{ description_1 }}" |
| 114 |
state: present |
| 115 |
|
| 116 |
- name: Map a client role to a user with no existing roles for that client |
| 117 |
vars: |
| 118 |
roles: |
| 119 |
- name: '{{ role }}' |
| 120 |
community.general.keycloak_user_rolemapping: |
| 121 |
auth_keycloak_url: "{{ url }}" |
| 122 |
auth_realm: "{{ admin_realm }}" |
| 123 |
auth_username: "{{ admin_user }}" |
| 124 |
auth_password: "{{ admin_password }}" |
| 125 |
realm: "{{ realm }}" |
| 126 |
client_id: "{{ client_id }}" |
| 127 |
service_account_user_client_id: "{{ client_id_2 }}" |
| 128 |
roles: "{{ roles }}" |
| 129 |
state: present |
| 130 |
register: result |
| 131 |
|
| 132 |
- name: Assert client role is assigned to user with no prior roles |
| 133 |
assert: |
| 134 |
that: |
| 135 |
- result is changed |
| 136 |
- result.end_state | selectattr("clientRole", "eq", true) | selectattr("name", "eq", role) | list | count > 0 |
| 137 |
|
| 138 |
- name: Unmap the cross-client role mapping |
| 139 |
vars: |
| 140 |
roles: |
| 141 |
- name: '{{ role }}' |
| 142 |
community.general.keycloak_user_rolemapping: |
| 143 |
auth_keycloak_url: "{{ url }}" |
| 144 |
auth_realm: "{{ admin_realm }}" |
| 145 |
auth_username: "{{ admin_user }}" |
| 146 |
auth_password: "{{ admin_password }}" |
| 147 |
realm: "{{ realm }}" |
| 148 |
client_id: "{{ client_id }}" |
| 149 |
service_account_user_client_id: "{{ client_id_2 }}" |
| 150 |
roles: "{{ roles }}" |
| 151 |
state: absent |
| 152 |
register: result |
| 153 |
|
| 154 |
- name: Assert cross-client role mapping is removed |
| 155 |
assert: |
| 156 |
that: |
| 157 |
- result is changed |
| 158 |
- result.end_state == [] |
| 159 |
|
| 160 |
- name: Map a client role to client service account |
| 161 |
vars: |
| 162 |
roles: |
| 163 |
- name: '{{ role }}' |
| 164 |
community.general.keycloak_user_rolemapping: |
| 165 |
auth_keycloak_url: "{{ url }}" |
| 166 |
auth_realm: "{{ admin_realm }}" |
| 167 |
auth_username: "{{ admin_user }}" |
| 168 |
auth_password: "{{ admin_password }}" |
| 169 |
realm: "{{ realm }}" |
| 170 |
client_id: "{{ client_id }}" |
| 171 |
service_account_user_client_id: "{{ client_id }}" |
| 172 |
roles: "{{ roles }}" |
| 173 |
state: present |
| 174 |
register: result |
| 175 |
|
| 176 |
- name: Assert client role is assigned |
| 177 |
assert: |
| 178 |
that: |
| 179 |
- result is changed |
| 180 |
- result.end_state | selectattr("clientRole", "eq", true) | selectattr("name", "eq", role) | list | count > 0 |
| 181 |
|
| 182 |
- name: Unmap a client role from client service account |
| 183 |
vars: |
| 184 |
roles: |
| 185 |
- name: '{{ role }}' |
| 186 |
community.general.keycloak_user_rolemapping: |
| 187 |
auth_keycloak_url: "{{ url }}" |
| 188 |
auth_realm: "{{ admin_realm }}" |
| 189 |
auth_username: "{{ admin_user }}" |
| 190 |
auth_password: "{{ admin_password }}" |
| 191 |
realm: "{{ realm }}" |
| 192 |
client_id: "{{ client_id }}" |
| 193 |
service_account_user_client_id: "{{ client_id }}" |
| 194 |
roles: "{{ roles }}" |
| 195 |
state: absent |
| 196 |
register: result |
| 197 |
|
| 198 |
- name: Assert client role is unassigned |
| 199 |
assert: |
| 200 |
that: |
| 201 |
- result is changed |
| 202 |
- result.end_state == [] |
| 203 |
- result.existing | selectattr("clientRole", "eq", true) | selectattr("name", "eq", role) | list | count > 0 |