@@ -58,3 +58,45 @@ def callback():
5858 return cert_bytes , key_bytes
5959
6060 return callback
61+
62+
63+ def default_client_encrypted_cert_source (cert_path , key_path ):
64+ """Get a callback which returns the default encrpyted client SSL credentials.
65+
66+ Args:
67+ cert_path (str): The cert file path. The default client certificate will
68+ be written to this file when the returned callback is called.
69+ key_path (str): The key file path. The default encrypted client key will
70+ be written to this file when the returned callback is called.
71+
72+ Returns:
73+ Callable[[], [str, str, bytes]]: A callback which generates the default
74+ client certificate, encrpyted private key and passphrase. It writes
75+ the certificate and private key into the cert_path and key_path, and
76+ returns the cert_path, key_path and passphrase bytes.
77+
78+ Raises:
79+ google.auth.exceptions.DefaultClientCertSourceError: If any problem
80+ occurs when loading or saving the client certificate and key.
81+ """
82+ if not has_default_client_cert_source ():
83+ raise exceptions .MutualTLSChannelError (
84+ "Default client encrypted cert source doesn't exist"
85+ )
86+
87+ def callback ():
88+ try :
89+ _ , cert_bytes , key_bytes , passphrase_bytes = _mtls_helper .get_client_ssl_credentials (
90+ generate_encrypted_key = True
91+ )
92+ with open (cert_path , "wb" ) as cert_file :
93+ cert_file .write (cert_bytes )
94+ with open (key_path , "wb" ) as key_file :
95+ key_file .write (key_bytes )
96+ except (exceptions .ClientCertError , OSError ) as caught_exc :
97+ new_exc = exceptions .MutualTLSChannelError (caught_exc )
98+ six .raise_from (new_exc , caught_exc )
99+
100+ return cert_path , key_path , passphrase_bytes
101+
102+ return callback
0 commit comments