Skip to content

Commit a7af12e

Browse files
committed
update javadocs + add mtlsServiceAddress.
1 parent f9eef5b commit a7af12e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
107107
@VisibleForTesting
108108
static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS";
109109

110+
// The public portion of the mTLS MDS root certificate is stored for performing
111+
// cert verification when establishing an mTLS connection with the MDS.
110112
private static final String MTLS_MDS_ROOT = "/run/google-mds-mtls/root.crt";
111113
// The mTLS MDS credentials are formatted as the concatenation of a PEM-encoded certificate chain
112114
// followed by a PEM-encoded private key.
@@ -463,7 +465,9 @@ ChannelCredentials createPlaintextToS2AChannelCredentials(String plaintextAddres
463465
* use the {@code mtlsAddress} address to reach S2A if it is non-empty and the MTLS-MDS
464466
* credentials can successfully be discovered and used to create {@link TlsChannelCredentials}. If
465467
* there is any failure using mTLS-to-S2A, fallback to using a plaintext connection to S2A using
466-
* the {@code plaintextAddress}.
468+
* the {@code plaintextAddress}. If {@code plaintextAddress} is not available, this function
469+
* returns null; in this case S2A will not be used, and a TLS connection to the service will be
470+
* established.
467471
*
468472
* @return {@link ChannelCredentials} configured to use S2A to create mTLS connection to
469473
* mtlsEndpoint.
@@ -524,6 +528,14 @@ private ManagedChannel createSingleChannel() throws IOException {
524528
int port = Integer.parseInt(endpoint.substring(colon + 1));
525529
String serviceAddress = endpoint.substring(0, colon);
526530

531+
int mtlsColon = endpointContext.mtlsEndpoint().lastIndexOf(':');
532+
if (mtlsColon < 0) {
533+
throw new IllegalStateException(
534+
"invalid mtlsEndpoint - should have been validated: " + endpointContext.mtlsEndpoint());
535+
}
536+
int mtlsPort = Integer.parseInt(endpointContext.mtlsEndpoint().substring(mtlsColon + 1));
537+
String mtlsServiceAddress = endpointContext.mtlsEndpoint().substring(0, mtlsColon);
538+
527539
ManagedChannelBuilder<?> builder;
528540

529541
// Check DirectPath traffic.
@@ -566,7 +578,7 @@ private ManagedChannel createSingleChannel() throws IOException {
566578
}
567579
if (channelCredentials != null) {
568580
// Create the channel using S2A-secured channel credentials.
569-
builder = Grpc.newChannelBuilder(endpointContext.mtlsEndpoint(), channelCredentials);
581+
builder = Grpc.newChannelBuilder(mtlsServiceAddress, channelCredentials);
570582
} else {
571583
// Use default if we cannot initialize channel credentials via DCA or S2A.
572584
builder = ManagedChannelBuilder.forAddress(serviceAddress, port);

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686

8787
class InstantiatingGrpcChannelProviderTest extends AbstractMtlsTransportChannelTest {
8888
private static final String DEFAULT_ENDPOINT = "test.googleapis.com:443";
89+
private static final String DEFAULT_MTLS_ENDPOINT = "test.mtls.googleapis.com:443";
8990
private static final String API_KEY_HEADER_VALUE = "fake_api_key_2";
9091
private static final String API_KEY_AUTH_HEADER_KEY = "x-goog-api-key";
9192
private static String originalOSName;
@@ -205,6 +206,7 @@ void testWithPoolSize() throws IOException {
205206
executor.shutdown();
206207
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
207208
Mockito.when(endpointContext.useS2A()).thenReturn(false);
209+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
208210
TransportChannelProvider provider =
209211
InstantiatingGrpcChannelProvider.newBuilder()
210212
.build()
@@ -272,6 +274,7 @@ private void testWithInterceptors(int numChannels) throws Exception {
272274

273275
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
274276
Mockito.when(endpointContext.useS2A()).thenReturn(false);
277+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
275278

276279
InstantiatingGrpcChannelProvider channelProvider =
277280
InstantiatingGrpcChannelProvider.newBuilder()
@@ -309,6 +312,7 @@ void testChannelConfigurator() throws IOException {
309312

310313
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
311314
Mockito.when(endpointContext.useS2A()).thenReturn(false);
315+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
312316

313317
// Invoke the provider
314318
InstantiatingGrpcChannelProvider.newBuilder()
@@ -334,6 +338,7 @@ void testWithGCECredentials() throws IOException {
334338

335339
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
336340
Mockito.when(endpointContext.useS2A()).thenReturn(false);
341+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
337342

338343
TransportChannelProvider provider =
339344
InstantiatingGrpcChannelProvider.newBuilder()
@@ -419,6 +424,7 @@ void testWithNonGCECredentials() throws IOException {
419424

420425
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
421426
Mockito.when(endpointContext.useS2A()).thenReturn(false);
427+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
422428

423429
TransportChannelProvider provider =
424430
InstantiatingGrpcChannelProvider.newBuilder()
@@ -451,6 +457,7 @@ void testWithDirectPathDisabled() throws IOException {
451457

452458
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
453459
Mockito.when(endpointContext.useS2A()).thenReturn(false);
460+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
454461

455462
TransportChannelProvider provider =
456463
InstantiatingGrpcChannelProvider.newBuilder()
@@ -483,6 +490,7 @@ void testWithNoDirectPathFlagSet() throws IOException {
483490

484491
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
485492
Mockito.when(endpointContext.useS2A()).thenReturn(false);
493+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
486494

487495
TransportChannelProvider provider =
488496
InstantiatingGrpcChannelProvider.newBuilder()
@@ -507,6 +515,7 @@ void testWithIPv6Address() throws IOException {
507515

508516
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
509517
Mockito.when(endpointContext.useS2A()).thenReturn(false);
518+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
510519

511520
TransportChannelProvider provider =
512521
InstantiatingGrpcChannelProvider.newBuilder()
@@ -526,6 +535,7 @@ void testWithIPv6Address() throws IOException {
526535
void testWithPrimeChannel() throws IOException {
527536
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
528537
Mockito.when(endpointContext.useS2A()).thenReturn(false);
538+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
529539
// create channelProvider with different pool sizes to verify ChannelPrimer is called the
530540
// correct number of times
531541
for (int poolSize = 1; poolSize < 5; poolSize++) {
@@ -659,6 +669,7 @@ private void createAndCloseTransportChannel(InstantiatingGrpcChannelProvider pro
659669
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
660670
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
661671
Mockito.when(endpointContext.useS2A()).thenReturn(false);
672+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
662673
InstantiatingGrpcChannelProvider provider =
663674
createChannelProviderBuilderForDirectPathLogTests()
664675
.setAttemptDirectPathXds()
@@ -706,6 +717,7 @@ void testLogDirectPathMisconfigWrongCredential() throws Exception {
706717
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
707718
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
708719
Mockito.when(endpointContext.useS2A()).thenReturn(false);
720+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
709721
InstantiatingGrpcChannelProvider provider =
710722
InstantiatingGrpcChannelProvider.newBuilder()
711723
.setAttemptDirectPathXds()
@@ -734,6 +746,7 @@ void testLogDirectPathMisconfigNotOnGCE() throws Exception {
734746
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
735747
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
736748
Mockito.when(endpointContext.useS2A()).thenReturn(false);
749+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
737750
InstantiatingGrpcChannelProvider provider =
738751
InstantiatingGrpcChannelProvider.newBuilder()
739752
.setAttemptDirectPathXds()
@@ -766,13 +779,16 @@ public void canUseDirectPath_happyPath() throws IOException {
766779
envProvider.getenv(
767780
InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH))
768781
.thenReturn("false");
782+
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
783+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
769784
InstantiatingGrpcChannelProvider.Builder builder =
770785
InstantiatingGrpcChannelProvider.newBuilder()
771786
.setAttemptDirectPath(true)
772787
.setCredentials(computeEngineCredentials)
773788
.setEndpoint(DEFAULT_ENDPOINT)
774789
.setEnvProvider(envProvider)
775-
.setHeaderProvider(Mockito.mock(HeaderProvider.class));
790+
.setHeaderProvider(Mockito.mock(HeaderProvider.class))
791+
.setEndpointContext(endpointContext);
776792
InstantiatingGrpcChannelProvider provider =
777793
new InstantiatingGrpcChannelProvider(builder, GCE_PRODUCTION_NAME_AFTER_2016);
778794
Truth.assertThat(provider.canUseDirectPath()).isTrue();
@@ -793,6 +809,7 @@ public void canUseDirectPath_directPathEnvVarDisabled() throws IOException {
793809
.thenReturn("true");
794810
EndpointContext endpointContext = Mockito.mock(EndpointContext.class);
795811
Mockito.when(endpointContext.useS2A()).thenReturn(false);
812+
Mockito.when(endpointContext.mtlsEndpoint()).thenReturn(DEFAULT_MTLS_ENDPOINT);
796813
InstantiatingGrpcChannelProvider.Builder builder =
797814
InstantiatingGrpcChannelProvider.newBuilder()
798815
.setAttemptDirectPath(true)

0 commit comments

Comments
 (0)