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 .SecureSessionAgent ;
50
- import com .google .auth .oauth2 .SecureSessionAgentConfig ;
51
49
import com .google .common .annotations .VisibleForTesting ;
52
50
import com .google .common .base .Preconditions ;
53
- import com .google .common .base .Strings ;
54
51
import com .google .common .collect .ImmutableList ;
55
52
import com .google .common .collect .ImmutableMap ;
56
53
import com .google .common .io .Files ;
57
54
import io .grpc .CallCredentials ;
58
55
import io .grpc .ChannelCredentials ;
59
56
import io .grpc .Grpc ;
60
- import io .grpc .InsecureChannelCredentials ;
61
57
import io .grpc .ManagedChannel ;
62
58
import io .grpc .ManagedChannelBuilder ;
63
59
import io .grpc .TlsChannelCredentials ;
64
60
import io .grpc .alts .GoogleDefaultChannelCredentials ;
65
61
import io .grpc .auth .MoreCallCredentials ;
66
- import io .grpc .s2a .S2AChannelCredentials ;
67
62
import java .io .File ;
68
63
import java .io .IOException ;
69
64
import java .nio .charset .StandardCharsets ;
@@ -104,15 +99,6 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
104
99
@ VisibleForTesting
105
100
static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS" ;
106
101
107
- // The public portion of the mTLS MDS root certificate is stored for performing
108
- // cert verification when establishing an mTLS connection with the MDS. See
109
- // https://cloud.google.com/compute/docs/metadata/overview#https-mds-root-certs
110
- private static final String MTLS_MDS_ROOT_PATH = "/run/google-mds-mtls/root.crt" ;
111
- // The mTLS MDS credentials are formatted as the concatenation of a PEM-encoded certificate chain
112
- // followed by a PEM-encoded private key. See
113
- // https://cloud.google.com/compute/docs/metadata/overview#https-mds-client-certs
114
- private static final String MTLS_MDS_CERT_CHAIN_AND_KEY_PATH = "/run/google-mds-mtls/client.key" ;
115
-
116
102
static final long DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS = 3600 ;
117
103
static final long DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS = 20 ;
118
104
static final String GCE_PRODUCTION_NAME_PRIOR_2016 = "Google" ;
@@ -121,7 +107,6 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
121
107
private final int processorCount ;
122
108
private final Executor executor ;
123
109
private final HeaderProvider headerProvider ;
124
- private final boolean useS2A ;
125
110
private final String endpoint ;
126
111
// TODO: remove. envProvider currently provides DirectPath environment variable, and is only used
127
112
// during initial rollout for DirectPath. This provider will be removed once the DirectPath
@@ -141,7 +126,6 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
141
126
@ Nullable private final Boolean allowNonDefaultServiceAccount ;
142
127
@ VisibleForTesting final ImmutableMap <String , ?> directPathServiceConfig ;
143
128
@ Nullable private final MtlsProvider mtlsProvider ;
144
- @ Nullable private final SecureSessionAgent s2aConfigProvider ;
145
129
@ VisibleForTesting final Map <String , String > headersWithDuplicatesRemoved = new HashMap <>();
146
130
147
131
@ Nullable
@@ -152,9 +136,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
152
136
this .executor = builder .executor ;
153
137
this .headerProvider = builder .headerProvider ;
154
138
this .endpoint = builder .endpoint ;
155
- this .useS2A = builder .useS2A ;
156
139
this .mtlsProvider = builder .mtlsProvider ;
157
- this .s2aConfigProvider = builder .s2aConfigProvider ;
158
140
this .envProvider = builder .envProvider ;
159
141
this .interceptorProvider = builder .interceptorProvider ;
160
142
this .maxInboundMessageSize = builder .maxInboundMessageSize ;
@@ -243,17 +225,6 @@ public TransportChannelProvider withEndpoint(String endpoint) {
243
225
return toBuilder ().setEndpoint (endpoint ).build ();
244
226
}
245
227
246
- /**
247
- * Specify whether or not to use S2A.
248
- *
249
- * @param useS2A
250
- * @return A new {@link InstantiatingGrpcChannelProvider} with useS2A set.
251
- */
252
- @ Override
253
- public TransportChannelProvider withUseS2A (boolean useS2A ) {
254
- return toBuilder ().setUseS2A (useS2A ).build ();
255
- }
256
-
257
228
/** @deprecated Please modify pool settings via {@link #toBuilder()} */
258
229
@ Deprecated
259
230
@ Override
@@ -439,101 +410,6 @@ ChannelCredentials createMtlsChannelCredentials() throws IOException, GeneralSec
439
410
return null ;
440
411
}
441
412
442
- /**
443
- * This method creates {@link TlsChannelCredentials} to be used by the client to establish an mTLS
444
- * connection to S2A. Returns null if any of {@param trustBundle}, {@param privateKey} or {@param
445
- * certChain} are missing.
446
- *
447
- * @param trustBundle the trust bundle to be used to establish the client -> S2A mTLS connection
448
- * @param privateKey the client's private key to be used to establish the client -> S2A mtls
449
- * connection
450
- * @param certChain the client's cert chain to be used to establish the client -> S2A mtls
451
- * connection
452
- * @return {@link ChannelCredentials} to use to create an mtls connection between client and S2A
453
- * @throws IOException on error
454
- */
455
- @ VisibleForTesting
456
- ChannelCredentials createMtlsToS2AChannelCredentials (
457
- File trustBundle , File privateKey , File certChain ) throws IOException {
458
- if (trustBundle == null || privateKey == null || certChain == null ) {
459
- return null ;
460
- }
461
- return TlsChannelCredentials .newBuilder ()
462
- .keyManager (privateKey , certChain )
463
- .trustManager (trustBundle )
464
- .build ();
465
- }
466
-
467
- /**
468
- * This method creates {@link ChannelCredentials} to be used by client to establish a plaintext
469
- * connection to S2A. if {@param plaintextAddress} is not present, returns null.
470
- *
471
- * @param plaintextAddress the address to reach S2A which accepts plaintext connections
472
- * @return {@link ChannelCredentials} to use to create a plaintext connection between client and
473
- * S2A
474
- */
475
- ChannelCredentials createPlaintextToS2AChannelCredentials (String plaintextAddress ) {
476
- if (Strings .isNullOrEmpty (plaintextAddress )) {
477
- return null ;
478
- }
479
- return S2AChannelCredentials .newBuilder (plaintextAddress , InsecureChannelCredentials .create ())
480
- .build ();
481
- }
482
-
483
- /**
484
- * This method creates gRPC {@link ChannelCredentials} configured to use S2A to estbalish a mTLS
485
- * connection. First, the address of S2A is discovered by using the {@link S2A} utility to learn
486
- * the {@code mtlsAddress} to reach S2A and the {@code plaintextAddress} to reach S2A. Prefer to
487
- * use the {@code mtlsAddress} address to reach S2A if it is non-empty and the MTLS-MDS
488
- * credentials can successfully be discovered and used to create {@link TlsChannelCredentials}. If
489
- * there is any failure using mTLS-to-S2A, fallback to using a plaintext connection to S2A using
490
- * the {@code plaintextAddress}. If {@code plaintextAddress} is not available, this function
491
- * returns null; in this case S2A will not be used, and a TLS connection to the service will be
492
- * established.
493
- *
494
- * @return {@link ChannelCredentials} configured to use S2A to create mTLS connection to
495
- * mtlsEndpoint.
496
- */
497
- ChannelCredentials createS2ASecuredChannelCredentials () {
498
- SecureSessionAgentConfig config = s2aConfigProvider .getConfig ();
499
- String plaintextAddress = config .getPlaintextAddress ();
500
- String mtlsAddress = config .getMtlsAddress ();
501
- if (Strings .isNullOrEmpty (mtlsAddress )) {
502
- // Fallback to plaintext connection to S2A.
503
- LOG .log (
504
- Level .INFO ,
505
- "Cannot establish an mTLS connection to S2A because autoconfig endpoint did not return a mtls address to reach S2A." );
506
- return createPlaintextToS2AChannelCredentials (plaintextAddress );
507
- }
508
- // Currently, MTLS to MDS is only available on GCE. See:
509
- // https://cloud.google.com/compute/docs/metadata/overview#https-mds
510
- // Try to load MTLS-MDS creds.
511
- File rootFile = new File (MTLS_MDS_ROOT_PATH );
512
- File certKeyFile = new File (MTLS_MDS_CERT_CHAIN_AND_KEY_PATH );
513
- if (rootFile .isFile () && certKeyFile .isFile ()) {
514
- // Try to connect to S2A using mTLS.
515
- ChannelCredentials mtlsToS2AChannelCredentials = null ;
516
- try {
517
- mtlsToS2AChannelCredentials =
518
- createMtlsToS2AChannelCredentials (rootFile , certKeyFile , certKeyFile );
519
- } catch (IOException ignore ) {
520
- // Fallback to plaintext-to-S2A connection on error.
521
- LOG .log (
522
- Level .WARNING ,
523
- "Cannot establish an mTLS connection to S2A due to error creating MTLS to MDS TlsChannelCredentials credentials, falling back to plaintext connection to S2A: "
524
- + ignore .getMessage ());
525
- return createPlaintextToS2AChannelCredentials (plaintextAddress );
526
- }
527
- return S2AChannelCredentials .newBuilder (mtlsAddress , mtlsToS2AChannelCredentials ).build ();
528
- } else {
529
- // Fallback to plaintext-to-S2A connection if MTLS-MDS creds do not exist.
530
- LOG .log (
531
- Level .INFO ,
532
- "Cannot establish an mTLS connection to S2A because MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A" );
533
- return createPlaintextToS2AChannelCredentials (plaintextAddress );
534
- }
535
- }
536
-
537
413
private ManagedChannel createSingleChannel () throws IOException {
538
414
GrpcHeaderInterceptor headerInterceptor =
539
415
new GrpcHeaderInterceptor (headersWithDuplicatesRemoved );
@@ -571,31 +447,16 @@ private ManagedChannel createSingleChannel() throws IOException {
571
447
builder .keepAliveTime (DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS , TimeUnit .SECONDS );
572
448
builder .keepAliveTimeout (DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS , TimeUnit .SECONDS );
573
449
} else {
574
- // Try and create credentials via DCA. See https://google.aip.dev/auth/4114.
575
450
ChannelCredentials channelCredentials ;
576
451
try {
577
452
channelCredentials = createMtlsChannelCredentials ();
578
453
} catch (GeneralSecurityException e ) {
579
454
throw new IOException (e );
580
455
}
581
456
if (channelCredentials != null ) {
582
- // Create the channel using channel credentials created via DCA.
583
457
builder = Grpc .newChannelBuilder (endpoint , channelCredentials );
584
458
} else {
585
- // Could not create channel credentials via DCA. In accordance with
586
- // https://google.aip.dev/auth/4115, if credentials not available through
587
- // DCA, try mTLS with credentials held by the S2A (Secure Session Agent).
588
- if (useS2A ) {
589
- channelCredentials = createS2ASecuredChannelCredentials ();
590
- }
591
- if (channelCredentials != null ) {
592
- // Create the channel using S2A-secured channel credentials.
593
- // {@code endpoint} is set to mtlsEndpoint in {@link EndpointContext} when useS2A is true.
594
- builder = Grpc .newChannelBuilder (endpoint , channelCredentials );
595
- } else {
596
- // Use default if we cannot initialize channel credentials via DCA or S2A.
597
- builder = ManagedChannelBuilder .forAddress (serviceAddress , port );
598
- }
459
+ builder = ManagedChannelBuilder .forAddress (serviceAddress , port );
599
460
}
600
461
}
601
462
// google-c2p resolver requires service config lookup
@@ -743,9 +604,7 @@ public static final class Builder {
743
604
private Executor executor ;
744
605
private HeaderProvider headerProvider ;
745
606
private String endpoint ;
746
- private boolean useS2A ;
747
607
private EnvironmentProvider envProvider ;
748
- private SecureSessionAgent s2aConfigProvider = SecureSessionAgent .create ();
749
608
private MtlsProvider mtlsProvider = new MtlsProvider ();
750
609
@ Nullable private GrpcInterceptorProvider interceptorProvider ;
751
610
@ Nullable private Integer maxInboundMessageSize ;
@@ -773,7 +632,6 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
773
632
this .executor = provider .executor ;
774
633
this .headerProvider = provider .headerProvider ;
775
634
this .endpoint = provider .endpoint ;
776
- this .useS2A = provider .useS2A ;
777
635
this .envProvider = provider .envProvider ;
778
636
this .interceptorProvider = provider .interceptorProvider ;
779
637
this .maxInboundMessageSize = provider .maxInboundMessageSize ;
@@ -790,7 +648,6 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
790
648
this .allowNonDefaultServiceAccount = provider .allowNonDefaultServiceAccount ;
791
649
this .directPathServiceConfig = provider .directPathServiceConfig ;
792
650
this .mtlsProvider = provider .mtlsProvider ;
793
- this .s2aConfigProvider = provider .s2aConfigProvider ;
794
651
}
795
652
796
653
/**
@@ -843,23 +700,12 @@ public Builder setEndpoint(String endpoint) {
843
700
return this ;
844
701
}
845
702
846
- Builder setUseS2A (boolean useS2A ) {
847
- this .useS2A = useS2A ;
848
- return this ;
849
- }
850
-
851
703
@ VisibleForTesting
852
704
Builder setMtlsProvider (MtlsProvider mtlsProvider ) {
853
705
this .mtlsProvider = mtlsProvider ;
854
706
return this ;
855
707
}
856
708
857
- @ VisibleForTesting
858
- Builder setS2AConfigProvider (SecureSessionAgent s2aConfigProvider ) {
859
- this .s2aConfigProvider = s2aConfigProvider ;
860
- return this ;
861
- }
862
-
863
709
/**
864
710
* Sets the GrpcInterceptorProvider for this TransportChannelProvider.
865
711
*
0 commit comments