Skip to content

Commit 25445d3

Browse files
committed
set mtls endpoint for S2A in EndpointContext.
1 parent 882047c commit 25445d3

File tree

10 files changed

+21
-101
lines changed

10 files changed

+21
-101
lines changed

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

+2-38
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
123123
private final HeaderProvider headerProvider;
124124
private final boolean useS2A;
125125
private final String endpoint;
126-
private final String mtlsEndpoint;
127126
// TODO: remove. envProvider currently provides DirectPath environment variable, and is only used
128127
// during initial rollout for DirectPath. This provider will be removed once the DirectPath
129128
// environment is not used.
@@ -154,7 +153,6 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
154153
this.headerProvider = builder.headerProvider;
155154
this.useS2A = builder.useS2A;
156155
this.endpoint = builder.endpoint;
157-
this.mtlsEndpoint = builder.mtlsEndpoint;
158156
this.mtlsProvider = builder.mtlsProvider;
159157
this.s2aConfigProvider = builder.s2aConfigProvider;
160158
this.envProvider = builder.envProvider;
@@ -231,11 +229,6 @@ public boolean needsEndpoint() {
231229
return endpoint == null;
232230
}
233231

234-
@Override
235-
public boolean needsMtlsEndpoint() {
236-
return mtlsEndpoint == null;
237-
}
238-
239232
/**
240233
* Specify the endpoint the channel should connect to.
241234
*
@@ -250,21 +243,6 @@ public TransportChannelProvider withEndpoint(String endpoint) {
250243
return toBuilder().setEndpoint(endpoint).build();
251244
}
252245

253-
/**
254-
* Specify the mtlsEndpoint the channel should connect to.
255-
*
256-
* <p>The value of {@code mtlsEndpoint} must be of the form {@code host:port}.
257-
*
258-
* @param mtlsEndpoint The mtlsEndpoint to connect to
259-
* @return A new {@link InstantiatingGrpcChannelProvider} with the specified mtlsEndpoint
260-
* configured
261-
*/
262-
@Override
263-
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
264-
validateEndpoint(mtlsEndpoint);
265-
return toBuilder().setMtlsEndpoint(mtlsEndpoint).build();
266-
}
267-
268246
/**
269247
* Specify whether or not to use S2A.
270248
*
@@ -549,8 +527,7 @@ ChannelCredentials createPlaintextToS2AChannelCredentials(String plaintextAddres
549527
* returns null; in this case S2A will not be used, and a TLS connection to the service will be
550528
* established.
551529
*
552-
* @return {@link ChannelCredentials} configured to use S2A to create mTLS connection to
553-
* mtlsEndpoint.
530+
* @return {@link ChannelCredentials} configured to use S2A to create mTLS connection.
554531
*/
555532
ChannelCredentials createS2ASecuredChannelCredentials() {
556533
SecureSessionAgentConfig config = s2aConfigProvider.getConfig();
@@ -648,7 +625,7 @@ private ManagedChannel createSingleChannel() throws IOException {
648625
}
649626
if (channelCredentials != null) {
650627
// Create the channel using S2A-secured channel credentials.
651-
builder = Grpc.newChannelBuilder(mtlsEndpoint, channelCredentials);
628+
builder = Grpc.newChannelBuilder(endpoint, channelCredentials);
652629
} else {
653630
// Use default if we cannot initialize channel credentials via DCA or S2A.
654631
builder = ManagedChannelBuilder.forAddress(serviceAddress, port);
@@ -800,7 +777,6 @@ public static final class Builder {
800777
private Executor executor;
801778
private HeaderProvider headerProvider;
802779
private String endpoint;
803-
private String mtlsEndpoint;
804780
private boolean useS2A;
805781
private EnvironmentProvider envProvider;
806782
private SecureSessionAgent s2aConfigProvider = SecureSessionAgent.create();
@@ -831,7 +807,6 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
831807
this.executor = provider.executor;
832808
this.headerProvider = provider.headerProvider;
833809
this.endpoint = provider.endpoint;
834-
this.mtlsEndpoint = provider.mtlsEndpoint;
835810
this.useS2A = provider.useS2A;
836811
this.envProvider = provider.envProvider;
837812
this.interceptorProvider = provider.interceptorProvider;
@@ -902,22 +877,11 @@ public Builder setEndpoint(String endpoint) {
902877
return this;
903878
}
904879

905-
/** Sets the mtlsEndpoint used to reach the service, eg "localhost:8080". */
906-
public Builder setMtlsEndpoint(String mtlsEndpoint) {
907-
validateEndpoint(mtlsEndpoint);
908-
this.mtlsEndpoint = mtlsEndpoint;
909-
return this;
910-
}
911-
912880
Builder setUseS2A(boolean useS2A) {
913881
this.useS2A = useS2A;
914882
return this;
915883
}
916884

917-
public String getMtlsEndpoint() {
918-
return mtlsEndpoint;
919-
}
920-
921885
@VisibleForTesting
922886
Builder setMtlsProvider(MtlsProvider mtlsProvider) {
923887
this.mtlsProvider = mtlsProvider;

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcLongRunningTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ void setUp() throws IOException {
103103
when(operationsChannelProvider.getTransportChannel()).thenReturn(transportChannel);
104104
when(operationsChannelProvider.withUseS2A(Mockito.any(boolean.class)))
105105
.thenReturn(operationsChannelProvider);
106-
when(operationsChannelProvider.withMtlsEndpoint(Mockito.any(String.class)))
107-
.thenReturn(operationsChannelProvider);
108106

109107
clock = new FakeApiClock(0L);
110108
executor = RecordingScheduler.create(clock);

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/testing/LocalChannelProvider.java

-10
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,11 @@ public boolean needsEndpoint() {
101101
return false;
102102
}
103103

104-
@Override
105-
public boolean needsMtlsEndpoint() {
106-
return false;
107-
}
108-
109104
@Override
110105
public TransportChannelProvider withEndpoint(String endpoint) {
111106
throw new UnsupportedOperationException("LocalChannelProvider doesn't need an endpoint");
112107
}
113108

114-
@Override
115-
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
116-
throw new UnsupportedOperationException("LocalChannelProvider doesn't need an mtlsEndpoint");
117-
}
118-
119109
@Override
120110
public TransportChannelProvider withUseS2A(boolean useS2A) {
121111
// Overriden for technical reasons. This method is a no-op for LocalChannelProvider.

gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/InstantiatingHttpJsonChannelProvider.java

-10
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,11 @@ public boolean needsEndpoint() {
119119
return endpoint == null;
120120
}
121121

122-
@Override
123-
public boolean needsMtlsEndpoint() {
124-
return false;
125-
}
126-
127122
@Override
128123
public TransportChannelProvider withEndpoint(String endpoint) {
129124
return toBuilder().setEndpoint(endpoint).build();
130125
}
131126

132-
@Override
133-
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
134-
return this;
135-
}
136-
137127
@Override
138128
public TransportChannelProvider withUseS2A(boolean useS2A) {
139129
return this;

gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

-4
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,6 @@ public static ClientContext create(StubSettings settings) throws IOException {
222222
if (transportChannelProvider.needsEndpoint()) {
223223
transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
224224
}
225-
if (transportChannelProvider.needsMtlsEndpoint()) {
226-
transportChannelProvider =
227-
transportChannelProvider.withMtlsEndpoint(endpointContext.mtlsEndpoint());
228-
}
229225
transportChannelProvider = transportChannelProvider.withUseS2A(endpointContext.useS2A());
230226
TransportChannel transportChannel = transportChannelProvider.getTransportChannel();
231227

gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java

+4
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ private String determineEndpoint() throws IOException {
303303
"mTLS is not supported in any universe other than googleapis.com");
304304
}
305305

306+
if (shouldUseS2A()) {
307+
return mtlsEndpoint();
308+
}
309+
306310
return endpoint;
307311
}
308312

gax-java/gax/src/main/java/com/google/api/gax/rpc/FixedTransportChannelProvider.java

-11
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,12 @@ public boolean needsEndpoint() {
8383
return false;
8484
}
8585

86-
@Override
87-
public boolean needsMtlsEndpoint() {
88-
return false;
89-
}
90-
9186
@Override
9287
public TransportChannelProvider withEndpoint(String endpoint) {
9388
throw new UnsupportedOperationException(
9489
"FixedTransportChannelProvider doesn't need an endpoint");
9590
}
9691

97-
@Override
98-
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
99-
throw new UnsupportedOperationException(
100-
"FixedTransportChannelProvider doesn't need an mtlsEndpoint");
101-
}
102-
10392
@Override
10493
public TransportChannelProvider withUseS2A(boolean useS2A) throws UnsupportedOperationException {
10594
// Overriden for technical reasons. This method is a no-op for FixedTransportChannelProvider.

gax-java/gax/src/main/java/com/google/api/gax/rpc/TransportChannelProvider.java

-10
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,13 @@ public interface TransportChannelProvider {
9090
/** True if the TransportProvider has no endpoint set. */
9191
boolean needsEndpoint();
9292

93-
/** True if the TransportProvider has no mtlsEndpoint set. */
94-
boolean needsMtlsEndpoint();
95-
9693
/**
9794
* Sets the endpoint to use when constructing a new {@link TransportChannel}.
9895
*
9996
* <p>This method should only be called if {@link #needsEndpoint()} returns true.
10097
*/
10198
TransportChannelProvider withEndpoint(String endpoint);
10299

103-
/**
104-
* Sets the mtlsEndpoint to use when constructing a new {@link TransportChannel}.
105-
*
106-
* <p>This method should only be called if {@link #needsMtlsEndpoint()} returns true.
107-
*/
108-
TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint);
109-
110100
/** Sets whether to use S2A when constructing a new {@link TransportChannel}. */
111101
default TransportChannelProvider withUseS2A(boolean useS2A) {
112102
throw new UnsupportedOperationException("S2A is not supported");

gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java

-16
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,6 @@ public boolean needsEndpoint() {
179179
return true;
180180
}
181181

182-
@Override
183-
public boolean needsMtlsEndpoint() {
184-
return false;
185-
}
186-
187182
@Override
188183
public String getEndpoint() {
189184
return endpoint;
@@ -200,17 +195,6 @@ public TransportChannelProvider withEndpoint(String endpoint) {
200195
endpoint);
201196
}
202197

203-
@Override
204-
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
205-
return new FakeTransportProvider(
206-
this.transport,
207-
this.executor,
208-
this.shouldAutoClose,
209-
this.headers,
210-
this.credentials,
211-
this.endpoint);
212-
}
213-
214198
@Override
215199
public TransportChannelProvider withUseS2A(boolean useS2A) {
216200
return new FakeTransportProvider(

gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ void endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHas
373373
.isEqualTo(clientSettingsUniverseDomain);
374374
}
375375

376+
@Test
377+
void endpointContextBuild_shouldUseS2A_mtlsEndpoint() throws IOException {
378+
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
379+
Mockito.when(envProvider.getenv(EndpointContext.S2A_ENV_ENABLE_USE_S2A)).thenReturn("true");
380+
defaultEndpointContextBuilder =
381+
defaultEndpointContextBuilder
382+
.setEnvProvider(envProvider)
383+
.setClientSettingsEndpoint("")
384+
.setTransportChannelProviderEndpoint("")
385+
.setUsingGDCH(false);
386+
EndpointContext endpointContext = defaultEndpointContextBuilder.build();
387+
Truth.assertThat(defaultEndpointContextBuilder.shouldUseS2A()).isTrue();
388+
Truth.assertThat(endpointContext.resolvedEndpoint()).isEqualTo(DEFAULT_MTLS_ENDPOINT);
389+
}
390+
376391
@Test
377392
void hasValidUniverseDomain_gdchFlow_anyCredentials() throws IOException {
378393
Credentials noCredentials = NoCredentialsProvider.create().getCredentials();

0 commit comments

Comments
 (0)