8000 [CIMD] Orphan cleanup mechanism by tnorimat · Pull Request #49770 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

[CIMD] Orphan cleanup mechanism - #49770

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

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

Conversation

@tnorimat
@tnorimat tnorimat commented Jun 8, 2026
Copy link
Copy Markdown
Contributor

closes #48390

closes keycloak#48390

Signed-off-by: Takashi Norimatsu <takashi.norimatsu.ws@hitachi.com>
@tnorimat
tnorimat commented Jun 9, 2026
Copy link
Copy Markdown
Contributor Author

Example workflow definition to delete orphaned CIMD clients that have been inactive for 30 days:

{
  "name": "cimd-orphan-cleanup",
  "schedule": { "after": "12h" },
  "if": "cimd-orphan-client(2592000)",
  "steps": [{ "uses": "delete-client" }]
}

@tnorimat
tnorimat marked this pull request as ready for review June 11, 2026 02:31
@tnorimat
tnorimat requested a review from a team as a code owner June 11, 2026 02:31
@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 had this exact gap open on our side, so this is very welcome. Two things on toPredicate, both about cb.lessThan against ClientAttributeEntity.value.

1. On Oracle this compares a CLOB with a VARCHAR.

JpaRealmProvider.searchClientsByAttributes, in this same module, special-cases the same column just to do equality:

if (dbProductName.equals("Oracle")) {
    // Oracle is not able to compare a CLOB with a VARCHAR unless it being converted with TO_CHAR
    // ... This leaves DBMS_LOB.COMPARE as the option to compare the CLOB with the value.
    Predicate attrValuePredicate = builder.equal(
        builder.function("DBMS_LOB.COMPARE", Integer.class, attributeJoin.get("value"), builder.literal(value)), 0);

cb.lessThan(attrRoot.get("value"), maxExpiryTimeStr) has the same exposure with no equivalent handling, so activateForAllEligibleResources looks likely to fail on Oracle. DBMS_LOB.COMPARE(value, :bound) < 0 would be the ordering equivalent. (PostgreSQL gets a substr prefix predicate there too, for a different reason.)

2. The comparison is lexicographic, and correct only while every value has the same digit count.

VALUE is a String column, so maxExpiryTimeStr is compared as text. That matches numeric order for 10-digit epoch seconds, which holds until 2286 — so this is latent rather than broken today, but nothing states the assumption and the bound is computed rather than stored.

The reachable case is a negative bound: validate() accepts any non-negative int, so a threshold above the current epoch (~1.79e9, comfortably inside Integer.MAX_VALUE) makes maxExpiryTime negative; "-…" sorts below every digit string, so the scan matches nothing and the workflow silently never activates. evaluate() does the same comparison numerically and is unaffected, so the two paths would disagree rather than both fail. A guard in validate() (threshold must not exceed the current time) or comparing as a number would close it.

A test with one client exactly at the boundary and one just inside it would pin the ordering; the per-database part is harder to cover, but at minimum worth a comment pointing at the precedent above.

Two notes outside the predicate:

Also worth saying: using Time.currentTime() in both evaluate and toPredicate is the right call. Scheduling elsewhere in the engine mixes Instant.now() with Time, which is what the first commit of #51629 fixes.

@antoniocasagrande-airia
antoniocasagrande-airia commented Sep 22, 2026 •
Copy link
Copy Markdown

Following up — first, a correction to my earlier comment: the lexicographic comparison
and the Oracle CLOB path are fine in practice for 10-digit epoch seconds, since string
and numeric ordering coincide there and the values stay well under the long-value
threshold. Robustness notes, not defects. Sorry for the noise.

The one I'd still raise is the predicate's input. cimd.cache.expiry.time.in.sec is
refreshed only from PRE_AUTHORIZATION_REQUEST, so a CIMD client that stays in use via
refresh-token or client_credentials traffic never refreshes it. It ages out on the
metadata-refresh clock rather than on use, and is disabled and deleted while live.
Companion issue 47981 documents the same gap from the other side, and the originating
issue asked for clients "not been used to issue tokens in N days" — last use, not last
metadata refresh.

ahus1 sketched the intended shape on 47982: "the workflows have a timer. The timer is
then reset once another event appears for that workflow. So we might be good without
adding another column."
An activity event resetting the timer matches that and sidesteps
the refresh-clock problem.

Smaller: CimdOrphanClientWorkflowConditionFactory doesn't override isSupported, so it
inherits the WORKFLOWS feature check and is exposed on servers with CIMD disabled. The
other three CIMD provider factories gate on Profile.Feature.CIMD.

Update (2026-10-14): for comparison, what we now run for CIMD clients is that shape: a
scheduled workflow, scoped by a client attribute, that deletes a CIMD client after 21 idle
days, with the timer reset by an activity event (LOGIN, CODE_TO_TOKEN, REFRESH_TOKEN;
proposed as client-activity in #53082). We also check that the client holds no live
sessions right before the step runs, since an event-driven timer is only as reliable as the
events that reach it. The engine re-evaluates a workflow's conditions before each scheduled
step, so that check fits as a condition.

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] Orphan cleanup mechanism

4 participants

0