| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 54502a7 commit 1535ecc
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -408,28 +408,23 @@ def client_cert_callback(): | |||
| 408 | 408 | ||
| 409 | 409 | ||
| 410 | 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. | ||
| 411 | + """Returns boolean for whether the client certificate should be used for mTLS. | ||
| 413 | 412 | ||
| 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". | ||
| 413 | + If GOOGLE_API_USE_CLIENT_CERTIFICATE is set to true or false, a corresponding | ||
| 414 | + bool value will be returned. If the value is set to an unexpected string, it | ||
| 415 | + will default to False. | ||
| 416 | + If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred | ||
| 417 | + by reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG, and verifying | ||
| 418 | + it contains a "workload" section. If so, the function will return True, | ||
| 419 | + otherwise False. | ||
| 422 | 420 | ||
| 423 | 421 | 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. | ||
| 422 | + bool: Whether the client certificate should be used for mTLS connection. | ||
| 428 | 423 | """ | |
| 429 | 424 | use_client_cert = getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE") | |
| 430 | 425 | # Check if the value of GOOGLE_API_USE_CLIENT_CERTIFICATE is set. | |
| 431 | 426 | if use_client_cert: | |
| 432 | - return use_client_cert.lower() | ||
| 427 | + return use_client_cert.lower() == "true" | ||
| 433 | 428 | else: | |
| 434 | 429 | # Check if the value of GOOGLE_API_CERTIFICATE_CONFIG is set. | |
| 435 | 430 | cert_path = getenv("GOOGLE_API_CERTIFICATE_CONFIG") | |
@@ -439,7 +434,7 @@ def check_use_client_cert(): | |||
| 439 | 434 | content = json.load(f) | |
| 440 | 435 | # verify json has workload key | |
| 441 | 436 | content["cert_configs"]["workload"] | |
| 442 | - return "true" | ||
| 437 | + return True | ||
| 443 | 438 | except ( | |
| 444 | 439 | FileNotFoundError, | |
| 445 | 440 | OSError, | |
@@ -448,4 +443,4 @@ def check_use_client_cert(): | |||
| 448 | 443 | json.JSONDecodeError, | |
| 449 | 444 | ) as e: | |
| 450 | 445 | _LOGGER.debug("error decoding certificate: %s", e) | |
| 451 | - return "false" | ||
| 446 | + return False | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,13 +255,13 @@ def my_client_cert_callback(): | |||
| 255 | 255 | # If SSL credentials are not explicitly set, try client_cert_callback and ADC. | |
| 256 | 256 | if not ssl_credentials: | |
| 257 | 257 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 258 | - if use_client_cert == "true" and client_cert_callback: | ||
| 258 | + if use_client_cert and client_cert_callback: | ||
| 259 | 259 | # Use the callback if provided. | |
| 260 | 260 | cert, key = client_cert_callback() | |
| 261 | 261 | ssl_credentials = grpc.ssl_channel_credentials( | |
| 262 | 262 | certificate_chain=cert, private_key=key | |
| 263 | 263 | ) | |
| 264 | - elif use_client_cert == "true": | ||
| 264 | + elif use_client_cert: | ||
| 265 | 265 | # Use application default SSL credentials. | |
| 266 | 266 | adc_ssl_credentils = SslCredentials() | |
| 267 | 267 | ssl_credentials = adc_ssl_credentils.ssl_credentials | |
@@ -292,7 +292,7 @@ class SslCredentials: | |||
| 292 | 292 | ||
| 293 | 293 | def __init__(self): | |
| 294 | 294 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 295 | - if use_client_cert != "true": | ||
| 295 | + if not use_client_cert: | ||
| 296 | 296 | self._is_mtls = False | |
| 297 | 297 | else: | |
| 298 | 298 | # Load client SSL credentials. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,3 +110,20 @@ def callback(): | |||
| 110 | 110 | return cert_path, key_path, passphrase_bytes | |
| 111 | 111 | ||
| 112 | 112 | return callback | |
| 113 | + | ||
| 114 | + | ||
| 115 | + def should_use_client_cert(): | ||
| 116 | + """Returns boolean for whether the client certificate should be used for mTLS. | ||
| 117 | + | ||
| 118 | + This is a wrapper around _mtls_helper.check_use_client_cert(). | ||
| 119 | + If GOOGLE_API_USE_CLIENT_CERTIFICATE is set to true or false, a corresponding | ||
| 120 | + bool value will be returned | ||
| 121 | + If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred by | ||
| 122 | + reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG, and verifying it | ||
| 123 | + contains a "workload" section. If so, the function will return True, | ||
| 124 | + otherwise False. | ||
| 125 | + | ||
| 126 | + Returns: | ||
| 127 | + bool: indicating whether the client certificate should be used for mTLS. | ||
| 128 | + """ | ||
| 129 | + return _mtls_helper.check_use_client_cert() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -443,7 +443,7 @@ def configure_mtls_channel(self, client_cert_callback=None): | |||
| 443 | 443 | creation failed for any reason. | |
| 444 | 444 | """ | |
| 445 | 445 | use_client_cert = google.auth.transport._mtls_helper.check_use_client_cert() | |
| 446 | - if use_client_cert != "true": | ||
| 446 | + if not use_client_cert: | ||
| 447 | 447 | self._is_mtls = False | |
| 448 | 448 | return | |
| 449 | 449 | try: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -334,7 +334,7 @@ def configure_mtls_channel(self, client_cert_callback=None): | |||
| 334 | 334 | creation failed for any reason. | |
| 335 | 335 | """ | |
| 336 | 336 | use_client_cert = transport._mtls_helper.check_use_client_cert() | |
| 337 | - if use_client_cert != "true": | ||
| 337 | + if not use_client_cert: | ||
| 338 | 338 | return False | |
| 339 | 339 | try: | |
| 340 | 340 | import OpenSSL | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -643,7 +643,7 @@ def test_crypto_error(self): | |||
| 643 | 643 | def test_check_use_client_cert(self, monkeypatch): | |
| 644 | 644 | monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true") | |
| 645 | 645 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 646 | - assert use_client_cert == "true" | ||
| 646 | + assert use_client_cert is True | ||
| 647 | 647 | ||
| 648 | 648 | def test_check_use_client_cert_for_workload_with_config_file(self, monkeypatch): | |
| 649 | 649 | config_data = { | |
@@ -663,19 +663,24 @@ def test_check_use_client_cert_for_workload_with_config_file(self, monkeypatch): | |||
| 663 | 663 | mock_file_handle = mock.mock_open(read_data=config_file_content) | |
| 664 | 664 | with mock.patch("builtins.open", mock_file_handle): | |
| 665 | 665 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 666 | - assert use_client_cert == "true" | ||
| 666 | + assert use_client_cert is True | ||
| 667 | 667 | ||
| 668 | 668 | def test_check_use_client_cert_false(self, monkeypatch): | |
| 669 | 669 | monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") | |
| 670 | 670 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 671 | - assert use_client_cert == "false" | ||
| 671 | + assert use_client_cert is False | ||
| 672 | + | ||
| 673 | + def test_check_use_client_cert_unsupported_value(self, monkeypatch): | ||
| 674 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "dummy") | ||
| 675 | + use_client_cert = _mtls_helper.check_use_client_cert() | ||
| 676 | + assert use_client_cert is False | ||
| 672 | 677 | ||
| 673 | 678 | def test_check_use_client_cert_for_workload_with_config_file_not_found( | |
| 674 | 679 | self, monkeypatch | |
| 675 | 680 | ): | |
| 676 | 681 | monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | |
| 677 | 682 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 678 | - assert use_client_cert == "false" | ||
| 683 | + assert use_client_cert is False | ||
| 679 | 684 | ||
| 680 | 685 | def test_check_use_client_cert_for_workload_with_config_file_not_json( | |
| 681 | 686 | self, monkeypatch | |
@@ -688,7 +693,7 @@ def test_check_use_client_cert_for_workload_with_config_file_not_json( | |||
| 688 | 693 | mock_file_handle = mock.mock_open(read_data=config_file_content) | |
| 689 | 694 | with mock.patch("builtins.open", mock_file_handle): | |
| 690 | 695 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 691 | - assert use_client_cert == "false" | ||
| 696 | + assert use_client_cert is False | ||
| 692 | 697 | ||
| 693 | 698 | def test_check_use_client_cert_for_workload_with_config_file_no_workload( | |
| 694 | 699 | self, monkeypatch | |
@@ -702,11 +707,11 @@ def test_check_use_client_cert_for_workload_with_config_file_no_workload( | |||
| 702 | 707 | mock_file_handle = mock.mock_open(read_data=config_file_content) | |
| 703 | 708 | with mock.patch("builtins.open", mock_file_handle): | |
| 704 | 709 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 705 | - assert use_client_cert == "false" | ||
| 710 | + assert use_client_cert is False | ||
| 706 | 711 | ||
| 707 | 712 | def test_check_use_client_cert_when_file_does_not_exist(self, monkeypatch): | |
| 708 | 713 | config_filename = "mock_certificate_config.json" | |
| 709 | 714 | monkeypatch.setenv("GOOGLE_API_CERTIFICATE_CONFIG", config_filename) | |
| 710 | 715 | monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") | |
| 711 | 716 | use_client_cert = _mtls_helper.check_use_client_cert() | |
| 712 | - assert use_client_cert == "false" | ||
| 717 | + assert use_client_cert is False | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -94,3 +94,12 @@ def test_default_client_encrypted_cert_source( | |||
| 94 | 94 | callback = mtls.default_client_encrypted_cert_source("cert_path", "key_path") | |
| 95 | 95 | with pytest.raises(exceptions.MutualTLSChannelError): | |
| 96 | 96 | callback() | |
| 97 | + | ||
| 98 | + | ||
| 99 | + @mock.patch("google.auth.transport._mtls_helper.check_use_client_cert", autospec=True) | ||
| 100 | + def test_should_use_client_cert(check_use_client_cert): | ||
| 101 | + check_use_client_cert.return_value = mock.Mock() | ||
| 102 | + assert mtls.should_use_client_cert() | ||
| 103 | + | ||
| 104 | + check_use_client_cert.return_value = False | ||
| 105 | + assert not mtls.should_use_client_cert() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments