| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f2708b2 commit 395e405
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | 17 | import json | |
| 18 | 18 | import logging | |
| 19 | - from os import environ, path | ||
| 19 | + from os import environ, getenv, path | ||
| 20 | 20 | import re | |
| 21 | 21 | import subprocess | |
| 22 | 22 | ||
@@ -405,3 +405,47 @@ def client_cert_callback(): | |||
| 405 | 405 | ||
| 406 | 406 | # Then dump the decrypted key bytes | |
| 407 | 407 | return crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey) | |
| 408 | + | ||
| 409 | + | ||
| 410 | + def check_use_client_cert(): | ||
| 411 | + """Returns the value of the GOOGLE_API_USE_CLIENT_CERTIFICATE variable, | ||
| 412 | + or an inferred value('true' or 'false') if unset. | ||
| 413 | + | ||
| 414 | + This value is meant to be interpreted as a "true" or "false" value | ||
| 415 | + representing whether the client certificate should be used, but could be any | ||
| 416 | + arbitrary string. | ||
| 417 | + | ||
| 418 | + If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value value will be | ||
| 419 | + inferred by reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG, and | ||
| 420 | + verifying it contains a "workload" section. If so, the function will return | ||
| 421 | + "true", otherwise "false". | ||
| 422 | + | ||
| 423 | + Returns: | ||
| 424 | + str: The value of GOOGLE_API_USE_CLIENT_CERTIFICATE, or an inferred value | ||
| 425 | + ("true" or "false") if unset. This string should contain a value, but may | ||
| 426 | + be an any arbitrary string read from the user's set | ||
| 427 | + GOOGLE_API_USE_CLIENT_CERTIFICATE. | ||
| 428 | + """ | ||
| 429 | + use_client_cert = getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE") | ||
| 430 | + # Check if the value of GOOGLE_API_USE_CLIENT_CERTIFICATE is set. | ||
| 431 | + if use_client_cert: | ||
| 432 | + return use_client_cert.lower() | ||
| 433 | + else: | ||
| 434 | + # Check if the value of GOOGLE_API_CERTIFICATE_CONFIG is set. | ||
| 435 | + cert_path = getenv("GOOGLE_API_CERTIFICATE_CONFIG") | ||
| 436 | + if cert_path: | ||
| 437 | + try: | ||
| 438 | + with open(cert_path, "r") as f: | ||
| 439 | + content = json.load(f) | ||
| 440 | + # verify json has workload key | ||
| 441 | + content["cert_configs"]["workload"] | ||
| 442 | + return "true" | ||
| 443 | + except ( | ||
| 444 | + FileNotFoundError, | ||
| 445 | + OSError, | ||
| 446 | + KeyError, | ||
| 447 | + TypeError, | ||
| 448 | + json.JSONDecodeError, | ||
| 449 | + ) as e: | ||
| 450 | + _LOGGER.debug("error decoding certificate: %s", e) | ||
| 451 | + return "false" | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,9 +17,7 @@ | |||
| 17 | 17 | from __future__ import absolute_import | |
| 18 | 18 | ||
| 19 | 19 | import logging | |
| 20 | - import os | ||
| 21 | 20 | ||
| 22 | - from google.auth import environment_vars | ||
| 23 | 21 | from google.auth import exceptions | |
| 24 | 22 | from google.auth.transport import _mtls_helper | |
| 25 | 23 | from google.oauth2 import service_account | |
@@ -256,9 +254,7 @@ def my_client_cert_callback(): | |||
| 256 | 254 | ||
| 257 | 255 | # If SSL credentials are not explicitly set, try client_cert_callback and ADC. | |
| 258 | 256 | if not ssl_credentials: | |
| 259 | - use_client_cert = os.getenv( | ||
| 260 | - environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false" | ||
| 261 | - ) | ||
| 257 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 262 | 258 | if use_client_cert == "true" and client_cert_callback: | |
| 263 | 259 | # Use the callback if provided. | |
| 264 | 260 | cert, key = client_cert_callback() | |
@@ -295,9 +291,7 @@ class SslCredentials: | |||
| 295 | 291 | """ | |
| 296 | 292 | ||
| 297 | 293 | def __init__(self): | |
| 298 | - use_client_cert = os.getenv( | ||
| 299 | - environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false" | ||
| 300 | - ) | ||
| 294 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 301 | 295 | if use_client_cert != "true": | |
| 302 | 296 | self._is_mtls = False | |
| 303 | 297 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,6 @@ | |||
| 19 | 19 | import functools | |
| 20 | 20 | import logging | |
| 21 | 21 | import numbers | |
| 22 | - import os | ||
| 23 | 22 | import time | |
| 24 | 23 | ||
| 25 | 24 | try: | |
@@ -35,7 +34,6 @@ | |||
| 35 | 34 | ) # pylint: disable=ungrouped-imports | |
| 36 | 35 | ||
| 37 | 36 | from google.auth import _helpers | |
| 38 | - from google.auth import environment_vars | ||
| 39 | 37 | from google.auth import exceptions | |
| 40 | 38 | from google.auth import transport | |
| 41 | 39 | import google.auth.transport._mtls_helper | |
@@ -444,13 +442,10 @@ def configure_mtls_channel(self, client_cert_callback=None): | |||
| 444 | 442 | google.auth.exceptions.MutualTLSChannelError: If mutual TLS channel | |
| 445 | 443 | creation failed for any reason. | |
| 446 | 444 | """ | |
| 447 | - use_client_cert = os.getenv( | ||
| 448 | - environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false" | ||
| 449 | - ) | ||
| 445 | + use_client_cert = google.auth.transport._mtls_helper.check_use_client_cert() | ||
| 450 | 446 | if use_client_cert != "true": | |
| 451 | 447 | self._is_mtls = False | |
| 452 | 448 | return | |
| 453 | - | ||
| 454 | 449 | try: | |
| 455 | 450 | import OpenSSL | |
| 456 | 451 | except ImportError as caught_exc: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,7 +17,6 @@ | |||
| 17 | 17 | from __future__ import absolute_import | |
| 18 | 18 | ||
| 19 | 19 | import logging | |
| 20 | - import os | ||
| 21 | 20 | import warnings | |
| 22 | 21 | ||
| 23 | 22 | # Certifi is Mozilla's certificate bundle. Urllib3 needs a certificate bundle | |
@@ -51,7 +50,6 @@ | |||
| 51 | 50 | ||
| 52 | 51 | ||
| 53 | 52 | from google.auth import _helpers | |
| 54 | - from google.auth import environment_vars | ||
| 55 | 53 | from google.auth import exceptions | |
| 56 | 54 | from google.auth import transport | |
| 57 | 55 | from google.oauth2 import service_account | |
@@ -335,12 +333,9 @@ def configure_mtls_channel(self, client_cert_callback=None): | |||
| 335 | 333 | google.auth.exceptions.MutualTLSChannelError: If mutual TLS channel | |
| 336 | 334 | creation failed for any reason. | |
| 337 | 335 | """ | |
| 338 | - use_client_cert = os.getenv( | ||
| 339 | - environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false" | ||
| 340 | - ) | ||
| 336 | + use_client_cert = transport._mtls_helper.check_use_client_cert() | ||
| 341 | 337 | if use_client_cert != "true": | |
| 342 | 338 | return False | |
| 343 | - | ||
| 344 | 339 | try: | |
| 345 | 340 | import OpenSSL | |
| 346 | 341 | except ImportError as caught_exc: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ | |||
| 12 | 12 | # See the License for the specific language governing permissions and | |
| 13 | 13 | # limitations under the License. | |
| 14 | 14 | ||
| 15 | + import json | ||
| 15 | 16 | import os | |
| 16 | 17 | import re | |
| 17 | 18 | ||
@@ -638,3 +639,74 @@ def test_crypto_error(self): | |||
| 638 | 639 | _mtls_helper.decrypt_private_key( | |
| 639 | 640 | ENCRYPTED_EC_PRIVATE_KEY, b"wrong_password" | |
| 640 | 641 | ) | |
| 642 | + | ||
| 643 | + def test_check_use_client_cert(self, monkeypatch): | ||
| 644 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true") | ||
| 645 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 646 | + assert use_client_cert == "true" | ||
| 647 | + | ||
| 648 | + def test_check_use_client_cert_for_workload_with_config_file(self, monkeypatch): | ||
| 649 | + config_data = { | ||
| 650 | + "version": 1, | ||
| 651 | + "cert_configs": { | ||
| 652 | + "workload": { | ||
| 653 | + "cert_path": "path/to/cert/file", | ||
| 654 | + "key_path": "path/to/key/file", | ||
| 655 | + } | ||
| 656 | + }, | ||
| 657 | + } | ||
| 658 | + config_filename = "mock_certificate_config.json" | ||
| 659 | + config_file_content = json.dumps(config_data) | ||
| 660 | + monkeypatch.setenv("GOOGLE_API_CERTIFICATE_CONFIG", config_filename) | ||
| 661 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | ||
| 662 | + # Use mock_open to simulate the file in memory | ||
| 663 | + mock_file_handle = mock.mock_open(read_data=config_file_content) | ||
| 664 | + with mock.patch("builtins.open", mock_file_handle): | ||
| 665 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 666 | + assert use_client_cert == "true" | ||
| 667 | + | ||
| 668 | + def test_check_use_client_cert_false(self, monkeypatch): | ||
| 669 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") | ||
| 670 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 671 | + assert use_client_cert == "false" | ||
| 672 | + | ||
| 673 | + def test_check_use_client_cert_for_workload_with_config_file_not_found( | ||
| 674 | + self, monkeypatch | ||
| 675 | + ): | ||
| 676 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | ||
| 677 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 678 | + assert use_client_cert == "false" | ||
| 679 | + | ||
| 680 | + def test_check_use_client_cert_for_workload_with_config_file_not_json( | ||
| 681 | + self, monkeypatch | ||
| 682 | + ): | ||
| 683 | + config_filename = "mock_certificate_config.json" | ||
| 684 | + config_file_content = "not_valid_json" | ||
| 685 | + monkeypatch.setenv("GOOGLE_API_CERTIFICATE_CONFIG", config_filename) | ||
| 686 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | ||
| 687 | + # Use mock_open to simulate the file in memory | ||
| 688 | + mock_file_handle = mock.mock_open(read_data=config_file_content) | ||
| 689 | + with mock.patch("builtins.open", mock_file_handle): | ||
| 690 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 691 | + assert use_client_cert == "false" | ||
| 692 | + | ||
| 693 | + def test_check_use_client_cert_for_workload_with_config_file_no_workload( | ||
| 694 | + self, monkeypatch | ||
| 695 | + ): | ||
| 696 | + config_data = {"version": 1, "cert_configs": {"dummy_key": {}}} | ||
| 697 | + config_filename = "mock_certificate_config.json" | ||
| 698 | + config_file_content = json.dumps(config_data) | ||
| 699 | + monkeypatch.setenv("GOOGLE_API_CERTIFICATE_CONFIG", config_filename) | ||
| 700 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | ||
| 701 | + # Use mock_open to simulate the file in memory | ||
| 702 | + mock_file_handle = mock.mock_open(read_data=config_file_content) | ||
| 703 | + with mock.patch("builtins.open", mock_file_handle): | ||
| 704 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 705 | + assert use_client_cert == "false" | ||
| 706 | + | ||
| 707 | + def test_check_use_client_cert_when_file_does_not_exist(self, monkeypatch): | ||
| 708 | + config_filename = "mock_certificate_config.json" | ||
| 709 | + monkeypatch.setenv("GOOGLE_API_CERTIFICATE_CONFIG", config_filename) | ||
| 710 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | ||
| 711 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 712 | + assert use_client_cert == "false" | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments