Skip to content

fix(spanner): preserve client_options, client_info, instance_type, and TLS settings in Client.copy() - #19193

Merged
sakthivelmanii merged 4 commits into
mainfrom
fix/spanner-universe-validation-client-copy
Nov 24, 2026
Merged

sakthivelmanii merged 4 commits into
mainfrom
fix/spanner-universe-validation-client-copy

Conversation

@sakthivelmanii

@sakthivelmanii sakthivelmanii commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem Statement

  • Client.copy() (both sync spanner_v1.Client and async spanner_v1._async.client.Client) only forwards project and credentials, dropping client_options (such as universe_domain, api_endpoint, and quota_project_id), client_info, instance_type, and TLS/plaintext settings.
  • Copying a client configured for a custom universe domain or custom api_endpoint silently reverts to spanner.googleapis.com.
  • Copying a Spanner Omni client or an AnonymousCredentials client fails because instance_type, endpoint, and TLS settings are lost.

Solution

  • Updated Client.copy() in google/cloud/spanner_v1/_async/client.py and regenerated google/cloud/spanner_v1/client.py to shallow-copy client_options (normalizing dict via google.api_core.client_options.from_dict) and forward client_info, instance_type, use_plain_text, ca_certificate, client_certificate, and client_key.
  • Cleared credentials_file and api_key on the copied client_options instance (without mutating the original) because resolved self._credentials is passed directly, avoiding DuplicateCredentialArgs.
  • Preserved self._host in client_options.api_endpoint when copying Omni clients configured via experimental_host (where _client_options may be None).
  • Added sync and async unit tests in tests/unit/test_client.py and tests/unit/_async/test_client.py covering client_options / TLS preservation, non-mutation of original options, credentials_file / api_key clearing, Omni clients, and AnonymousCredentials clients.

…_options in Client.copy()

Validate that credentials match the configured universe domain in
spanner_v1.Client (sync and async) and CloudMonitoringMetricsExporter,
and preserve client_options (clearing credentials_file and api_key),
client_info, instance_type, and TLS settings in Client.copy().
@sakthivelmanii
sakthivelmanii requested a review from a team as a code owner November 24, 2026 06:58

@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 introduces universe domain validation for both synchronous and asynchronous Google Cloud Spanner clients, as well as the metrics exporter, ensuring that credentials match the configured universe domain. It also refactors the client copy() method to correctly copy local configurations (such as client options, TLS settings, and instance types) while clearing credential files and API keys to prevent duplicate credential arguments. Extensive unit tests have been added to cover these validation and copying behaviors. There are no review comments to address, and the changes look solid.

@sakthivelmanii sakthivelmanii changed the title fix(spanner): validate credential universe domain and preserve client_options in Client.copy() fix(spanner): preserve client_options, client_info, instance_type, and TLS settings in Client.copy() Nov 24, 2026

@olavloite olavloite 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.

A few questions before merging:

  1. Preserve the remaining Client configuration options in Client.copy()
    Client.__init__ accepts several additional configuration options that are still silently dropped on copy():

    • observability_options (notably, if a caller sets {"enable_extended_tracing": False} to prevent SQL statements from being recorded in OpenTelemetry spans, copy() currently drops it and reverts enable_extended_tracing back to True)
    • route_to_leader_enabled (silently resets False to True)
    • directed_read_options
    • default_transaction_options
    • query_options
    • client_context
    • disable_builtin_metrics (if stored on self in __init__, copy() won't accidentally initialize built-in metrics when copying a client created with disable_builtin_metrics=True)
  2. Test and branch coverage gaps in Client.copy()
    Running coverage on google/cloud/spanner_v1/_async/client.py and google/cloud/spanner_v1/client.py shows two uncovered paths in copy():

    • elif isinstance(self._client_options, dict): — Client.__init__ already converts non-empty dict inputs via from_dict (when type(client_options) is dict), so this branch is effectively unreachable under normal construction and currently has no coverage.
    • elif not client_options.api_endpoint: (False branch) — neither suite tests copying a standard (non-deprecated) Omni client constructed with instance_type="omni" and client_options={"api_endpoint": ...} (e.g. with SpannerOmniCredentials / username+password and TLS certs).
    • Also consider asserting copied._client_options.quota_project_id == "quota-proj" (which is currently passed in orig_options but not asserted) and verifying copied._instance_admin_api is None / copied._database_admin_api is None so cached transports aren't shared.
  3. Edge case with experimental_host + client_options.api_endpoint
    In __init__, experimental_host takes precedence over client_options.api_endpoint for self._host. In copy(), elif not client_options.api_endpoint: will leave a conflicting client_options.api_endpoint intact instead of using self._host.

  4. Minor style / cleanup nits

    • Add a return type annotation (-> "Client") on the modified copy(self) method in _async/client.py (and regenerate client.py).
    • In the new tests, prefer full words over abbreviations (original_options, credentials, empty_options_client, omni_client_options, anonymous_client), rename _make_universe_credentials / tpc_creds / gdu_creds (leftover naming from the removed universe-validation changes), and consider splitting test_copy_clears_credential_options_and_handles_omni_and_anonymous into smaller focused test methods.

@sakthivelmanii

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @olavloite! Addressed all four points in the latest commit — Client.copy() now preserves the remaining client options (including disable_builtin_metrics, observability_options, route_to_leader_enabled, directed_read_options, default_transaction_options, query_options, and client_context), fixes the experimental_host precedence edge case, drops the dead dict branch, adds the -> "Client" return annotation, and expands/cleans up the sync and async tests.

@olavloite olavloite 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.

LGTM

@sakthivelmanii
sakthivelmanii merged commit 6a00b4a into main Nov 24, 2026
85 checks passed
@sakthivelmanii
sakthivelmanii deleted the fix/spanner-universe-validation-client-copy branch November 24, 2026 19:10
daniel-sanche pushed a commit that referenced this pull request Nov 25, 2026
…d TLS settings in Client.copy() (#19193)

## Problem Statement
- `Client.copy()` (both sync `spanner_v1.Client` and async
`spanner_v1._async.client.Client`) only forwards `project` and
`credentials`, dropping `client_options` (such as `universe_domain`,
`api_endpoint`, and `quota_project_id`), `client_info`, `instance_type`,
and TLS/plaintext settings.
- Copying a client configured for a custom universe domain or custom
`api_endpoint` silently reverts to `spanner.googleapis.com`.
- Copying a Spanner Omni client or an `AnonymousCredentials` client
fails because `instance_type`, endpoint, and TLS settings are lost.

## Solution
- Updated `Client.copy()` in `google/cloud/spanner_v1/_async/client.py`
and regenerated `google/cloud/spanner_v1/client.py` to shallow-copy
`client_options` (normalizing `dict` via
`google.api_core.client_options.from_dict`) and forward `client_info`,
`instance_type`, `use_plain_text`, `ca_certificate`,
`client_certificate`, and `client_key`.
- Cleared `credentials_file` and `api_key` on the copied
`client_options` instance (without mutating the original) because
resolved `self._credentials` is passed directly, avoiding
`DuplicateCredentialArgs`.
- Preserved `self._host` in `client_options.api_endpoint` when copying
Omni clients configured via `experimental_host` (where `_client_options`
may be `None`).
- Added sync and async unit tests in `tests/unit/test_client.py` and
`tests/unit/_async/test_client.py` covering `client_options` / TLS
preservation, non-mutation of original options, `credentials_file` /
`api_key` clearing, Omni clients, and `AnonymousCredentials` clients.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants