Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions alts/src/main/java/io/grpc/alts/HandshakerServiceChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.grpc.alts;

import com.google.common.base.MoreObjects;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
Expand All @@ -38,13 +37,25 @@
* application will have at most one connection to the handshaker service.
*/
final class HandshakerServiceChannel {

private static final int ALTS_PORT = 8080;
private static final String DEFAULT_TARGET = "metadata.google.internal.:8080";

static final Resource<Channel> SHARED_HANDSHAKER_CHANNEL =
new ChannelResource(
MoreObjects.firstNonNull(
System.getenv("GCE_METADATA_HOST"), "metadata.google.internal.:8080"));


new ChannelResource(getHandshakerTarget(System.getenv("GCE_METADATA_HOST")));

static String getHandshakerTarget(String envValue) {
if (envValue == null || envValue.isEmpty()) {
return DEFAULT_TARGET;
}
String host = envValue;
int portIndex = host.lastIndexOf(':');
if (portIndex != -1) {
host = host.substring(0, portIndex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding the port is surprising. There should be some comment or such about what is going on.

(From what I can tell, the environment variable is normally accessing port 80. But for ALTS we use a different port, so we want to ignore the port because isn't the same port as used for authentication tokens.)

}
return host + ":" + ALTS_PORT;
}

/** Returns a resource of handshaker service channel for testing only. */
static Resource<Channel> getHandshakerChannelForTesting(String handshakerAddress) {
return new ChannelResource(handshakerAddress);
Expand Down
18 changes: 18 additions & 0 deletions alts/src/test/java/io/grpc/alts/HandshakerServiceChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public void sharedChannel_authority() {
}
}

@Test
public void getHandshakerTarget_nullEnvVar() {
assertThat(HandshakerServiceChannel.getHandshakerTarget(null))
.isEqualTo("metadata.google.internal.:8080");
}

@Test
public void getHandshakerTarget_envVarWithPort() {
assertThat(HandshakerServiceChannel.getHandshakerTarget("169.254.169.254:80"))
.isEqualTo("169.254.169.254:8080");
}

@Test
public void getHandshakerTarget_envVarWithHostOnly() {
assertThat(HandshakerServiceChannel.getHandshakerTarget("169.254.169.254"))
.isEqualTo("169.254.169.254:8080");
}

@Test
public void resource_works() {
Channel channel = resource.create();
Expand Down