46
46
import com .google .auth .ApiKeyCredentials ;
47
47
import com .google .auth .Credentials ;
48
48
import com .google .auth .oauth2 .ComputeEngineCredentials ;
49
+ import com .google .auth .oauth2 .S2A ;
49
50
import com .google .common .annotations .VisibleForTesting ;
50
51
import com .google .common .base .Preconditions ;
51
52
import com .google .common .collect .ImmutableList ;
54
55
import io .grpc .CallCredentials ;
55
56
import io .grpc .ChannelCredentials ;
56
57
import io .grpc .Grpc ;
58
+ import io .grpc .InsecureChannelCredentials ;
57
59
import io .grpc .ManagedChannel ;
58
60
import io .grpc .ManagedChannelBuilder ;
59
61
import io .grpc .TlsChannelCredentials ;
60
62
import io .grpc .alts .GoogleDefaultChannelCredentials ;
61
63
import io .grpc .auth .MoreCallCredentials ;
64
+ import io .grpc .s2a .S2AChannelCredentials ;
62
65
import java .io .File ;
63
66
import java .io .IOException ;
64
67
import java .nio .charset .StandardCharsets ;
@@ -99,6 +102,12 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
99
102
@ VisibleForTesting
100
103
static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS" ;
101
104
105
+ private static final String S2A_ENV_ENABLE_USE_S2A = "EXPERIMENTAL_GOOGLE_API_USE_S2A" ;
106
+ private static final String MTLS_MDS_ROOT = "/run/google-mds-mtls/root.crt" ;
107
+ // The mTLS MDS credentials are formatted as the concatenation of a PEM-encoded certificate chain
108
+ // followed by a PEM-encoded private key.
109
+ private static final String MTLS_MDS_CERT_CHAIN_AND_KEY = "/run/google-mds-mtls/client.key" ;
110
+
102
111
static final long DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS = 3600 ;
103
112
static final long DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS = 20 ;
104
113
static final String GCE_PRODUCTION_NAME_PRIOR_2016 = "Google" ;
@@ -108,6 +117,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
108
117
private final Executor executor ;
109
118
private final HeaderProvider headerProvider ;
110
119
private final String endpoint ;
120
+ private final String mtlsEndpoint ;
111
121
// TODO: remove. envProvider currently provides DirectPath environment variable, and is only used
112
122
// during initial rollout for DirectPath. This provider will be removed once the DirectPath
113
123
// environment is not used.
@@ -136,6 +146,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
136
146
this .executor = builder .executor ;
137
147
this .headerProvider = builder .headerProvider ;
138
148
this .endpoint = builder .endpoint ;
149
+ this .mtlsEndpoint = builder .mtlsEndpoint ;
139
150
this .mtlsProvider = builder .mtlsProvider ;
140
151
this .envProvider = builder .envProvider ;
141
152
this .interceptorProvider = builder .interceptorProvider ;
@@ -211,6 +222,10 @@ public boolean needsEndpoint() {
211
222
return endpoint == null ;
212
223
}
213
224
225
+ public boolean needsMtlsEndpoint () {
226
+ return mtlsEndpoint == null ;
227
+ }
228
+
214
229
/**
215
230
* Specify the endpoint the channel should connect to.
216
231
*
@@ -225,6 +240,17 @@ public TransportChannelProvider withEndpoint(String endpoint) {
225
240
return toBuilder ().setEndpoint (endpoint ).build ();
226
241
}
227
242
243
+ /**
244
+ * Specify the MTLS endpoint the channel should connect to.
245
+ *
246
+ * @param mtlsEndpoint
247
+ * @return A new {@link InstantiatingGrpcChannelProvider} with the specified MTLS endpoint
248
+ * configured
249
+ */
250
+ public TransportChannelProvider withMtlsEndpoint (String mtlsEndpoint ) {
251
+ return toBuilder ().setMtlsEndpoint (mtlsEndpoint ).build ();
252
+ }
253
+
228
254
/** @deprecated Please modify pool settings via {@link #toBuilder()} */
229
255
@ Deprecated
230
256
@ Override
@@ -410,6 +436,76 @@ ChannelCredentials createMtlsChannelCredentials() throws IOException, GeneralSec
410
436
return null ;
411
437
}
412
438
439
+ @ VisibleForTesting
440
+ boolean isGoogleS2AEnabled () {
441
+ String S2AEnv = envProvider .getenv (S2A_ENV_ENABLE_USE_S2A );
442
+ boolean isS2AEnv = Boolean .parseBoolean (S2AEnv );
443
+ if (isS2AEnv ) {
444
+ return true ;
445
+ }
446
+ return false ;
447
+ }
448
+
449
+ @ VisibleForTesting
450
+ boolean shouldUseS2A () {
451
+ // If EXPERIMENTAL_GOOGLE_API_USE_S2A is not set to true, skip S2A.
452
+ if (!isGoogleS2AEnabled ()) {
453
+ return false ;
454
+ }
455
+ // If {@link mtlsEndpoint} is not set, skip S2A. S2A is also skipped when there is endpoint
456
+ // override. Endpoint override is respected when the {@link endpoint} is resolved via AIP#4114,
457
+ // see EndpointContext.java
458
+ if (endpoint != mtlsEndpoint ) {
459
+ return false ;
460
+ }
461
+ return true ;
462
+ }
463
+
464
+ @ VisibleForTesting
465
+ ChannelCredentials createMtlsToS2AChannelCredentials () throws IOException {
466
+ if (!isOnComputeEngine ()) {
467
+ // Currently, MTLS to MDS is only available on GCE. See:
468
+ // https://cloud.google.com/compute/docs/metadata/overview#https-mds
469
+ return null ;
470
+ }
471
+ File privateKeyFile = new File (MTLS_MDS_CERT_CHAIN_AND_KEY );
472
+ File certChainFile = new File (MTLS_MDS_CERT_CHAIN_AND_KEY );
473
+ File trustBundleFile = new File (MTLS_MDS_ROOT );
474
+ if (!privateKeyFile .isFile () || !certChainFile .isFile () || !trustBundleFile .isFile ()) {
475
+ return null ;
476
+ }
477
+ return TlsChannelCredentials .newBuilder ()
478
+ .keyManager (privateKeyFile , certChainFile )
479
+ .trustManager (trustBundleFile )
480
+ .build ();
481
+ }
482
+
483
+ @ VisibleForTesting
484
+ ChannelCredentials createS2ASecuredChannelCredentials () {
485
+ S2A s2aUtils = S2A .newBuilder ().build ();
486
+ String plaintextAddress = s2aUtils .getPlaintextS2AAddress ();
487
+ String mtlsAddress = s2aUtils .getMtlsS2AAddress ();
488
+ if (!mtlsAddress .isEmpty ()) {
489
+ try {
490
+ // Try to connect to S2A using mTLS.
491
+ ChannelCredentials mtlsToS2AChannelCredentials = createMtlsToS2AChannelCredentials ();
492
+ if (mtlsToS2AChannelCredentials != null ) {
493
+ return S2AChannelCredentials .newBuilder (mtlsAddress , mtlsToS2AChannelCredentials ).build ();
494
+ }
495
+ } catch (IOException ignore ) {
496
+ // Fallback to plaintext connection to S2A.
497
+ }
498
+ }
499
+
500
+ if (!plaintextAddress .isEmpty ()) {
501
+ // Fallback to plaintext connection to S2A.
502
+ return S2AChannelCredentials .newBuilder (plaintextAddress , InsecureChannelCredentials .create ())
503
+ .build ();
504
+ }
505
+
506
+ return null ;
507
+ }
508
+
413
509
private ManagedChannel createSingleChannel () throws IOException {
414
510
GrpcHeaderInterceptor headerInterceptor =
415
511
new GrpcHeaderInterceptor (headersWithDuplicatesRemoved );
@@ -447,16 +543,30 @@ private ManagedChannel createSingleChannel() throws IOException {
447
543
builder .keepAliveTime (DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS , TimeUnit .SECONDS );
448
544
builder .keepAliveTimeout (DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS , TimeUnit .SECONDS );
449
545
} else {
546
+ // Try and create credentials via DCA. See https://google.aip.dev/auth/4114.
450
547
ChannelCredentials channelCredentials ;
451
548
try {
452
549
channelCredentials = createMtlsChannelCredentials ();
453
550
} catch (GeneralSecurityException e ) {
454
551
throw new IOException (e );
455
552
}
456
553
if (channelCredentials != null ) {
554
+ // Create the channel using channel credentials created via DCA.
457
555
builder = Grpc .newChannelBuilder (endpoint , channelCredentials );
458
556
} else {
459
- builder = ManagedChannelBuilder .forAddress (serviceAddress , port );
557
+ // Could not create channel credentials via DCA. In accordance with
558
+ // https://google.aip.dev/auth/4115, if credentials not available through
559
+ // DCA, try mTLS with credentials held by the S2A (Secure Session Agent).
560
+ if (shouldUseS2A ()) {
561
+ channelCredentials = createS2ASecuredChannelCredentials ();
562
+ }
563
+ if (channelCredentials != null ) {
564
+ // Create the channel using S2A-secured channel credentials.
565
+ builder = Grpc .newChannelBuilder (mtlsEndpoint , channelCredentials );
566
+ } else {
567
+ // Use default if we cannot initialize channel credentials via DCA or S2A.
568
+ builder = ManagedChannelBuilder .forAddress (serviceAddress , port );
569
+ }
460
570
}
461
571
}
462
572
// google-c2p resolver requires service config lookup
@@ -604,6 +714,7 @@ public static final class Builder {
604
714
private Executor executor ;
605
715
private HeaderProvider headerProvider ;
606
716
private String endpoint ;
717
+ private String mtlsEndpoint ;
607
718
private EnvironmentProvider envProvider ;
608
719
private MtlsProvider mtlsProvider = new MtlsProvider ();
609
720
@ Nullable private GrpcInterceptorProvider interceptorProvider ;
@@ -632,6 +743,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
632
743
this .executor = provider .executor ;
633
744
this .headerProvider = provider .headerProvider ;
634
745
this .endpoint = provider .endpoint ;
746
+ this .mtlsEndpoint = provider .mtlsEndpoint ;
635
747
this .envProvider = provider .envProvider ;
636
748
this .interceptorProvider = provider .interceptorProvider ;
637
749
this .maxInboundMessageSize = provider .maxInboundMessageSize ;
@@ -700,6 +812,11 @@ public Builder setEndpoint(String endpoint) {
700
812
return this ;
701
813
}
702
814
815
+ public Builder setMtlsEndpoint (String mtlsEndpoint ) {
816
+ this .mtlsEndpoint = mtlsEndpoint ;
817
+ return this ;
818
+ }
819
+
703
820
@ VisibleForTesting
704
821
Builder setMtlsProvider (MtlsProvider mtlsProvider ) {
705
822
this .mtlsProvider = mtlsProvider ;
0 commit comments