Repository navigation
Description
Before reporting an issue
- I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
admin/api
Describe the bug
I cannot set the field "Kerberos principal attribute" in the "LDAP User Federation" section to an empty value using the REST API (POST /auth/admin/realms). It only works if I set it to an empty value using the UI.
When I use the REST API and set the field "krbPrincipalAttribute" to an empty string, then this empty string is used in the AD filter ([&(=john@KEYCLOAK.ORG)..)] which obviously does not work. In the database table "component_config" a row with name "krbPrincipalAttribute" and an empty "value" exists.
If I set the field "krbPrincipalAttribute" to null when using the REST API, then it is simply ignored.
If I use the UI and set the field to an empty value it works as expected. In this case the database table "component_config" does not contain a row with name "krbPrincipalAttribute".
It might have to do with this change:
57e51e9
Is there some other way I can get this setting to work using the REST API? Btw. I did not see the field "krbPrincipalAttribute" in the REST documentation, I infered it from the payload of the UI.
Version
24.0.1
Regression
- The issue is a regression
Expected behavior
Using the REST API, creating a realm with field "krbPrincipalAttribute" with an empty string should behave the same way as setting the field "Kerberos principal attribute" to an empty value in the UI.
Actual behavior
An empty string is used as if this was an attribute in the AD resulting in an invalid filter.
Log message: Could not query server using DN [OU=X,DC=Y,DC=Z] and filter [(&(=john@KEYCLOAK.ORG)...
How to Reproduce?
Create a realm using the REST API (POST /auth/admin/realms) and set the JSON field "/userFederationProviders/0/config/krbPrincipalAttribute" to an empty string. Compare the behavior when setting the field ""Kerberos principal attribute" to an empty value using the UI.
Anything else?
In version 22.0.4 this field did not exist in the UI and therefore it was not an issue for me.
Activity
vramik commentedon Mar 21, 2024
@mposolda could you please take a look? To me it sounds like a bug as there is different behavior when you call REST API directly and when done via UI. But I'm not sure what the expected behavior should be. Thanks
mposolda commentedon Mar 21, 2024
@hoepfnerj One thing to note: Admin console UI is just a "wrapper" over the REST API. Admin console always calls REST API under the covers. So if something works with admin console, but does not work for you with REST API, it can rather indicate that you are using REST API in somehow wrong way...
Could you try to check some browser plugin (For instance some developer console plugins for Chrome or FF) and check what REST requests are called by admin console? Then you can compare the difference between your REST API usage and maybe it can give some insight why the update work for you with the UI, but doesn't work with your REST API calls.
Any info here would be useful.
hoepfnerj commentedon Mar 22, 2024
@mposolda Thanks for the quick reply and explanation. I am currently using the realm endpoint
POST /admin/realmsto create a realm. The payload includes the JSON attributeuserFederationProvidersfor the user federation part. If I try to set the field "Kerberos principal attribute" to an empty value by setting the JSON attributeuserFederationProviders[0].config.krbPrincipalAttributeto an empty string the result is not the same as when I change it in the UI. The UI seems to use a different endpointPUT /admin/realms/<realm>/components/<user_federation_id>. So these two approaches do not seem to be compatible.rmartinc commentedon Mar 22, 2024
@hoepfnerj I think that the update realm endpoint does not manage
userFederationProvidersat all (the same way thanrolesorgroupsthat are on there for import/export). Does it even create a new provider if you add a new one?As @mposolda commented you better use the specific components endpoint as the admin console does.
hoepfnerj commentedon Mar 22, 2024
@rmartinc yes, I was always using this endpoint to create the
userFederationProvidersalong with the realms. TheuserFederationProvidersfield is part of the RealmRepresentation. It does create everything as expected, even thekrbPrincipalAttributewhen setting it to some non-empty value. However, the result differs between the mentioned API endpoints when setting the value to an empty string.I can use the other endpoint as a workaround, but the API does not seem to be consistent.
mposolda commentedon Mar 26, 2024
@hoepfnerj Thanks for the clarification. This have very low priority as the proper endpoint for updating user storage providers is
componentsendpoint mentioned by @rmartinc and that is also what admin console is using.We should perhaps clarify a docs that realm endpoint is not the best one for this use-case. PR might be welcome to fix this, but just in case that it doesn't have other side-effects (EG. requirement to create all
ComponentFactoryinstances for the callbacks etc).3 remaining items
keycloak-github-bot commentedon Mar 26, 2024
Due to the amount of issues reported by the community we are not able to prioritise resolving this issue at the moment.
If you are affected by this issue, upvote it by adding a 👍 to the description. We would also welcome a contribution to fix the issue.
hoepfnerj commentedon Mar 27, 2024
@mposolda I found another bug regarding this field when using the UI:
The only way to set this field to an empty value is to edit it again after creation, set to value to empty and save it again. So when creating a new LDAP user federation, it must first be saved (POST) and then edited again (PUT).
rmartinc commentedon Apr 1, 2024
@hoepfnerj I think that this configuration attribute has a default value in the onCreate method. The default value for AD is
userPrincipalName. That's why it's created with that value whennullis sent (but just on create, if you update later you can set to null as you have experienced).And my previous comment about the endpoint was for update. The create/import endpoint creates the ldap provider but the update does not update them. And I think the issue is the same for the endpoint. This specific attribute has a default value so null is always transformed into the default value on create (no matter how you create it).
We can think about removing the default assignment in the
onCreatemethod, but, AFAIK in AD theuserPrincipalNameis the correct attribute for the kerberos login. Why is that value wrong for you? Is there any error when you assign that value instead of leving the attribute empty?hoepfnerj commentedon Apr 2, 2024
@rmartinc The
krbPrincipalAttributeattribute was added to Keycloak after version 22 and changed the way how the user lookup in LDAP is done. The tooltip of the attribute explains the behavior when the attribute is set to an empty string, which was the default behavior in Keycloak <= 22:This commit seems to have introduced this change:
57e51e9
rmartinc commentedon Apr 2, 2024
@hoepfnerj Yes, the addition of the attribute for kerberos was added to fix an issue when the kerberos setup is more complicated (there are several issues, for example #20045). Now the solution is more robust.
As you mention the backwards compatibility was added to work with previous configurations (in which the attribute was not set for sure). As the use of the attribute is more robust it was decided to setup that attribute by default (waht you are complaining in this issue). But my question is still the same, why do you want to remove the default kerberos attribute for AD? Is it not working? Is there any issue if you maintain the default
userPrincipalName?As I commented we can remove the default value, but at that moment it was decided that adding the default value was better than using the old empty solution that generated some issues. And that's why we are asking.
hoepfnerj commentedon Apr 2, 2024
@rmartinc I just tested both settings with our AD. The LDAP operations are as follows:
The LDAP search is:
userPrincipalName=<username>@<kerberos-realm>which returns 0 results and then it aborts with the message: "Not found LDAP user with kerberos principal [<username>@<kerberos-realm>]. Kerberos principal attribute is [userPrincipalName]".The LDAP search is:
sAMAccountName=<username>which matches and then it continues.The second one is what I need, since the
userPrincipalNamein our case contains the email address, which does not contain the username nor the kerberos realm.rmartinc commentedon Apr 3, 2024
The error in (1) means that the search in LDAP for a user with filter
userPrincipalName=<username>@<kerberos-realm>returned nothing and it cannot link the principal to a user. What is theuserPricipalNamein your AD for that user? Is it different?hoepfnerj commentedon Apr 3, 2024
Yes in our case the
userPrincipalNamecontains the email address, something likefirstname.lastname@company.com. But Keycloak expects something likeusername@kerberos-realm.rmartinc commentedon Apr 12, 2024
I brought up this issue in the weekly meeting.
Little summary about the behavior. The ldap federation factory has an
onCreatemethod that initializes some defaults in the provider when it's created (default mappers and other things). Among those defaults it configures the kerberos attribute (if null is passed) based on the ldap vendor. That's why the fieldKerberos principal attributeis always filled touserPrincipalNamefor AD orkrbPrincipalNamefor RHDS if none is passed when creating it. The default value can be changed after creation. The issue here is that theuserPrincipalNameis not the default in AD. The field is modified for the user to other value different to the UPN. Therefore there is no attribute with the UPN that can be configured to look for users. That's why the config option is needed to be configured empty and search using the username (stripping the kerberos realm).We discussed about the following three alternatives:
legacy_usernamefor example).Option(3) is a horrible workaround so we just discussed options (1) and (2). It was decided that the default value continues, option (1), because almost all ldap/kerberos installations will work with that configuration. The attribute can still be updated after creation to remove it if necessary.
As usual we can reconsider this if a lot of people complain about the decision. Closing the issue for now as not planned.