Skip to content

Commit b13eb7a

Browse files
feat: Autoupdate the GOOGLE_API_USE_CLIENT_CERTIFICATE flag to true if not set, if the MWID/X.509 cert sources detected
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent aca34b5 commit b13eb7a

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

google/auth/transport/_mtls_helper.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818
import logging
1919
from os import environ, path
20+
import os
2021
import re
2122
import subprocess
2223

@@ -405,3 +406,14 @@ def client_cert_callback():
405406

406407
# Then dump the decrypted key bytes
407408
return crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)
409+
410+
def check_use_client_cert_for_workload(use_client_cert):
411+
"""Checks if the workload should use client cert for mutual TLS."""
412+
if use_client_cert == "":
413+
cert_path = os.getenv("GOOGLE_API_CERTIFICATE_CONFIG")
414+
if cert_path:
415+
with open(cert_path, "r") as f:
416+
content = f.read()
417+
if "workload" in content:
418+
return True
419+
return False

google/auth/transport/requests.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,20 @@ def configure_mtls_channel(self, client_cert_callback=None):
445445
creation failed for any reason.
446446
"""
447447
use_client_cert = os.getenv(
448-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false"
449-
)
448+
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE)
450449
if use_client_cert != "true":
451-
self._is_mtls = False
452-
return
453-
450+
## Checking if the GOOGLE_API_USE_CLIENT_CERTIFICATE is unset.
451+
if _mtls_helper.check_use_client_cert_for_workload(
452+
use_client_cert
453+
):
454+
os.putenv(
455+
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "true"
456+
)
457+
use_client_cert = "true"
458+
else:
459+
use_client_cert = "false"
460+
self._is_mtls = False
461+
return
454462
try:
455463
import OpenSSL
456464
except ImportError as caught_exc:

google/auth/transport/urllib3.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,20 @@ def configure_mtls_channel(self, client_cert_callback=None):
336336
creation failed for any reason.
337337
"""
338338
use_client_cert = os.getenv(
339-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false"
340-
)
339+
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE)
341340
if use_client_cert != "true":
342-
return False
341+
## Check if workload is present in the certificate config file
342+
## and GOOGLE_API_USE_CLIENT_CERTIFICATE is unset.
343+
if _mtls_helper.check_use_client_cert_for_workload(
344+
use_client_cert
345+
):
346+
os.putenv(
347+
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "true"
348+
)
349+
use_client_cert = "true"
350+
else:
351+
use_client_cert = "false"
352+
return False
343353

344354
try:
345355
import OpenSSL

tests/transport/test__mtls_helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
import os
1617
import re
1718

@@ -638,3 +639,28 @@ def test_crypto_error(self):
638639
_mtls_helper.decrypt_private_key(
639640
ENCRYPTED_EC_PRIVATE_KEY, b"wrong_password"
640641
)
642+
643+
def test_check_use_client_cert_for_workload(self):
644+
use_client_cert = _mtls_helper.check_use_client_cert_for_workload("")
645+
assert use_client_cert == False
646+
647+
def test_check_use_client_cert_for_workload_with_config_file(self):
648+
config_data = {
649+
"version": 1,
650+
"cert_configs": {
651+
"workload": {
652+
"cert_path": "path/to/cert/file",
653+
"key_path": "path/to/key/file",
654+
}
655+
},
656+
}
657+
config_filename = "mock_certificate_config.json"
658+
config_file_content = json.dumps(config_data)
659+
# Use mock_open to simulate the file in memory
660+
m = mock.mock_open(read_data=config_file_content)
661+
with mock.patch("builtins.open", m):
662+
os.environ["GOOGLE_API_CERTIFICATE_CONFIG"] = config_filename
663+
use_client_cert = _mtls_helper.check_use_client_cert_for_workload(
664+
""
665+
)
666+
assert use_client_cert == True

0 commit comments

Comments
 (0)