Skip to content

Commit e2d5bad

Browse files
authored
alts: Metadata server address modification to account for default port
Fixing the utilization of the GCE Metadata host server address environment variable to account for the case where the user does not specify a port (defaults to port 8080) b/451639946
1 parent 2f64092 commit e2d5bad

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

alts/src/main/java/io/grpc/alts/HandshakerServiceChannel.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.grpc.alts;
1818

19-
import com.google.common.base.MoreObjects;
2019
import io.grpc.CallOptions;
2120
import io.grpc.Channel;
2221
import io.grpc.ClientCall;
@@ -38,13 +37,29 @@
3837
* application will have at most one connection to the handshaker service.
3938
*/
4039
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";
4143

4244
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+
4863
/** Returns a resource of handshaker service channel for testing only. */
4964
static Resource<Channel> getHandshakerChannelForTesting(String handshakerAddress) {
5065
return new ChannelResource(handshakerAddress);

alts/src/test/java/io/grpc/alts/HandshakerServiceChannelTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ public void sharedChannel_authority() {
6767
}
6868
}
6969

70+
@Test
71+
public void getHandshakerTarget_nullEnvVar() {
72+
assertThat(HandshakerServiceChannel.getHandshakerTarget(null))
73+
.isEqualTo("metadata.google.internal.:8080");
74+
}
75+
76+
@Test
77+
public void getHandshakerTarget_envVarWithPort() {
78+
assertThat(HandshakerServiceChannel.getHandshakerTarget("169.254.169.254:80"))
79+
.isEqualTo("169.254.169.254:8080");
80+
}
81+
82+
@Test
83+
public void getHandshakerTarget_envVarWithHostOnly() {
84+
assertThat(HandshakerServiceChannel.getHandshakerTarget("169.254.169.254"))
85+
.isEqualTo("169.254.169.254:8080");
86+
}
87+
7088
@Test
7189
public void resource_works() {
7290
Channel channel = resource.create();

0 commit comments

Comments
 (0)