Skip to content

fix(spanner): scope metrics auth to monitoring.write and isolate authClient - #9981

Draft
sakthivelmanii wants to merge 6 commits into
mainfrom
fix_spanner_metrics_universe_domain
Draft

sakthivelmanii wants to merge 6 commits into
mainfrom
fix_spanner_metrics_universe_domain

Conversation

@sakthivelmanii

@sakthivelmanii sakthivelmanii commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem Statement

  • Built-in metrics previously shared this.auth directly with Spanner and its GAPIC clients, so Spanner-only options.scopes could prevent Cloud Monitoring writes, and on google-auth-library >= 11.3.2 sharing a JWT authClient could latch defaultServicePath on the caller's instance.
  • Checking options.auth instanceof GoogleAuth missed cross-realm or duplicate-install GoogleAuth instances, and stripping options.auth without deriving its credential source would fall back to ambient ADC instead of using the caller's configured credentials.

Solution

  • Use a structural isGoogleAuthLike() check (with instanceof GoogleAuth fast-path) so Spanner.auth continues to honor options.auth without breaking across duplicate google-auth-library installs.
  • Add lazy _createMetricsAuth() (invoked only when MetricsTracerFactory attaches its PeriodicExportingMetricReader) that constructs a fresh GoogleAuth scoped strictly to https://www.googleapis.com/auth/monitoring.write without forcing universeDomain (preserving credential universe-domain mismatch validation).
  • When options.auth is provided, derive the fresh metrics GoogleAuth from its credential source (jsonContent, keyFilename, cloned JWT cachedCredential, and clientOptions) without sharing or mutating the caller's GoogleAuth instance; when options.authClient is a JWT, clone it via createScoped(jwt.scopes) so unscoped JWTs remain in local self-signed mode (zero OAuth2 token exchanges) while isolating the instance.
  • Add unit and unmocked RSA-key tests in test/index.ts and test/metrics/spanner-metrics-exporter.ts covering monitoring.write scoping, caller GoogleAuth derivation across jsonContent / keyFilename / authClient / clientOptions / structural auth, JWT isolation, and TPC mismatch rejection.
  • Pairs with fix(spanner): fix universeDomain validation and default endpoint metrics #10000 for universeDomain validation and default endpoint handling.

… 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 thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/src/index.ts Resolved
Comment thread handwritten/spanner/test/index.ts Resolved
Comment thread handwritten/spanner/README.md Resolved
@sakthivelmanii sakthivelmanii added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Nov 30, 2026
…Client

## Problem Statement

- Built-in metrics reused `Spanner.auth`, inheriting Spanner-specific `options.scopes` (such as `spanner.data` and `spanner.admin`) instead of requesting only the Cloud Monitoring write scope required to publish metrics. When a caller restricted `options.scopes` to Spanner-only scopes, metrics export lacked `https://www.googleapis.com/auth/monitoring.write`.
- When a caller passed an unscoped `JWT` via `options.authClient` on `google-auth-library >= 11.3.2` (resolved by `^11.3.1`), `GoogleAuth.getClient()` calls `setGapicJWTValues()` on `cachedCredential`, latching `defaultServicePath` across `SpannerClient` and `MetricServiceClient` and causing `UNAUTHENTICATED` errors.

## Solution

- Always construct a dedicated `GoogleAuth` lazily inside `_createMetricsAuth()` scoped strictly to `['https://www.googleapis.com/auth/monitoring.write']` when initializing a new metrics reader.
- Clone a `JWT` passed via `options.authClient` with `jwt.createScoped(jwt.scopes)` so it gets an isolated instance for `google-auth-library >= 11.3.2` while preserving its original scope semantics (unscoped `JWT`s stay unscoped and keep local self-signed JWT signing with zero `oauth2.googleapis.com/token` calls on all versions).
- Omit `universeDomain` from `_createMetricsAuth()` unless explicitly configured on `options` so TPC credential universe-domain mismatch validation remains active.
- Note: Related changes were split into #9995 (partial revert of #9957 `options.auth` sharing) and #10000 (`universeDomain` validation and default-endpoint handling).
@sakthivelmanii sakthivelmanii removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Dec 1, 2026
@sakthivelmanii sakthivelmanii changed the title fix(spanner): isolate metrics GoogleAuth and handle default endpoints and universeDomain edge cases fix(spanner): scope metrics auth to monitoring.write and isolate authClient Dec 1, 2026
@sakthivelmanii
sakthivelmanii marked this pull request as draft December 1, 2026 15:21

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.

2 participants