Wrong keycloak session when restarting workflows

Closes #44756

Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
This commit is contained in:
Pedro Igor 2025-12-09 06:17:28 -03:00 committed by GitHub
parent 96b92b1c70
commit 590538c99d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 20 deletions

View File

@ -49,16 +49,16 @@ final class DefaultWorkflowExecutionContext implements WorkflowExecutionContext
* @param step the scheduled step
*/
DefaultWorkflowExecutionContext(KeycloakSession session, Workflow workflow, ScheduledStep step) {
this(session, workflow, null, step, step.executionId(), step.resourceId());
this(session, workflow, null, step.stepId(), step.executionId(), step.resourceId());
}
private DefaultWorkflowExecutionContext(KeycloakSession session, Workflow workflow, WorkflowEvent event, ScheduledStep step, String executionId, String resourceId) {
DefaultWorkflowExecutionContext(KeycloakSession session, Workflow workflow, WorkflowEvent event, String stepId, String executionId, String resourceId) {
this.session = session;
this.workflow = workflow;
this.event = event;
if (step != null) {
this.currentStep = workflow.getStepById(step.stepId());
if (stepId != null) {
this.currentStep = workflow.getStepById(stepId);
} else {
this.currentStep = null;
}

View File

@ -13,16 +13,24 @@ class RunWorkflowTask extends WorkflowTransactionalTask {
private static final Logger log = Logger.getLogger(RunWorkflowTask.class);
private final DefaultWorkflowExecutionContext context;
private final String executionId;
private final String resourceId;
private final Workflow workflow;
private final WorkflowStep currentStep;
private final WorkflowEvent event;
RunWorkflowTask(DefaultWorkflowExecutionContext context) {
super(context.getSession());
this.context = context;
this.executionId = context.getExecutionId();
this.resourceId = context.getResourceId();
this.workflow = context.getWorkflow();
this.currentStep = context.getCurrentStep();
this.event = context.getEvent();
}
@Override
public void run(KeycloakSession session) {
DefaultWorkflowProvider provider = (DefaultWorkflowProvider) session.getProvider(WorkflowProvider.class);
DefaultWorkflowExecutionContext context = new DefaultWorkflowExecutionContext(session, workflow, event, currentStep == null ? null : currentStep.getId(), executionId, resourceId);
String executionId = context.getExecutionId();
String resourceId = context.getResourceId();
Workflow workflow = context.getWorkflow();
@ -30,7 +38,7 @@ class RunWorkflowTask extends WorkflowTransactionalTask {
if (currentStep != null) {
// we are resuming from a scheduled step - run it and then continue with the rest of the workflow
runWorkflowStep(session, provider, context);
runWorkflowStep(context);
}
List<WorkflowStep> stepsToRun = workflow.getSteps()
@ -47,27 +55,27 @@ class RunWorkflowTask extends WorkflowTransactionalTask {
} else {
// Otherwise, run the step right away
context.setCurrentStep(step);
runWorkflowStep(session, provider, context);
runWorkflowStep(context);
if (context.isRestarted()) {
return;
}
}
}
if (context.isRestarted()) {
// last step was a restart, so we restart the workflow from the beginning
context.restart();
return;
}
// not recurring, remove the state record
log.debugf("Workflow '%s' completed for resource %s (execution id: %s)", workflow.getName(), resourceId, executionId);
stateProvider.remove(executionId);
}
private void runWorkflowStep(KeycloakSession session, DefaultWorkflowProvider provider, DefaultWorkflowExecutionContext context) {
private void runWorkflowStep(DefaultWorkflowExecutionContext context) {
String executionId = context.getExecutionId();
WorkflowStep step = context.getCurrentStep();
String resourceId = context.getResourceId();
log.debugf("Running step %s on resource %s (execution id: %s)", step.getProviderId(), resourceId, executionId);
try {
getStepProvider(session, step).run(context);
getStepProvider(context.getSession(), step).run(context);
log.debugf("Step %s completed successfully (execution id: %s)", step.getProviderId(), executionId);
} catch(WorkflowExecutionException e) {
StringBuilder sb = new StringBuilder();

View File

@ -10,11 +10,17 @@ final class ScheduleWorkflowTask extends WorkflowTransactionalTask {
private static final Logger log = Logger.getLogger(ScheduleWorkflowTask.class);
private final DefaultWorkflowExecutionContext workflowContext;
private final String executionId;
private final String resourceId;
private final Workflow workflow;
private final WorkflowEvent event;
ScheduleWorkflowTask(DefaultWorkflowExecutionContext context) {
super(context.getSession());
this.workflowContext = context;
this.executionId = context.getExecutionId();
this.resourceId = context.getResourceId();
this.workflow = context.getWorkflow();
this.event = context.getEvent();
}
@Override
@ -27,7 +33,7 @@ final class ScheduleWorkflowTask extends WorkflowTransactionalTask {
return;
}
DefaultWorkflowExecutionContext workflowContext = new DefaultWorkflowExecutionContext(session, workflow, event, null, executionId, resourceId);
Workflow workflow = workflowContext.getWorkflow();
WorkflowEvent event = workflowContext.getEvent();
WorkflowStep firstStep = workflow.getSteps().findFirst().orElseThrow(() -> new WorkflowInvalidStateException("No steps found for workflow " + workflow.getName()));
@ -46,7 +52,6 @@ final class ScheduleWorkflowTask extends WorkflowTransactionalTask {
@Override
public String toString() {
WorkflowEvent event = workflowContext.getEvent();
return "eventType=" + event.getOperation() +
",resourceType=" + event.getResourceType() +
",resourceId=" + event.getResourceId();