Skip to content

Commit ffa4e7b

Browse files
committedJan 7, 2025
1 parent 9b4b405 commit ffa4e7b

File tree

9 files changed

+109
-2
lines changed

9 files changed

+109
-2
lines changed
 

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

+36-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ 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;
126127
// TODO: remove. envProvider currently provides DirectPath environment variable, and is only used
127128
// during initial rollout for DirectPath. This provider will be removed once the DirectPath
128129
// environment is not used.
@@ -153,6 +154,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
153154
this.headerProvider = builder.headerProvider;
154155
this.useS2A = builder.useS2A;
155156
this.endpoint = builder.endpoint;
157+
this.mtlsEndpoint = builder.mtlsEndpoint;
156158
this.mtlsProvider = builder.mtlsProvider;
157159
this.s2aConfigProvider = builder.s2aConfigProvider;
158160
this.envProvider = builder.envProvider;
@@ -229,6 +231,11 @@ public boolean needsEndpoint() {
229231
return endpoint == null;
230232
}
231233

234+
@Override
235+
public boolean needsMtlsEndpoint() {
236+
return mtlsEndpoint == null;
237+
}
238+
232239
/**
233240
* Specify the endpoint the channel should connect to.
234241
*
@@ -243,6 +250,21 @@ public TransportChannelProvider withEndpoint(String endpoint) {
243250
return toBuilder().setEndpoint(endpoint).build();
244251
}
245252

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+
246268
/**
247269
* Specify whether or not to use S2A.
248270
*
@@ -623,8 +645,7 @@ private ManagedChannel createSingleChannel() throws IOException {
623645
}
624646
if (channelCredentials != null) {
625647
// Create the channel using S2A-secured channel credentials.
626-
// {@code endpoint} is set to mtlsEndpoint in {@link EndpointContext} when useS2A is true.
627-
builder = Grpc.newChannelBuilder(endpoint, channelCredentials);
648+
builder = Grpc.newChannelBuilder(mtlsEndpoint, channelCredentials);
628649
} else {
629650
// Use default if we cannot initialize channel credentials via DCA or S2A.
630651
builder = ManagedChannelBuilder.forAddress(serviceAddress, port);
@@ -777,6 +798,7 @@ public static final class Builder {
777798
private Executor executor;
778799
private HeaderProvider headerProvider;
779800
private String endpoint;
801+
private String mtlsEndpoint;
780802
private boolean useS2A;
781803
private EnvironmentProvider envProvider;
782804
private SecureSessionAgent s2aConfigProvider = SecureSessionAgent.create();
@@ -807,6 +829,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
807829
this.executor = provider.executor;
808830
this.headerProvider = provider.headerProvider;
809831
this.endpoint = provider.endpoint;
832+
this.mtlsEndpoint = provider.mtlsEndpoint;
810833
this.useS2A = provider.useS2A;
811834
this.envProvider = provider.envProvider;
812835
this.interceptorProvider = provider.interceptorProvider;
@@ -877,11 +900,22 @@ public Builder setEndpoint(String endpoint) {
877900
return this;
878901
}
879902

903+
/** Sets the mtlsEndpoint used to reach the service, eg "localhost:8080". */
904+
public Builder setMtlsEndpoint(String mtlsEndpoint) {
905+
validateEndpoint(mtlsEndpoint);
906+
this.mtlsEndpoint = mtlsEndpoint;
907+
return this;
908+
}
909+
880910
Builder setUseS2A(boolean useS2A) {
881911
this.useS2A = useS2A;
882912
return this;
883913
}
884914

915+
public String getMtlsEndpoint() {
916+
return mtlsEndpoint;
917+
}
918+
885919
@VisibleForTesting
886920
Builder setMtlsProvider(MtlsProvider mtlsProvider) {
887921
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,6 +103,8 @@ 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);
106108

107109
clock = new FakeApiClock(0L);
108110
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,11 +101,21 @@ public boolean needsEndpoint() {
101101
return false;
102102
}
103103

