Repository navigation
Replies: 1 comment
|
In case it helps anyone with the same setup, this is the workaround we are using on 26.8.1. It is a Caveat: it extends internal classes (
package org.example.keycloak.kerberos;
import org.jboss.logging.Logger;
import org.keycloak.federation.kerberos.KerberosFederationProvider;
import org.keycloak.federation.kerberos.KerberosFederationProviderFactory;
import org.keycloak.federation.kerberos.KerberosPrincipal;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.storage.UserStoragePrivateUtil;
import org.keycloak.storage.UserStorageProviderModel;
/**
* Same as the built-in Kerberos provider, except for the username of the users it creates: always the
* full principal, lowercased ("john@a"), also for the provider's own realm. The built-in provider uses
* just "john" in that case, so two independent realms with the same account name collide on the
* unique (realm, username) index.
*/
public class PrincipalKerberosFederationProvider extends KerberosFederationProvider {
private static final Logger logger = Logger.getLogger(PrincipalKerberosFederationProvider.class);
public PrincipalKerberosFederationProvider(KeycloakSession session, UserStorageProviderModel model,
KerberosFederationProviderFactory factory) {
super(session, model, factory);
}
@Override
protected UserModel importUserToKeycloak(RealmModel realm, KerberosPrincipal kerberosPrincipal) {
String username = (kerberosPrincipal.getPrefix() + "@" + kerberosPrincipal.getRealm()).toLowerCase();
logger.debugf("Creating kerberos user %s with username: %s to local Keycloak storage", kerberosPrincipal, username);
UserModel user = UserStoragePrivateUtil.userLocalStorage(session).addUser(realm, username);
user.setEnabled(true);
user.setFederationLink(model.getId());
user.setSingleAttribute(KERBEROS_PRINCIPAL, kerberosPrincipal.toString());
if (kerberosConfig.isUpdateProfileFirstLogin()) {
user.addRequiredAction(UserModel.RequiredAction.UPDATE_PROFILE);
}
return validate(realm, user);
}
}
package org.example.keycloak.kerberos;
import org.keycloak.component.ComponentModel;
import org.keycloak.federation.kerberos.KerberosFederationProvider;
import org.keycloak.federation.kerberos.KerberosFederationProviderFactory;
import org.keycloak.models.KeycloakSession;
import org.keycloak.storage.UserStorageProviderModel;
/** Provider "kerberos-principal": same configuration as "kerberos", different username. */
public class PrincipalKerberosFederationProviderFactory extends KerberosFederationProviderFactory {
public static final String PROVIDER_ID = "kerberos-principal";
@Override
public String getId() {
return PROVIDER_ID;
}
@Override
public KerberosFederationProvider create(KeycloakSession session, ComponentModel model) {
return new PrincipalKerberosFederationProvider(session, new UserStorageProviderModel(model), this);
}
}
Compile-time dependencies (all |
Uh oh!
There was an error while loading. Please reload this page.
Description
A Kerberos user storage provider creates the Keycloak username from the Kerberos principal. When the
principal's realm matches the provider's configured Kerberos realm, the realm suffix is dropped
(
john@A→john); for other realms (cross-realm trust) the full principal is used (john@b), asdescribed in the Server Administration Guide (Kerberos → cross-realm trust).
With several independent Kerberos realms without trust (e.g. two AD forests of different
organizations), each realm is configured as its own Kerberos provider (its own server principal and
keytab, same SPN registered in each forest). SPNEGO works: Keycloak tries each provider and the one
whose keytab decrypts the ticket accepts it. But every provider drops its own realm, so
john@A(provider A) and
john@B(provider B) both map to usernamejohn. The second user fails withModelDuplicateException(unique index realm + username) and cannot log in.Lookup is already correct (by the
KERBEROS_PRINCIPALattribute, so users are never mixed up); onlythe username generated in
KerberosFederationProvider.importUserToKeycloakcollides.Value Proposition
One Keycloak can offer passwordless SSO (SPNEGO) to users of several AD forests that do not trust
each other, without LDAP access to each forest and without custom code. Each organization only
provides a service account with the SPN and a keytab.
Goals
Only for other realms(currentbehavior, default) /
Always. WithAlways, the username is the full principal lowercased(
john@a).Non-Goals
userPrincipalNameas username).Notes
Tested on 26.8.1 with two Windows Server 2022 forests and Edge.
Our current workaround is a custom provider packaged as a JAR and deployed to
providers/: a factorywith its own id (
kerberos-principal) whose provider extendsKerberosFederationProviderandoverrides only
importUserToKeycloakto always use the full principal as username. It works, but itdepends on internal classes (
KerberosFederationProvider,UserStoragePrivateUtil) and has to berebuilt and retested on every Keycloak upgrade. With the proposed option this would be plain
configuration of the built-in Kerberos provider, and the custom JAR would no longer be needed.
Related: #20045 (full principal for trusted realms), #22352 (fallback between Kerberos providers).
All reactions