Skip to content

Commit ff06970

Browse files
authored
Preserve project-id when stashing thread context (#1755)
"stashing" the thread context is typically used to execute something with a differenet set of privileges (access to system indices, or a different user). In these cases we don't want to lose the project-id from the context (just like we don't want to lose APM tracing information). This change preserves the project-id during stash.
1 parent 2f785be commit ff06970

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

server/src/main/java/org/elasticsearch/common/util/concurrent/ThreadContext.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ public ThreadContext(Settings settings) {
101101
}
102102

103103
/**
104-
* Removes the current context and resets a default context except for headers involved in task tracing. The removed context can be
105-
* restored by closing the returned {@link StoredContext}.
104+
* Removes the current context and resets a default context except for headers involved in task tracing and project awareness.
105+
* The removed context can be restored by closing the returned {@link StoredContext}.
106106
* @return a stored context that will restore the current context to its state at the point this method was called
107107
*/
108108
public StoredContext stashContext() {
109109
return stashContextPreservingRequestHeaders(Collections.emptySet());
110110
}
111111

112112
/**
113-
* Just like {@link #stashContext()} but preserves request headers specified via {@code requestHeaders},
113+
* Just like {@link #stashContext()} but also preserves request headers specified via {@code requestHeaders},
114114
* if these exist in the context before stashing.
115115
*/
116116
public StoredContext stashContextPreservingRequestHeaders(Set<String> requestHeaders) {

server/src/main/java/org/elasticsearch/tasks/Task.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public class Task implements Traceable {
6565
X_OPAQUE_ID_HTTP_HEADER,
6666
TRACE_PARENT_HTTP_HEADER,
6767
TRACE_ID,
68-
X_ELASTIC_PRODUCT_ORIGIN_HTTP_HEADER
68+
X_ELASTIC_PRODUCT_ORIGIN_HTTP_HEADER,
69+
X_ELASTIC_PROJECT_ID_HTTP_HEADER
6970
);
7071

7172
private final long id;

server/src/test/java/org/elasticsearch/common/util/concurrent/ThreadContextTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.tasks.Task;
1919
import org.elasticsearch.test.ESTestCase;
2020
import org.elasticsearch.test.MockLog;
21+
import org.hamcrest.Matcher;
2122

2223
import java.io.IOException;
2324
import java.util.Arrays;
@@ -468,14 +469,25 @@ public void testStashWithOrigin() {
468469
threadContext.putHeader("foo", "bar");
469470
}
470471

472+
final Matcher<? super String> matchesProjectId;
473+
if (randomBoolean()) {
474+
final String projectId = randomUUID();
475+
matchesProjectId = equalTo(projectId);
476+
threadContext.putHeader(Task.X_ELASTIC_PROJECT_ID_HTTP_HEADER, projectId);
477+
} else {
478+
matchesProjectId = nullValue();
479+
}
480+
471481
assertNull(threadContext.getTransient(ThreadContext.ACTION_ORIGIN_TRANSIENT_NAME));
472482
try (ThreadContext.StoredContext storedContext = threadContext.stashWithOrigin(origin)) {
473483
assertEquals(origin, threadContext.getTransient(ThreadContext.ACTION_ORIGIN_TRANSIENT_NAME));
474484
assertNull(threadContext.getTransient("foo"));
475485
assertNull(threadContext.getTransient("bar"));
486+
assertThat(threadContext.getHeader(Task.X_ELASTIC_PROJECT_ID_HTTP_HEADER), matchesProjectId);
476487
}
477488

478489
assertNull(threadContext.getTransient(ThreadContext.ACTION_ORIGIN_TRANSIENT_NAME));
490+
assertThat(threadContext.getHeader(Task.X_ELASTIC_PROJECT_ID_HTTP_HEADER), matchesProjectId);
479491

480492
if (setOtherValues) {
481493
assertEquals("bar", threadContext.getTransient("foo"));

0 commit comments

Comments
 (0)