Skip to content

Commit 996e26f

Browse files
committed
code refactor to handle edge cases
1 parent c33ae1b commit 996e26f

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.google.common.annotations.VisibleForTesting;
5959
import com.google.common.base.MoreObjects;
6060
import com.google.common.base.Preconditions;
61+
import com.google.common.base.Strings;
6162
import com.google.common.collect.ImmutableMap;
6263
import com.google.common.collect.ImmutableSet;
6364
import com.google.common.util.concurrent.ThreadFactoryBuilder;
@@ -810,18 +811,6 @@ protected SpannerOptions(Builder builder) {
810811
enableBuiltInMetrics = builder.enableBuiltInMetrics;
811812
enableEndToEndTracing = builder.enableEndToEndTracing;
812813
monitoringHost = builder.monitoringHost;
813-
String externalHostTokenPath = System.getenv("SPANNER_EXTERNAL_HOST_AUTH_TOKEN");
814-
if (builder.isExternalHost && builder.emulatorHost == null && externalHostTokenPath != null) {
815-
String token;
816-
try {
817-
token =
818-
Base64.getEncoder()
819-
.encodeToString(Files.readAllBytes(Paths.get(externalHostTokenPath)));
820-
} catch (IOException e) {
821-
throw SpannerExceptionFactory.newSpannerException(e);
822-
}
823-
credentials = new GoogleCredentials(new AccessToken(token, null));
824-
}
825814
}
826815

827816
/**
@@ -1483,7 +1472,7 @@ public Builder setDecodeMode(DecodeMode decodeMode) {
14831472
@Override
14841473
public Builder setHost(String host) {
14851474
super.setHost(host);
1486-
if (this.emulatorHost == null && !CLOUD_SPANNER_HOST_PATTERN.matcher(host).matches()) {
1475+
if (!CLOUD_SPANNER_HOST_PATTERN.matcher(host).matches()) {
14871476
this.isExternalHost = true;
14881477
}
14891478
// Setting a host should override any SPANNER_EMULATOR_HOST setting.
@@ -1656,6 +1645,19 @@ public SpannerOptions build() {
16561645
this.setChannelConfigurator(ManagedChannelBuilder::usePlaintext);
16571646
// As we are using plain text, we should never send any credentials.
16581647
this.setCredentials(NoCredentials.getInstance());
1648+
} else if (isExternalHost && credentials == null) {
1649+
String externalHostTokenPath = System.getenv("SPANNER_EXTERNAL_HOST_AUTH_TOKEN");
1650+
if (!Strings.isNullOrEmpty(externalHostTokenPath)) {
1651+
String token;
1652+
try {
1653+
token =
1654+
Base64.getEncoder()
1655+
.encodeToString(Files.readAllBytes(Paths.get(externalHostTokenPath)));
1656+
} catch (IOException e) {
1657+
throw SpannerExceptionFactory.newSpannerException(e);
1658+
}
1659+
credentials = GoogleCredentials.create(new AccessToken(token, null));
1660+
}
16591661
}
16601662
if (this.numChannels == null) {
16611663
this.numChannels =

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionOptions.java

+14
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@
8282
import io.opentelemetry.api.OpenTelemetry;
8383
import java.io.IOException;
8484
import java.net.URL;
85+
import java.nio.file.Files;
86+
import java.nio.file.Paths;
8587
import java.time.Duration;
8688
import java.util.ArrayList;
8789
import java.util.Arrays;
90+
import java.util.Base64;
8891
import java.util.Collections;
8992
import java.util.HashMap;
9093
import java.util.HashSet;
@@ -921,6 +924,7 @@ private ConnectionOptions(Builder builder) {
921924
getInitialConnectionPropertyValue(AUTO_CONFIG_EMULATOR),
922925
usePlainText,
923926
System.getenv());
927+
String externalHostTokenPath = System.getenv("SPANNER_EXTERNAL_HOST_AUTH_TOKEN");
924928
// Using credentials on a plain text connection is not allowed, so if the user has not specified
925929
// any credentials and is using a plain text connection, we should not try to get the
926930
// credentials from the environment, but default to NoCredentials.
@@ -935,6 +939,16 @@ && getInitialConnectionPropertyValue(OAUTH_TOKEN) == null
935939
this.credentials =
936940
new GoogleCredentials(
937941
new AccessToken(getInitialConnectionPropertyValue(OAUTH_TOKEN), null));
942+
} else if (isExternalHost && !Strings.isNullOrEmpty(externalHostTokenPath)) {
943+
String token;
944+
try {
945+
token =
946+
Base64.getEncoder()
947+
.encodeToString(Files.readAllBytes(Paths.get(externalHostTokenPath)));
948+
} catch (IOException e) {
949+
throw SpannerExceptionFactory.newSpannerException(e);
950+
}
951+
this.credentials = GoogleCredentials.create(new AccessToken(token, null));
938952
} else if (getInitialConnectionPropertyValue(CREDENTIALS_PROVIDER) != null) {
939953
try {
940954
this.credentials = getInitialConnectionPropertyValue(CREDENTIALS_PROVIDER).getCredentials();

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerPool.java

-3
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,6 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
407407
if (options.getConfigurator() != null) {
408408
options.getConfigurator().configure(builder);
409409
}
410-
if (options.usesEmulator()) {
411-
builder.setEmulatorHost(key.host);
412-
}
413410
return builder.build().getService();
414411
}
415412

0 commit comments

Comments
 (0)