| 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 |
|
| 6 |
- name: Reset public login in master admin-cli (if potentially previous test failed) |
| 7 |
community.general.keycloak_client: |
| 8 |
auth_keycloak_url: "{{ url }}" |
| 9 |
auth_realm: "{{ admin_realm }}" |
| 10 |
auth_username: "{{ admin_user }}" |
| 11 |
auth_password: "{{ admin_password }}" |
| 12 |
auth_client_id: "admin-cli" |
| 13 |
auth_client_secret: "{{ client_secret }}" |
| 14 |
client_id: "admin-cli" |
| 15 |
secret: "{{ client_secret }}" |
| 16 |
public_client: true |
| 17 |
state: present |
| 18 |
|
| 19 |
- name: Create realm |
| 20 |
community.general.keycloak_realm: |
| 21 |
auth_keycloak_url: "{{ url }}" |
| 22 |
auth_realm: "{{ admin_realm }}" |
| 23 |
auth_username: "{{ admin_user }}" |
| 24 |
auth_password: "{{ admin_password }}" |
| 25 |
id: "{{ realm }}" |
| 26 |
realm: "{{ realm }}" |
| 27 |
state: present |
| 28 |
|
| 29 |
- name: Create client |
| 30 |
community.general.keycloak_client: |
| 31 |
auth_keycloak_url: "{{ url }}" |
| 32 |
auth_realm: "{{ admin_realm }}" |
| 33 |
auth_username: "{{ admin_user }}" |
| 34 |
auth_password: "{{ admin_password }}" |
| 35 |
realm: "{{ realm }}" |
| 36 |
client_id: "{{ client_id }}" |
| 37 |
state: present |
| 38 |
register: client |
| 39 |
|
| 40 |
- name: Create new realm role with username/password authentication |
| 41 |
community.general.keycloak_role: |
| 42 |
auth_keycloak_url: "{{ url }}" |
| 43 |
auth_realm: "{{ admin_realm }}" |
| 44 |
auth_username: "{{ admin_user }}" |
| 45 |
auth_password: "{{ admin_password }}" |
| 46 |
realm: "{{ realm }}" |
| 47 |
name: "{{ role }}" |
| 48 |
description: "{{ keycloak_role_description }}" |
| 49 |
state: present |
| 50 |
register: result |
| 51 |
|
| 52 |
- name: Debug |
| 53 |
debug: |
| 54 |
var: result |
| 55 |
|
| 56 |
- name: Remove created realm role |
| 57 |
community.general.keycloak_role: |
| 58 |
auth_keycloak_url: "{{ url }}" |
| 59 |
auth_realm: "{{ admin_realm }}" |
| 60 |
auth_username: "{{ admin_user }}" |
| 61 |
auth_password: "{{ admin_password }}" |
| 62 |
realm: "{{ realm }}" |
| 63 |
name: "{{ role }}" |
| 64 |
state: absent |
| 65 |
register: result |
| 66 |
|
| 67 |
- name: Debug |
| 68 |
debug: |
| 69 |
var: result |
| 70 |
|
| 71 |
- name: Get Keycloak token |
| 72 |
ansible.builtin.uri: |
| 73 |
url: "{{ url }}/realms/{{ admin_realm }}/protocol/openid-connect/token" |
| 74 |
method: POST |
| 75 |
return_content: true |
| 76 |
status_code: 200 |
| 77 |
body_format: form-urlencoded |
| 78 |
body: |
| 79 |
grant_type: "password" |
| 80 |
client_id: "admin-cli" |
| 81 |
username: "{{ admin_user }}" |
| 82 |
password: "{{ admin_password }}" |
| 83 |
register: token_response |
| 84 |
|
| 85 |
- name: Extract tokens |
| 86 |
ansible.builtin.set_fact: |
| 87 |
access_token: "{{ token_response.json | json_query('access_token') }}" |
| 88 |
refresh_token: "{{ token_response.json | json_query('refresh_token') }}" |
| 89 |
|
| 90 |
- name: Create new realm role with provided token authentication |
| 91 |
community.general.keycloak_role: |
| 92 |
auth_keycloak_url: "{{ url }}" |
| 93 |
token: "{{ access_token }}" |
| 94 |
realm: "{{ realm }}" |
| 95 |
name: "{{ role }}" |
| 96 |
description: "{{ keycloak_role_description }}" |
| 97 |
state: present |
| 98 |
register: result |
| 99 |
|
| 100 |
- name: Debug |
| 101 |
debug: |
| 102 |
var: result |
| 103 |
|
| 104 |
- name: Remove created realm 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 |
name: "{{ role }}" |
| 112 |
state: absent |
| 113 |
register: result |
| 114 |
|
| 115 |
- name: Debug |
| 116 |
debug: |
| 117 |
var: result |
| 118 |
|
| 119 |
- name: Create new realm role with invalid auth token and valid refresh token |
| 120 |
community.general.keycloak_role: |
| 121 |
auth_keycloak_url: "{{ url }}" |
| 122 |
auth_realm: "{{ admin_realm }}" |
| 123 |
auth_username: "{{ admin_user }}" |
| 124 |
auth_password: "{{ admin_password }}" |
| 125 |
token: "invalidtoken!!!" |
| 126 |
refresh_token: "{{ refresh_token }}" |
| 127 |
realm: "{{ realm }}" |
| 128 |
name: "{{ role }}" |
| 129 |
description: "{{ keycloak_role_description }}" |
| 130 |
state: present |
| 131 |
register: result |
| 132 |
|
| 133 |
- name: Debug |
| 134 |
debug: |
| 135 |
var: result |
| 136 |
|
| 137 |
- name: Remove created realm role |
| 138 |
community.general.keycloak_role: |
| 139 |
auth_keycloak_url: "{{ url }}" |
| 140 |
auth_realm: "{{ admin_realm }}" |
| 141 |
auth_username: "{{ admin_user }}" |
| 142 |
auth_password: "{{ admin_password }}" |
| 143 |
realm: "{{ realm }}" |
| 144 |
name: "{{ role }}" |
| 145 |
state: absent |
| 146 |
register: result |
| 147 |
|
| 148 |
- name: Debug |
| 149 |
debug: |
| 150 |
var: result |
| 151 |
|
| 152 |
- name: Create new realm role with invalid auth token and valid username/password |
| 153 |
community.general.keycloak_role: |
| 154 |
auth_keycloak_url: "{{ url }}" |
| 155 |
auth_realm: "{{ admin_realm }}" |
| 156 |
auth_username: "{{ admin_user }}" |
| 157 |
auth_password: "{{ admin_password }}" |
| 158 |
token: "invalidtoken!!!" |
| 159 |
realm: "{{ realm }}" |
| 160 |
name: "{{ role }}" |
| 161 |
description: "{{ keycloak_role_description }}" |
| 162 |
state: present |
| 163 |
register: result |
| 164 |
|
| 165 |
- name: Debug |
| 166 |
debug: |
| 167 |
var: result |
| 168 |
|
| 169 |
- name: Remove created realm role |
| 170 |
community.general.keycloak_role: |
| 171 |
auth_keycloak_url: "{{ url }}" |
| 172 |
auth_realm: "{{ admin_realm }}" |
| 173 |
auth_username: "{{ admin_user }}" |
| 174 |
auth_password: "{{ admin_password }}" |
| 175 |
realm: "{{ realm }}" |
| 176 |
name: "{{ role }}" |
| 177 |
state: absent |
| 178 |
register: result |
| 179 |
|
| 180 |
- name: Debug |
| 181 |
debug: |
| 182 |
var: result |
| 183 |
|
| 184 |
- name: Create new realm role with invalid auth token, invalid refresh token, and valid username/password |
| 185 |
community.general.keycloak_role: |
| 186 |
auth_keycloak_url: "{{ url }}" |
| 187 |
auth_realm: "{{ admin_realm }}" |
| 188 |
auth_username: "{{ admin_user }}" |
| 189 |
auth_password: "{{ admin_password }}" |
| 190 |
token: "invalidtoken!!!" |
| 191 |
refresh_token: "invalidrefreshtoken!!!" |
| 192 |
realm: "{{ realm }}" |
| 193 |
name: "{{ role }}" |
| 194 |
description: "{{ keycloak_role_description }}" |
| 195 |
state: present |
| 196 |
register: result |
| 197 |
|
| 198 |
- name: Debug |
| 199 |
debug: |
| 200 |
var: result |
| 201 |
|
| 202 |
- name: Remove created realm role |
| 203 |
community.general.keycloak_role: |
| 204 |
auth_keycloak_url: "{{ url }}" |
| 205 |
auth_realm: "{{ admin_realm }}" |
| 206 |
auth_username: "{{ admin_user }}" |
| 207 |
auth_password: "{{ admin_password }}" |
| 208 |
realm: "{{ realm }}" |
| 209 |
name: "{{ role }}" |
| 210 |
state: absent |
| 211 |
register: result |
| 212 |
|
| 213 |
- name: Debug |
| 214 |
debug: |
| 215 |
var: result |
| 216 |
|
| 217 |
- name: PREPARE - Temporarily disable public login in master admin-cli |
| 218 |
community.general.keycloak_client: |
| 219 |
auth_keycloak_url: "{{ url }}" |
| 220 |
auth_realm: "{{ admin_realm }}" |
| 221 |
auth_username: "{{ admin_user }}" |
| 222 |
auth_password: "{{ admin_password }}" |
| 223 |
auth_client_id: "admin-cli" |
| 224 |
auth_client_secret: "{{ client_secret }}" |
| 225 |
client_id: "admin-cli" |
| 226 |
secret: "{{ client_secret }}" |
| 227 |
public_client: false |
| 228 |
service_accounts_enabled: true |
| 229 |
client_authenticator_type: "client-secret" |
| 230 |
state: present |
| 231 |
|
| 232 |
- name: PREPARE - Get admin role id |
| 233 |
community.general.keycloak_role: |
| 234 |
auth_keycloak_url: "{{ url }}" |
| 235 |
auth_realm: "{{ admin_realm }}" |
| 236 |
auth_username: "{{ admin_user }}" |
| 237 |
auth_password: "{{ admin_password }}" |
| 238 |
auth_client_id: "admin-cli" |
| 239 |
auth_client_secret: "{{ client_secret }}" |
| 240 |
name: "admin" |
| 241 |
register: admin_role |
| 242 |
|
| 243 |
- name: PREPARE - Assign admin role to admin-cli in master |
| 244 |
community.general.keycloak_user_rolemapping: |
| 245 |
auth_keycloak_url: "{{ url }}" |
| 246 |
auth_realm: "{{ admin_realm }}" |
| 247 |
auth_username: "{{ admin_user }}" |
| 248 |
auth_password: "{{ admin_password }}" |
| 249 |
auth_client_id: "admin-cli" |
| 250 |
auth_client_secret: "{{ client_secret }}" |
| 251 |
realm: "master" |
| 252 |
roles: |
| 253 |
- name: "admin" |
| 254 |
service_account_user_client_id: "admin-cli" |
| 255 |
|
| 256 |
- name: Create new realm role with valid client_id and client_secret |
| 257 |
community.general.keycloak_role: |
| 258 |
auth_keycloak_url: "{{ url }}" |
| 259 |
auth_realm: "{{ admin_realm }}" |
| 260 |
auth_client_id: "admin-cli" |
| 261 |
auth_client_secret: "{{ client_secret }}" |
| 262 |
realm: "{{ realm }}" |
| 263 |
name: "{{ role }}" |
| 264 |
description: "{{ keycloak_role_description }}" |
| 265 |
state: present |
| 266 |
register: result |
| 267 |
|
| 268 |
- name: Debug |
| 269 |
debug: |
| 270 |
var: result |
| 271 |
|
| 272 |
- name: Reset temporarily disabled public login in master admin-cli |
| 273 |
community.general.keycloak_client: |
| 274 |
auth_keycloak_url: "{{ url }}" |
| 275 |
auth_realm: "{{ admin_realm }}" |
| 276 |
auth_username: "{{ admin_user }}" |
| 277 |
auth_password: "{{ admin_password }}" |
| 278 |
auth_client_id: "admin-cli" |
| 279 |
auth_client_secret: "{{ client_secret }}" |
| 280 |
client_id: "admin-cli" |
| 281 |
secret: "{{ client_secret }}" |
| 282 |
public_client: true |
| 283 |
state: present |
| 284 |
|
| 285 |
- name: Remove created realm role |
| 286 |
community.general.keycloak_role: |
| 287 |
auth_keycloak_url: "{{ url }}" |
| 288 |
auth_realm: "{{ admin_realm }}" |
| 289 |
auth_username: "{{ admin_user }}" |
| 290 |
auth_password: "{{ admin_password }}" |
| 291 |
realm: "{{ realm }}" |
| 292 |
name: "{{ role }}" |
| 293 |
state: absent |
| 294 |
register: result |
| 295 |
|
| 296 |
- name: Debug |
| 297 |
debug: |
| 298 |
var: result |
| 299 |
|
| 300 |
### Unhappy path tests |
| 301 |
|
| 302 |
- name: Fail to create new realm role with invalid username/password |
| 303 |
community.general.keycloak_role: |
| 304 |
auth_keycloak_url: "{{ url }}" |
| 305 |
auth_realm: "{{ admin_realm }}" |
| 306 |
auth_username: "{{ admin_user }}" |
| 307 |
auth_password: "invalid_password" |
| 308 |
realm: "{{ realm }}" |
| 309 |
name: "{{ role }}" |
| 310 |
description: "{{ keycloak_role_description }}" |
| 311 |
state: present |
| 312 |
register: result |
| 313 |
failed_when: > |
| 314 |
("HTTP Error 401: Unauthorized" not in result.msg) |
| 315 |
|
| 316 |
- name: Fail to create new realm role with invalid auth token |
| 317 |
community.general.keycloak_role: |
| 318 |
auth_keycloak_url: "{{ url }}" |
| 319 |
token: "invalidtoken!!!" |
| 320 |
realm: "{{ realm }}" |
| 321 |
name: "{{ role }}" |
| 322 |
description: "{{ keycloak_role_description }}" |
| 323 |
state: present |
| 324 |
register: result |
| 325 |
failed_when: > |
| 326 |
("HTTP Error 401: Unauthorized" not in result.msg) |
| 327 |
|
| 328 |
- name: Fail to create new realm role with invalid auth and refresh tokens, and invalid username/password |
| 329 |
community.general.keycloak_role: |
| 330 |
auth_keycloak_url: "{{ url }}" |
| 331 |
auth_realm: "{{ admin_realm }}" |
| 332 |
auth_username: "{{ admin_user }}" |
| 333 |
auth_password: "invalid_password" |
| 334 |
token: "invalidtoken!!!" |
| 335 |
refresh_token: "invalidtoken!!!" |
| 336 |
realm: "{{ realm }}" |
| 337 |
name: "{{ role }}" |
| 338 |
description: "{{ keycloak_role_description }}" |
| 339 |
state: present |
| 340 |
register: result |
| 341 |
failed_when: > |
| 342 |
("HTTP Error 401: Unauthorized" not in result.msg) |