8000 Add a client-activity workflow event provider by antoniocasagrande-airia · Pull Request #53082 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Add a client-activity workflow event provider - #53082

Open
antoniocasagrande-airia wants to merge 3 commits into
keycloak:mainfrom
antoniocasagrande-airia:workflow-client-activity-event
Open

antoniocasagrande-airia wants to merge 3 commits into
keycloak:mainfrom
antoniocasagrande-airia:workflow-client-activity-event

Conversation

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

Part of #51595.

Workflows can already react to a client authenticating, but that event is EventType.CLIENT_LOGIN, which only the client_credentials grant emits. A public authorization-code client — what dynamic client registration produces — never raises it. So a CLIENTS workflow meaning "disable this client if it has been idle for N days" silently degrades to "N days after it was created" for exactly the population most likely to accumulate.

This adds client-activity over LOGIN, CODE_TO_TOKEN and REFRESH_TOKEN, so any use of the client resets the timer.

The *_ERROR variants are deliberately excluded. They carry the client id too, so counting them would let failed authentication against a dormant client keep it alive indefinitely — a property worth stating explicitly, since it is the kind of thing that looks like an oversight later.

The first commit is a separate defect

JpaWorkflowStateProvider schedules steps with Instant.now() while the due-check queries against Time.currentTimeMillis():

entity.setScheduledStepTimestamp(Instant.now().plus(duration).toEpochMilli());   // real clock
...
cb.lessThan(stateRoot.get("scheduledStepTimestamp"), Time.currentTimeMillis());  // offsettable clock

Under any time offset the due-check moves and the schedule does not, so steps fire at the wrong time. This is independent of the event provider, and production is unaffected because Time delegates to the system clock; it is first here because the activity test cannot express "the timer resets on use" without both sides agreeing on a clock. Happy to split it into its own PR if you would rather review it separately.

It only matters where a step is scheduled while an offset is set, as when a later login restarts the workflow. Steps scheduled from runScheduledSteps are unaffected: the step task runs after the transaction completes, by which point the test help 8000 er has already reset the offset. Without this commit, the two reset cases in ClientActivityWorkflowTest fail; existing tests pass either way.

Testing

Integration tests cover activation on use and the reset after a login, a reset where a token refresh is the only activity, and a case pinning that failed logins do not activate. A unit test pins each counted and ignored event type, since an authorization code is only issued at the end of a login and an integration test cannot exercise CODE_TO_TOKEN on its own.

Verified locally against a server built from this branch: ClientActivityWorkflowTest (3) and the unchanged BrokeredUserLifecycleWorkflowTest (4) pass, as does the unit test (3), and mvn spotless:check -pl model/jpa,tests/base is clean. The unit test fails if an *_ERROR type is added to the counted set.

In practice

We have run this event as a custom extension since August, as the reset trigger for disable and delete workflows on dynamically registered clients. One thing we added alongside it: a condition that checks the client has no live sessions before each due step, because an event-driven clock is only as reliable as the events that reach it. The engine already re-evaluates a workflow's conditions before running a scheduled step, so that check fits without engine changes. Happy to propose it separately if it is useful.

Context: this was originally part of #51629, which I have closed since the client-attribute condition it also carried is superseded by #52189. #52189 does not include an activity event, so this half is still open and is here on its own.

@antoniocasagrande-airia
antoniocasagrande-airia requested a review from a team as a code owner September 22, 2026 17:04
Copilot AI balanced review requested due to automatic review settings September 22, 2026 17:04
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.

Copilot review overview

🟡 Changes recommended

The public event is undocumented, and two supported activity event paths lack integration coverage.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Adds client activity tracking for workflows and aligns workflow scheduling with Keycloak’s offset-aware clock.

Changes:

  • Adds client-activity for successful login, code exchange, and token refresh events.
  • Adds activation/reset and failed-login integration coverage.
  • Fixes cumulative scheduling under time offsets.
File Description
BrokeredUserLifecycleWorkflowTest.java Updates cumulative time offsets.
ClientActivityWorkflowTest.java Tests client activity workflow behavior.
WorkflowEventProviderFactory Registers the new provider.
JpaWorkflowStateProvider.java Uses the offset-aware clock.
ClientActivityWorkflowEventProvider.java Defines qualifying activity events.
ClientActivityWorkflowEventFactory.java Exposes the client-activity event.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:15
@antoniocasagrande-airia
antoniocasagrande-airia force-pushed the workflow-client-activity-event branch from f176028 to acb31b5 Compare October 14, 2026 18: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.

🟡 Changes recommended

The lifecycle test clears the clock offset before deferred execution, leaving its stated cumulative scheduling behavior unverified.

3 open findings

🧠 Review effort: Balanced

… the same clock

JpaWorkflowStateProvider.scheduleStep stamped scheduledStepTimestamp with
Instant.now() while getDueScheduledSteps compares it against
Time.currentTimeMillis(). Production behavior is unchanged (Time delegates
to the system clock), but a step scheduled while a time offset is set, such
as a workflow restarted by an event, was scheduled against the real clock
and fell due early.

Signed-off-by: Antonio Casagrande <antoniocasagrande@airia.com>
Workflows can already react to a client authenticating, but that event is
EventType.CLIENT_LOGIN, which only the client_credentials grant emits. A public
authorization-code client - what dynamic client registration produces - never
raises it, so a workflow that means "disable this client if it has been idle for
N days" silently degrades to "N days after it was created" for exactly the
population most likely to accumulate.

Add client-activity over LOGIN, CODE_TO_TOKEN and REFRESH_TOKEN, so any use of
the client resets the timer. The *_ERROR variants are deliberately excluded:
they carry the client id too, so counting them would let failed authentication
against a dormant client keep it alive indefinitely.

Signed-off-by: Antonio Casagrande <antoniocasagrande@airia.com>
Add the event to the event-functions table. Pin the counted and ignored event types in a
unit test, since an authorization code is only issued at the end of a login and an
integration test cannot exercise CODE_TO_TOKEN apart from LOGIN. Add an integration case
where a token refresh is the only activity and still resets the workflow.

Signed-off-by: Antonio Casagrande <antoniocasagrande@airia.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:52
@antoniocasagrande-airia
antoniocasagrande-airia force-pushed the workflow-client-activity-event branch from acb31b5 to 1b121dd Compare October 14, 2026 18:52
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

The focused implementation matches its documented scope, includes regression coverage, and has no unresolved blocking findings.

0 open findings

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0