8000 [CIMD] Realm-level cap on total CIMD-persisted clients by tnorimat · Pull Request #48222 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

[CIMD] Realm-level cap on total CIMD-persisted clients - #48222

Open
tnorimat wants to merge 1 commit into
keycloak:mainfrom
Hitachi:ISSUE-48116
Open

tnorimat wants to merge 1 commit into
keycloak:mainfrom
Hitachi:ISSUE-48116

Conversation

@tnorimat
Copy link
Copy Markdown
Contributor

closes #48116

@tnorimat
tnorimat requested a review from a team as a code owner April 18, 2026 12:39
@mposolda mposolda self-assigned this Jun 16, 2026
@antoniocasagrande-airia
Copy link
Copy Markdown

Outside contributor here; we run a custom CIMD provider on 26.6.4 and reached the same conclusion about where this belongs, so a data point in favour of the placement.

We first went looking for this in the client-registration policies, since a max-clients policy already exists there for RFC 7591. That cannot work for CIMD: AbstractPersistentClientIdMetadataDocumentProvider.createClientMetadata calls ClientManager.createClient(session, realm, clientRep) directly, so no ClientRegistrationPolicy ever runs on this path. The executor's FetchOperation.CREATE branch is the only reachable point, which is what this PR uses.

Two questions on the semantics:

1. getClientsCount() counts every client in the realm, not the CIMD-created subset. The risk this bounds is "how many clients can an unauthenticated party cause to exist", but the budget is shared with admin-provisioned clients. In a realm holding 900 ordinary clients, a max-clients of 1000 leaves a CIMD headroom of 100 that nobody configured and that shrinks whenever an admin adds an unrelated client — surfacing to end users as invalid_request on an authorization request. Counting clients that carry cimd.cache.expiry.time.in.sec would bound the thing being defended, and it is the same selector PR #49770 uses to identify CIMD clients.

2. The limit is a factory-global, so every realm on the server shares one number. ERR_CLIENTS_LIMIT_REACHED and the log message both say "per realm", which is true of the count but not of the limit. A deployment where one realm exposes CIMD and others do not cannot express that.

Would this be better on the executor's client-profile Configuration, next to cimd-allow-permitted-domains? That is per realm and per profile by construction, and a cap reads more like policy than like the operational knobs (min-cache-time, upper-limit-metadata-bytes) that share the factory-global scope today. We recently moved our own provider's settings onto the executor Configuration for exactly that reason and it was a clean fit.

@VinodAnandan
Copy link
Copy Markdown
Contributor

@tnorimat : Thanks for the PR! When you get a chance, could you please rebase/resolve conflicts?

@antoniocasagrande-airia
Copy link
Copy Markdown

Picking up my earlier comment — and I'd read your note on 48116 about the counting query
first, so this is about the configuration surface rather than the count.

You cited DCR as the precedent, and I think that's right. It also answers placement, the
other way: MaxClientsClientRegistrationPolicy is configured per-realm via
ProviderConfigProperty/ComponentModel, defaults to 200, and
DefaultClientRegistrationPolicies installs it into every new realm. So following DCR
for the counting arguably means following it for the surface too — as written this adds a
second, server-wide cap under the same max-clients name at 1000, alongside a per-realm
one already active at 200 everywhere.

The bit missing from both threads: createClientMetadata() calls
ClientManager.createClient() directly and never goes through
ClientRegistrationPolicyManager, so every anonymous registration policy is bypassed on
the CIMD path — including the max-clients cap that already exists — while it still
emits CLIENT_REGISTER. That reframes this PR: it isn't adding a cap where none was
wanted, it's restoring one the realm already has and CIMD silently skips.

One deployment note: a factory-global SPI option isn't realm state, so declarative
realm-config tooling can't manage it at all.

@antoniocasagrande-airia
Copy link
Copy Markdown

A follow-up to my comment above, because I think one part of it pointed the wrong way, based on what we ran into since.

I suggested the existing max-clients registration policy as the model. That policy counts every client in the realm. Where most clients come from elsewhere (service accounts, other integrations), a total-count limit can't tell registration growth apart from everything else: set it low and it starts refusing as the realm grows for unrelated reasons; set it high enough to clear that and it no longer limits the clients it was meant to. We hit exactly this with the registration policy and had to raise it by orders of magnitude, at which point it stopped bounding anything we cared about.

This PR has the same shape: checkClientsLimit() compares realm.getClientsCount(), the whole realm, against a server-wide default of 1000. Two consequences:

  • On upgrade, any realm that already holds more than 1000 clients of any kind refuses every new CIMD client until an operator raises the option. Subclasses of AbstractClientIdMetadataDocumentExecutor inherit the check as well.
  • One server-wide number has to clear the largest realm on the server, so it can't bound a small one.

A suggestion that leaves the policy to the operator: let them choose what the limit counts (the whole realm, as now, or only clients CIMD created) and set it per realm. A CIMD-only count needs no new marker: createClientMetadata already stamps cimd.cache.expiry.time.in.sec on every client it creates, and determineFetchOperation already treats that attribute as "managed by CIMD". On the cost you raised in #48116: like the check here, the count would only run when a new client is about to be created, not on every authorization request.

Prepared with AI assistance; the code paths above were checked against current main.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 06:53
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.

🟡 Changes recommended

Non-atomic quota checks and client persistence allow concurrent authorization requests to exceed the realm’s client cap.

2 open issues
What changed in this PR

This PR adds a client-count limit intended to curb uncontrolled CIMD client persistence. It checks the realm’s total client count before creating a new CIMD client, using a server-wide provider setting.

Changes:

  • Adds max-clients configuration with a default of 1,000.
  • Rejects new CIMD client creation when the realm reaches the limit.
  • Adds an integration test for rejection at capacity.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​client/​policies/​ClientIdMetadataDocumentTest.java Configures a test limit and verifies rejection at capacity.
services/​src/​main/​java/​org/​keycloak/​protocol/​oauth2/​cimd/​clientpolicy/​executor/​ClientIdMetadataDocumentExecutorFactoryProviderConfig.java Loads the limit and defines its default value.
services/​src/​main/​java/​org/​keycloak/​protocol/​oauth2/​cimd/​clientpolicy/​executor/​AbstractClientIdMetadataDocumentExecutorFactory.java Exposes metadata for the max-clients provider option.
services/​src/​main/​java/​org/​keycloak/​protocol/​oauth2/​cimd/​clientpolicy/​executor/​AbstractClientIdMetadataDocumentExecutor.java Checks the realm’s client count before CIMD client creation.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 5, 2026 08:49
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.

🟡 Changes recommended

Concurrent client creation can still exceed the realm’s client cap because quota checks cannot see other transactions’ uncommitted inserts, requiring cross-node serialization through transaction completion and a regression test.

1 open issue
2 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 6, 2026 04:55
Copilot AI left a comment
Copy link
Copy Markdown
8000 Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The client cap can still be exceeded under MySQL/MariaDB REPEATABLE READ because the separate-connection lock does not refresh the request’s existing transaction snapshot.

3 open issues
1 resolved since last review

Review effort: Balanced

closes keycloak#48116

Signed-off-by: Takashi Norim
8BD0
atsu <takashi.norimatsu.ws@hitachi.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 13:31
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.

🟢 Approval recommended

No active review issues remain for the configurable CIMD client-creation cap.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

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.

[CIMD] Realm-level cap on total CIMD-persisted clients

6 participants

0