|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 | # |
| 16 | + |
| 17 | +# DO NOT EDIT THIS FILE OUTSIDE OF `.librarian/generator-input` |
| 18 | +# The source of truth for this file is `.librarian/generator-input` |
| 19 | + |
16 | 20 | from collections import OrderedDict |
17 | 21 | from http import HTTPStatus |
18 | 22 | import json |
19 | 23 | import logging as std_logging |
20 | 24 | import os |
21 | 25 | import re |
22 | 26 | from typing import ( |
23 | | - Dict, |
24 | 27 | Callable, |
| 28 | + Dict, |
25 | 29 | Mapping, |
26 | 30 | MutableMapping, |
27 | 31 | MutableSequence, |
|
34 | 38 | ) |
35 | 39 | import warnings |
36 | 40 |
|
37 | | -from google.cloud.firestore_admin_v1 import gapic_version as package_version |
38 | | - |
39 | 41 | from google.api_core import client_options as client_options_lib |
40 | 42 | from google.api_core import exceptions as core_exceptions |
41 | 43 | from google.api_core import gapic_v1 |
42 | 44 | from google.api_core import retry as retries |
43 | 45 | from google.auth import credentials as ga_credentials # type: ignore |
| 46 | +from google.auth.exceptions import MutualTLSChannelError # type: ignore |
44 | 47 | from google.auth.transport import mtls # type: ignore |
45 | 48 | from google.auth.transport.grpc import SslCredentials # type: ignore |
46 | | -from google.auth.exceptions import MutualTLSChannelError # type: ignore |
47 | 49 | from google.oauth2 import service_account # type: ignore |
48 | 50 | import google.protobuf |
49 | 51 |
|
| 52 | +from google.cloud.firestore_admin_v1 import gapic_version as package_version |
| 53 | + |
50 | 54 | try: |
51 | 55 | OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] |
52 | 56 | except AttributeError: # pragma: NO COVER |
|
63 | 67 |
|
64 | 68 | from google.api_core import operation as gac_operation # type: ignore |
65 | 69 | from google.api_core import operation_async # type: ignore |
| 70 | +from google.cloud.location import locations_pb2 # type: ignore |
| 71 | +from google.longrunning import operations_pb2 # type: ignore |
| 72 | +from google.protobuf import duration_pb2 # type: ignore |
| 73 | +from google.protobuf import empty_pb2 # type: ignore |
| 74 | +from google.protobuf import field_mask_pb2 # type: ignore |
| 75 | +from google.protobuf import timestamp_pb2 # type: ignore |
| 76 | + |
66 | 77 | from google.cloud.firestore_admin_v1.services.firestore_admin import pagers |
67 | 78 | from google.cloud.firestore_admin_v1.types import backup |
68 | 79 | from google.cloud.firestore_admin_v1.types import database |
|
76 | 87 | from google.cloud.firestore_admin_v1.types import schedule |
77 | 88 | from google.cloud.firestore_admin_v1.types import user_creds |
78 | 89 | from google.cloud.firestore_admin_v1.types import user_creds as gfa_user_creds |
79 | | -from google.cloud.location import locations_pb2 # type: ignore |
80 | | -from google.longrunning import operations_pb2 # type: ignore |
81 | | -from google.protobuf import duration_pb2 # type: ignore |
82 | | -from google.protobuf import empty_pb2 # type: ignore |
83 | | -from google.protobuf import field_mask_pb2 # type: ignore |
84 | | -from google.protobuf import timestamp_pb2 # type: ignore |
85 | | -from .transports.base import FirestoreAdminTransport, DEFAULT_CLIENT_INFO |
| 90 | + |
| 91 | +from .transports.base import DEFAULT_CLIENT_INFO, FirestoreAdminTransport |
86 | 92 | from .transports.grpc import FirestoreAdminGrpcTransport |
87 | 93 | from .transports.grpc_asyncio import FirestoreAdminGrpcAsyncIOTransport |
88 | 94 | from .transports.rest import FirestoreAdminRestTransport |
@@ -198,6 +204,34 @@ def _get_default_mtls_endpoint(api_endpoint): |
198 | 204 | _DEFAULT_ENDPOINT_TEMPLATE = "firestore.{UNIVERSE_DOMAIN}" |
199 | 205 | _DEFAULT_UNIVERSE = "googleapis.com" |
200 | 206 |
|
| 207 | + @staticmethod |
| 208 | + def _use_client_cert_effective(): |
| 209 | + """Returns whether client certificate should be used for mTLS if the |
| 210 | + google-auth version supports should_use_client_cert automatic mTLS enablement. |
| 211 | +
|
| 212 | + Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. |
| 213 | +
|
| 214 | + Returns: |
| 215 | + bool: whether client certificate should be used for mTLS |
| 216 | + Raises: |
| 217 | + ValueError: (If using a version of google-auth without should_use_client_cert and |
| 218 | + GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) |
| 219 | + """ |
| 220 | + # check if google-auth version supports should_use_client_cert for automatic mTLS enablement |
| 221 | + if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER |
| 222 | + return mtls.should_use_client_cert() |
| 223 | + else: # pragma: NO COVER |
| 224 | + # if unsupported, fallback to reading from env var |
| 225 | + use_client_cert_str = os.getenv( |
| 226 | + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" |
| 227 | + ).lower() |
| 228 | + if use_client_cert_str not in ("true", "false"): |
| 229 | + raise ValueError( |
| 230 | + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" |
| 231 | + " either `true` or `false`" |
| 232 | + ) |
| 233 | + return use_client_cert_str == "true" |
| 234 | + |
201 | 235 | @classmethod |
202 | 236 | def from_service_account_info(cls, info: dict, *args, **kwargs): |
203 | 237 | """Creates an instance of this client using the provided credentials |
@@ -555,20 +589,16 @@ def get_mtls_endpoint_and_cert_source( |
555 | 589 | ) |
556 | 590 | if client_options is None: |
557 | 591 | client_options = client_options_lib.ClientOptions() |
558 | | - use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") |
| 592 | + use_client_cert = FirestoreAdminClient._use_client_cert_effective() |
559 | 593 | use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") |
560 | | - if use_client_cert not in ("true", "false"): |
561 | | - raise ValueError( |
562 | | - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" |
563 | | - ) |
564 | 594 | if use_mtls_endpoint not in ("auto", "never", "always"): |
565 | 595 | raise MutualTLSChannelError( |
566 | 596 | "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" |
567 | 597 | ) |
568 | 598 |
|
569 | 599 | # Figure out the client cert source to use. |
570 | 600 | client_cert_source = None |
571 | | - if use_client_cert == "true": |
| 601 | + if use_client_cert: |
572 | 602 | if client_options.client_cert_source: |
573 | 603 | client_cert_source = client_options.client_cert_source |
574 | 604 | elif mtls.has_default_client_cert_source(): |
@@ -600,20 +630,14 @@ def _read_environment_variables(): |
600 | 630 | google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT |
601 | 631 | is not any of ["auto", "never", "always"]. |
602 | 632 | """ |
603 | | - use_client_cert = os.getenv( |
604 | | - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" |
605 | | - ).lower() |
| 633 | + use_client_cert = FirestoreAdminClient._use_client_cert_effective() |
606 | 634 | use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() |
607 | 635 | universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") |
608 | | - if use_client_cert not in ("true", "false"): |
609 | | - raise ValueError( |
610 | | - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" |
611 | | - ) |
612 | 636 | if use_mtls_endpoint not in ("auto", "never", "always"): |
613 | 637 | raise MutualTLSChannelError( |
614 | 638 | "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" |
615 | 639 | ) |
616 | | - return use_client_cert == "true", use_mtls_endpoint, universe_domain_env |
| 640 | + return use_client_cert, use_mtls_endpoint, universe_domain_env |
617 | 641 |
|
618 | 642 | @staticmethod |
619 | 643 | def _get_client_cert_source(provided_cert_source, use_cert_flag): |
|
0 commit comments