-
Notifications
You must be signed in to change notification settings - Fork 65
feat: Add experimental S2A integration in client libraries grpc transport #3326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 30 commits
aebd139
b456935
3510643
e799526
70ae8d0
7ef7313
6d98115
250fecc
8469a1a
e880fbe
28adb31
b3e2e06
90892ef
33b710c
b320cc2
035e17e
759c3df
c096cb7
dc4b61e
d4fcc71
393190a
30a37c2
c3b93a0
18a4cf2
0897730
9901656
257c515
e4565f4
33bd7a6
f9eef5b
a7af12e
eb70225
2a50985
943683d
34f030f
fb3d608
926faca
4ee2ee9
2f70bf8
cb5768f
c96c760
30ff3d6
791ab2c
a4ee6c5
6eeccb2
03eb991
91175ef
04d47a0
7d7b233
a42dcbd
56551c6
1ff7a92
2958fb4
c9a7edd
8bf770d
33faba1
924a4e4
b00fd53
ca5be85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import com.google.api.core.InternalApi; | ||
import com.google.api.core.ObsoleteApi; | ||
import com.google.api.gax.core.ExecutorProvider; | ||
import com.google.api.gax.rpc.EndpointContext; | ||
import com.google.api.gax.rpc.FixedHeaderProvider; | ||
import com.google.api.gax.rpc.HeaderProvider; | ||
import com.google.api.gax.rpc.TransportChannel; | ||
|
@@ -46,21 +47,28 @@ | |
import com.google.auth.ApiKeyCredentials; | ||
import com.google.auth.Credentials; | ||
import com.google.auth.oauth2.ComputeEngineCredentials; | ||
import com.google.auth.oauth2.S2A; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.io.Files; | ||
import io.grpc.CallCredentials; | ||
import io.grpc.ChannelCredentials; | ||
import io.grpc.Grpc; | ||
import io.grpc.InsecureChannelCredentials; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.TlsChannelCredentials; | ||
import io.grpc.alts.GoogleDefaultChannelCredentials; | ||
import io.grpc.auth.MoreCallCredentials; | ||
import io.grpc.s2a.S2AChannelCredentials; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyStore; | ||
|
@@ -99,6 +107,11 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP | |
@VisibleForTesting | ||
static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS"; | ||
|
||
private static final String MTLS_MDS_ROOT = "/run/google-mds-mtls/root.crt"; | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess these are well-known locations on GCE? Do we have any public or internal docs for these locations? If not, where do we get these? What I'm worried is that how do we get notified if they change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes
I included a link down below in the logic, I've moved it up here as well: https://cloud.google.com/compute/docs/metadata/overview#https-mds. Specifically https://cloud.google.com/compute/docs/metadata/overview#https-mds-root-certs and https://cloud.google.com/compute/docs/metadata/overview#https-mds-client-certs |
||
// The mTLS MDS credentials are formatted as the concatenation of a PEM-encoded certificate chain | ||
// followed by a PEM-encoded private key. | ||
private static final String MTLS_MDS_CERT_CHAIN_AND_KEY = "/run/google-mds-mtls/client.key"; | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static final long DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS = 3600; | ||
static final long DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS = 20; | ||
static final String GCE_PRODUCTION_NAME_PRIOR_2016 = "Google"; | ||
|
@@ -107,6 +120,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP | |
private final int processorCount; | ||
private final Executor executor; | ||
private final HeaderProvider headerProvider; | ||
private final EndpointContext endpointContext; | ||
private final String endpoint; | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO: remove. envProvider currently provides DirectPath environment variable, and is only used | ||
// during initial rollout for DirectPath. This provider will be removed once the DirectPath | ||
|
@@ -136,6 +150,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) { | |
this.executor = builder.executor; | ||
this.headerProvider = builder.headerProvider; | ||
this.endpoint = builder.endpoint; | ||
this.endpointContext = builder.endpointContext; | ||
this.mtlsProvider = builder.mtlsProvider; | ||
this.envProvider = builder.envProvider; | ||
this.interceptorProvider = builder.interceptorProvider; | ||
|
@@ -225,6 +240,17 @@ public TransportChannelProvider withEndpoint(String endpoint) { | |
return toBuilder().setEndpoint(endpoint).build(); | ||
} | ||
|
||
/** | ||
* Specify the {@link EndpointContext}. | ||
* | ||
* @param endpointContext | ||
* @return A new {@link InstantiatingGrpcChannelProvider} with the endpointContext | ||
*/ | ||
@Override | ||
public TransportChannelProvider withEndpointContext(EndpointContext endpointContext) { | ||
return toBuilder().setEndpointContext(endpointContext).build(); | ||
} | ||
|
||
/** @deprecated Please modify pool settings via {@link #toBuilder()} */ | ||
@Deprecated | ||
@Override | ||
|
@@ -410,6 +436,80 @@ ChannelCredentials createMtlsChannelCredentials() throws IOException, GeneralSec | |
return null; | ||
} | ||
|
||
@VisibleForTesting | ||
ChannelCredentials createMtlsToS2AChannelCredentials( | ||
InputStream trustBundle, InputStream privateKey, InputStream certChain) throws IOException { | ||
if (trustBundle == null || privateKey == null || certChain == null) { | ||
return null; | ||
} | ||
return TlsChannelCredentials.newBuilder() | ||
.keyManager(privateKey, certChain) | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.trustManager(trustBundle) | ||
.build(); | ||
} | ||
|
||
ChannelCredentials createPlaintextToS2AChannelCredentials(String plaintextAddress) { | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Strings.isNullOrEmpty(plaintextAddress)) { | ||
return null; | ||
} | ||
return S2AChannelCredentials.newBuilder(plaintextAddress, InsecureChannelCredentials.create()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* This method creates gRPC {@link ChannelCredentials} configured to use S2A to estbalish a mTLS | ||
* connection. First, the address of S2A is discovered by using the {@link S2A} utility to learn | ||
* the {@code mtlsAddress} to reach S2A and the {@code plaintextAddress} to reach S2A. Prefer to | ||
* use the {@code mtlsAddress} address to reach S2A if it is non-empty and the MTLS-MDS | ||
* credentials can successfully be discovered and used to create {@link TlsChannelCredentials}. If | ||
* there is any failure using mTLS-to-S2A, fallback to using a plaintext connection to S2A using | ||
* the {@code plaintextAddress}. | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @return {@link ChannelCredentials} configured to use S2A to create mTLS connection to | ||
* mtlsEndpoint. | ||
*/ | ||
ChannelCredentials createS2ASecuredChannelCredentials() { | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
S2A s2aUtils = S2A.newBuilder().build(); | ||
String plaintextAddress = s2aUtils.getPlaintextS2AAddress(); | ||
String mtlsAddress = s2aUtils.getMtlsS2AAddress(); | ||
if (Strings.isNullOrEmpty(mtlsAddress)) { | ||
// Fallback to plaintext connection to S2A. | ||
return createPlaintextToS2AChannelCredentials(plaintextAddress); | ||
} | ||
// Currently, MTLS to MDS is only available on GCE. See: | ||
// https://cloud.google.com/compute/docs/metadata/overview#https-mds | ||
// Try to load MTLS-MDS creds. | ||
InputStream trustBundle = null; | ||
InputStream privateKey = null; | ||
InputStream certChain = null; | ||
try { | ||
trustBundle = new FileInputStream(MTLS_MDS_ROOT); | ||
privateKey = new FileInputStream(MTLS_MDS_CERT_CHAIN_AND_KEY); | ||
certChain = new FileInputStream(MTLS_MDS_CERT_CHAIN_AND_KEY); | ||
} catch (FileNotFoundException ignore) { | ||
// Fallback to plaintext-to-S2A connection. | ||
LOG.log( | ||
Level.INFO, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a misconfiguration or expected use case? If we are supposed to create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an expected use case since currently MTLS-MDS is only GA on GCE. To be clear: the only time we fail to create When a Because this exception can happen if MTLS-MDS is not available in that environment, I think we should leave it as INFO. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, if that's the case, we should check if the file exists, only create |
||
"Cannot establish an mTLS connection to S2A due to error loading MTLS to MDS credentials, falling back to plaintext connection to S2A: " | ||
+ ignore.getMessage()); | ||
return createPlaintextToS2AChannelCredentials(plaintextAddress); | ||
} | ||
ChannelCredentials mtlsToS2AChannelCredentials = null; | ||
try { | ||
// Try to connect to S2A using mTLS. | ||
mtlsToS2AChannelCredentials = | ||
createMtlsToS2AChannelCredentials(trustBundle, privateKey, certChain); | ||
} catch (IOException ignore) { | ||
// Fallback to plaintext-to-S2A connection. | ||
LOG.log( | ||
Level.INFO, | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Cannot establish an mTLS connection to S2A due to error creating MTLS to MDS TlsChannelCredentials credentials, falling back to plaintext connection to S2A: " | ||
+ ignore.getMessage()); | ||
return createPlaintextToS2AChannelCredentials(plaintextAddress); | ||
} | ||
return S2AChannelCredentials.newBuilder(mtlsAddress, mtlsToS2AChannelCredentials).build(); | ||
} | ||
|
||
private ManagedChannel createSingleChannel() throws IOException { | ||
GrpcHeaderInterceptor headerInterceptor = | ||
new GrpcHeaderInterceptor(headersWithDuplicatesRemoved); | ||
|
@@ -447,16 +547,30 @@ private ManagedChannel createSingleChannel() throws IOException { | |
builder.keepAliveTime(DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS, TimeUnit.SECONDS); | ||
builder.keepAliveTimeout(DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS, TimeUnit.SECONDS); | ||
} else { | ||
// Try and create credentials via DCA. See https://google.aip.dev/auth/4114. | ||
ChannelCredentials channelCredentials; | ||
try { | ||
channelCredentials = createMtlsChannelCredentials(); | ||
} catch (GeneralSecurityException e) { | ||
throw new IOException(e); | ||
} | ||
if (channelCredentials != null) { | ||
// Create the channel using channel credentials created via DCA. | ||
builder = Grpc.newChannelBuilder(endpoint, channelCredentials); | ||
} else { | ||
builder = ManagedChannelBuilder.forAddress(serviceAddress, port); | ||
// Could not create channel credentials via DCA. In accordance with | ||
// https://google.aip.dev/auth/4115, if credentials not available through | ||
// DCA, try mTLS with credentials held by the S2A (Secure Session Agent). | ||
if (endpointContext.useS2A()) { | ||
channelCredentials = createS2ASecuredChannelCredentials(); | ||
} | ||
if (channelCredentials != null) { | ||
// Create the channel using S2A-secured channel credentials. | ||
builder = Grpc.newChannelBuilder(endpointContext.mtlsEndpoint(), channelCredentials); | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// Use default if we cannot initialize channel credentials via DCA or S2A. | ||
builder = ManagedChannelBuilder.forAddress(serviceAddress, port); | ||
} | ||
} | ||
} | ||
// google-c2p resolver requires service config lookup | ||
|
@@ -604,6 +718,7 @@ public static final class Builder { | |
private Executor executor; | ||
private HeaderProvider headerProvider; | ||
private String endpoint; | ||
private EndpointContext endpointContext; | ||
private EnvironmentProvider envProvider; | ||
private MtlsProvider mtlsProvider = new MtlsProvider(); | ||
@Nullable private GrpcInterceptorProvider interceptorProvider; | ||
|
@@ -632,6 +747,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) { | |
this.executor = provider.executor; | ||
this.headerProvider = provider.headerProvider; | ||
this.endpoint = provider.endpoint; | ||
this.endpointContext = provider.endpointContext; | ||
this.envProvider = provider.envProvider; | ||
this.interceptorProvider = provider.interceptorProvider; | ||
this.maxInboundMessageSize = provider.maxInboundMessageSize; | ||
|
@@ -700,6 +816,11 @@ public Builder setEndpoint(String endpoint) { | |
return this; | ||
} | ||
|
||
Builder setEndpointContext(EndpointContext endpointContext) { | ||
this.endpointContext = endpointContext; | ||
return this; | ||
} | ||
|
||
@VisibleForTesting | ||
Builder setMtlsProvider(MtlsProvider mtlsProvider) { | ||
this.mtlsProvider = mtlsProvider; | ||
|
Uh oh!
There was an error while loading. Please reload this page.