Skip to content

Commit 47762d0

Browse files
committed
chore: abstract plain text settings into a SpannerOption
1 parent afd7d6b commit 47762d0

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ protected SpannerOptions(Builder builder) {
825825
openTelemetry = builder.openTelemetry;
826826
enableApiTracing = builder.enableApiTracing;
827827
enableExtendedTracing = builder.enableExtendedTracing;
828-
if (builder.isExperimentalHost) {
828+
if (builder.experimentalHost != null) {
829829
enableBuiltInMetrics = false;
830830
} else {
831831
enableBuiltInMetrics = builder.enableBuiltInMetrics;
@@ -1047,7 +1047,8 @@ public static class Builder
10471047
private boolean enableBuiltInMetrics = SpannerOptions.environment.isEnableBuiltInMetrics();
10481048
private String monitoringHost = SpannerOptions.environment.getMonitoringHost();
10491049
private SslContext mTLSContext = null;
1050-
private boolean isExperimentalHost = false;
1050+
private String experimentalHost = null;
1051+
private boolean usePlainText = false;
10511052
private TransactionOptions defaultTransactionOptions = TransactionOptions.getDefaultInstance();
10521053

10531054
private static String createCustomClientLibToken(String token) {
@@ -1550,10 +1551,13 @@ public Builder setHost(String host) {
15501551

15511552
@ExperimentalApi("https://github.com/googleapis/java-spanner/pull/3676")
15521553
public Builder setExperimentalHost(String host) {
1554+
if (usePlainText && !host.startsWith("http")) {
1555+
host = "http://" + host;
1556+
}
15531557
super.setHost(host);
15541558
super.setProjectId(EXPERIMENTAL_HOST_PROJECT_ID);
15551559
setSessionPoolOption(SessionPoolOptions.newBuilder().setExperimentalHost().build());
1556-
this.isExperimentalHost = true;
1560+
experimentalHost = host;
15571561
return this;
15581562
}
15591563

@@ -1614,6 +1618,22 @@ public Builder useClientCert(String clientCertificate, String clientCertificateK
16141618
return this;
16151619
}
16161620

1621+
/**
1622+
* {@code usePlainText} is only supported for experimental spanner hosts. This will configure
1623+
* the transport to use plaintext (no TLS) and will set credentials to {@link
1624+
* com.google.cloud.NoCredentials} to avoid sending authentication over an unsecured channel.
1625+
*/
1626+
@ExperimentalApi("https://github.com/googleapis/java-spanner/pull/3574")
1627+
public Builder usePlainText() {
1628+
this.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
1629+
.setCredentials(NoCredentials.getInstance());
1630+
if (this.experimentalHost != null && !this.experimentalHost.startsWith("http")) {
1631+
this.experimentalHost = "http://" + this.experimentalHost;
1632+
super.setHost(this.experimentalHost);
1633+
}
1634+
return this;
1635+
}
1636+
16171637
/**
16181638
* Sets OpenTelemetry object to be used for Spanner Metrics and Traces. GlobalOpenTelemetry will
16191639
* be used as fallback if this options is not set.
@@ -1789,7 +1809,7 @@ public SpannerOptions build() {
17891809
this.setChannelConfigurator(ManagedChannelBuilder::usePlaintext);
17901810
// As we are using plain text, we should never send any credentials.
17911811
this.setCredentials(NoCredentials.getInstance());
1792-
} else if (isExperimentalHost && credentials == null) {
1812+
} else if (experimentalHost != null && credentials == null) {
17931813
credentials = environment.getDefaultExperimentalHostCredentials();
17941814
}
17951815
if (this.numChannels == null) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/testing/ExperimentalHostHelper.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
package com.google.cloud.spanner.testing;
1818

19-
import com.google.cloud.NoCredentials;
2019
import com.google.cloud.spanner.SpannerOptions;
2120
import com.google.common.base.Strings;
22-
import io.grpc.ManagedChannelBuilder;
2321

2422
public class ExperimentalHostHelper {
2523
private static final String EXPERIMENTAL_HOST = "spanner.experimental_host";
@@ -58,9 +56,7 @@ public static void setExperimentalHostSpannerOptions(SpannerOptions.Builder buil
5856
builder.setExperimentalHost(experimentalHost);
5957
builder.setBuiltInMetricsEnabled(false);
6058
if (usePlainText) {
61-
builder
62-
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
63-
.setCredentials(NoCredentials.getInstance());
59+
builder.usePlainText();
6460
}
6561
if (isMtlsSetup()) {
6662
String clientCertificate = System.getProperty(CLIENT_CERT_PATH, "");

0 commit comments

Comments
 (0)