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,20 @@ public TransportChannelProvider withEndpoint(String endpoint) {
225
240
return toBuilder ().setEndpoint (endpoint ).build ();
226
241
}
227
242
243
+ /**
244
+ * Specify the MTLS endpoint.
245
+ *
246
+ * <p>The value of {@code mtlsEndpoint} must be of the form {@code host:port}.
247
+ *
248
+ * @param mtlsEndpoint
249
+ * @return A new {@link InstantiatingGrpcChannelProvider} with the specified MTLS endpoint
250
+ * configured
251
+ */
252
+ public TransportChannelProvider withMtlsEndpoint (String mtlsEndpoint ) {
253
+ validateEndpoint (mtlsEndpoint );
254
+ return toBuilder ().setMtlsEndpoint (mtlsEndpoint ).build ();
255
+ }
256
+
228
257
/** @deprecated Please modify pool settings via {@link #toBuilder()} */
229
258
@ Deprecated
230
259
@ Override
@@ -410,6 +439,75 @@ ChannelCredentials createMtlsChannelCredentials() throws IOException, GeneralSec
410
439
return null ;
411
440
}
412
441
442
+ private boolean isGoogleS2AEnabled () {
443
+ String S2AEnv = envProvider .getenv (S2A_ENV_ENABLE_USE_S2A );
444
+ boolean isS2AEnv = Boolean .parseBoolean (S2AEnv );
445
+ if (isS2AEnv ) {
446
+ return true ;
447
+ }
448
+ return false ;
449
+ }
450
+
451
+ @ VisibleForTesting
452
+ boolean shouldUseS2A () {
453
+ // If EXPERIMENTAL_GOOGLE_API_USE_S2A is not set to true, skip S2A.
454
+ if (!isGoogleS2AEnabled ()) {
455
+ return false ;
456
+ }
457
+ // If {@link mtlsEndpoint} is not set, skip S2A. S2A is also skipped when there is endpoint
458
+ // override. Endpoint override is respected when the {@link endpoint} is resolved via AIP#4114,
459
+ // see EndpointContext.java
460
+ if (endpoint != mtlsEndpoint ) {
461
+ return false ;
462
+ }
463
+ return true ;
464
+ }
465
+
466
+ @ VisibleForTesting
467
+ ChannelCredentials createMtlsToS2AChannelCredentials () throws IOException {
468
+ if (!isOnComputeEngine ()) {
469
+ // Currently, MTLS to MDS is only available on GCE. See:
470
+ // https://cloud.google.com/compute/docs/metadata/overview#https-mds
471
+ return null ;
472
+ }
473
+ File privateKeyFile = new File (MTLS_MDS_CERT_CHAIN_AND_KEY );
474
+ File certChainFile = new File (MTLS_MDS_CERT_CHAIN_AND_KEY );
475
+ File trustBundleFile = new File (MTLS_MDS_ROOT );
476
+ if (!privateKeyFile .isFile () || !certChainFile .isFile () || !trustBundleFile .isFile ()) {
477
+ return null ;
478
+ }
479
+ return TlsChannelCredentials .newBuilder ()
480
+ .keyManager (privateKeyFile , certChainFile )
481
+ .trustManager (trustBundleFile )
482
+ .build ();
483
+ }
484
+
485
+ @ VisibleForTesting
486
+ ChannelCredentials createS2ASecuredChannelCredentials () {
487
+ S2A s2aUtils = S2A .newBuilder ().build ();
488
+ String plaintextAddress = s2aUtils .getPlaintextS2AAddress ();
489
+ String mtlsAddress = s2aUtils .getMtlsS2AAddress ();
490
+ if (!mtlsAddress .isEmpty ()) {
491
+ try {
492
+ // Try to connect to S2A using mTLS.
493
+ ChannelCredentials mtlsToS2AChannelCredentials = createMtlsToS2AChannelCredentials ();
494
+ if (mtlsToS2AChannelCredentials != null ) {
495
+ return S2AChannelCredentials .newBuilder (mtlsAddress , mtlsToS2AChannelCredentials ).build ();
496
+ }
497
+ } catch (IOException ignore ) {
498
+ // Fallback to plaintext connection to S2A.
499
+ }
500
+ }
501
+
502
+ if (!plaintextAddress .isEmpty ()) {
503
+ // Fallback to plaintext connection to S2A.
504
+ return S2AChannelCredentials .newBuilder (plaintextAddress , InsecureChannelCredentials .create ())
505
+ .build ();
506
+ }
507
+
508
+ return null ;
509
+ }
510
+
413
511
private ManagedChannel createSingleChannel () throws IOException {
414
512
GrpcHeaderInterceptor headerInterceptor =
415
513
new GrpcHeaderInterceptor (headersWithDuplicatesRemoved );
@@ -447,16 +545,30 @@ private ManagedChannel createSingleChannel() throws IOException {
447
545
builder .keepAliveTime (DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS , TimeUnit .SECONDS );
448
546
builder .keepAliveTimeout (DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS , TimeUnit .SECONDS );
449
547
} else {
548
+ // Try and create credentials via DCA. See https://google.aip.dev/auth/4114.
450
549
ChannelCredentials channelCredentials ;
451
550
try {
452
551
channelCredentials = createMtlsChannelCredentials ();
453
552
} catch (GeneralSecurityException e ) {
454
553
throw new IOException (e );
455
554
}
456
555
if (channelCredentials != null ) {
556
+ // Create the channel using channel credentials created via DCA.
457
557
builder = Grpc .newChannelBuilder (endpoint , channelCredentials );
458
558
} else {
459
- builder = ManagedChannelBuilder .forAddress (serviceAddress , port );
559
+ // Could not create channel credentials via DCA. In accordance with
560
+ // https://google.aip.dev/auth/4115, if credentials not available through
561
+ // DCA, try mTLS with credentials held by the S2A (Secure Session Agent).
562
+ if (shouldUseS2A ()) {
563
+ channelCredentials = createS2ASecuredChannelCredentials ();
564
+ }
565
+ if (channelCredentials != null ) {
566
+ // Create the channel using S2A-secured channel credentials.
567
+ builder = Grpc .newChannelBuilder (mtlsEndpoint , channelCredentials );
568
+ } else {
569
+ // Use default if we cannot initialize channel credentials via DCA or S2A.
570
+ builder = ManagedChannelBuilder .forAddress (serviceAddress , port );
571
+ }
460
572
}
461
573
}
462
574
// google-c2p resolver requires service config lookup
@@ -547,6 +659,11 @@ public String getEndpoint() {
547
659
return endpoint ;
548
660
}
549
661
662
+ /** The mTLS endpoint. */
663
+ public String getMtlsEndpoint () {
664
+ return mtlsEndpoint ;
665
+ }
666
+
550
667
/** This method is obsolete. Use {@link #getKeepAliveTimeDuration()} instead. */
551
668
@ ObsoleteApi ("Use getKeepAliveTimeDuration() instead" )
552
669
public org .threeten .bp .Duration getKeepAliveTime () {
@@ -604,6 +721,7 @@ public static final class Builder {
604
721
private Executor executor ;
605
722
private HeaderProvider headerProvider ;
606
723
private String endpoint ;
724
+ private String mtlsEndpoint ;
607
725
private EnvironmentProvider envProvider ;
608
726
private MtlsProvider mtlsProvider = new MtlsProvider ();
609
727
@ Nullable private GrpcInterceptorProvider interceptorProvider ;
@@ -632,6 +750,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
632
750
this .executor = provider .executor ;
633
751
this .headerProvider = provider .headerProvider ;
634
752
this .endpoint = provider .endpoint ;
753
+ this .mtlsEndpoint = provider .mtlsEndpoint ;
635
754
this .envProvider = provider .envProvider ;
636
755
this .interceptorProvider = provider .interceptorProvider ;
637
756
this .maxInboundMessageSize = provider .maxInboundMessageSize ;
@@ -700,6 +819,12 @@ public Builder setEndpoint(String endpoint) {
700
819
return this ;
701
820
}
702
821
822
+ public Builder setMtlsEndpoint (String mtlsEndpoint ) {
823
+ validateEndpoint (mtlsEndpoint );
824
+ this .mtlsEndpoint = mtlsEndpoint ;
825
+ return this ;
826
+ }
827
+
703
828
@ VisibleForTesting
704
829
Builder setMtlsProvider (MtlsProvider mtlsProvider ) {
705
830
this .mtlsProvider = mtlsProvider ;
@@ -722,6 +847,10 @@ public String getEndpoint() {
722
847
return endpoint ;
723
848
}
724
849
850
+ public String getMtlsEndpoint () {
851
+ return mtlsEndpoint ;
852
+ }
853
+
725
854
/** The maximum message size allowed to be received on the channel. */
726
855
public Builder setMaxInboundMessageSize (Integer max ) {
727
856
this .maxInboundMessageSize = max ;
0 commit comments