Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Dec 6, 2023
1 parent 6c86a93 commit 4c589dc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class RemoteClusterConnection implements Closeable {
* @param clusterAlias the configured alias of the cluster to connect to
* @param transportService the local nodes transport service
* @param credentialsManager object to lookup remote cluster credentials by cluster alias. If a cluster is protected by a credential,
* i.e. it has a credentials configured via secure setting.
* i.e. it has a credential configured via secure setting.
* This means the remote cluster uses the advances RCS model (as opposed to the basic model).
*/
RemoteClusterConnection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.transport.RemoteClusterService.REMOTE_CLUSTER_HANDSHAKE_ACTION_NAME;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -148,6 +150,26 @@ public void testRewriteHandshakeAction() throws IOException {
verify(connection).sendRequest(requestId, anotherAction, request, options);
}

public void testWrapAndResolveConnectionRoundTrip() {
final Transport.Connection connection = mock(Transport.Connection.class);
final String clusterAlias = randomAlphaOfLengthBetween(3, 8);
final RemoteClusterCredentialsManager credentialsResolver = mock(RemoteClusterCredentialsManager.class);
final SecureString credentials = new SecureString(randomAlphaOfLength(42));
// second credential will never be resolved
when(credentialsResolver.resolveCredentials(clusterAlias)).thenReturn(credentials, (SecureString) null);
final Transport.Connection wrappedConnection = RemoteConnectionManager.wrapConnectionWithRemoteClusterInfo(
connection,
clusterAlias,
credentialsResolver
);

final Optional<RemoteConnectionManager.RemoteClusterAliasWithCredentials> actual = RemoteConnectionManager
.resolveRemoteClusterAliasWithCredentials(wrappedConnection);

assertThat(actual.isPresent(), is(true));
assertThat(actual.get(), equalTo(new RemoteConnectionManager.RemoteClusterAliasWithCredentials(clusterAlias, credentials)));
}

private static class TestRemoteConnection extends CloseableConnection {

private final DiscoveryNode node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,23 @@ public void testSecurityMustBeEnableToConnectRemoteClusterWithCredentials() {
+ "Please either enable security or remove these settings from the keystore."
)
);

// Security off, remote cluster with credentials on reload call
final MockSecureSettings secureSettings5 = new MockSecureSettings();
secureSettings5.setString("cluster.remote.my1.credentials", randomAlphaOfLength(20));
secureSettings5.setString("cluster.remote.my2.credentials", randomAlphaOfLength(20));
final Settings.Builder builder5 = Settings.builder().setSecureSettings(secureSettings5);
// Use builder with security disabled to construct valid Security instance
final var security = new Security(builder2.build());
final IllegalArgumentException e5 = expectThrows(IllegalArgumentException.class, () -> security.reload(builder5.build()));
assertThat(
e5.getMessage(),
containsString(
"Found [2] remote clusters with credentials [cluster.remote.my1.credentials,cluster.remote.my2.credentials]. "
+ "Security [xpack.security.enabled] must be enabled to connect to them. "
+ "Please either enable security or remove these settings from the keystore."
)
);
}

public void testLoadExtensions() throws Exception {
Expand Down

0 comments on commit 4c589dc

Please sign in to comment.