| 1 |
--- |
| 2 |
# Copyright (c) Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
- name: Wait for Keycloak |
| 6 |
uri: |
| 7 |
url: "{{ url }}/admin/" |
| 8 |
status_code: 200 |
| 9 |
validate_certs: false |
| 10 |
register: result |
| 11 |
until: result.status == 200 |
| 12 |
retries: 10 |
| 13 |
delay: 10 |
| 14 |
|
| 15 |
- name: Delete realm if exists |
| 16 |
community.general.keycloak_realm: |
| 17 |
auth_keycloak_url: "{{ url }}" |
| 18 |
auth_realm: "{{ admin_realm }}" |
| 19 |
auth_username: "{{ admin_user }}" |
| 20 |
auth_password: "{{ admin_password }}" |
| 21 |
realm: "{{ realm }}" |
| 22 |
state: absent |
| 23 |
|
| 24 |
- name: Create realm |
| 25 |
community.general.keycloak_realm: |
| 26 |
auth_keycloak_url: "{{ url }}" |
| 27 |
auth_realm: "{{ admin_realm }}" |
| 28 |
auth_username: "{{ admin_user }}" |
| 29 |
auth_password: "{{ admin_password }}" |
| 30 |
id: "{{ realm }}" |
| 31 |
realm: "{{ realm }}" |
| 32 |
state: present |
| 33 |
|
| 34 |
- name: Create a Keycloak realm role |
| 35 |
community.general.keycloak_role: |
| 36 |
auth_keycloak_url: "{{ url }}" |
| 37 |
auth_realm: "{{ admin_realm }}" |
| 38 |
auth_username: "{{ admin_user }}" |
| 39 |
auth_password: "{{ admin_password }}" |
| 40 |
name: "{{ item }}" |
| 41 |
realm: "{{ realm }}" |
| 42 |
with_items: |
| 43 |
- "{{ realm_role_admin }}" |
| 44 |
- "{{ realm_role_user }}" |
| 45 |
|
| 46 |
- name: Client private |
| 47 |
community.general.keycloak_client: |
| 48 |
auth_keycloak_url: "{{ url }}" |
| 49 |
auth_realm: "{{ admin_realm }}" |
| 50 |
auth_username: "{{ admin_user }}" |
| 51 |
auth_password: "{{ admin_password }}" |
| 52 |
realm: "{{ realm }}" |
| 53 |
client_id: "{{ client_name_private }}" |
| 54 |
state: present |
| 55 |
redirect_uris: |
| 56 |
- "https://my-backend-api.c.org/" |
| 57 |
fullScopeAllowed: true |
| 58 |
attributes: '{{client_attributes1}}' |
| 59 |
public_client: false |
| 60 |
|
| 61 |
- name: Create a Keycloak client role |
| 62 |
community.general.keycloak_role: |
| 63 |
auth_keycloak_url: "{{ url }}" |
| 64 |
auth_realm: "{{ admin_realm }}" |
| 65 |
auth_username: "{{ admin_user }}" |
| 66 |
auth_password: "{{ admin_password }}" |
| 67 |
name: "{{ item }}" |
| 68 |
realm: "{{ realm }}" |
| 69 |
client_id: "{{ client_name_private }}" |
| 70 |
with_items: |
| 71 |
- "{{ client_role_admin }}" |
| 72 |
- "{{ client_role_user }}" |
| 73 |
|
| 74 |
- name: Client public |
| 75 |
community.general.keycloak_client: |
| 76 |
auth_keycloak_url: "{{ url }}" |
| 77 |
auth_realm: "{{ admin_realm }}" |
| 78 |
auth_username: "{{ admin_user }}" |
| 79 |
auth_password: "{{ admin_password }}" |
| 80 |
realm: "{{ realm }}" |
| 81 |
client_id: "{{ client_name_public }}" |
| 82 |
redirect_uris: |
| 83 |
- "https://my-onepage-app-frontend.c.org/" |
| 84 |
attributes: '{{client_attributes1}}' |
| 85 |
full_scope_allowed: false |
| 86 |
public_client: true |
| 87 |
|
| 88 |
|
| 89 |
- name: Map roles to public client |
| 90 |
community.general.keycloak_client_rolescope: |
| 91 |
auth_keycloak_url: "{{ url }}" |
| 92 |
auth_realm: "{{ admin_realm }}" |
| 93 |
auth_username: "{{ admin_user }}" |
| 94 |
auth_password: "{{ admin_password }}" |
| 95 |
realm: "{{ realm }}" |
| 96 |
client_id: "{{ client_name_public }}" |
| 97 |
client_scope_id: "{{ client_name_private }}" |
| 98 |
role_names: |
| 99 |
- "{{ client_role_admin }}" |
| 100 |
- "{{ client_role_user }}" |
| 101 |
register: result |
| 102 |
|
| 103 |
- name: Assert mapping created |
| 104 |
assert: |
| 105 |
that: |
| 106 |
- result is changed |
| 107 |
- result.end_state | length == 2 |
| 108 |
|
| 109 |
- name: remap role user to public client |
| 110 |
community.general.keycloak_client_rolescope: |
| 111 |
auth_keycloak_url: "{{ url }}" |
| 112 |
auth_realm: "{{ admin_realm }}" |
| 113 |
auth_username: "{{ admin_user }}" |
| 114 |
auth_password: "{{ admin_password }}" |
| 115 |
realm: "{{ realm }}" |
| 116 |
client_id: "{{ client_name_public }}" |
| 117 |
client_scope_id: "{{ client_name_private }}" |
| 118 |
role_names: |
| 119 |
- "{{ client_role_user }}" |
| 120 |
register: result |
| 121 |
|
| 122 |
- name: Assert mapping created |
| 123 |
assert: |
| 124 |
that: |
| 125 |
- result is not changed |
| 126 |
- result.end_state | length == 2 |
| 127 |
|
| 128 |
- name: Remove Map role admin to public client |
| 129 |
community.general.keycloak_client_rolescope: |
| 130 |
auth_keycloak_url: "{{ url }}" |
| 131 |
auth_realm: "{{ admin_realm }}" |
| 132 |
auth_username: "{{ admin_user }}" |
| 133 |
auth_password: "{{ admin_password }}" |
| 134 |
realm: "{{ realm }}" |
| 135 |
client_id: "{{ client_name_public }}" |
| 136 |
client_scope_id: "{{ client_name_private }}" |
| 137 |
role_names: |
| 138 |
- "{{ client_role_admin }}" |
| 139 |
state: absent |
| 140 |
register: result |
| 141 |
|
| 142 |
- name: Assert mapping deleted |
| 143 |
assert: |
| 144 |
that: |
| 145 |
- result is changed |
| 146 |
- result.end_state | length == 1 |
| 147 |
- result.end_state[0].name == client_role_user |
| 148 |
|
| 149 |
- name: Map missing roles to public client |
| 150 |
community.general.keycloak_client_rolescope: |
| 151 |
auth_keycloak_url: "{{ url }}" |
| 152 |
auth_realm: "{{ admin_realm }}" |
| 153 |
auth_username: "{{ admin_user }}" |
| 154 |
auth_password: "{{ admin_password }}" |
| 155 |
realm: "{{ realm }}" |
| 156 |
client_id: "{{ client_name_public }}" |
| 157 |
client_scope_id: "{{ client_name_private }}" |
| 158 |
role_names: |
| 159 |
- "{{ client_role_admin }}" |
| 160 |
- "{{ client_role_not_exists }}" |
| 161 |
ignore_errors: true |
| 162 |
register: result |
| 163 |
|
| 164 |
- name: Assert failed mapping missing role |
| 165 |
assert: |
| 166 |
that: |
| 167 |
- result is failed |
| 168 |
|
| 169 |
- name: Map roles duplicate |
| 170 |
community.general.keycloak_client_rolescope: |
| 171 |
auth_keycloak_url: "{{ url }}" |
| 172 |
auth_realm: "{{ admin_realm }}" |
| 173 |
auth_username: "{{ admin_user }}" |
| 174 |
auth_password: "{{ admin_password }}" |
| 175 |
realm: "{{ realm }}" |
| 176 |
client_id: "{{ client_name_public }}" |
| 177 |
client_scope_id: "{{ client_name_private }}" |
| 178 |
role_names: |
| 179 |
- "{{ client_role_admin }}" |
| 180 |
- "{{ client_role_admin }}" |
| 181 |
register: result |
| 182 |
|
| 183 |
- name: Assert result |
| 184 |
assert: |
| 185 |
that: |
| 186 |
- result is changed |
| 187 |
- result.end_state | length == 2 |
| 188 |
|
| 189 |
- name: Map roles to private client |
| 190 |
community.general.keycloak_client_rolescope: |
| 191 |
auth_keycloak_url: "{{ url }}" |
| 192 |
auth_realm: "{{ admin_realm }}" |
| 193 |
auth_username: "{{ admin_user }}" |
| 194 |
auth_password: "{{ admin_password }}" |
| 195 |
realm: "{{ realm }}" |
| 196 |
client_id: "{{ client_name_private }}" |
| 197 |
role_names: |
| 198 |
- "{{ realm_role_admin }}" |
| 199 |
ignore_errors: true |
| 200 |
register: result |
| 201 |
|
| 202 |
- name: Assert failed mapping role to full scope client |
| 203 |
assert: |
| 204 |
that: |
| 205 |
- result is failed |
| 206 |
|
| 207 |
- name: Map realm role to public client |
| 208 |
community.general.keycloak_client_rolescope: |
| 209 |
auth_keycloak_url: "{{ url }}" |
| 210 |
auth_realm: "{{ admin_realm }}" |
| 211 |
auth_username: "{{ admin_user }}" |
| 212 |
auth_password: "{{ admin_password }}" |
| 213 |
realm: "{{ realm }}" |
| 214 |
client_id: "{{ client_name_public }}" |
| 215 |
role_names: |
| 216 |
- "{{ realm_role_admin }}" |
| 217 |
register: result |
| 218 |
|
| 219 |
- name: Assert result |
| 220 |
assert: |
| 221 |
that: |
| 222 |
- result is changed |
| 223 |
- result.end_state | length == 1 |
| 224 |
|
| 225 |
- name: Map two realm roles to public client |
| 226 |
community.general.keycloak_client_rolescope: |
| 227 |
auth_keycloak_url: "{{ url }}" |
| 228 |
auth_realm: "{{ admin_realm }}" |
| 229 |
auth_username: "{{ admin_user }}" |
| 230 |
auth_password: "{{ admin_password }}" |
| 231 |
realm: "{{ realm }}" |
| 232 |
client_id: "{{ client_name_public }}" |
| 233 |
role_names: |
| 234 |
- "{{ realm_role_admin }}" |
| 235 |
- "{{ realm_role_user }}" |
| 236 |
register: result |
| 237 |
|
| 238 |
- name: Assert result |
| 239 |
assert: |
| 240 |
that: |
| 241 |
- result is changed |
| 242 |
- result.end_state | length == 2 |
| 243 |
|
| 244 |
- name: Unmap all realm roles to public client |
| 245 |
community.general.keycloak_client_rolescope: |
| 246 |
auth_keycloak_url: "{{ url }}" |
| 247 |
auth_realm: "{{ admin_realm }}" |
| 248 |
auth_username: "{{ admin_user }}" |
| 249 |
auth_password: "{{ admin_password }}" |
| 250 |
realm: "{{ realm }}" |
| 251 |
client_id: "{{ client_name_public }}" |
| 252 |
role_names: |
| 253 |
- "{{ realm_role_admin }}" |
| 254 |
- "{{ realm_role_user }}" |
| 255 |
state: absent |
| 256 |
register: result |
| 257 |
|
| 258 |
- name: Assert result |
| 259 |
assert: |
| 260 |
that: |
| 261 |
- result is changed |
| 262 |
- result.end_state | length == 0 |
| 263 |
|
| 264 |
- name: Map missing realm role to public client |
| 265 |
community.general.keycloak_client_rolescope: |
| 266 |
auth_keycloak_url: "{{ url }}" |
| 267 |
auth_realm: "{{ admin_realm }}" |
| 268 |
auth_username: "{{ admin_user }}" |
| 269 |
auth_password: "{{ admin_password }}" |
| 270 |
realm: "{{ realm }}" |
| 271 |
client_id: "{{ client_name_public }}" |
| 272 |
role_names: |
| 273 |
- "{{ realm_role_not_exists }}" |
| 274 |
ignore_errors: true |
| 275 |
register: result |
| 276 |
|
| 277 |
- name: Assert failed mapping missing realm role |
| 278 |
assert: |
| 279 |
that: |
| 280 |
- result is failed |
| 281 |
|
| 282 |
- name: Check-mode try to Map realm roles to public client |
| 283 |
community.general.keycloak_client_rolescope: |
| 284 |
auth_keycloak_url: "{{ url }}" |
| 285 |
auth_realm: "{{ admin_realm }}" |
| 286 |
auth_username: "{{ admin_user }}" |
| 287 |
auth_password: "{{ admin_password }}" |
| 288 |
realm: "{{ realm }}" |
| 289 |
client_id: "{{ client_name_public }}" |
| 290 |
role_names: |
| 291 |
- "{{ realm_role_admin }}" |
| 292 |
- "{{ realm_role_user }}" |
| 293 |
check_mode: true |
| 294 |
register: result |
| 295 |
|
| 296 |
- name: Assert result |
| 297 |
assert: |
| 298 |
that: |
| 299 |
- result is changed |
| 300 |
- result.end_state | length == 2 |
| 301 |
|
| 302 |
- name: Check-mode step two, check if change where applied |
| 303 |
community.general.keycloak_client_rolescope: |
| 304 |
auth_keycloak_url: "{{ url }}" |
| 305 |
auth_realm: "{{ admin_realm }}" |
| 306 |
auth_username: "{{ admin_user }}" |
| 307 |
auth_password: "{{ admin_password }}" |
| 308 |
realm: "{{ realm }}" |
| 309 |
client_id: "{{ client_name_public }}" |
| 310 |
role_names: [] |
| 311 |
register: result |
| 312 |
|
| 313 |
- name: Assert result |
| 314 |
assert: |
| 315 |
that: |
| 316 |
- result is not changed |
| 317 |
- result.end_state | length == 0 |