Skip to content
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

Java: implement support for using insecure TLS #3386

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public abstract class BaseClientConfiguration {
*/
@Builder.Default private final boolean useTLS = false;

/**
* True if communication with the cluster should check certificate validity.
*
* <p>If the server/cluster's certificate does not validate, not setting this will cause the
* connection attempt to fail.
*
* <p>If the server/cluster's certificate does not validate, setting this will cause the
* connection to ignore the certificate's validity and succeed.
*
* <p>This is useful for when CNAMEs are used to point to a server/cluster.
*/
@Builder.Default private final boolean useInsecureTLS = false;

/** Represents the client's read from strategy. */
@NonNull @Builder.Default private final ReadFrom readFrom = ReadFrom.PRIMARY;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,15 @@ private ConnectionRequest.Builder setupConnectionRequestBuilderBaseConfiguration
.build());
}

connectionRequestBuilder
.setTlsMode(configuration.isUseTLS() ? TlsMode.SecureTls : TlsMode.NoTls)
.setReadFrom(mapReadFromEnum(configuration.getReadFrom()));
if (configuration.isUseTLS()) {
connectionRequestBuilder
.setTlsMode(configuration.isUseInsecureTLS() ? TlsMode.InsecureTls : TlsMode.SecureTls)
.setReadFrom(mapReadFromEnum(configuration.getReadFrom()));
} else {
connectionRequestBuilder
.setTlsMode(TlsMode.NoTls)
.setReadFrom(mapReadFromEnum(configuration.getReadFrom()));
}

if (configuration.getCredentials() != null) {
AuthenticationInfo.Builder authenticationInfoBuilder = AuthenticationInfo.newBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void connection_request_protobuf_generation_with_all_fields_set() {
.address(NodeAddress.builder().host(HOST).port(PORT).build())
.address(NodeAddress.builder().host(DEFAULT_HOST).port(DEFAULT_PORT).build())
.useTLS(true)
.useInsecureTLS(false)
.readFrom(ReadFrom.PREFER_REPLICA)
.credentials(ServerCredentials.builder().username(USERNAME).password(PASSWORD).build())
.requestTimeout(REQUEST_TIMEOUT)
Expand Down Expand Up @@ -348,6 +349,32 @@ private void testAzAffinityWithoutClientAzThrowsConfigurationError(ReadFrom read
assertThrows(ConfigurationError.class, () -> connectionManager.connectToValkey(config));
}

@SneakyThrows
@Test
public void connection_request_protobuf_generation_use_insecure_tls() {
// setup
GlideClusterClientConfiguration glideClusterClientConfiguration =
GlideClusterClientConfiguration.builder().useTLS(true).useInsecureTLS(true).build();
ConnectionRequest expectedProtobufConnectionRequest =
ConnectionRequest.newBuilder()
.setTlsMode(TlsMode.InsecureTls)
.setClusterModeEnabled(true)
.setReadFrom(ConnectionRequestOuterClass.ReadFrom.Primary)
.build();
CompletableFuture<Response> completedFuture = new CompletableFuture<>();
Response response = Response.newBuilder().setConstantResponse(ConstantResponse.OK).build();
completedFuture.complete(response);

// execute
when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture);
CompletableFuture<Void> result =
connectionManager.connectToValkey(glideClusterClientConfiguration);

// verify
assertNull(result.get());
verify(channel).connect(eq(expectedProtobufConnectionRequest));
}

private ConnectionRequestOuterClass.ReadFrom mapReadFrom(ReadFrom readFrom) {
switch (readFrom) {
case AZ_AFFINITY:
Expand Down
Loading