Skip to content

Add user to task #124584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/src/main/java/org/elasticsearch/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class Task implements Traceable {
public static final String TRACE_START_TIME = "trace.starttime";
public static final String TRACE_PARENT = "traceparent";
public static final String X_ELASTIC_PROJECT_ID_HTTP_HEADER = "X-Elastic-Project-Id";
public static final String USER_KEY = "user";

public static final Set<String> HEADERS_TO_COPY = Set.of(
X_OPAQUE_ID_HTTP_HEADER,
Expand Down
4 changes: 4 additions & 0 deletions server/src/main/java/org/elasticsearch/tasks/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public Task register(String type, String action, TaskAwareRequest request, boole
headers.put(key, httpHeader);
}
}
String user = threadContext.getTransient(Task.USER_KEY);
if (user != null) {
headers.put(Task.USER_KEY, user);
}
Task task = request.createTask(taskIdGenerator.incrementAndGet(), type, action, request.getParentTask(), headers);
Copy link
Member Author

Choose a reason for hiding this comment

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

I think because there are so many implementations of this we might should make an object CreateTaskParams and stuff everything in there.

Objects.requireNonNull(task);
assert task.getParentTaskId().equals(request.getParentTask()) : "Request [ " + request + "] didn't preserve it parentTaskId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.telemetry.metric.MeterRegistry;
import org.elasticsearch.xpack.core.common.IteratingActionListener;
import org.elasticsearch.xpack.core.security.authc.Authentication;
Expand Down Expand Up @@ -243,6 +244,7 @@ private void consumeToken(Context context, ActionListener<AuthenticationResult<A
final AuthenticationResult<User> result = authenticationResultRef.get();
assert result != null : "authentication result must not be null when user is not null";
context.getThreadContext().putTransient(AuthenticationResult.THREAD_CONTEXT_KEY, result);
context.getThreadContext().putTransient(Task.USER_KEY, result.getValue().principal());
listener.onResponse(
AuthenticationResult.success(Authentication.newRealmAuthentication(user, authenticatedByRef.get().realmRef()))
);
Expand Down