@@ -216,9 +216,12 @@ def test_secure_authorized_channel_adc_without_client_cert_env(
216216 request = mock .create_autospec (transport .Request )
217217 target = "example.com:80"
218218
219- channel = google .auth .transport .grpc .secure_authorized_channel (
220- credentials , request , target , options = mock .sentinel .options
221- )
219+ with mock .patch .dict (
220+ os .environ , {environment_vars .GOOGLE_API_USE_CLIENT_CERTIFICATE : "false" }
221+ ):
222+ channel = google .auth .transport .grpc .secure_authorized_channel (
223+ credentials , request , target , options = mock .sentinel .options
224+ )
222225
223226 # Check the auth plugin construction.
224227 auth_plugin = metadata_call_credentials .call_args [0 ][0 ]
@@ -375,9 +378,12 @@ def test_secure_authorized_channel_cert_callback_without_client_cert_env(
375378 target = "example.com:80"
376379 client_cert_callback = mock .Mock ()
377380
378- google .auth .transport .grpc .secure_authorized_channel (
379- credentials , request , target , client_cert_callback = client_cert_callback
380- )
381+ with mock .patch .dict (
382+ os .environ , {environment_vars .GOOGLE_API_USE_CLIENT_CERTIFICATE : "false" }
383+ ):
384+ google .auth .transport .grpc .secure_authorized_channel (
385+ credentials , request , target , client_cert_callback = client_cert_callback
386+ )
381387
382388 # Check client_cert_callback is not called because GOOGLE_API_USE_CLIENT_CERTIFICATE
383389 # is not set.
@@ -510,12 +516,61 @@ def test_get_client_ssl_credentials_without_client_cert_env(
510516 mock_get_client_ssl_credentials ,
511517 mock_ssl_channel_credentials ,
512518 ):
513- # Test client cert won't be used if GOOGLE_API_USE_CLIENT_CERTIFICATE is not set.
514- ssl_credentials = google .auth .transport .grpc .SslCredentials ()
519+ with mock .patch .dict (
520+ os .environ , {environment_vars .GOOGLE_API_USE_CLIENT_CERTIFICATE : "false" }
521+ ):
522+ ssl_credentials = google .auth .transport .grpc .SslCredentials ()
515523
516524 assert ssl_credentials .ssl_credentials is not None
517525 assert not ssl_credentials .is_mtls
518526 mock_check_config_path .assert_not_called ()
519527 mock_load_json_file .assert_not_called ()
520528 mock_get_client_ssl_credentials .assert_not_called ()
521529 mock_ssl_channel_credentials .assert_called_once ()
530+
531+ def test_get_client_ssl_credentials_no_workload_cert (
532+ self ,
533+ mock_check_config_path ,
534+ mock_load_json_file ,
535+ mock_get_client_ssl_credentials ,
536+ mock_ssl_channel_credentials ,
537+ ):
538+ mock_check_config_path .return_value = METADATA_PATH
539+ mock_load_json_file .return_value = {"cert_provider_command" : ["some command" ]}
540+ mock_get_client_ssl_credentials .return_value = (
541+ False ,
542+ None ,
543+ None ,
544+ None ,
545+ )
546+
547+ with mock .patch .dict (
548+ os .environ , {environment_vars .GOOGLE_API_USE_CLIENT_CERTIFICATE : "true" }
549+ ):
550+ ssl_credentials = google .auth .transport .grpc .SslCredentials ()
551+
552+ assert ssl_credentials .ssl_credentials is not None
553+ assert not ssl_credentials .is_mtls
554+ mock_get_client_ssl_credentials .assert_called_once ()
555+ mock_ssl_channel_credentials .assert_called_once_with ()
556+
557+ def test_get_client_ssl_credentials_os_error (
558+ self ,
559+ mock_check_config_path ,
560+ mock_load_json_file ,
561+ mock_get_client_ssl_credentials ,
562+ mock_ssl_channel_credentials ,
563+ ):
564+ mock_check_config_path .return_value = METADATA_PATH
565+ mock_load_json_file .return_value = {"cert_provider_command" : ["some command" ]}
566+ mock_get_client_ssl_credentials .side_effect = OSError ("Mock file read error" )
567+
568+ with mock .patch .dict (
569+ os .environ , {environment_vars .GOOGLE_API_USE_CLIENT_CERTIFICATE : "true" }
570+ ):
571+ ssl_credentials = google .auth .transport .grpc .SslCredentials ()
572+
573+ with pytest .raises (exceptions .MutualTLSChannelError ):
574+ _ = ssl_credentials .ssl_credentials
575+
576+ assert not ssl_credentials .is_mtls
0 commit comments