feat(iam): support an alternative env to decide if mtls should be ena… · googleapis/google-cloud-python@89fc6f2 · GitHub
Skip to content

Commit 89fc6f2

Browse files
authored
feat(iam): support an alternative env to decide if mtls should be enabled (#1945)
`CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE` is another endpoint that can be set in Gcloud CLI to enable Certificate Based Access. We should support it as well.
1 parent 4598454 commit 89fc6f2

4 files changed

Lines changed: 139 additions & 20 deletions

File tree

packages/google-auth/google/auth/environment_vars.py

Lines changed: 12 additions & 0 deletions

packages/google-auth/google/auth/iam.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
import base64
2323
import http.client as http_client
2424
import json
25-
import os
2625

2726
from google.auth import _exponential_backoff
2827
from google.auth import _helpers
2928
from google.auth import credentials
3029
from google.auth import crypt
3130
from google.auth import exceptions
32-
from google.auth.transport import mtls
31+
from google.auth.transport import _mtls_helper
3332

3433
IAM_RETRY_CODES = {
3534
http_client.INTERNAL_SERVER_ERROR,
@@ -40,20 +39,12 @@
4039

4140
_IAM_SCOPE = ["https://www.googleapis.com/auth/iam"]
4241

43-
# 1. Determine if we should use mTLS.
44-
# Note: We only support automatic mTLS on the default googleapis.com universe.
45-
if hasattr(mtls, "should_use_client_cert"):
46-
use_client_cert = mtls.should_use_client_cert()
47-
else: # pragma: NO COVER
48-
# if unsupported, fallback to reading from env var
49-
use_client_cert = (
50-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() == "true"
51-
)
52-
53-
# 2. Construct the template domain using the library's DEFAULT_UNIVERSE_DOMAIN constant.
54-
# This ensures that the .replace() calls in the classes will work correctly.
55-
if use_client_cert:
56-
# We use the .mtls. prefix only for the default universe template
42+
# Determine if we should use mTLS.
43+
if (
44+
hasattr(_mtls_helper, "check_use_client_cert")
45+
and _mtls_helper.check_use_client_cert()
46+
):
47+
# Construct the template domain using the library's DEFAULT_UNIVERSE_DOMAIN constant.
5748
_IAM_DOMAIN = f"iamcredentials.mtls.{credentials.DEFAULT_UNIVERSE_DOMAIN}"
5849
else:
5950
_IAM_DOMAIN = f"iamcredentials.{credentials.DEFAULT_UNIVERSE_DOMAIN}"

packages/google-auth/google/auth/transport/_mtls_helper.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ def _get_cert_config_path(certificate_config_path=None):
151151
if env_path is not None and env_path != "":
152152
certificate_config_path = env_path
153153
else:
154-
certificate_config_path = CERTIFICATE_CONFIGURATION_DEFAULT_PATH
154+
env_path = environ.get(
155+
environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH,
156+
None,
157+
)
158+
if env_path is not None and env_path != "":
159+
certificate_config_path = env_path
160+
else:
161+
certificate_config_path = CERTIFICATE_CONFIGURATION_DEFAULT_PATH
155162

156163
certificate_config_path = path.expanduser(certificate_config_path)
157164
if not path.exists(certificate_config_path):
@@ -452,13 +459,23 @@ def check_use_client_cert():
452459
Returns:
453460
bool: Whether the client certificate should be used for mTLS connection.
454461
"""
455-
use_client_cert = getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE")
462+
use_client_cert = getenv(environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE)
463+
if use_client_cert is None or use_client_cert == "":
464+
use_client_cert = getenv(
465+
environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE
466+
)
467+
456468
# Check if the value of GOOGLE_API_USE_CLIENT_CERTIFICATE is set.
457469
if use_client_cert:
458470
return use_client_cert.lower() == "true"
459471
else:
460472
# Check if the value of GOOGLE_API_CERTIFICATE_CONFIG is set.
461-
cert_path = getenv("GOOGLE_API_CERTIFICATE_CONFIG")
473+
cert_path = getenv(environment_vars.GOOGLE_API_CERTIFICATE_CONFIG)
474+
if cert_path is None:
475+
cert_path = getenv(
476+
environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH
477+
)
478+
462479
if cert_path:
463480
try:
464481
with open(cert_path, "r") as f:

packages/google-auth/tests/transport/test__mtls_helper.py

Lines changed: 100 additions & 1 deletion

0 commit comments

Comments
 (0)