Repository navigation
fix(spanner): isolate metrics GoogleAuth and handle default endpoints and universeDomain edge cases - #9981
fix(spanner): isolate metrics GoogleAuth and handle default endpoints and universeDomain edge cases#9981sakthivelmanii wants to merge 1 commit into
Conversation
… and universeDomain edge cases ## Problem Statement - Passing a shared `GoogleAuth` instance or an unscoped `JWT` `authClient` to `Spanner` could share mutable JWT audience state between Spanner RPCs and built-in Cloud Monitoring metrics, causing `UNAUTHENTICATED` errors. - Explicitly configuring the default Spanner `apiEndpoint`, `servicePath` (`spanner.<universeDomain>` with or without `:443`), or `port: 443` caused built-in metrics to be silently disabled. - Conflicting `universeDomain` and `universe_domain` options were not rejected when one value was empty or whitespace, an exported empty `GOOGLE_CLOUD_UNIVERSE_DOMAIN` environment variable caused errors instead of falling back to `googleapis.com`, explicit empty `universeDomain` options were not rejected when built-in metrics were disabled, and direct metrics exporter callers did not forward the normalized environment `universeDomain` into the Cloud Monitoring client options. ## Solution - Isolate the `GoogleAuth` / `JWT` used by built-in Cloud Monitoring metrics when `auth` or an unscoped `JWT` `authClient` is provided, preventing cross-service JWT audience clobbering while keeping built-in metrics credentials resolved from `Spanner` options or Application Default Credentials. - Treat explicit default `apiEndpoint`, `servicePath` (`spanner.<universeDomain>` or `spanner.<universeDomain>:443`), and `port: 443` as default endpoints so built-in metrics remain enabled unless a non-default endpoint or port is configured. - Reject conflicting `universeDomain`/`universe_domain` values even when one is empty or whitespace, reject explicit empty `universeDomain`/`universe_domain` options consistently regardless of `disableBuiltInMetrics`, treat exported empty or whitespace `GOOGLE_CLOUD_UNIVERSE_DOMAIN` as unset (`googleapis.com`), and forward the normalized `universeDomain` to Cloud Monitoring client options.
There was a problem hiding this comment.
Code Review
This pull request improves the Spanner client's handling of custom endpoints, universe domains, and built-in metrics authentication by isolating the metrics client's authentication state (using a dedicated GoogleAuth or scoped JWT) to prevent clobbering the caller's shared credentials. It also refactors universe domain validation and adds comprehensive unit tests. The review feedback highlights two key issues: a potential runtime TypeError when writing to a potentially null or undefined options object in getUniverseDomain, and an authorization risk where using Spanner-specific scopes for the metrics client could cause export failures instead of using the default metrics scopes.
| if (hasExplicitOption || universeDomainEnvVar) { | ||
| options.universeDomain = universeDomain; | ||
| } |
There was a problem hiding this comment.
If options is null or undefined, hasExplicitOption will be false. However, if universeDomainEnvVar is truthy, the condition hasExplicitOption || universeDomainEnvVar will evaluate to true, leading to a runtime TypeError when attempting to write to options.universeDomain. Since optional chaining is used elsewhere to defensively handle a potentially null/undefined options, we should also guard this assignment.
| if (hasExplicitOption || universeDomainEnvVar) { | |
| options.universeDomain = universeDomain; | |
| } | |
| if (options && (hasExplicitOption || universeDomainEnvVar)) { | |
| options.universeDomain = universeDomain; | |
| } |
References
- Ensure appropriate null/nil/None checks or other language-idiomatic guards exist before object property accesses for nullable references.
| const scopes = hasExplicitScopes(opts.scopes) | ||
| ? opts.scopes | ||
| : METRICS_DEFAULT_SCOPES; |
There was a problem hiding this comment.
Using opts.scopes (which are Spanner-specific scopes) for the metrics client will cause metrics export to fail with authorization errors (e.g., PERMISSION_DENIED) because Spanner scopes typically do not include the required Cloud Monitoring scopes. We should always use METRICS_DEFAULT_SCOPES for the metrics client, consistent with how it is handled for GoogleAuth on line 1910.
const scopes = METRICS_DEFAULT_SCOPES;
Problem Statement
GoogleAuthinstance or an unscopedJWTauthClienttoSpannercould share mutable JWT audience state between Spanner RPCs and built-in Cloud Monitoring metrics, causingUNAUTHENTICATEDerrors.apiEndpoint,servicePath(spanner.<universeDomain>with or without:443), orport: 443caused built-in metrics to be silently disabled.universeDomainanduniverse_domainoptions were not rejected when one value was empty or whitespace, an exported emptyGOOGLE_CLOUD_UNIVERSE_DOMAINenvironment variable caused errors instead of falling back togoogleapis.com, explicit emptyuniverseDomainoptions were not rejected when built-in metrics were disabled, and direct metrics exporter callers did not forward the normalized environmentuniverseDomaininto the Cloud Monitoring client options.Solution
GoogleAuth/JWTused by built-in Cloud Monitoring metrics whenauthor an unscopedJWTauthClientis provided, preventing cross-service JWT audience clobbering while keeping built-in metrics credentials resolved fromSpanneroptions or Application Default Credentials.apiEndpoint,servicePath(spanner.<universeDomain>orspanner.<universeDomain>:443), andport: 443as default endpoints so built-in metrics remain enabled unless a non-default endpoint or port is configured.universeDomain/universe_domainvalues even when one is empty or whitespace, reject explicit emptyuniverseDomain/universe_domainoptions consistently regardless ofdisableBuiltInMetrics, treat exported empty or whitespaceGOOGLE_CLOUD_UNIVERSE_DOMAINas unset (googleapis.com), and forward the normalizeduniverseDomainto Cloud Monitoring client options.