|
58 | 58 | import com.google.common.annotations.VisibleForTesting;
|
59 | 59 | import com.google.common.base.MoreObjects;
|
60 | 60 | import com.google.common.base.Preconditions;
|
| 61 | +import com.google.common.base.Strings; |
61 | 62 | import com.google.common.collect.ImmutableMap;
|
62 | 63 | import com.google.common.collect.ImmutableSet;
|
63 | 64 | import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
@@ -810,18 +811,6 @@ protected SpannerOptions(Builder builder) {
|
810 | 811 | enableBuiltInMetrics = builder.enableBuiltInMetrics;
|
811 | 812 | enableEndToEndTracing = builder.enableEndToEndTracing;
|
812 | 813 | 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 |
| - } |
825 | 814 | }
|
826 | 815 |
|
827 | 816 | /**
|
@@ -1483,7 +1472,7 @@ public Builder setDecodeMode(DecodeMode decodeMode) {
|
1483 | 1472 | @Override
|
1484 | 1473 | public Builder setHost(String host) {
|
1485 | 1474 | super.setHost(host);
|
1486 |
| - if (this.emulatorHost == null && !CLOUD_SPANNER_HOST_PATTERN.matcher(host).matches()) { |
| 1475 | + if (!CLOUD_SPANNER_HOST_PATTERN.matcher(host).matches()) { |
1487 | 1476 | this.isExternalHost = true;
|
1488 | 1477 | }
|
1489 | 1478 | // Setting a host should override any SPANNER_EMULATOR_HOST setting.
|
@@ -1656,6 +1645,19 @@ public SpannerOptions build() {
|
1656 | 1645 | this.setChannelConfigurator(ManagedChannelBuilder::usePlaintext);
|
1657 | 1646 | // As we are using plain text, we should never send any credentials.
|
1658 | 1647 | 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 | + } |
1659 | 1661 | }
|
1660 | 1662 | if (this.numChannels == null) {
|
1661 | 1663 | this.numChannels =
|
|
0 commit comments