| 1 |
# Copyright (c) 2021, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from contextlib import contextmanager |
| 8 |
from io import StringIO |
| 9 |
from itertools import count |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 13 |
AnsibleExitJson, |
| 14 |
ModuleTestCase, |
| 15 |
set_module_args, |
| 16 |
) |
| 17 |
|
| 18 |
from ansible_collections.community.general.plugins.modules import keycloak_user_federation |
| 19 |
|
| 20 |
|
| 21 |
@contextmanager |
| 22 |
def patch_keycloak_api( |
| 23 |
get_components=None, get_component=None, create_component=None, update_component=None, delete_component=None |
| 24 |
): |
| 25 |
"""Mock context manager for patching the methods in KeycloakAPI""" |
| 26 |
|
| 27 |
obj = keycloak_user_federation.KeycloakAPI |
| 28 |
with patch.object(obj, "get_components", side_effect=get_components) as mock_get_components: |
| 29 |
with patch.object(obj, "get_component", side_effect=get_component) as mock_get_component: |
| 30 |
with patch.object(obj, "create_component", side_effect=create_component) as mock_create_component: |
| 31 |
with patch.object(obj, "update_component", side_effect=update_component) as mock_update_component: |
| 32 |
with patch.object(obj, "delete_component", side_effect=delete_component) as mock_delete_component: |
| 33 |
yield ( |
| 34 |
mock_get_components, |
| 35 |
mock_get_component, |
| 36 |
mock_create_component, |
| 37 |
mock_update_component, |
| 38 |
mock_delete_component, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def get_response(object_with_future_response, method, get_id_call_count): |
| 43 |
if callable(object_with_future_response): |
| 44 |
return object_with_future_response() |
| 45 |
if isinstance(object_with_future_response, dict): |
| 46 |
return get_response(object_with_future_response[method], method, get_id_call_count) |
| 47 |
if isinstance(object_with_future_response, list): |
| 48 |
call_number = next(get_id_call_count) |
| 49 |
return get_response(object_with_future_response[call_number], method, get_id_call_count) |
| 50 |
return object_with_future_response |
| 51 |
|
| 52 |
|
| 53 |
def build_mocked_request(get_id_user_count, response_dict): |
| 54 |
def _mocked_requests(*args, **kwargs): |
| 55 |
url = args[0] |
| 56 |
method = kwargs["method"] |
| 57 |
future_response = response_dict.get(url, None) |
| 58 |
return get_response(future_response, method, get_id_user_count) |
| 59 |
|
| 60 |
return _mocked_requests |
| 61 |
|
| 62 |
|
| 63 |
def create_wrapper(text_as_string): |
| 64 |
"""Allow to mock many times a call to one address. |
| 65 |
Without this function, the StringIO is empty for the second call. |
| 66 |
""" |
| 67 |
|
| 68 |
def _create_wrapper(): |
| 69 |
return StringIO(text_as_string) |
| 70 |
|
| 71 |
return _create_wrapper |
| 72 |
|
| 73 |
|
| 74 |
def mock_good_connection(): |
| 75 |
token_response = { |
| 76 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper( |
| 77 |
'{"access_token": "alongtoken"}' |
| 78 |
), |
| 79 |
} |
| 80 |
return patch( |
| 81 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 82 |
side_effect=build_mocked_request(count(), token_response), |
| 83 |
autospec=True, |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
class TestKeycloakUserFederation(ModuleTestCase): |
| 88 |
def setUp(self): |
| 89 |
super().setUp() |
| 90 |
self.module = keycloak_user_federation |
| 91 |
|
| 92 |
def test_create_when_absent(self): |
| 93 |
"""Add a new user federation""" |
| 94 |
|
| 95 |
module_args = { |
| 96 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 97 |
"auth_realm": "master", |
| 98 |
"auth_username": "admin", |
| 99 |
"auth_password": "admin", |
| 100 |
"realm": "realm-name", |
| 101 |
"name": "kerberos", |
| 102 |
"state": "present", |
| 103 |
"provider_id": "kerberos", |
| 104 |
"provider_type": "org.keycloak.storage.UserStorageProvider", |
| 105 |
"config": { |
| 106 |
"priority": 0, |
| 107 |
"enabled": True, |
| 108 |
"cachePolicy": "DEFAULT", |
| 109 |
"kerberosRealm": "REALM", |
| 110 |
"serverPrincipal": "princ", |
| 111 |
"keyTab": "keytab", |
| 112 |
"allowPasswordAuthentication": False, |
| 113 |
"updateProfileFirstLogin": False, |
| 114 |
}, |
| 115 |
} |
| 116 |
return_value_component_create = [ |
| 117 |
{ |
| 118 |
"id": "ebb7d999-60cc-4dfe-ab79-48f7bbd9d4d9", |
| 119 |
"name": "kerberos", |
| 120 |
"providerId": "kerberos", |
| 121 |
"providerType": "org.keycloak.storage.UserStorageProvider", |
| 122 |
"parentId": "kerberos", |
| 123 |
"config": { |
| 124 |
"serverPrincipal": ["princ"], |
| 125 |
"allowPasswordAuthentication": ["false"], |
| 126 |
"keyTab": ["keytab"], |
| 127 |
"cachePolicy": ["DEFAULT"], |
| 128 |
"updateProfileFirstLogin": ["false"], |
| 129 |
"kerberosRealm": ["REALM"], |
| 130 |
"priority": ["0"], |
| 131 |
"enabled": ["true"], |
| 132 |
}, |
| 133 |
} |
| 134 |
] |
| 135 |
# get before_comp, get default_mapper, get after_mapper |
| 136 |
return_value_components_get = [[], [], []] |
| 137 |
changed = True |
| 138 |
|
| 139 |
# Run the module |
| 140 |
|
| 141 |
with set_module_args(module_args): |
| 142 |
with mock_good_connection(): |
| 143 |
with patch_keycloak_api( |
| 144 |
get_components=return_value_components_get, create_component=return_value_component_create |
| 145 |
) as ( |
| 146 |
mock_get_components, |
| 147 |
mock_get_component, |
| 148 |
mock_create_component, |
| 149 |
mock_update_component, |
| 150 |
mock_delete_component, |
| 151 |
): |
| 152 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 153 |
self.module.main() |
| 154 |
|
| 155 |
self.assertEqual(len(mock_get_components.mock_calls), 3) |
| 156 |
self.assertEqual(len(mock_get_component.mock_calls), 0) |
| 157 |
self.assertEqual(len(mock_create_component.mock_calls), 1) |
| 158 |
self.assertEqual(len(mock_update_component.mock_calls), 0) |
| 159 |
self.assertEqual(len(mock_delete_component.mock_calls), 0) |
| 160 |
|
| 161 |
# Verify that the module's changed status matches what is expected |
| 162 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 163 |
|
| 164 |
def test_create_when_present(self): |
| 165 |
"""Update existing user federation""" |
| 166 |
|
| 167 |
module_args = { |
| 168 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 169 |
"auth_realm": "master", |
| 170 |
"auth_username": "admin", |
| 171 |
"auth_password": "admin", |
| 172 |
"realm": "realm-name", |
| 173 |
"name": "kerberos", |
| 174 |
"state": "present", |
| 175 |
"provider_id": "kerberos", |
| 176 |
"provider_type": "org.keycloak.storage.UserStorageProvider", |
| 177 |
"config": { |
| 178 |
"priority": 0, |
| 179 |
"enabled": True, |
| 180 |
"cachePolicy": "DEFAULT", |
| 181 |
"kerberosRealm": "REALM", |
| 182 |
"serverPrincipal": "princ", |
| 183 |
"keyTab": "keytab", |
| 184 |
"allowPasswordAuthentication": False, |
| 185 |
"updateProfileFirstLogin": False, |
| 186 |
}, |
| 187 |
} |
| 188 |
return_value_components_get = [ |
| 189 |
[ |
| 190 |
{ |
| 191 |
"id": "ebb7d999-60cc-4dfe-ab79-48f7bbd9d4d9", |
| 192 |
"name": "kerberos", |
| 193 |
"providerId": "kerberos", |
| 194 |
"providerType": "org.keycloak.storage.UserStorageProvider", |
| 195 |
"parentId": "kerberos", |
| 196 |
"config": { |
| 197 |
"serverPrincipal": ["princ"], |
| 198 |
"allowPasswordAuthentication": ["false"], |
| 199 |
"keyTab": ["keytab"], |
| 200 |
"cachePolicy": ["DEFAULT"], |
| 201 |
"updateProfileFirstLogin": ["false"], |
| 202 |
"kerberosRealm": ["REALM"], |
| 203 |
"priority": ["0"], |
| 204 |
"enabled": ["false"], |
| 205 |
}, |
| 206 |
} |
| 207 |
], |
| 208 |
[], |
| 209 |
[], |
| 210 |
] |
| 211 |
return_value_component_get = [ |
| 212 |
{ |
| 213 |
"id": "ebb7d999-60cc-4dfe-ab79-48f7bbd9d4d9", |
| 214 |
"name": "kerberos", |
| 215 |
"providerId": "kerberos", |
| 216 |
"providerType": "org.keycloak.storage.UserStorageProvider", |
| 217 |
"parentId": "kerberos", |
| 218 |
"config": { |
| 219 |
"serverPrincipal": ["princ"], |
| 220 |
"allowPasswordAuthentication": ["false"], |
| 221 |
"keyTab": ["keytab"], |
| 222 |
"cachePolicy": ["DEFAULT"], |
| 223 |
"updateProfileFirstLogin": ["false"], |
| 224 |
"kerberosRealm": ["REALM"], |
| 225 |
"priority": ["0"], |
| 226 |
"enabled": ["true"], |
| 227 |
}, |
| 228 |
} |
| 229 |
] |
| 230 |
return_value_component_update = [None] |
| 231 |
changed = True |
| 232 |
|
| 233 |
# Run the module |
| 234 |
|
| 235 |
with set_module_args(module_args): |
| 236 |
with mock_good_connection(): |
| 237 |
with patch_keycloak_api( |
| 238 |
get_components=return_value_components_get, |
| 239 |
get_component=return_value_component_get, |
| 240 |
update_component=return_value_component_update, |
| 241 |
) as ( |
| 242 |
mock_get_components, |
| 243 |
mock_get_component, |
| 244 |
mock_create_component, |
| 245 |
mock_update_component, |
| 246 |
mock_delete_component, |
| 247 |
): |
| 248 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 249 |
self.module.main() |
| 250 |
|
| 251 |
self.assertEqual(len(mock_get_components.mock_calls), 3) |
| 252 |
self.assertEqual(len(mock_get_component.mock_calls), 1) |
| 253 |
self.assertEqual(len(mock_create_component.mock_calls), 0) |
| 254 |
self.assertEqual(len(mock_update_component.mock_calls), 1) |
| 255 |
self.assertEqual(len(mock_delete_component.mock_calls), 0) |
| 256 |
|
| 257 |
# Verify that the module's changed status matches what is expected |
| 258 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 259 |
|
| 260 |
def test_create_with_mappers(self): |
| 261 |
"""Add a new user federation with mappers""" |
| 262 |
|
| 263 |
module_args = { |
| 264 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 265 |
"auth_realm": "master", |
| 266 |
"auth_username": "admin", |
| 267 |
"auth_password": "admin", |
| 268 |
"realm": "realm-name", |
| 269 |
"name": "ldap", |
| 270 |
"state": "present", |
| 271 |
"provider_id": "ldap", |
| 272 |
"provider_type": "org.keycloak.storage.UserStorageProvider", |
| 273 |
"config": { |
| 274 |
"priority": 0, |
| 275 |
"enabled": True, |
| 276 |
"cachePolicy": "DEFAULT", |
| 277 |
"batchSizeForSync": 1000, |
| 278 |
"editMode": "READ_ONLY", |
| 279 |
"importEnabled": True, |
| 280 |
"syncRegistrations": False, |
| 281 |
"vendor": "other", |
| 282 |
"usernameLDAPAttribute": "uid", |
| 283 |
"rdnLDAPAttribute": "uid", |
| 284 |
"uuidLDAPAttribute": "entryUUID", |
| 285 |
"userObjectClasses": "inetOrgPerson, organizationalPerson", |
| 286 |
"connectionUrl": "ldaps://ldap.example.com:636", |
| 287 |
"usersDn": "ou=Users,dc=example,dc=com", |
| 288 |
"authType": "none", |
| 289 |
"searchScope": 1, |
| 290 |
"validatePasswordPolicy": False, |
| 291 |
"trustEmail": False, |
| 292 |
"useTruststoreSpi": "ldapsOnly", |
| 293 |
"connectionPooling": True, |
| 294 |
"pagination": True, |
| 295 |
"allowKerberosAuthentication": False, |
| 296 |
"krbPrincipalAttribute": "krbPrincipalName", |
| 297 |
"debug": False, |
| 298 |
"useKerberosForPasswordAuthentication": False, |
| 299 |
}, |
| 300 |
"mappers": [ |
| 301 |
{ |
| 302 |
"name": "full name", |
| 303 |
"providerId": "full-name-ldap-mapper", |
| 304 |
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper", |
| 305 |
"config": { |
| 306 |
"ldap.full.name.attribute": "cn", |
| 307 |
"read.only": True, |
| 308 |
"write.only": False, |
| 309 |
}, |
| 310 |
} |
| 311 |
], |
| 312 |
} |
| 313 |
return_value_components_get = [ |
| 314 |
[], |
| 315 |
# exemplary default mapper created by keylocak |
| 316 |
[ |
| 317 |
{ |
| 318 |
"config": { |
| 319 |
"always.read.value.from.ldap": "false", |
| 320 |
"is.mandatory.in.ldap": "false", |
| 321 |
"ldap.attribute": "mail", |
| 322 |
"read.only": "true", |
| 323 |
"user.model.attribute": "email", |
| 324 |
}, |
| 325 |
"id": "77e1763f-c51a-4286-bade-75577d64803c", |
| 326 |
"name": "email", |
| 327 |
"parentId": "e5f48aa3-b56b-4983-a8ad-2c7b8b5e77cb", |
| 328 |
"providerId": "user-attribute-ldap-mapper", |
| 329 |
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper", |
| 330 |
}, |
| 331 |
], |
| 332 |
[ |
| 333 |
{ |
| 334 |
"id": "2dfadafd-8b34-495f-a98b-153e71a22311", |
| 335 |
"name": "full name", |
| 336 |
"providerId": "full-name-ldap-mapper", |
| 337 |
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper", |
| 338 |
"parentId": "eb691537-b73c-4cd8-b481-6031c26499d8", |
| 339 |
"config": {"ldap.full.name.attribute": ["cn"], "read.only": ["true"], "write.only": ["false"]}, |
| 340 |
} |
| 341 |
], |
| 342 |
] |
| 343 |
return_value_component_create = [ |
| 344 |
{ |
| 345 |
"id": "eb691537-b73c-4cd8-b481-6031c26499d8", |
| 346 |
"name": "ldap", |
| 347 |
"providerId": "ldap", |
| 348 |
"providerType": "org.keycloak.storage.UserStorageProvider", |
| 349 |
"parentId": "ldap", |
| 350 |
"config": { |
| 351 |
"pagination": ["true"], |
| 352 |
"connectionPooling": ["true"], |
| 353 |
"usersDn": ["ou=Users,dc=example,dc=com"], |
| 354 |
"cachePolicy": ["DEFAULT"], |
| 355 |
"useKerberosForPasswordAuthentication": ["false"], |
| 356 |
"importEnabled": ["true"], |
| 357 |
"enabled": ["true"], |
| 358 |
"krbPrincipalAttribute": ["krb5PrincipalName"], |
| 359 |
"usernameLDAPAttribute": ["uid"], |
| 360 |
"vendor": ["other"], |
| 361 |
"uuidLDAPAttribute": ["entryUUID"], |
| 362 |
"connectionUrl": ["ldaps://ldap.example.com:636"], |
| 363 |
"allowKerberosAuthentication": ["false"], |
| 364 |
"syncRegistrations": ["false"], |
| 365 |
"authType": ["none"], |
| 366 |
"debug": ["false"], |
| 367 |
"searchScope": ["1"], |
| 368 |
"useTruststoreSpi": ["ldapsOnly"], |
| 369 |
"trustEmail": ["false"], |
| 370 |
"priority": ["0"], |
| 371 |
"userObjectClasses": ["inetOrgPerson, organizationalPerson"], |
| 372 |
"rdnLDAPAttribute": ["uid"], |
| 373 |
"editMode": ["READ_ONLY"], |
| 374 |
"validatePasswordPolicy": ["false"], |
| 375 |
"batchSizeForSync": ["1000"], |
| 376 |
}, |
| 377 |
}, |
| 378 |
{ |
| 379 |
"id": "2dfadafd-8b34-495f-a98b-153e71a22311", |
| 380 |
"name": "full name", |
| 381 |
"providerId": "full-name-ldap-mapper", |
| 382 |
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper", |
| 383 |
"parentId": "eb691537-b73c-4cd8-b481-6031c26499d8", |
| 384 |
"config": {"ldap.full.name.attribute": ["cn"], "read.only": ["true"], "write.only": ["false"]}, |
| 385 |
}, |
| 386 |
] |
| 387 |
changed = True |
| 388 |
|
| 389 |
# Run the module |
| 390 |
|
| 391 |
with set_module_args(module_args): |
| 392 |
with mock_good_connection(): |
| 393 |
with patch_keycloak_api( |
| 394 |
get_components=return_value_components_get, create_component=return_value_component_create |
| 395 |
) as ( |
| 396 |
mock_get_components, |
| 397 |
mock_get_component, |
| 398 |
mock_create_component, |
| 399 |
mock_update_component, |
| 400 |
mock_delete_component, |
| 401 |
): |
| 402 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 403 |
self.module.main() |
| 404 |
|
| 405 |
self.assertEqual(len(mock_get_components.mock_calls), 3) |
| 406 |
self.assertEqual(len(mock_get_component.mock_calls), 0) |
| 407 |
self.assertEqual(len(mock_create_component.mock_calls), 2) |
| 408 |
self.assertEqual(len(mock_update_component.mock_calls), 0) |
| 409 |
self.assertEqual(len(mock_delete_component.mock_calls), 1) |
| 410 |
|
| 411 |
# Verify that the module's changed status matches what is expected |
| 412 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 413 |
|
| 414 |
def test_delete_when_absent(self): |
| 415 |
"""Remove an absent user federation""" |
| 416 |
|
| 417 |
module_args = { |
| 418 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 419 |
"auth_realm": "master", |
| 420 |
"auth_username": "admin", |
| 421 |
"auth_password": "admin", |
| 422 |
"realm": "realm-name", |
| 423 |
"name": "kerberos", |
| 424 |
"state": "absent", |
| 425 |
} |
| 426 |
return_value_components_get = [[]] |
| 427 |
changed = False |
| 428 |
|
| 429 |
# Run the module |
| 430 |
|
| 431 |
with set_module_args(module_args): |
| 432 |
with mock_good_connection(): |
| 433 |
with patch_keycloak_api(get_components=return_value_components_get) as ( |
| 434 |
mock_get_components, |
| 435 |
mock_get_component, |
| 436 |
mock_create_component, |
| 437 |
mock_update_component, |
| 438 |
mock_delete_component, |
| 439 |
): |
| 440 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 441 |
self.module.main() |
| 442 |
|
| 443 |
self.assertEqual(len(mock_get_components.mock_calls), 1) |
| 444 |
self.assertEqual(len(mock_get_component.mock_calls), 0) |
| 445 |
self.assertEqual(len(mock_create_component.mock_calls), 0) |
| 446 |
self.assertEqual(len(mock_update_component.mock_calls), 0) |
| 447 |
self.assertEqual(len(mock_delete_component.mock_calls), 0) |
| 448 |
|
| 449 |
# Verify that the module's changed status matches what is expected |
| 450 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 451 |
|
| 452 |
def test_delete_when_present(self): |
| 453 |
"""Remove an existing user federation""" |
| 454 |
|
| 455 |
module_args = { |
| 456 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 457 |
"auth_realm": "master", |
| 458 |
"auth_username": "admin", |
| 459 |
"auth_password": "admin", |
| 460 |
"realm": "realm-name", |
| 461 |
"name": "kerberos", |
| 462 |
"state": "absent", |
| 463 |
} |
| 464 |
return_value_components_get = [ |
| 465 |
[ |
| 466 |
{ |
| 467 |
"id": "ebb7d999-60cc-4dfe-ab79-48f7bbd9d4d9", |
| 468 |
"name": "kerberos", |
| 469 |
"providerId": "kerberos", |
| 470 |
"providerType": "org.keycloak.storage.UserStorageProvider", |
| 471 |
"parentId": "kerberos", |
| 472 |
"config": { |
| 473 |
"serverPrincipal": ["princ"], |
| 474 |
"allowPasswordAuthentication": ["false"], |
| 475 |
"keyTab": ["keytab"], |
| 476 |
"cachePolicy": ["DEFAULT"], |
| 477 |
"updateProfileFirstLogin": ["false"], |
| 478 |
"kerberosRealm": ["REALM"], |
| 479 |
"priority": ["0"], |
| 480 |
"enabled": ["false"], |
| 481 |
}, |
| 482 |
} |
| 483 |
], |
| 484 |
[], |
| 485 |
] |
| 486 |
return_value_component_delete = [None] |
| 487 |
changed = True |
| 488 |
|
| 489 |
# Run the module |
| 490 |
|
| 491 |
with set_module_args(module_args): |
| 492 |
with mock_good_connection(): |
| 493 |
with patch_keycloak_api( |
| 494 |
get_components=return_value_components_get, delete_component=return_value_component_delete |
| 495 |
) as ( |
| 496 |
mock_get_components, |
| 497 |
mock_get_component, |
| 498 |
mock_create_component, |
| 499 |
mock_update_component, |
| 500 |
mock_delete_component, |
| 501 |
): |
| 502 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 503 |
self.module.main() |
| 504 |
|
| 505 |
self.assertEqual(len(mock_get_components.mock_calls), 2) |
| 506 |
self.assertEqual(len(mock_get_component.mock_calls), 0) |
| 507 |
self.assertEqual(len(mock_create_component.mock_calls), 0) |
| 508 |
self.assertEqual(len(mock_update_component.mock_calls), 0) |
| 509 |
self.assertEqual(len(mock_delete_component.mock_calls), 1) |
| 510 |
|
| 511 |
# Verify that the module's changed status matches what is expected |
| 512 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |