-
Notifications
You must be signed in to change notification settings - Fork 336
Ensure all restHeaders from ActionPlugin.getRestHeaders are carried to threadContext for tracing #5396
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
base: main
Are you sure you want to change the base?
Ensure all restHeaders from ActionPlugin.getRestHeaders are carried to threadContext for tracing #5396
Changes from all commits
fed6098
f3e7f9d
bb90012
2a34997
08f641d
3aee8b1
a3183b1
243093e
560507d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
@@ -63,7 +64,6 @@ | |
import org.opensearch.security.support.ConfigConstants; | ||
import org.opensearch.security.support.HTTPHelper; | ||
import org.opensearch.security.user.User; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.opensearch.transport.client.node.NodeClient; | ||
|
||
|
@@ -72,6 +72,7 @@ | |
import static org.opensearch.security.OpenSearchSecurityPlugin.LEGACY_OPENDISTRO_PREFIX; | ||
import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX; | ||
import static org.opensearch.security.support.ConfigConstants.OPENDISTRO_SECURITY_INITIATING_USER; | ||
import static org.opensearch.security.support.ConfigConstants.OPENSEARCH_SECURITY_REQUEST_HEADERS; | ||
|
||
public class SecurityRestFilter { | ||
|
||
|
@@ -138,10 +139,13 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c | |
|
||
NettyAttribute.popFrom(request, Netty4HttpRequestHeaderVerifier.CONTEXT_TO_RESTORE).ifPresent(storedContext -> { | ||
// X_OPAQUE_ID will be overritten on restore - save to apply after restoring the saved context | ||
final String xOpaqueId = threadContext.getHeader(Task.X_OPAQUE_ID); | ||
final Map<String, String> tmpHeaders = threadContext.getHeaders(); | ||
storedContext.restore(); | ||
if (xOpaqueId != null) { | ||
threadContext.putHeader(Task.X_OPAQUE_ID, xOpaqueId); | ||
for (Map.Entry<String, String> header : tmpHeaders.entrySet()) { | ||
threadContext.putHeader(header.getKey(), header.getValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. qq, will values be null for any header? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, null is not possible. There could be empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this need specific headers or all headers? Copying/populating all headers might be costly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 Could this cause perf regression? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the linked issue: #4799 There is a bug with ActionPlugin.getRestHeaders() where the headers are getting dropped because the security plugin is not carrying them from rest -> transport and then also propagating them internally in the transport layer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think this would have a high performance penalty. |
||
} | ||
if (!tmpHeaders.isEmpty()) { | ||
threadContext.putHeader(OPENSEARCH_SECURITY_REQUEST_HEADERS, String.join(",", tmpHeaders.keySet())); | ||
} | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,12 @@ | |
package org.opensearch.security.transport; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
@@ -64,6 +67,7 @@ | |
import org.opensearch.security.support.ConfigConstants; | ||
import org.opensearch.security.user.User; | ||
import org.opensearch.security.user.UserFactory; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.opensearch.transport.Transport.Connection; | ||
import org.opensearch.transport.TransportException; | ||
|
@@ -158,6 +162,14 @@ public <T extends TransportResponse> void sendRequestDecorate( | |
final boolean isDebugEnabled = log.isDebugEnabled(); | ||
|
||
final boolean isSameNodeRequest = localNode != null && localNode.equals(connection.getNode()); | ||
final Set<String> requestHeadersToCopy = new HashSet<>(); | ||
if (getThreadContext().getHeader(ConfigConstants.OPENSEARCH_SECURITY_REQUEST_HEADERS) != null) { | ||
Collections.addAll( | ||
requestHeadersToCopy, | ||
getThreadContext().getHeader(ConfigConstants.OPENSEARCH_SECURITY_REQUEST_HEADERS).split(",") | ||
); | ||
requestHeadersToCopy.remove(Task.X_OPAQUE_ID); // Special case where this header is preserved during stashContext. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this file are necessary because currently only Ideally, this is solved another way through the core. Either:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, option 1 is better. |
||
} | ||
|
||
try (ThreadContext.StoredContext stashedContext = getThreadContext().stashContext()) { | ||
final TransportResponseHandler<T> restoringHandler = new RestoringTransportResponseHandler<T>(handler, stashedContext); | ||
|
@@ -178,11 +190,13 @@ public <T extends TransportResponse> void sendRequestDecorate( | |
|| k.equals(ConfigConstants.OPENDISTRO_SECURITY_FILTER_LEVEL_DLS_DONE) | ||
|| k.equals(ConfigConstants.OPENDISTRO_SECURITY_DLS_MODE_HEADER) | ||
|| k.equals(ConfigConstants.OPENDISTRO_SECURITY_DLS_FILTER_LEVEL_QUERY_HEADER) | ||
|| k.equals(ConfigConstants.OPENSEARCH_SECURITY_REQUEST_HEADERS) | ||
|| (k.equals("_opendistro_security_source_field_context") | ||
&& !(request instanceof SearchRequest) | ||
&& !(request instanceof GetRequest)) | ||
|| k.startsWith("_opendistro_security_trace") | ||
|| k.startsWith(ConfigConstants.OPENDISTRO_SECURITY_INITIAL_ACTION_CLASS_HEADER)) | ||
|| k.startsWith(ConfigConstants.OPENDISTRO_SECURITY_INITIAL_ACTION_CLASS_HEADER) | ||
|| requestHeadersToCopy.contains(k)) | ||
) | ||
); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the motivation for this change? Without this how hard is it be debug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the issue as described here: #4799
WLM (workload management) found this issue bc they have logic to tag queries and assign them to groups. It was noticed that after overridding
ActionPlugin.getRestHeaders()
the the headers they expected to trace around with the entire request handling process (i.e. from rest down to transport and carried node-to-node) were getting dropped bc of logic in the security repo to control what headers are sent internally. Not all HTTP Headers should be copied to the threadcontext and carried around, but the one's explicitly allowed viaActionPlugin.getRestHeaders()
should be.