Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fb keycloak client improvement #9644

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- keycloak_client - fix and improve existing tests. The module showed a diff without actual changes, solved by improving the ``normalise_cr()`` function (https://github.com/ansible-collections/community.general/pull/9644).
10 changes: 10 additions & 0 deletions plugins/modules/keycloak_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,19 @@ def normalise_cr(clientrep, remove_ids=False):
if remove_ids:
mapper.pop('id', None)

# Convert bool to string
if 'config' in mapper:
for key, value in mapper['config'].items():
if isinstance(value, bool):
mapper['config'][key] = str(value).lower()
Comment on lines +762 to +765
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be overthinking this, but you are modifying items of a dict that you are iterating over. I remember this causing problems in some versions of Python back in the day. But assuming that the tests cover this part of the code, it should be alright.

Also, an alternative way that would not incur in the non-existent problem 😆 (feel free to disregard) to implement this could be:

Suggested change
if 'config' in mapper:
for key, value in mapper['config'].items():
if isinstance(value, bool):
mapper['config'][key] = str(value).lower()
if 'config' in mapper:
bool_items = {k: str(v).lower() for k,v in mapper['config'].items() if isinstance(v, bool)}
mapper['config'].update(bool_items)

Copy link
Contributor Author

@amPrimeSign amPrimeSign Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think editing an existing value should be ok, but I'm not a Python expert. For me, both options are fine, please pick your preferred one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying dict values is fine, deleting or adding items would cause issues.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for chipping in @apollo13 !
@amPrimeSign so there you go, we're good the way it is now. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@russoz Thanks :)


# Set to a default value.
mapper['consentRequired'] = mapper.get('consentRequired', False)

if 'attributes' in clientrep:
for key, value in clientrep['attributes'].items():
if isinstance(value, bool):
clientrep['attributes'][key] = str(value).lower()
return clientrep


Expand Down
12 changes: 6 additions & 6 deletions tests/integration/targets/keycloak_client/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ protocol_mappers1:
"claim.name": "email"
"user.attribute": "email"
"jsonType.label": "String"
"id.token.claim": "true"
"access.token.claim": "true"
"userinfo.token.claim": "true"
"id.token.claim": true
"access.token.claim": true
"userinfo.token.claim": true

- name: 'email_verified'
protocol: 'openid-connect'
Expand All @@ -45,9 +45,9 @@ protocol_mappers1:
"claim.name": "email_verified"
"user.attribute": "emailVerified"
"jsonType.label": "boolean"
"id.token.claim": "true"
"access.token.claim": "true"
"userinfo.token.claim": "true"
"id.token.claim": true
"access.token.claim": true
"userinfo.token.claim": true

- name: 'family_name'
protocol: 'openid-connect'
Expand Down