Repository navigation
fix(spanner): scope metrics auth to monitoring.write and isolate authClient - #9981
Draft
sakthivelmanii wants to merge 6 commits into
Draft
sakthivelmanii wants to merge 6 commits into
sakthivelmanii wants to merge 6 commits 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.
Contributor
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.
olavloite
requested changes
Nov 27, 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
marked this pull request as draft
December 1, 2026 15:21
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem Statement
this.authdirectly withSpannerand its GAPIC clients, so Spanner-onlyoptions.scopescould prevent Cloud Monitoring writes, and ongoogle-auth-library >= 11.3.2sharing aJWTauthClientcould latchdefaultServicePathon the caller's instance.options.auth instanceof GoogleAuthmissed cross-realm or duplicate-installGoogleAuthinstances, and strippingoptions.authwithout deriving its credential source would fall back to ambient ADC instead of using the caller's configured credentials.Solution
isGoogleAuthLike()check (withinstanceof GoogleAuthfast-path) soSpanner.authcontinues to honoroptions.authwithout breaking across duplicategoogle-auth-libraryinstalls._createMetricsAuth()(invoked only whenMetricsTracerFactoryattaches itsPeriodicExportingMetricReader) that constructs a freshGoogleAuthscoped strictly tohttps://www.googleapis.com/auth/monitoring.writewithout forcinguniverseDomain(preserving credential universe-domain mismatch validation).options.authis provided, derive the fresh metricsGoogleAuthfrom its credential source (jsonContent,keyFilename, clonedJWTcachedCredential, andclientOptions) without sharing or mutating the caller'sGoogleAuthinstance; whenoptions.authClientis aJWT, clone it viacreateScoped(jwt.scopes)so unscoped JWTs remain in local self-signed mode (zero OAuth2 token exchanges) while isolating the instance.test/index.tsandtest/metrics/spanner-metrics-exporter.tscoveringmonitoring.writescoping, callerGoogleAuthderivation acrossjsonContent/keyFilename/authClient/clientOptions/ structural auth, JWT isolation, and TPC mismatch rejection.universeDomainvalidation and default endpoint handling.