-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(api-core): centralize MTLS fallback functions in gapic_v1 config #17817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,14 @@ | |
| """ | ||
|
|
||
| import collections | ||
| import os | ||
| from typing import Callable, Optional, Tuple | ||
|
|
||
| import grpc | ||
|
|
||
| from google.api_core import exceptions, retry, timeout | ||
| from google.auth.exceptions import MutualTLSChannelError # type: ignore | ||
| from google.auth.transport import mtls # type: ignore | ||
|
|
||
| _MILLIS_PER_SECOND = 1000.0 | ||
|
|
||
|
|
@@ -170,3 +174,53 @@ def parse_method_configs(interface_config, retry_impl=retry.Retry): | |
| method_configs[method_name] = MethodConfig(retry=retry_, timeout=timeout_) | ||
|
|
||
| return method_configs | ||
|
|
||
|
|
||
| def use_client_cert_effective() -> bool: | ||
| """Returns whether client certificate should be used for mTLS.""" | ||
| if hasattr(mtls, "should_use_client_cert"): | ||
| return mtls.should_use_client_cert() | ||
| else: | ||
| use_client_cert_str = os.getenv( | ||
| "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" | ||
| ).lower() | ||
| if use_client_cert_str not in ("true", "false"): | ||
| raise ValueError( | ||
| "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" | ||
| " either `true` or `false`" | ||
| ) | ||
| return use_client_cert_str == "true" | ||
|
|
||
|
|
||
| def get_client_cert_source( | ||
| provided_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]], | ||
| use_cert_flag: bool, | ||
| ) -> Optional[Callable[[], Tuple[bytes, bytes]]]: | ||
| """Return the client cert source to be used by the client.""" | ||
| client_cert_source = None | ||
| if use_cert_flag: | ||
| if provided_cert_source: | ||
| client_cert_source = provided_cert_source | ||
| elif ( | ||
| hasattr(mtls, "has_default_client_cert_source") | ||
| and mtls.has_default_client_cert_source() | ||
| ): | ||
| client_cert_source = mtls.default_client_cert_source() | ||
| else: | ||
| raise ValueError( | ||
| "Client certificate is required for mTLS, but no client certificate source was provided or found." | ||
| ) | ||
| return client_cert_source | ||
|
|
||
|
|
||
| def read_environment_variables() -> Tuple[bool, str, Optional[str]]: | ||
| """Returns the environment variables used by the client.""" | ||
| use_client_cert = use_client_cert_effective() | ||
| use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() | ||
| universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") | ||
| if use_mtls_endpoint not in ("auto", "never", "always"): | ||
| raise MutualTLSChannelError( | ||
| "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` " | ||
| "must be `never`, `auto` or `always`" | ||
| ) | ||
| return use_client_cert, use_mtls_endpoint, universe_domain_env | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My feedback from #17750 (comment) stands: can we defer to google.auth.transport.mtls.should_use_mtls_endpoint to parse this variable? That would be a real centralization improvement, since that's the source of truth for this kind of thing I don't actually think read_environment_variables is worthwile in api-core, since it's just reading back a couple variables. We probably don't need a helper for that |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think use_client_cert_effective or get_client_cert_source need to be in api-core either. These are basically just aliases to
google-auth, and it creates an extra dependency link for no reason.In the clients, we can just reach out to
mtls.default_client_cert_source(), and provide a fallback in _compat