1818
1919import google .auth
2020import google .auth .credentials
21+ from google .auth .transport import mtls
2122import google .auth .transport .requests
2223import google .auth .transport .urllib3
2324
2425MTLS_ENDPOINT = "https://pubsub.mtls.googleapis.com/v1/projects/{}/topics"
2526REGULAR_ENDPOINT = "https://pubsub.googleapis.com/v1/projects/{}/topics"
2627
2728
28- def check_context_aware_metadata ():
29- metadata_path = path .expanduser ("~/.secureConnect/context_aware_metadata.json" )
30- return path .exists (metadata_path )
31-
32-
3329def test_requests ():
3430 credentials , project_id = google .auth .default ()
3531 credentials = google .auth .credentials .with_scopes_if_required (
@@ -39,9 +35,9 @@ def test_requests():
3935 authed_session = google .auth .transport .requests .AuthorizedSession (credentials )
4036 authed_session .configure_mtls_channel ()
4137
42- # If the devices has context aware metadata , then a mutual TLS channel is
43- # supposed to be created.
44- assert authed_session .is_mtls == check_context_aware_metadata ()
38+ # If the devices has default client cert source , then a mutual TLS channel
39+ # is supposed to be created.
40+ assert authed_session .is_mtls == mtls . has_default_client_cert_source ()
4541
4642 # Sleep 1 second to avoid 503 error.
4743 time .sleep (1 )
@@ -63,9 +59,9 @@ def test_urllib3():
6359 authed_http = google .auth .transport .urllib3 .AuthorizedHttp (credentials )
6460 is_mtls = authed_http .configure_mtls_channel ()
6561
66- # If the devices has context aware metadata , then a mutual TLS channel is
67- # supposed to be created.
68- assert is_mtls == check_context_aware_metadata ()
62+ # If the devices has default client cert source , then a mutual TLS channel
63+ # is supposed to be created.
64+ assert is_mtls == mtls . has_default_client_cert_source ()
6965
7066 # Sleep 1 second to avoid 503 error.
7167 time .sleep (1 )
@@ -76,3 +72,45 @@ def test_urllib3():
7672 response = authed_http .request ("GET" , REGULAR_ENDPOINT .format (project_id ))
7773
7874 assert response .status == 200
75+
76+
77+ def test_requests_with_default_client_cert_source ():
78+ credentials , project_id = google .auth .default ()
79+ credentials = google .auth .credentials .with_scopes_if_required (
80+ credentials , ["https://www.googleapis.com/auth/pubsub" ]
81+ )
82+
83+ authed_session = google .auth .transport .requests .AuthorizedSession (credentials )
84+
85+ if mtls .has_default_client_cert_source ():
86+ authed_session .configure_mtls_channel (
87+ client_cert_callback = mtls .default_client_cert_source ()
88+ )
89+
90+ assert authed_session .is_mtls
91+
92+ # Sleep 1 second to avoid 503 error.
93+ time .sleep (1 )
94+
95+ response = authed_session .get (MTLS_ENDPOINT .format (project_id ))
96+ assert response .ok
97+
98+
99+ def test_urllib3_with_default_client_cert_source ():
100+ credentials , project_id = google .auth .default ()
101+ credentials = google .auth .credentials .with_scopes_if_required (
102+ credentials , ["https://www.googleapis.com/auth/pubsub" ]
103+ )
104+
105+ authed_http = google .auth .transport .urllib3 .AuthorizedHttp (credentials )
106+
107+ if mtls .has_default_client_cert_source ():
108+ assert authed_http .configure_mtls_channel (
109+ client_cert_callback = mtls .default_client_cert_source ()
110+ )
111+
112+ # Sleep 1 second to avoid 503 error.
113+ time .sleep (1 )
114+
115+ response = authed_http .request ("GET" , MTLS_ENDPOINT .format (project_id ))
116+ assert response .status == 200
0 commit comments