Skip to content

Commit

Permalink
Changes same node evaluation logic to use TransportService instead of…
Browse files Browse the repository at this point in the history
… ClusterState

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
DarshitChanpura committed May 23, 2023
1 parent 21b52a6 commit a9e31cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.opensearch.action.support.ActionFilter;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.component.Lifecycle.State;
Expand Down Expand Up @@ -1189,6 +1190,7 @@ public static class GuiceHolder implements LifecycleComponent {
private static RemoteClusterService remoteClusterService;
private static IndicesService indicesService;
private static PitService pitService;
private static DiscoveryNode localNode;

// CS-SUPPRESS-SINGLE: RegexpSingleline Extensions manager used to allow/disallow TLS connections to extensions
private static ExtensionsManager extensionsManager;
Expand All @@ -1201,6 +1203,7 @@ public GuiceHolder(final RepositoriesService repositoriesService,
GuiceHolder.indicesService = indicesService;
GuiceHolder.pitService = pitService;
GuiceHolder.extensionsManager = extensionsManager;
GuiceHolder.localNode = remoteClusterService.getLocalNode();
}
// CS-ENFORCE-SINGLE

Expand All @@ -1222,6 +1225,7 @@ public static IndicesService getIndicesService() {
public static ExtensionsManager getExtensionsManager() { return extensionsManager; }
// CS-ENFORCE-SINGLE

public static DiscoveryNode getLocalNode() { return localNode; }

@Override
public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.opensearch.action.get.GetRequest;
import org.opensearch.action.search.SearchAction;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.settings.Settings;
Expand All @@ -68,6 +69,7 @@
import org.opensearch.transport.TransportRequestOptions;
import org.opensearch.transport.TransportResponse;
import org.opensearch.transport.TransportResponseHandler;
import org.opensearch.transport.TransportService;

import static org.opensearch.security.OpenSearchSecurityPlugin.isActionTraceEnabled;

Expand All @@ -85,14 +87,16 @@ public class SecurityInterceptor {
private final ClusterInfoHolder clusterInfoHolder;
private final SSLConfig SSLConfig;

private final DiscoveryNode localNode;

public SecurityInterceptor(final Settings settings,
final ThreadPool threadPool, final BackendRegistry backendRegistry,
final AuditLog auditLog, final PrincipalExtractor principalExtractor,
final InterClusterRequestEvaluator requestEvalProvider,
final ClusterService cs,
final SslExceptionHandler sslExceptionHandler,
final ClusterInfoHolder clusterInfoHolder,
final SSLConfig SSLConfig) {
final ThreadPool threadPool, final BackendRegistry backendRegistry,
final AuditLog auditLog, final PrincipalExtractor principalExtractor,
final InterClusterRequestEvaluator requestEvalProvider,
final ClusterService cs,
final SslExceptionHandler sslExceptionHandler,
final ClusterInfoHolder clusterInfoHolder,
final SSLConfig SSLConfig) {
this.backendRegistry = backendRegistry;
this.auditLog = auditLog;
this.threadPool = threadPool;
Expand All @@ -103,6 +107,7 @@ public SecurityInterceptor(final Settings settings,
this.sslExceptionHandler = sslExceptionHandler;
this.clusterInfoHolder = clusterInfoHolder;
this.SSLConfig = SSLConfig;
this.localNode = OpenSearchSecurityPlugin.GuiceHolder.getLocalNode();
}

public <T extends TransportRequest> SecurityRequestHandler<T> getHandler(String action,
Expand All @@ -127,12 +132,14 @@ public <T extends TransportResponse> void sendRequestDecorate(AsyncSender sender
final String origCCSTransientMf = getThreadContext().getTransient(ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_CCS);

final boolean isDebugEnabled = log.isDebugEnabled();
boolean isSameNodeRequest = false;
try {
isSameNodeRequest = cs.localNode().equals(connection.getNode()); // using DiscoveryNode equals comparison here
} catch (AssertionError e) {
// do nothing
}
boolean isSameNodeRequest = localNode != null && localNode.equals(connection.getNode());
// try {
// isSameNodeRequest = cs.localNode().equals(connection.getNode()); // using DiscoveryNode equals comparison here
// } catch (AssertionError e) {
// // do nothing
// log.info(e);
// }


try (ThreadContext.StoredContext stashedContext = getThreadContext().stashContext()) {
final TransportResponseHandler<T> restoringHandler = new RestoringTransportResponseHandler<T>(handler, stashedContext);
Expand Down

0 comments on commit a9e31cf

Please sign in to comment.