104+
@Override
105+
public boolean needsMtlsEndpoint() {
106+
return false;
107+
}
108+
104109
@Override
105110
public TransportChannelProvider withEndpoint(String endpoint) {
106111
throw new UnsupportedOperationException("LocalChannelProvider doesn't need an endpoint");
107112
}
108113

114+
@Override
115+
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
116+
throw new UnsupportedOperationException("LocalChannelProvider doesn't need an mtlsEndpoint");
117+
}
118+
109119
@Override
110120
public TransportChannelProvider withUseS2A(boolean useS2A) {
111121
// 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,11 +119,21 @@ public boolean needsEndpoint() {
119119
return endpoint == null;
120120
}
121121

122+
@Override
123+
public boolean needsMtlsEndpoint() {
124+
return false;
125+
}
126+
122127
@Override
123128
public TransportChannelProvider withEndpoint(String endpoint) {
124129
return toBuilder().setEndpoint(endpoint).build();
125130
}
126131

132+
@Override
133+
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
134+
return this;
135+
}
136+
127137
@Override
128138
public TransportChannelProvider withUseS2A(boolean useS2A) {
129139
return this;

‎gax-java/gax/clirr-ignored-differences.xml

+10
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,14 @@
118118
<className>com/google/api/gax/rpc/TransportChannelProvider</className>
119119
<method>* withUseS2A(*)</method>
120120
</difference>
121+
<difference>
122+
<differenceType>7012</differenceType>
123+
<className>com/google/api/gax/rpc/TransportChannelProvider</className>
124+
<method>* needsMtlsEndpoint()</method>
125+
</difference>
126+
<difference>
127+
<differenceType>7012</differenceType>
128+
<className>com/google/api/gax/rpc/TransportChannelProvider</className>
129+
<method>* withMtlsEndpoint(*)</method>
130+
</difference>
121131
</differences>

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

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ 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+
}
225229
transportChannelProvider = transportChannelProvider.withUseS2A(endpointContext.useS2A());
226230
TransportChannel transportChannel = transportChannelProvider.getTransportChannel();
227231

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

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

86+
@Override
87+
public boolean needsMtlsEndpoint() {
88+
return false;
89+
}
90+
8691
@Override
8792
public TransportChannelProvider withEndpoint(String endpoint) {
8893
throw new UnsupportedOperationException(
8994
"FixedTransportChannelProvider doesn't need an endpoint");
9095
}
9196

97+
@Override
98+
public TransportChannelProvider withMtlsEndpoint(String mtlsEndpoint) {
99+
throw new UnsupportedOperationException(
100+
"FixedTransportChannelProvider doesn't need an mtlsEndpoint");
101+
}
102+
92103
@Override
93104
public TransportChannelProvider withUseS2A(boolean useS2A) throws UnsupportedOperationException {
94105
// 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,13 +90,23 @@ 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+
9396
/**
9497
* Sets the endpoint to use when constructing a new {@link TransportChannel}.
9598
*
9699
* <p>This method should only be called if {@link #needsEndpoint()} returns true.
97100
*/
98101
TransportChannelProvider withEndpoint(String endpoint);
99102

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+
100110
/** Sets whether to use S2A when constructing a new {@link TransportChannel}. */
101111
default TransportChannelProvider withUseS2A(boolean useS2A) {
102112
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,6 +179,11 @@ public boolean needsEndpoint() {
179179
return true;
180180
}
181181

182+
@Override
183+
public boolean needsMtlsEndpoint() {
184+
return false;
185+
}
186+
182187
@Override
183188
public String getEndpoint() {
184189
return endpoint;
@@ -195,6 +200,17 @@ public TransportChannelProvider withEndpoint(String endpoint) {
195200
endpoint);
196201
}
197202

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+
198214
@Override
199215
public TransportChannelProvider withUseS2A(boolean useS2A) {
200216
return new FakeTransportProvider(

0 commit comments

Comments
 (0)
Please sign in to comment.