|
16 | 16 |
|
17 | 17 | package io.grpc.alts; |
18 | 18 |
|
19 | | -import com.google.common.base.MoreObjects; |
20 | 19 | import io.grpc.CallOptions; |
21 | 20 | import io.grpc.Channel; |
22 | 21 | import io.grpc.ClientCall; |
|
38 | 37 | * application will have at most one connection to the handshaker service. |
39 | 38 | */ |
40 | 39 | final class HandshakerServiceChannel { |
| 40 | + // Port 8080 is necessary for ALTS handshake. |
| 41 | + private static final int ALTS_PORT = 8080; |
| 42 | + private static final String DEFAULT_TARGET = "metadata.google.internal.:8080"; |
41 | 43 |
|
42 | 44 | static final Resource<Channel> SHARED_HANDSHAKER_CHANNEL = |
43 | | - new ChannelResource( |
44 | | - MoreObjects.firstNonNull( |
45 | | - System.getenv("GCE_METADATA_HOST"), "metadata.google.internal.:8080")); |
46 | | - |
47 | | - |
| 45 | + new ChannelResource(getHandshakerTarget(System.getenv("GCE_METADATA_HOST"))); |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns handshaker target. When GCE_METADATA_HOST is provided, it might contain port which we |
| 49 | + * will discard and use ALTS_PORT instead. |
| 50 | + */ |
| 51 | + static String getHandshakerTarget(String envValue) { |
| 52 | + if (envValue == null || envValue.isEmpty()) { |
| 53 | + return DEFAULT_TARGET; |
| 54 | + } |
| 55 | + String host = envValue; |
| 56 | + int portIndex = host.lastIndexOf(':'); |
| 57 | + if (portIndex != -1) { |
| 58 | + host = host.substring(0, portIndex); // Discard port if specified |
| 59 | + } |
| 60 | + return host + ":" + ALTS_PORT; // Utilize ALTS port in all cases |
| 61 | + } |
| 62 | + |
48 | 63 | /** Returns a resource of handshaker service channel for testing only. */ |
49 | 64 | static Resource<Channel> getHandshakerChannelForTesting(String handshakerAddress) { |
50 | 65 | return new ChannelResource(handshakerAddress); |
|
0 commit comments