8000 Reuse the pooled LDAP admin connection instead of re-binding per operation by R-Owino · Pull Request #50218 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Reuse the pooled LDAP admin connection instead of re-binding per operation - #50218

Merged
pedroigor merged 1 commit into
keycloak:mainfrom
R-Owino:fix/ldap-service-account-connection-reuse
Jun 23, 2026
Merged

pedroigor merged 1 commit into
keycloak:mainfrom
R-Owino:fix/ldap-service-account-connection-reuse

Conversation

@R-Owino
@R-Owino R-Owino commented Jun 22, 2026 •
Copy link
Copy Markdown

Closes #50201

Since 26.6.0 the LDAP user-federation admin/service-account connection is created anonymously and bound afterwards via addToEnvironment(). With JNDI connection pooling the pooled connection stays keyed as anonymous, so the service account is re-bound (preceded by an implicit anonymous bind) on every operation, flooding the LDAP server with BINDs.

For the non-StartTLS case, this restores putting the bind credentials in the initial connection environment so the bound connection can be pooled and reused, as before 26.6.0. The deferred bind is kept only for StartTLS, where the TLS negotiation must complete before the bind is sent. Full analysis and reproduction are in #50201.

Added LDAPContextManagerTest covering the admin connection environment for simple and anonymous auth.

10000
Copilot AI review requested due to automatic review settings June 22, 2026 12:15
@R-Owino
R-Owino requested a review from a team as a code owner June 22, 2026 12:15
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an LDAP user-federation regression where the pooled admin/service-account connection was effectively re-binding on each operation when JNDI connection pooling is enabled (non-StartTLS), by ensuring credentials are present in the initial JNDI environment so the pool can reuse an already-authenticated connection.

Changes:

  • Populate bind credentials in the initial LDAP context environment for non-StartTLS admin connections to allow proper JNDI pool reuse.
  • Keep deferred binding for StartTLS connections (bind only after TLS negotiation).
  • Add unit tests verifying anonymous vs authenticated environment construction for the admin connection setup.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
federation/ldap/src/main/java/org/keycloak/storage/ldap/idm/store/ldap/LDAPContextManager.java Restores pre-26.6 pooling behavior by binding in the initial env for non-StartTLS while preserving deferred bind for StartTLS.
federation/ldap/src/test/java/org/keycloak/storage/ldap/idm/store/ldap/LDAPContextManagerTest.java Adds tests to validate how initial JNDI env properties are built for anonymous vs simple-auth cases.

@R-Owino
R-Owino force-pushed the fix/ldap-service-account-connection-reuse branch from 12d6672 to b576ae4 Compare June 22, 2026 12:26
Copilot AI review requested due to automatic review settings June 22, 2026 14:31
@R-Owino
R-Owino force-pushed the fix/ldap-service-account-connection-reuse branch from b576ae4 to eed79a0 Compare June 22, 2026 14:31
< 8000 /span> Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment on lines +124 to +141
// Fill auth properties into the initial connection env so the bound connection can be pooled and reused.
static void setAuthConnectionProperties(Hashtable<Object, Object> connProp, LDAPConfig ldapConfig, String bindPassword) {
String authType = ldapConfig.getAuthType();
if (authType != null) {
connProp.put(Context.SECURITY_AUTHENTICATION, authType);
}

if (!LDAPConstants.AUTH_TYPE_NONE.equals(authType)) {
String bindDN = ldapConfig.getBindDN();
if (bindDN != null) {
connProp.put(Context.SECURITY_PRINCIPAL, bindDN);
}

if (bindPassword != null) {
connProp.put(SECURITY_CREDENTIALS, bindPassword);
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is a valid observation - worth adding similar debug logging after setAuthConnectionProperties() in the non-StartTLS path, or moving it to a shared spot.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

or moving it to a shared spot

I did exactly this: shared spot to avoid duplicating the masking logic. Extracted a small logConnectionProperties(Map<?, ?> env) helper and call it from both paths:

  • setAuthConnectionProperties() (non-StartTLS) logs the initial connection env, and
  • setAdminConnectionAuthProperties() (StartTLS) logs the context env after the bind properties are applied.

So the masked-credentials debug log now fires consistently in both flows, with the masking in a single place.

@R-Owino
R-Owino force-pushed the fix/ldap-service-account-connection-reuse branch from eed79a0 to 6138ac8 Compare June 23, 2026 07:31
@sguilhen 8000 sguilhen left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks a lot for the PR, @R-Owino. This is great work.

@sguilhen
Copy link
Copy Markdown
Contributor

@pedroigor @ahus1 Could either of you trigger the CI for this PR?

…ation

Closes keycloak#50201

Signed-off-by: Rehema Owino <rehema.owino@univention.de>
Copilot AI review requested due to automatic review settings June 23, 2026 10:51
@R-Owino
R-Owino force-pushed the fix/ldap-service-account-connection-reuse branch from 6138ac8 to c10e9f7 Compare June 23, 2026 10:51
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@pedroigor
pedroigor merged commit b9fb0f0 into keycloak:main Jun 23, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LDAP user federation re-binds the service account on every operation since 26.6.0 (connection pool not reused)

4 participants

0