Repository navigation
fix(spanner): preserve client_options, client_info, instance_type, and TLS settings in Client.copy() - #19193
Conversation
…_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().
There was a problem hiding this comment.
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.
olavloite
left a comment
There was a problem hiding this comment.
A few questions before merging:
-
Preserve the remaining
Clientconfiguration options inClient.copy()
Client.__init__accepts several additional configuration options that are still silently dropped oncopy():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 revertsenable_extended_tracingback toTrue)route_to_leader_enabled(silently resetsFalsetoTrue)directed_read_optionsdefault_transaction_optionsquery_optionsclient_contextdisable_builtin_metrics(if stored onselfin__init__,copy()won't accidentally initialize built-in metrics when copying a client created withdisable_builtin_metrics=True)
-
Test and branch coverage gaps in
Client.copy()
Running coverage ongoogle/cloud/spanner_v1/_async/client.pyandgoogle/cloud/spanner_v1/client.pyshows two uncovered paths incopy():elif isinstance(self._client_options, dict):—Client.__init__already converts non-emptydictinputs viafrom_dict(whentype(client_options) is dict), so this branch is effectively unreachable under normal construction and currently has no coverage.elif not client_options.api_endpoint:(Falsebranch) — neither suite tests copying a standard (non-deprecated) Omni client constructed withinstance_type="omni"andclient_options={"api_endpoint": ...}(e.g. withSpannerOmniCredentials/username+passwordand TLS certs).- Also consider asserting
copied._client_options.quota_project_id == "quota-proj"(which is currently passed inorig_optionsbut not asserted) and verifyingcopied._instance_admin_api is None/copied._database_admin_api is Noneso cached transports aren't shared.
-
Edge case with
experimental_host+client_options.api_endpoint
In__init__,experimental_hosttakes precedence overclient_options.api_endpointforself._host. Incopy(),elif not client_options.api_endpoint:will leave a conflictingclient_options.api_endpointintact instead of usingself._host. -
Minor style / cleanup nits
- Add a return type annotation (
-> "Client") on the modifiedcopy(self)method in_async/client.py(and regenerateclient.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 splittingtest_copy_clears_credential_options_and_handles_omni_and_anonymousinto smaller focused test methods.
- Add a return type annotation (
|
Thanks for the thorough review @olavloite! Addressed all four points in the latest commit — |
…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.
Problem Statement
Client.copy()(both syncspanner_v1.Clientand asyncspanner_v1._async.client.Client) only forwardsprojectandcredentials, droppingclient_options(such asuniverse_domain,api_endpoint, andquota_project_id),client_info,instance_type, and TLS/plaintext settings.api_endpointsilently reverts tospanner.googleapis.com.AnonymousCredentialsclient fails becauseinstance_type, endpoint, and TLS settings are lost.Solution
Client.copy()ingoogle/cloud/spanner_v1/_async/client.pyand regeneratedgoogle/cloud/spanner_v1/client.pyto shallow-copyclient_options(normalizingdictviagoogle.api_core.client_options.from_dict) and forwardclient_info,instance_type,use_plain_text,ca_certificate,client_certificate, andclient_key.credentials_fileandapi_keyon the copiedclient_optionsinstance (without mutating the original) because resolvedself._credentialsis passed directly, avoidingDuplicateCredentialArgs.self._hostinclient_options.api_endpointwhen copying Omni clients configured viaexperimental_host(where_client_optionsmay beNone).tests/unit/test_client.pyandtests/unit/_async/test_client.pycoveringclient_options/ TLS preservation, non-mutation of original options,credentials_file/api_keyclearing, Omni clients, andAnonymousCredentialsclients.