Repository navigation
Conversation
With DirectPath enabled, location-aware routing built each routed channel from the base channel provider with the address of a single Spanner server as its endpoint. gax allows DirectPath only for endpoints that contain googleapis.com, so these channels connected to the server with TLS instead of ALTS. Routed channels now use ALTS when the base provider can use DirectPath. They set ALTS as the security of the channel builder that gax created, so routed DirectPath calls keep the interceptors, headers, user agent, executor, keepalive settings, size limits and channel configurator that other routed channels have, and the authority of the default channel. When gax created that builder with channel credentials, as it does for mTLS and S2A, gRPC does not allow changing its security. The routed channel then uses a new ALTS builder with the routed keepalive settings and the inbound message and metadata size limits, but without the interceptors and headers that gax adds. Routed DirectPath calls do not use ALTS hard-bound tokens. With DirectPath over Interconnect outside Google Cloud, routed channels now use ALTS, which cannot connect there.
There was a problem hiding this comment.
Code Review
This pull request adds support for routed DirectPath channels using ALTS credentials in GrpcChannelEndpointCache, including a new constructor for testing and dynamic configuration of routed channels when DirectPath is eligible. Feedback on the changes recommends catching both Exception and LinkageError instead of only IllegalStateException when interacting with internal gRPC APIs, ensuring a robust fallback mechanism if there are classpath or version incompatibilities.
| try { | ||
| // gRPC has no public API that changes the security of an existing builder. | ||
| InternalNettyChannelBuilder.setProtocolNegotiatorFactory( | ||
| (NettyChannelBuilder) channelBuilder, | ||
| InternalNettyChannelCredentials.toNegotiator(credentials)); | ||
| return channelBuilder; | ||
| } catch (IllegalStateException e) { | ||
| // setProtocolNegotiatorFactory throws IllegalStateException when channelBuilder was | ||
| // created with ChannelCredentials. gax creates channelBuilder that way for mTLS and S2A. | ||
| } |
There was a problem hiding this comment.
Using internal gRPC APIs (InternalNettyChannelBuilder and InternalNettyChannelCredentials) carries a high risk of compatibility issues across different gRPC versions. If a user runs an environment with a mismatched or upgraded gRPC version, these internal classes might be missing or have different method signatures, leading to LinkageError (such as NoSuchMethodError or NoClassDefFoundError).
Currently, only IllegalStateException is caught. To ensure the client does not crash and can safely fall back to the replacement builder, we should catch both Exception and LinkageError.
try {
// gRPC has no public API that changes the security of an existing builder.
InternalNettyChannelBuilder.setProtocolNegotiatorFactory(
(NettyChannelBuilder) channelBuilder,
InternalNettyChannelCredentials.toNegotiator(credentials));
return channelBuilder;
} catch (Exception | LinkageError e) {
// Fall back to replacement builder if setProtocolNegotiatorFactory fails.
// This can happen if channelBuilder was created with ChannelCredentials (throws IllegalStateException),
// or if internal gRPC APIs are incompatible/absent (throws LinkageError or other exceptions).
logger.log(
Level.FINE,
"Failed to set protocol negotiator factory on NettyChannelBuilder, falling back to replacement builder",
e);
}
olavloite
left a comment
There was a problem hiding this comment.
Before we use this in production — and especially considering that GOOGLE_SPANNER_ENABLE_DIRECT_ACCESS=true might become the default in the near future — there are a few follow-up items we should keep in mind:
-
Move the channel creation support into
gax-grpc. Sincegax-grpclives in this same repo and is released together with the Spanner client, adding an internal helper there would let us use public gRPC APIs instead of internal Netty APIs, avoid the fallback builder that drops interceptors, headers, user agent, and the executor, skip unnecessary mTLS/S2A initialization on routed channels, and keep DirectPath over Interconnect using TLS instead of ALTS. -
Disable DirectPath for Omni (and plaintext / emulator) before making DirectPath the default. Omni enables location-aware routing by default, and DirectPath is currently not turned off for it. If DirectPath becomes the default as-is:
- Every new routed Omni channel on Linux will still read
/sys/class/dmi/id/product_nameinsidegax-grpc, because the Compute Engine check currently runs before thegoogleapis.comcheck. - The GCP fallback setup will wrap every Omni client's default channel in a fallback channel with two channel pools pointing to the same host (and if grpc-gcp is disabled, it will also wrap every routed channel using the same shared fallback builder). When fallback is disabled, Omni pool metrics also get labeled as DirectPath.
- Any Omni or plaintext endpoint whose host happens to contain
googleapis.comas a substring would be treated as DirectPath-eligible on GCE, even withNoCredentialsor Omni auth tokens, because non-default service accounts are allowed.
- Every new routed Omni channel on Linux will still read
-
Call credentials and
isDirectPathhandling. Routed channels do not currently attach call credentials directly. They only authenticate because the key-aware transport channel leavesisDirectPathasfalse, so gax does not strip call credentials from calls (this also means the default channel currently gets credentials attached twice, and theDIRECTPATH_CHANNEL_CREATEDflag staysfalse). If that gets fixed later, routed channels will start failing withUNAUTHENTICATEDunless they attach call credentials as well. -
Hard-bound tokens and ALTS peer verification. As noted in the PR description, routed channels send unbound bearer tokens even when ALTS hard-bound tokens are enabled. They also use default ALTS credentials without target service account restrictions or checking that the routed address is in a DirectPath IP range. That matches the default channel's credentials, though routed addresses come from Spanner cache updates rather than Traffic Director, so it is worth checking before moving to production.
-
Smaller follow-ups:
- In
gax-grpc, checking thegoogleapis.comcondition before readingproduct_name(or caching that read) would avoid repeated disk reads in general. - There is a helper in
GapicSpannerRpcthat readsGOOGLE_SPANNER_ENABLE_DIRECT_ACCESSdirectly from the environment instead of going throughSpannerOptions, which should be updated if the default changes. - Before going to production, we should also add tests for Interconnect (verifying TLS instead of ALTS), routed addresses containing
googleapis.com, IPv6 addresses, the target name sent in the ALTS handshake request, and an end-to-end routed call verifying that call credentials are sent.
- In
| // Read DirectPath eligibility from baseProvider. canUseDirectPath() returns false when the | ||
| // endpoint does not contain googleapis.com, so the provider for a server address such as | ||
| // 10.0.0.1:443 would return false and its channel would never use ALTS. | ||
| final boolean useDirectPath = baseProvider.canUseDirectPath(); |
There was a problem hiding this comment.
Can we compute and cache this once in the constructor instead of calling canUseDirectPath() on every new routed endpoint? On Linux it reads /sys/class/dmi/id/product_name on every call without caching, so if DirectPath becomes the default in the future, this would add repeated file reads for Omni as well. Computing it once also ensures routed channels stay consistent with what the default channel did.
Also, when the cached flag is true, we should check that the routed endpoint provider cannot already use DirectPath before overriding its credentials (short-circuiting on the cached flag first). Otherwise, if a routed address ever contains googleapis.com, gax will already have created a DirectPath builder with channel credentials, which will hit the IllegalStateException fallback below and drop the google-c2p setup along with all interceptors and headers.
| // gRPC has no public API that changes the security of an existing builder. | ||
| InternalNettyChannelBuilder.setProtocolNegotiatorFactory( | ||
| (NettyChannelBuilder) channelBuilder, | ||
| InternalNettyChannelCredentials.toNegotiator(credentials)); | ||
| return channelBuilder; |
There was a problem hiding this comment.
Can we add a short TODO here noting that using the internal Netty APIs and the fallback builder is a temporary workaround for testing, and that we should move this into gax-grpc before using it in production?
| } catch (IllegalStateException e) { | ||
| // setProtocolNegotiatorFactory throws IllegalStateException when channelBuilder was | ||
| // created with ChannelCredentials. gax creates channelBuilder that way for mTLS and S2A. | ||
| } | ||
| } | ||
| logger.log( | ||
| Level.FINE, | ||
| "Routed DirectPath channel for {0} does not get the interceptors and headers that gax adds", | ||
| address); |
There was a problem hiding this comment.
Could we log this at WARNING instead of FINE, and include the caught IllegalStateException instead of swallowing it? If a test run hits this fallback path, routed calls will silently lose Spanner's error, request-id, and metrics interceptors as well as headers and the configured executor, so it should be easy to spot in the logs.
With DirectPath enabled, location-aware routing built each routed channel from the base channel provider with the address of a single Spanner server as its endpoint. gax allows DirectPath only for endpoints that contain googleapis.com, so these channels connected to the server with TLS instead of ALTS.
Routed channels now use ALTS when the base provider can use DirectPath. They set ALTS as the security of the channel builder that gax created, so routed DirectPath calls keep the interceptors, headers, user agent, executor, keepalive settings, size limits and channel configurator that other routed channels have, and the authority of the default channel.
When gax created that builder with channel credentials, as it does for mTLS and S2A, gRPC does not allow changing its security. The routed channel then uses a new ALTS builder with the routed keepalive settings and the inbound message and metadata size limits, but without the interceptors and headers that gax adds.
Routed DirectPath calls do not use ALTS hard-bound tokens. With DirectPath over Interconnect outside Google Cloud, routed channels now use ALTS, which cannot connect there.