1616#
1717
1818from collections import OrderedDict
19+ from distutils import util
1920import os
2021import re
21- from typing import Callable , Dict , Sequence , Tuple , Type , Union
22+ from typing import Callable , Dict , Optional , Sequence , Tuple , Type , Union
2223import pkg_resources
2324
24- import google .api_core . client_options as ClientOptions # type: ignore
25+ from google .api_core import client_options as client_options_lib # type: ignore
2526from google .api_core import exceptions # type: ignore
2627from google .api_core import gapic_v1 # type: ignore
2728from google .api_core import retry as retries # type: ignore
2829from google .auth import credentials # type: ignore
2930from google .auth .transport import mtls # type: ignore
31+ from google .auth .transport .grpc import SslCredentials # type: ignore
3032from google .auth .exceptions import MutualTLSChannelError # type: ignore
3133from google .oauth2 import service_account # type: ignore
3234
33- from google .api_core import operation
34- from google .api_core import operation_async
35+ from google .api_core import operation # type: ignore
36+ from google .api_core import operation_async # type: ignore
3537from google .cloud .automl_v1 .services .auto_ml import pagers
3638from google .cloud .automl_v1 .types import annotation_spec
3739from google .cloud .automl_v1 .types import classification
@@ -196,9 +198,9 @@ def parse_model_path(path: str) -> Dict[str, str]:
196198 def __init__ (
197199 self ,
198200 * ,
199- credentials : credentials .Credentials = None ,
200- transport : Union [str , AutoMlTransport ] = None ,
201- client_options : ClientOptions = None ,
201+ credentials : Optional [ credentials .Credentials ] = None ,
202+ transport : Union [str , AutoMlTransport , None ] = None ,
203+ client_options : Optional [ client_options_lib . ClientOptions ] = None ,
202204 client_info : gapic_v1 .client_info .ClientInfo = DEFAULT_CLIENT_INFO ,
203205 ) -> None :
204206 """Instantiate the auto ml client.
@@ -212,19 +214,22 @@ def __init__(
212214 transport (Union[str, ~.AutoMlTransport]): The
213215 transport to use. If set to None, a transport is chosen
214216 automatically.
215- client_options (ClientOptions): Custom options for the client. It
216- won't take effect if a ``transport`` instance is provided.
217+ client_options (client_options_lib. ClientOptions): Custom options for the
218+ client. It won't take effect if a ``transport`` instance is provided.
217219 (1) The ``api_endpoint`` property can be used to override the
218- default endpoint provided by the client. GOOGLE_API_USE_MTLS
220+ default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
219221 environment variable can also be used to override the endpoint:
220222 "always" (always use the default mTLS endpoint), "never" (always
221- use the default regular endpoint, this is the default value for
222- the environment variable) and "auto" (auto switch to the default
223- mTLS endpoint if client SSL credentials is present). However,
224- the ``api_endpoint`` property takes precedence if provided.
225- (2) The ``client_cert_source`` property is used to provide client
226- SSL credentials for mutual TLS transport. If not provided, the
227- default SSL credentials will be used if present.
223+ use the default regular endpoint) and "auto" (auto switch to the
224+ default mTLS endpoint if client certificate is present, this is
225+ the default value). However, the ``api_endpoint`` property takes
226+ precedence if provided.
227+ (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable
228+ is "true", then the ``client_cert_source`` property can be used
229+ to provide client certificate for mutual TLS transport. If
230+ not provided, the default SSL client certificate will be used if
231+ present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
232+ set, no client certificate will be used.
228233 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
229234 The client info used to send a user-agent string along with
230235 API requests. If ``None``, then default info will be used.
@@ -236,29 +241,47 @@ def __init__(
236241 creation failed for any reason.
237242 """
238243 if isinstance (client_options , dict ):
239- client_options = ClientOptions .from_dict (client_options )
244+ client_options = client_options_lib .from_dict (client_options )
240245 if client_options is None :
241- client_options = ClientOptions .ClientOptions ()
246+ client_options = client_options_lib .ClientOptions ()
242247
243- if client_options .api_endpoint is None :
244- use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS" , "never" )
248+ # Create SSL credentials for mutual TLS if needed.
249+ use_client_cert = bool (
250+ util .strtobool (os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ))
251+ )
252+
253+ ssl_credentials = None
254+ is_mtls = False
255+ if use_client_cert :
256+ if client_options .client_cert_source :
257+ import grpc # type: ignore
258+
259+ cert , key = client_options .client_cert_source ()
260+ ssl_credentials = grpc .ssl_channel_credentials (
261+ certificate_chain = cert , private_key = key
262+ )
263+ is_mtls = True
264+ else :
265+ creds = SslCredentials ()
266+ is_mtls = creds .is_mtls
267+ ssl_credentials = creds .ssl_credentials if is_mtls else None
268+
269+ # Figure out which api endpoint to use.
270+ if client_options .api_endpoint is not None :
271+ api_endpoint = client_options .api_endpoint
272+ else :
273+ use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
245274 if use_mtls_env == "never" :
246- client_options . api_endpoint = self .DEFAULT_ENDPOINT
275+ api_endpoint = self .DEFAULT_ENDPOINT
247276 elif use_mtls_env == "always" :
248- client_options . api_endpoint = self .DEFAULT_MTLS_ENDPOINT
277+ api_endpoint = self .DEFAULT_MTLS_ENDPOINT
249278 elif use_mtls_env == "auto" :
250- has_client_cert_source = (
251- client_options .client_cert_source is not None
252- or mtls .has_default_client_cert_source ()
253- )
254- client_options .api_endpoint = (
255- self .DEFAULT_MTLS_ENDPOINT
256- if has_client_cert_source
257- else self .DEFAULT_ENDPOINT
279+ api_endpoint = (
280+ self .DEFAULT_MTLS_ENDPOINT if is_mtls else self .DEFAULT_ENDPOINT
258281 )
259282 else :
260283 raise MutualTLSChannelError (
261- "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always"
284+ "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always"
262285 )
263286
264287 # Save or instantiate the transport.
@@ -282,10 +305,9 @@ def __init__(
282305 self ._transport = Transport (
283306 credentials = credentials ,
284307 credentials_file = client_options .credentials_file ,
285- host = client_options . api_endpoint ,
308+ host = api_endpoint ,
286309 scopes = client_options .scopes ,
287- api_mtls_endpoint = client_options .api_endpoint ,
288- client_cert_source = client_options .client_cert_source ,
310+ ssl_channel_credentials = ssl_credentials ,
289311 quota_project_id = client_options .quota_project_id ,
290312 client_info = client_info ,
291313 )
0 commit comments