From 5bc8a859c6fd80a62995c47643bd219c4241ae8c Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Fri, 17 Apr 2020 12:40:32 -0400 Subject: [PATCH] Remove ban tasks with the current thread context (#55404) If we start unbanning when the last child task completed and that child task executed with a specific user, then unban requests are denied because internal requests can't run with a user. We need to remove bans with the current thread context. --- .../cancel/TransportCancelTasksAction.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java index 1d2300c6996..61af76cbd01 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java @@ -19,6 +19,8 @@ package org.elasticsearch.action.admin.cluster.node.tasks.cancel; +import org.elasticsearch.ElasticsearchSecurityException; +import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; @@ -121,8 +123,12 @@ public class TransportCancelTasksAction extends TransportTasksAction banOnNodesListener = new StepListener<>(); setBanOnNodes(reason, waitForCompletion, task, childrenNodes, banOnNodesListener); banOnNodesListener.whenComplete(groupedListener::onResponse, groupedListener::onFailure); + // If we start unbanning when the last child task completed and that child task executed with a specific user, then unban + // requests are denied because internal requests can't run with a user. We need to remove bans with the current thread context. + final Runnable removeBansRunnable = transportService.getThreadPool().getThreadContext() + .preserveContext(() -> removeBanOnNodes(task, childrenNodes)); // We remove bans after all child tasks are completed although in theory we can do it on a per-node basis. - completedListener.whenComplete(r -> removeBanOnNodes(task, childrenNodes), e -> removeBanOnNodes(task, childrenNodes)); + completedListener.whenComplete(r -> removeBansRunnable.run(), e -> removeBansRunnable.run()); // if wait_for_completion is true, then only return when (1) bans are placed on child nodes, (2) child tasks are // completed or failed, (3) the main task is cancelled. Otherwise, return after bans are placed on child nodes. if (waitForCompletion) { @@ -162,6 +168,7 @@ public class TransportCancelTasksAction extends TransportTasksAction