diff --git a/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java b/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java index 8cad9f0383..fd23b11e16 100644 --- a/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java +++ b/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java @@ -123,6 +123,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP private final HeaderProvider headerProvider; private final boolean useS2A; private final String endpoint; + private final String mtlsEndpoint; // TODO: remove. envProvider currently provides DirectPath environment variable, and is only used // during initial rollout for DirectPath. This provider will be removed once the DirectPath // environment is not used. @@ -152,6 +153,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) { this.executor = builder.executor; this.headerProvider = builder.headerProvider; this.endpoint = builder.endpoint; + this.mtlsEndpoint = builder.mtlsEndpoint; this.useS2A = builder.useS2A; this.mtlsProvider = builder.mtlsProvider; this.s2aConfigProvider = builder.s2aConfigProvider; @@ -243,6 +245,21 @@ public TransportChannelProvider withEndpoint(String endpoint) { return toBuilder().setEndpoint(endpoint).build(); } + /** + * Specify the mtlsEndpoint the channel should connect to. + * + *

The value of {@code mtlsEndpoint} must be of the form {@code host:port}. + * + * @param mtlsEndpoint The mtlsEndpoint to connect to + * @return A new {@link InstantiatingGrpcChannelProvider} with the specified mtlsEndpoint + * configured + */ + @Override + public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) { + validateEndpoint(mtlsEndpoint); + return toBuilder().setMtlsEndpoint(mtlsEndpoint).build(); + } + /** * Specify whether or not to use S2A. * @@ -590,8 +607,7 @@ private ManagedChannel createSingleChannel() throws IOException { } if (channelCredentials != null) { // Create the channel using S2A-secured channel credentials. - // {@code endpoint} is set to mtlsEndpoint in {@link EndpointContext} when useS2A is true. - builder = Grpc.newChannelBuilder(endpoint, channelCredentials); + builder = Grpc.newChannelBuilder(mtlsEndpoint, channelCredentials); } else { // Use default if we cannot initialize channel credentials via DCA or S2A. builder = ManagedChannelBuilder.forAddress(serviceAddress, port); @@ -743,6 +759,7 @@ public static final class Builder { private Executor executor; private HeaderProvider headerProvider; private String endpoint; + private String mtlsEndpoint; private boolean useS2A; private EnvironmentProvider envProvider; private SecureSessionAgent s2aConfigProvider = SecureSessionAgent.create(); @@ -773,6 +790,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) { this.executor = provider.executor; this.headerProvider = provider.headerProvider; this.endpoint = provider.endpoint; + this.mtlsEndpoint = provider.mtlsEndpoint; this.useS2A = provider.useS2A; this.envProvider = provider.envProvider; this.interceptorProvider = provider.interceptorProvider; @@ -843,6 +861,13 @@ public Builder setEndpoint(String endpoint) { return this; } + /** Sets the mtlsEndpoint used to reach the service, eg "localhost:8080". */ + public Builder setMtlsEndpoint(String mtlsEndpoint) { + validateEndpoint(mtlsEndpoint); + this.mtlsEndpoint = mtlsEndpoint; + return this; + } + Builder setUseS2A(boolean useS2A) { this.useS2A = useS2A; return this; @@ -876,6 +901,10 @@ public String getEndpoint() { return endpoint; } + public String getMtlsEndpoint() { + return mtlsEndpoint; + } + /** The maximum message size allowed to be received on the channel. */ public Builder setMaxInboundMessageSize(Integer max) { this.maxInboundMessageSize = max; diff --git a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcLongRunningTest.java b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcLongRunningTest.java index ac88e4acec..f0fc4278c3 100644 --- a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcLongRunningTest.java +++ b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcLongRunningTest.java @@ -103,6 +103,8 @@ void setUp() throws IOException { when(operationsChannelProvider.getTransportChannel()).thenReturn(transportChannel); when(operationsChannelProvider.withUseS2A(Mockito.any(boolean.class))) .thenReturn(operationsChannelProvider); + when(operationsChannelProvider.withMtlsEndpoint(Mockito.any(String.class))) + .thenReturn(operationsChannelProvider); clock = new FakeApiClock(0L); executor = RecordingScheduler.create(clock); diff --git a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/testing/LocalChannelProvider.java b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/testing/LocalChannelProvider.java index 856a2850bb..30ae54dd11 100644 --- a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/testing/LocalChannelProvider.java +++ b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/testing/LocalChannelProvider.java @@ -106,6 +106,11 @@ public TransportChannelProvider withEndpoint(String endpoint) { throw new UnsupportedOperationException("LocalChannelProvider doesn't need an endpoint"); } + @Override + public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) { + throw new UnsupportedOperationException("LocalChannelProvider doesn't need an mtlsEndpoint"); + } + @Override public TransportChannelProvider withUseS2A(boolean useS2A) { // Overriden for technical reasons. This method is a no-op for LocalChannelProvider. diff --git a/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/InstantiatingHttpJsonChannelProvider.java b/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/InstantiatingHttpJsonChannelProvider.java index 170b955c2a..237b1ab82c 100644 --- a/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/InstantiatingHttpJsonChannelProvider.java +++ b/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/InstantiatingHttpJsonChannelProvider.java @@ -124,6 +124,11 @@ public TransportChannelProvider withEndpoint(String endpoint) { return toBuilder().setEndpoint(endpoint).build(); } + @Override + public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) { + return this; + } + @Override public TransportChannelProvider withUseS2A(boolean useS2A) { return this; diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 8e7c9a3090..df17330846 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -222,6 +222,8 @@ public static ClientContext create(StubSettings settings) throws IOException { if (transportChannelProvider.needsEndpoint()) { transportChannelProvider = transportChannelProvider.withEndpoint(endpoint); } + transportChannelProvider = + transportChannelProvider.withMtlsEndpoint(endpointContext.mtlsEndpoint()); transportChannelProvider = transportChannelProvider.withUseS2A(endpointContext.useS2A()); TransportChannel transportChannel = transportChannelProvider.getTransportChannel(); diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java index 0148c07a01..9b89082df7 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java @@ -272,10 +272,6 @@ private String determineUniverseDomain() { /** Determines the fully resolved endpoint and universe domain values */ private String determineEndpoint() throws IOException { - if (shouldUseS2A()) { - return mtlsEndpoint(); - } - MtlsProvider mtlsProvider = mtlsProvider() == null ? new MtlsProvider() : mtlsProvider(); // TransportChannelProvider's endpoint will override the ClientSettings' endpoint String customEndpoint = diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/FixedTransportChannelProvider.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/FixedTransportChannelProvider.java index 2f70c06b5f..9105137ebb 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/FixedTransportChannelProvider.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/FixedTransportChannelProvider.java @@ -89,6 +89,12 @@ public TransportChannelProvider withEndpoint(String endpoint) { "FixedTransportChannelProvider doesn't need an endpoint"); } + @Override + public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) { + throw new UnsupportedOperationException( + "FixedTransportChannelProvider doesn't need an mtlsEndpoint"); + } + @Override public TransportChannelProvider withUseS2A(boolean useS2A) throws UnsupportedOperationException { // Overriden for technical reasons. This method is a no-op for FixedTransportChannelProvider. diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/TransportChannelProvider.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/TransportChannelProvider.java index f58acffc54..7e476f5a79 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/TransportChannelProvider.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/TransportChannelProvider.java @@ -97,6 +97,9 @@ public interface TransportChannelProvider { */ TransportChannelProvider withEndpoint(String endpoint); + /** Sets the mtlsEndpoint to use when constructing a new {@link TransportChannel}. */ + TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint); + /** Sets whether to use S2A when constructing a new {@link TransportChannel}. */ default TransportChannelProvider withUseS2A(boolean useS2A) { throw new UnsupportedOperationException("S2A is not supported"); diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index facc93ed86..217b95aee8 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -195,6 +195,17 @@ public TransportChannelProvider withEndpoint(String endpoint) { endpoint); } + @Override + public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) { + return new FakeTransportProvider( + this.transport, + this.executor, + this.shouldAutoClose, + this.headers, + this.credentials, + this.endpoint); + } + @Override public TransportChannelProvider withUseS2A(boolean useS2A) { return new FakeTransportProvider(