8000 Cleanup orphaned broker links when duplicate federated identities found by Rathan-Naik · Pull Request #45124 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Cleanup orphaned broker links when duplicate federated identities found - #45124

Open
Rathan-Naik wants to merge 1 commit into
keycloak:mainfrom
Rathan-Naik:feature/cleanup-orphaned-broker-links
Open

Rathan-Naik wants to merge 1 commit into
keycloak:mainfrom
Rathan-Naik:feature/cleanup-orphaned-broker-links

Conversation

@Rathan-Naik
Copy link
Copy Markdown
Contributor

When a federated user (e.g., from LDAP) is deleted from the external source, the broker link record in Keycloak is not cleaned up. If a new user with the same IdP mapping logs in later, a second broker link is created. On subsequent logins, the query returns multiple results and throws IllegalStateException.

This fix:

In both JpaUserFederatedStorageProvider and JpaUserProvider, when multiple results are found:

  • Iterate through each result
  • Check if the user still exists using session.users().getUserById(realm, userId)
  • If the user no longer exists (returns null), remove the orphaned broker link
  • If multiple valid users exist, throw the IllegalStateException (this is a real data integrity issue)
  • Return the single valid user

This approach cleans up stale data automatically while preserving the error for genuine duplicate scenarios.

Closes #44933

@Rathan-Naik
Rathan-Naik marked this pull request as ready for review January 4, 2026 20:04
@Rathan-Naik
Rathan-Naik requested a review from a team as a code owner January 4, 2026 20:04
@vramik vramik 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.

@Rathan-Naik thank you for preparing the PR. LGTM.

@pedroigor since you're more familiar with the context, can you please also take a look?

@Rathan-Naik
Rathan-Naik force-pushed the feature/cleanup-orphaned-broker-links branch from 7674b9f to cc50c0f Compare January 8, 2026 04:04
When a federated user (e.g., from LDAP) is deleted from the external source,
the broker link record in Keycloak is not cleaned up. If a new user with the
same IdP mapping logs in later, a second broker link is created. On subsequent
logins, the query returns multiple results and throws IllegalStateException.

This fix:
- In JpaUserFederatedStorageProvider: resolves multiple BrokerLinkEntity records
  by checking if each user still exists, removing orphaned links
- In JpaUserProvider: resolves multiple FederatedIdentityEntity records
  by checking if federated users still exist, removing orphaned identities

Closes keycloak#44933

Signed-off-by: Rathan Naik <30756840+Rathan-Naik@users.noreply.github.com>
@Rathan-Naik
Rathan-Naik force-pushed the feature/cleanup-orphaned-broker-links branch from 4dd7c40 to 4c1244c Compare January 15, 2026 06:40
@vramik
vramik requested a review from pedroigor January 15, 2026 08:34
@ahus1
ahus1 commented Jan 15, 2026
Copy link
Copy Markdown
Member

@Rathan-Naik - Please avoid merging the main branch into the PR or rebasing the PR unless there are merge conflicts reported by GitHub or a reviewer asks you for a rebase. This prevents spamming reviewers with notifications and saves minutes on the GitHub actions CI. Thanks!

@Rathan-Naik
Copy link
Copy Markdown
Contributor Author

@Rathan-Naik - Please avoid merging the main branch into the PR or rebasing the PR unless there are merge conflicts reported by GitHub or a reviewer asks you for a rebase. This prevents spamming reviewers with notifications and saves minutes on the GitHub actions CI. Thanks!

Noted.
Thank You

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.

Broker links are not cleaned up for deleted users

4 participants

0