Skip to content

fix(spanner): isolate metrics GoogleAuth and handle default endpoints and universeDomain edge cases - #9981

Open
sakthivelmanii wants to merge 1 commit into
mainfrom
fix_spanner_metrics_universe_domain
Open

sakthivelmanii wants to merge 1 commit into
mainfrom
fix_spanner_metrics_universe_domain

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

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.

… 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.
@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 26, 2026 07:26
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Nov 26, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +381 to 383
if (hasExplicitOption || universeDomainEnvVar) {
options.universeDomain = universeDomain;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
if (hasExplicitOption || universeDomainEnvVar) {
options.universeDomain = universeDomain;
}
if (options && (hasExplicitOption || universeDomainEnvVar)) {
options.universeDomain = universeDomain;
}
References
  1. Ensure appropriate null/nil/None checks or other language-idiomatic guards exist before object property accesses for nullable references.

Comment on lines +1917 to +1919
const scopes = hasExplicitScopes(opts.scopes)
? opts.scopes
: METRICS_DEFAULT_SCOPES;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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;

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant