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
+ import com .google .auth .oauth2 .SecureSessionAgent ;
50
+ import com .google .auth .oauth2 .SecureSessionAgentConfig ;
50
51
import com .google .common .annotations .VisibleForTesting ;
51
52
import com .google .common .base .Preconditions ;
52
53
import com .google .common .base .Strings ;
@@ -140,6 +141,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
140
141
@ Nullable private final Boolean allowNonDefaultServiceAccount ;
141
142
@ VisibleForTesting final ImmutableMap <String , ?> directPathServiceConfig ;
142
143
@ Nullable private final MtlsProvider mtlsProvider ;
144
+ @ Nullable private final SecureSessionAgent s2aConfigProvider ;
143
145
@ VisibleForTesting final Map <String , String > headersWithDuplicatesRemoved = new HashMap <>();
144
146
145
147
@ Nullable
@@ -152,6 +154,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
152
154
this .endpoint = builder .endpoint ;
153
155
this .useS2A = builder .useS2A ;
154
156
this .mtlsProvider = builder .mtlsProvider ;
157
+ this .s2aConfigProvider = builder .s2aConfigProvider ;
155
158
this .envProvider = builder .envProvider ;
156
159
this .interceptorProvider = builder .interceptorProvider ;
157
160
this .maxInboundMessageSize = builder .maxInboundMessageSize ;
@@ -492,11 +495,14 @@ ChannelCredentials createPlaintextToS2AChannelCredentials(String plaintextAddres
492
495
* mtlsEndpoint.
493
496
*/
494
497
ChannelCredentials createS2ASecuredChannelCredentials () {
495
- S2A s2aUtils = S2A . newBuilder (). build ();
496
- String plaintextAddress = s2aUtils . getPlaintextS2AAddress ();
497
- String mtlsAddress = s2aUtils . getMtlsS2AAddress ();
498
+ SecureSessionAgentConfig config = s2aConfigProvider . getConfig ();
499
+ String plaintextAddress = config . getPlaintextAddress ();
500
+ String mtlsAddress = config . getMtlsAddress ();
498
501
if (Strings .isNullOrEmpty (mtlsAddress )) {
499
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." );
500
506
return createPlaintextToS2AChannelCredentials (plaintextAddress );
501
507
}
502
508
// Currently, MTLS to MDS is only available on GCE. See:
@@ -523,7 +529,7 @@ ChannelCredentials createS2ASecuredChannelCredentials() {
523
529
// Fallback to plaintext-to-S2A connection if MTLS-MDS creds do not exist.
524
530
LOG .log (
525
531
Level .INFO ,
526
- "Cannot establish an mTLS connection to S2A MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A" );
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" );
527
533
return createPlaintextToS2AChannelCredentials (plaintextAddress );
528
534
}
529
535
}
@@ -739,6 +745,7 @@ public static final class Builder {
739
745
private String endpoint ;
740
746
private boolean useS2A ;
741
747
private EnvironmentProvider envProvider ;
748
+ private SecureSessionAgent s2aConfigProvider = SecureSessionAgent .create ();
742
749
private MtlsProvider mtlsProvider = new MtlsProvider ();
743
750
@ Nullable private GrpcInterceptorProvider interceptorProvider ;
744
751
@ Nullable private Integer maxInboundMessageSize ;
@@ -783,6 +790,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
783
790
this .allowNonDefaultServiceAccount = provider .allowNonDefaultServiceAccount ;
784
791
this .directPathServiceConfig = provider .directPathServiceConfig ;
785
792
this .mtlsProvider = provider .mtlsProvider ;
793
+ this .s2aConfigProvider = provider .s2aConfigProvider ;
786
794
}
787
795
788
796
/**
@@ -846,6 +854,12 @@ Builder setMtlsProvider(MtlsProvider mtlsProvider) {
846
854
return this ;
847
855
}
848
856
857
+ @ VisibleForTesting
858
+ Builder setS2AConfigProvider (SecureSessionAgent s2aConfigProvider ) {
859
+ this .s2aConfigProvider = s2aConfigProvider ;
860
+ return this ;
861
+ }
862
+
849
863
/**
850
864
* Sets the GrpcInterceptorProvider for this TransportChannelProvider.
851
865
*
0 commit comments