Skip to content

Commit 1caa26d

Browse files
authored
Skip automatically preserved request headers when rewriting (#79973) (#79985)
In #79412 we fixed a bug that request headers got dropped when the request is sent across to a node of different version. The fix is to restore all existing request headers during the threadContext rewriting. However, there are headers that are always automatically preserved by the ThreadContext infrastructure, e.g. x-opaque-id. This causes failures when the code tries to re-add the x-opaque-id header since it already exists. An example of this issue is for CCS where the remote cluster is often on a different version compared to the local cluster. Resolves: #79412
1 parent 272b76b commit 1caa26d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.elasticsearch.node.Node;
2222
import org.elasticsearch.xpack.core.security.authc.Authentication;
2323
import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType;
24-
import org.elasticsearch.xpack.core.security.authc.AuthenticationField;
2524
import org.elasticsearch.xpack.core.security.authc.support.AuthenticationContextSerializer;
2625
import org.elasticsearch.xpack.core.security.authc.support.SecondaryAuthentication;
2726
import org.elasticsearch.xpack.core.security.user.User;
@@ -167,7 +166,7 @@ public void executeAfterRewritingAuthentication(Consumer<StoredContext> consumer
167166
authentication.getLookedUpBy(), version, authentication.getAuthenticationType(),
168167
rewriteMetadataForApiKeyRoleDescriptors(version, authentication)));
169168
existingRequestHeaders.forEach((k, v) -> {
170-
if (false == AuthenticationField.AUTHENTICATION_KEY.equals(k)) {
169+
if (threadContext.getHeader(k) == null) {
171170
threadContext.putHeader(k, v);
172171
}
173172
});

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.common.util.concurrent.ThreadContext;
1414
import org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext;
1515
import org.elasticsearch.core.List;
16+
import org.elasticsearch.tasks.Task;
1617
import org.elasticsearch.test.ESTestCase;
1718
import org.elasticsearch.test.VersionUtils;
1819
import org.elasticsearch.xpack.core.security.SecurityContext;
@@ -121,8 +122,14 @@ public void testExecuteAfterRewritingAuthentication() throws IOException {
121122
final Authentication original = new Authentication(user, authBy, authBy);
122123
original.writeToContext(threadContext);
123124
final Map<String, String> requestHeaders = org.elasticsearch.core.Map.of(
124-
AuthenticationField.PRIVILEGE_CATEGORY_KEY, randomAlphaOfLengthBetween(3, 10),
125-
randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)
125+
AuthenticationField.PRIVILEGE_CATEGORY_KEY,
126+
randomAlphaOfLengthBetween(3, 10),
127+
randomAlphaOfLengthBetween(3, 8),
128+
randomAlphaOfLengthBetween(3, 8),
129+
Task.X_OPAQUE_ID,
130+
randomAlphaOfLength(10),
131+
Task.TRACE_ID,
132+
randomAlphaOfLength(20)
126133
);
127134
threadContext.putHeader(requestHeaders);
128135

0 commit comments

Comments
 (0)