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

feat: mTLS support for external hosts #2796

Merged
Merged
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 @@ -33,6 +33,7 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.spanner.v1.DatabaseName;
import io.grpc.ExperimentalApi;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -108,6 +109,8 @@ public static class Builder {
private String endpoint;
private boolean usePlainText;
private Duration startupTimeout = DEFAULT_STARTUP_TIMEOUT;
private String clientCertificate;
private String clientKey;

Builder() {}

Expand Down Expand Up @@ -413,6 +416,20 @@ Builder setStartupTimeout(Duration timeout) {
return this;
}

/**
* Configures mTLS authentication using the provided client certificate and key files. mTLS is
* only supported for external spanner hosts.
*
* @param clientCertificate Path to the client certificate file.
* @param clientKey Path to the client private key file.
*/
@ExperimentalApi("https://github.com/googleapis/java-spanner/pull/3574")
Builder useClientCert(String clientCertificate, String clientKey) {
this.clientCertificate = clientCertificate;
this.clientKey = clientKey;
return this;
}

public OptionsMetadata build() {
if (Strings.isNullOrEmpty(project) && !Strings.isNullOrEmpty(instance)) {
throw SpannerExceptionFactory.newSpannerException(
Expand Down Expand Up @@ -484,7 +501,8 @@ private String[] toCommandLineArguments() {
|| databaseRole != null
|| autoConfigEmulator
|| useVirtualGrpcTransportThreads
|| enableEndToEndTracing) {
|| enableEndToEndTracing
|| (clientKey != null && clientCertificate != null)) {
StringBuilder jdbcOptionBuilder = new StringBuilder();
if (usePlainText) {
jdbcOptionBuilder.append("usePlainText=true;");
Expand All @@ -506,6 +524,10 @@ private String[] toCommandLineArguments() {
if (enableEndToEndTracing) {
jdbcOptionBuilder.append(ENABLE_END_TO_END_TRACING_PROPERTY_NAME).append("=true;");
}
if (clientKey != null && clientCertificate != null) {
jdbcOptionBuilder.append("clientCertificate=").append(clientCertificate).append(";");
jdbcOptionBuilder.append("clientKey=").append(clientKey).append(";");
}
addOption(args, OPTION_JDBC_PROPERTIES, jdbcOptionBuilder.toString());
}
if (logGrpcMessages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,12 @@ public void testBuildConnectionUrlWithEmulator() {
.build()
.buildConnectionURL("projects/my-project/instances/my-instance/databases/my-database"));
}

@Test
public void testUseClientCertParameters() {
OptionsMetadata options =
OptionsMetadata.newBuilder().useClientCert("client.crt", "client.key").build();
assertEquals("client.crt", options.getPropertyMap().get("clientCertificate"));
assertEquals("client.key", options.getPropertyMap().get("clientKey"));
}
}
Loading