Federating LDAP or Active Directory with Keycloak

LDAP
Federation means registering an existing LDAP directory or Active Directory as a user source for a Keycloak realm. Keycloak can then use the accounts already stored there. To set this up, you configure an LDAP provider in User Federation, enter the connection details, choose an edit mode and, if needed, enable user synchronisation.

For an IT department, federation addresses a common constraint: accounts already exist in a corporate directory, and recreating them elsewhere would be both costly and risky. Keycloak, an open source Identity and Access Management (IAM) solution, follows a simple principle: connect to that directory rather than replace it.

Identity federation without migrating accounts

In practice, Active Directory, Microsoft’s directory service, or an LDAP server such as OpenLDAP remains the authoritative source of identities. Keycloak queries it to authenticate users and give them access to connected applications.

There are two benefits. First, there is no risky cutover: accounts stay where they are, and the directory remains authoritative. Second, it becomes the foundation for a single sign-on (SSO) point. Once users are federated, security policies, multifactor authentication (MFA), federation with other applications and logging, can be configured in Keycloak on top of the directory, without deploying them separately for each service.

What Keycloak federates: LDAP and Active Directory

Keycloak integrates directories through its User Federation feature, which covers two main types:

  • Generic LDAP directories, such as OpenLDAP;
  • Active Directory, for which Keycloak offers settings tailored to Microsoft’s directory.

When creating the provider, selecting the directory vendor (Active Directory, Red Hat Directory Server, Novell eDirectory, Tivoli or another option) prepopulates the expected technical settings, such as the identifier attribute and object classes. This reduces the risk of errors. A single realm can have several providers and federate multiple directories in parallel.

Imports, synchronisation and edit modes: managing federated accounts

By default, when Import Users is enabled, Keycloak imports directory accounts and some of their attributes into its local database when users first sign in or during synchronisation. It does not import passwords from LDAP or Active Directory. As long as no password is set locally, the directory verifies them.

Three edit modes

determine how Keycloak handles changes:

READ_ONLY

Keycloak does not change mapped attributes in the directory and does not allow passwords to be changed there.

WRITABLE

Changes to mapped attributes and passwords made in Keycloak are written to the directory, provided the account used for the connection has the necessary permissions.

UNSYNCED

When users are imported, changes made in Keycloak, including a new password, are kept locally and are not automatically written back to the directory. The data can therefore diverge.

It is best to choose the mode when creating the provider: Keycloak configures its mappers accordingly. Changing the mode later does not automatically reconfigure mappers that have already been created.

Federating LDAP or Active Directory, step by step

The process is the same for self-hosted and managed Keycloak; only the way you access the administration console differs.

  1. 01

    01 Access the administration console

    Sign in with an administrator account for the relevant realm. On Clever Cloud’s managed Keycloak service, the console address and initial credentials are available in the service’s environment variables (CC_KEYCLOAK_ADMIN_URL, CC_KEYCLOAK_ADMIN, CC_KEYCLOAK_ADMIN_DEFAULT_PASSWORD).

  2. 02

    02 Add an LDAP provider

    In the User Federation menu, add an LDAP provider, then select the directory vendor. Choosing Active Directory or an LDAP directory presets the corresponding settings.

  3. 03

    03 Enter the connection details

    Provide the directory address (Connection URL) and the service account Keycloak uses to query it (Bind DN and password). In production, use LDAPS or StartTLS and check that Keycloak trusts the certificate presented by the directory. The Test connection and Test authentication buttons validate network access and credentials respectively.

  4. 04

    04 Define the users to federate

    Enter the directory branch (Users DN) and, if needed, an LDAP filter to limit which accounts are included.

  5. 05

    05 Choose the edit mode

    Set READ_ONLY, WRITABLE or UNSYNCED, depending on whether the directory must remain the only place where changes are written. This is a key decision.

  6. 06

    06 Configure synchronisation

    Leave imports enabled to copy accounts into Keycloak, then enable periodic synchronisation if needed, either in full and/or for modified users only.

  7. 07

    07 Import and verify

    Save the provider, run Synchronize all users for an initial full import, then test an end-to-end sign-in with a directory account.

The Keycloak documentation on Clever Cloud covers each screen and advanced cases, including directory federation, IP address filtering and clustered deployments. To connect your applications to this authentication point, see our article Configuring SSO with Keycloak.

Synchronisation and mappers: keeping identities up to date

Once the directory is federated, two mechanisms maintain consistency:

