diff --git a/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java b/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java index a5cf177f5d1..0e9970f36e0 100644 --- a/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java +++ b/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java @@ -164,11 +164,18 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl (ns, key) -> boolSetting(key, TransportSettings.TRANSPORT_COMPRESS, new RemoteConnectionEnabled<>(ns, key), Setting.Property.Dynamic, Setting.Property.NodeScope)); + private final boolean enabled; + + public boolean isEnabled() { + return enabled; + } + private final TransportService transportService; private final Map remoteClusters = ConcurrentCollections.newConcurrentMap(); RemoteClusterService(Settings settings, TransportService transportService) { super(settings); + this.enabled = ENABLE_REMOTE_CLUSTERS.get(settings); this.transportService = transportService; } @@ -248,6 +255,9 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl } RemoteClusterConnection getRemoteClusterConnection(String cluster) { + if (enabled == false) { + throw new IllegalArgumentException("remote cluster service is not enabled"); + } RemoteClusterConnection connection = remoteClusters.get(cluster); if (connection == null) { throw new NoSuchRemoteClusterException(cluster); @@ -385,6 +395,9 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl * function on success. */ public void collectNodes(Set clusters, ActionListener> listener) { + if (enabled == false) { + throw new IllegalArgumentException("remote cluster service is not enabled"); + } Map remoteClusters = this.remoteClusters; for (String cluster : clusters) { if (remoteClusters.containsKey(cluster) == false) { @@ -428,6 +441,9 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl * @throws IllegalArgumentException if the given clusterAlias doesn't exist */ public Client getRemoteClusterClient(ThreadPool threadPool, String clusterAlias) { + if (transportService.getRemoteClusterService().isEnabled() == false) { + throw new IllegalArgumentException("remote cluster service is not enabled"); + } if (transportService.getRemoteClusterService().getRemoteClusterNames().contains(clusterAlias) == false) { throw new NoSuchRemoteClusterException(clusterAlias); } diff --git a/server/src/test/java/org/elasticsearch/transport/RemoteClusterClientTests.java b/server/src/test/java/org/elasticsearch/transport/RemoteClusterClientTests.java index ed71b7f85c8..7c1d3edf455 100644 --- a/server/src/test/java/org/elasticsearch/transport/RemoteClusterClientTests.java +++ b/server/src/test/java/org/elasticsearch/transport/RemoteClusterClientTests.java @@ -34,6 +34,7 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import static org.elasticsearch.transport.RemoteClusterConnectionTests.startTransport; +import static org.hamcrest.Matchers.equalTo; public class RemoteClusterClientTests extends ESTestCase { private final ThreadPool threadPool = new TestThreadPool(getClass().getName()); @@ -119,4 +120,17 @@ public class RemoteClusterClientTests extends ESTestCase { } } } + + public void testRemoteClusterServiceNotEnabled() { + final Settings settings = Settings.builder().put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), false).build(); + try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) { + service.start(); + service.acceptIncomingRequests(); + final RemoteClusterService remoteClusterService = service.getRemoteClusterService(); + final IllegalArgumentException e = + expectThrows(IllegalArgumentException.class, () -> remoteClusterService.getRemoteClusterClient(threadPool, "test")); + assertThat(e.getMessage(), equalTo("remote cluster service is not enabled")); + } + } + } diff --git a/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java b/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java index 2d81ff5705b..f676ebb46f2 100644 --- a/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java +++ b/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java @@ -42,6 +42,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -850,10 +851,34 @@ public class RemoteClusterServiceTests extends ESTestCase { } } + public void testRemoteClusterServiceNotEnabledGetRemoteClusterConnection() { + final Settings settings = Settings.builder().put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), false).build(); + try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) { + service.start(); + service.acceptIncomingRequests(); + final IllegalArgumentException e = + expectThrows(IllegalArgumentException.class, () -> service.getRemoteClusterService().getRemoteClusterConnection("test")); + assertThat(e.getMessage(), equalTo("remote cluster service is not enabled")); + } + } + + public void testRemoteClusterServiceNotEnabledGetCollectNodes() { + final Settings settings = Settings.builder().put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), false).build(); + try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) { + service.start(); + service.acceptIncomingRequests(); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> service.getRemoteClusterService().collectNodes(Set.of(), ActionListener.wrap(r -> {}, r -> {}))); + assertThat(e.getMessage(), equalTo("remote cluster service is not enabled")); + } + } + private static Settings createSettings(String clusterAlias, List seeds) { Settings.Builder builder = Settings.builder(); builder.put(SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS.getConcreteSettingForNamespace(clusterAlias).getKey(), Strings.collectionToCommaDelimitedString(seeds)); return builder.build(); } + }