-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(generator): move read_environment_variables to _compat.py.j2
#17997
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 |
|---|---|---|
|
|
@@ -50,6 +50,34 @@ except ImportError: # pragma: NO COVER | |
| ) | ||
| return use_client_cert == "true" | ||
|
|
||
|
|
||
| {# TODO(https://github.com/googleapis/google-cloud-python/issues/17883): | ||
| Defer to google.auth.transport.mtls.should_use_mtls_endpoint to parse | ||
| GOOGLE_API_USE_MTLS_ENDPOINT when available in minimum supported google-auth. #} | ||
|
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. Can you add the minimum supported version here? |
||
| def read_environment_variables(): | ||
| """Returns the environment variables used by the client. | ||
|
|
||
| Returns: | ||
| Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE, | ||
| GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables. | ||
|
|
||
| Raises: | ||
| ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not | ||
| any of ["true", "false"]. | ||
| google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT | ||
| is not any of ["auto", "never", "always"]. | ||
| """ | ||
| use_client_cert = should_use_client_cert() | ||
|
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. |
||
| 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. it seems like we could return a bool for use_mtls_endpoint, to align with the auth implementaiton. IIUC, the only place this value is used, we convert it to a bool anyway: |
||
|
|
||
|
|
||
| DEFAULT_UNIVERSE = "googleapis.com" | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import google.auth.transport.mtls | |
|
|
||
| {% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %} | ||
| from {{package_path}}._compat import transcode_request | ||
| from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert | ||
| from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables | ||
|
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.
|
||
| {% if has_auto_populated_fields %} | ||
| from {{package_path}}._compat import setup_request_id | ||
| {% endif %} | ||
|
|
@@ -497,4 +497,16 @@ def test_transcode_request_proto_plus_wrapper(): | |
| transcoded, _, _ = transcode_request(http_options, mock_proto_plus) | ||
| assert transcoded["uri"] == "/v1/test/proto-plus-field" | ||
|
|
||
|
|
||
| def test_read_environment_variables(): | ||
| with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true", "GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}): | ||
| use_cert, mtls_endpoint, universe_domain = read_environment_variables() | ||
| assert use_cert is True | ||
| assert mtls_endpoint == "always" | ||
| assert universe_domain == "foo.com" | ||
|
|
||
| with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): | ||
| with pytest.raises(MutualTLSChannelError): | ||
| read_environment_variables() | ||
|
|
||
| {% endblock %} | ||
Uh oh!
There was an error while loading. Please reload this page.