Synchronisation

Keycloak recommends a full import (Synchronize all users) during setup, followed by periodic synchronisation of users created or modified since the previous run. Full synchronisation processes all users in scope; only change synchronisation is limited to recently created or modified accounts.

Mappers

When the LDAP provider is created, Keycloak sets up mappings for common attributes such as the identifier, email address and name. Group or role mappers can also be configured to map directory memberships to those in Keycloak; not all groups and roles are imported automatically.

Active Directory: seamless authentication with Kerberos

On devices joined to an Active Directory domain, Keycloak can offer seamless sign-in through Kerberos/SPNEGO. This requires Kerberos configuration in Keycloak, activation of the method in the authentication flow and a browser authorised to send the ticket. If a user has a valid ticket, Keycloak can authenticate them without asking for their password again; otherwise, the usual sign-in form can remain available. This option complements LDAP federation, but connecting to Active Directory does not enable it automatically.

Fédérer un annuaire sur un Keycloak managé

The configuration described above does not change with managed Keycloak. What changes is the operational work around it. A server that federates a directory must remain available, up to date and backed up: a managed service takes care of these operations.

Keycloak as a Service, Clever Cloud’s offering operated with the expertise of Please Open It, deploys a managed Keycloak instance hosted in France. The service supports directory federation via LDAP (Active Directory and OpenLDAP), while the platform handles updates, monitoring and backups. High availability requires a multi-node cluster deployment. Teams remain responsible for the configuration that meets their needs: the LDAP provider, edit mode, mappers and synchronisation.

One consideration specific to federation is that Keycloak must be able to reach the directory over the network. If the directory is hosted within the company’s IT system, connectivity between the managed service and the directory must be arranged without exposing the directory publicly.

« Some directories have had several lives: multiple ‘structures’ may exist within groups or organisational units. To bring several user populations together, it is possible and recommended to set up multiple federations to the same directory, using filters to select each population and apply specific mappers. This allows you to establish a common user model in Keycloak. »

— Mathieu Passenaud, Co-founder at Please Open It

Evaluating an IAM solution? Our checklist, 11 questions to ask before choosing an identity management solution, brings together the criteria to check before making a commitment.

FAQ

Can Keycloak connect to Active Directory?

Yes. Through User Federation, Keycloak connects to Active Directory as it does to a generic LDAP directory such as OpenLDAP. It offers settings tailored to Microsoft’s directory and can add Kerberos authentication for domain-joined devices.

Does Keycloak import passwords from the directory?

No. Keycloak does not import passwords from LDAP or Active Directory. It normally verifies federated users’ passwords against the directory. In UNSYNCED mode, however, a new password set in Keycloak can be stored and used locally.

What is the difference between READ_ONLY, WRITABLE and UNSYNCED?

READ_ONLY treats the directory as read-only: Keycloak does not write to it. WRITABLE writes changes made in Keycloak back to the directory. UNSYNCED imports accounts but keeps changes in Keycloak without writing them back. The mode is chosen when the provider is created.

Do users need to be migrated to use Keycloak?

No. Federation connects Keycloak to the existing directory without moving accounts; the directory remains the authoritative source. Migration is a separate process.

Can a directory be federated with managed Keycloak?

Yes. Federation is configured in the same way. Clever Cloud’s Keycloak as a Service supports federation via LDAP (Active Directory and OpenLDAP), while the platform handles server operations, including updates, availability and backups. Keycloak must be able to reach the directory over the network.

Blog

À lire également

Federating LDAP or Active Directory with Keycloak

Federation means registering an existing LDAP directory or Active Directory as a user source for a Keycloak realm. Keycloak can then use the accounts already stored there. To set this up, you configure an LDAP provider in User Federation, enter the connection details, choose an edit mode and, if needed, enable user synchronisation.
Features

Scalability: definition, horizontal vs vertical scaling and how autoscaling works

When traffic grows, you are usually offered two fixes. A bigger machine. Or more machines. Both assume that your application can make use of them, and that is not a given.
Engineering

From the Console to Terraform: import your existing Clever Cloud infrastructure as code (IaC)

Nobody starts with Terraform. You create your first application in the Console, add a PostgreSQL database with Clever Tools, then a Redis instance, a Cellar bucket, and a “temporary” staging environment that is about to turn two. One morning, the organisation has forty resources, and nobody can say why the production application runs on two M instances instead of one, or who changed that variable on a Friday evening.
Engineering