Skip to content

Commit 6ec5a8e

Browse files
committed
chore: create util method for reading token from file
1 parent 996e26f commit 6ec5a8e

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,15 @@ default boolean isEnableEndToEndTracing() {
855855
default String getMonitoringHost() {
856856
return null;
857857
}
858+
859+
default GoogleCredentials getDefaultExternalHostCredentials() {
860+
return null;
861+
}
858862
}
859863

864+
static final String DEFAULT_SPANNER_EXTERNAL_HOST_CREDENTIALS =
865+
"SPANNER_EXTERNAL_HOST_AUTH_TOKEN";
866+
860867
/**
861868
* Default implementation of {@link SpannerEnvironment}. Reads all configuration from environment
862869
* variables.
@@ -912,6 +919,11 @@ public boolean isEnableEndToEndTracing() {
912919
public String getMonitoringHost() {
913920
return System.getenv(SPANNER_MONITORING_HOST);
914921
}
922+
923+
@Override
924+
public GoogleCredentials getDefaultExternalHostCredentials() {
925+
return getOAuthTokenFromFile(System.getenv(DEFAULT_SPANNER_EXTERNAL_HOST_CREDENTIALS));
926+
}
915927
}
916928

917929
/** Builder for {@link SpannerOptions} instances. */
@@ -1646,18 +1658,7 @@ public SpannerOptions build() {
16461658
// As we are using plain text, we should never send any credentials.
16471659
this.setCredentials(NoCredentials.getInstance());
16481660
} 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-
}
1661+
credentials = environment.getDefaultExternalHostCredentials();
16611662
}
16621663
if (this.numChannels == null) {
16631664
this.numChannels =
@@ -1698,6 +1699,24 @@ public static void useDefaultEnvironment() {
16981699
SpannerOptions.environment = SpannerEnvironmentImpl.INSTANCE;
16991700
}
17001701

1702+
@InternalApi
1703+
public static GoogleCredentials getDefaultExternalHostCredentialsFromSysEnv() {
1704+
return getOAuthTokenFromFile(System.getenv(DEFAULT_SPANNER_EXTERNAL_HOST_CREDENTIALS));
1705+
}
1706+
1707+
private static @Nullable GoogleCredentials getOAuthTokenFromFile(@Nullable String file) {
1708+
if (!Strings.isNullOrEmpty(file)) {
1709+
String token;
1710+
try {
1711+
token = Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(file)));
1712+
} catch (IOException e) {
1713+
throw SpannerExceptionFactory.newSpannerException(e);
1714+
}
1715+
return GoogleCredentials.create(new AccessToken(token, null));
1716+
}
1717+
return null;
1718+
}
1719+
17011720
/**
17021721
* Enables OpenTelemetry traces. Enabling OpenTelemetry traces will disable OpenCensus traces. By
17031722
* default, OpenCensus traces are enabled.

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,9 @@
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;
8785
import java.time.Duration;
8886
import java.util.ArrayList;
8987
import java.util.Arrays;
90-
import java.util.Base64;
9188
import java.util.Collections;
9289
import java.util.HashMap;
9390
import java.util.HashSet;
@@ -924,7 +921,8 @@ private ConnectionOptions(Builder builder) {
924921
getInitialConnectionPropertyValue(AUTO_CONFIG_EMULATOR),
925922
usePlainText,
926923
System.getenv());
927-
String externalHostTokenPath = System.getenv("SPANNER_EXTERNAL_HOST_AUTH_TOKEN");
924+
GoogleCredentials defaultExternalHostCredentials =
925+
SpannerOptions.getDefaultExternalHostCredentialsFromSysEnv();
928926
// Using credentials on a plain text connection is not allowed, so if the user has not specified
929927
// any credentials and is using a plain text connection, we should not try to get the
930928
// credentials from the environment, but default to NoCredentials.
@@ -939,16 +937,8 @@ && getInitialConnectionPropertyValue(OAUTH_TOKEN) == null
939937
this.credentials =
940938
new GoogleCredentials(
941939
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));
940+
} else if (isExternalHost && defaultExternalHostCredentials != null) {
941+
this.credentials = defaultExternalHostCredentials;
952942
} else if (getInitialConnectionPropertyValue(CREDENTIALS_PROVIDER) != null) {
953943
try {
954944
this.credentials = getInitialConnectionPropertyValue(CREDENTIALS_PROVIDER).getCredentials();

0 commit comments

Comments
 (0)