blob: 2738e22bc82aea4647ce74748a09704ffe972dbe [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2016 Google LLC
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -07002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import json
16import os
17
18import mock
19import pytest
20
21from google.auth import _default
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -070022from google.auth import app_engine
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070023from google.auth import compute_engine
Bu Sun Kim3dda7b22020-07-09 10:39:39 -070024from google.auth import credentials
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070025from google.auth import environment_vars
26from google.auth import exceptions
27from google.oauth2 import service_account
28import google.oauth2.credentials
29
30
Bu Sun Kim9eec0912019-10-21 17:04:21 -070031DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
32AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070033
34with open(AUTHORIZED_USER_FILE) as fh:
35 AUTHORIZED_USER_FILE_DATA = json.load(fh)
36
Thea Flowersa8d93482018-05-31 14:52:06 -070037AUTHORIZED_USER_CLOUD_SDK_FILE = os.path.join(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070038 DATA_DIR, "authorized_user_cloud_sdk.json"
39)
Thea Flowersa8d93482018-05-31 14:52:06 -070040
arithmetic1728f30b45a2020-06-17 23:36:04 -070041AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE = os.path.join(
42 DATA_DIR, "authorized_user_cloud_sdk_with_quota_project_id.json"
43)
44
Bu Sun Kim9eec0912019-10-21 17:04:21 -070045SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070046
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070047CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, "client_secrets.json")
48
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070049with open(SERVICE_ACCOUNT_FILE) as fh:
50 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
51
Bu Sun Kim41599ae2020-09-02 12:55:42 -060052MOCK_CREDENTIALS = mock.Mock(spec=credentials.CredentialsWithQuotaProject)
Bu Sun Kim3dda7b22020-07-09 10:39:39 -070053MOCK_CREDENTIALS.with_quota_project.return_value = MOCK_CREDENTIALS
54
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070055LOAD_FILE_PATCH = mock.patch(
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070056 "google.auth._default.load_credentials_from_file",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -070057 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -070058 autospec=True,
59)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070060
61
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070062def test_load_credentials_from_missing_file():
weitaiting6e86c932017-08-12 03:26:59 +080063 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070064 _default.load_credentials_from_file("")
weitaiting6e86c932017-08-12 03:26:59 +080065
Bu Sun Kim9eec0912019-10-21 17:04:21 -070066 assert excinfo.match(r"not found")
weitaiting6e86c932017-08-12 03:26:59 +080067
68
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070069def test_load_credentials_from_file_invalid_json(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070070 jsonfile = tmpdir.join("invalid.json")
71 jsonfile.write("{")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070072
73 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070074 _default.load_credentials_from_file(str(jsonfile))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070075
Bu Sun Kim9eec0912019-10-21 17:04:21 -070076 assert excinfo.match(r"not a valid json file")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070077
78
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070079def test_load_credentials_from_file_invalid_type(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070080 jsonfile = tmpdir.join("invalid.json")
81 jsonfile.write(json.dumps({"type": "not-a-real-type"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070082
83 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070084 _default.load_credentials_from_file(str(jsonfile))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070085
Bu Sun Kim9eec0912019-10-21 17:04:21 -070086 assert excinfo.match(r"does not have a valid type")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070087
88
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070089def test_load_credentials_from_file_authorized_user():
90 credentials, project_id = _default.load_credentials_from_file(AUTHORIZED_USER_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070091 assert isinstance(credentials, google.oauth2.credentials.Credentials)
92 assert project_id is None
93
94
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070095def test_load_credentials_from_file_no_type(tmpdir):
96 # use the client_secrets.json, which is valid json but not a
97 # loadable credentials type
98 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
99 _default.load_credentials_from_file(CLIENT_SECRETS_FILE)
100
101 assert excinfo.match(r"does not have a valid type")
102 assert excinfo.match(r"Type is None")
103
104
105def test_load_credentials_from_file_authorized_user_bad_format(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700106 filename = tmpdir.join("authorized_user_bad.json")
107 filename.write(json.dumps({"type": "authorized_user"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700108
109 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700110 _default.load_credentials_from_file(str(filename))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700111
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700112 assert excinfo.match(r"Failed to load authorized user")
113 assert excinfo.match(r"missing fields")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700114
115
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700116def test_load_credentials_from_file_authorized_user_cloud_sdk():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700117 with pytest.warns(UserWarning, match="Cloud SDK"):
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700118 credentials, project_id = _default.load_credentials_from_file(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700119 AUTHORIZED_USER_CLOUD_SDK_FILE
120 )
Thea Flowersa8d93482018-05-31 14:52:06 -0700121 assert isinstance(credentials, google.oauth2.credentials.Credentials)
122 assert project_id is None
123
arithmetic1728f30b45a2020-06-17 23:36:04 -0700124 # No warning if the json file has quota project id.
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700125 credentials, project_id = _default.load_credentials_from_file(
arithmetic1728f30b45a2020-06-17 23:36:04 -0700126 AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE
127 )
128 assert isinstance(credentials, google.oauth2.credentials.Credentials)
129 assert project_id is None
130
Thea Flowersa8d93482018-05-31 14:52:06 -0700131
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700132def test_load_credentials_from_file_authorized_user_cloud_sdk_with_scopes():
133 with pytest.warns(UserWarning, match="Cloud SDK"):
134 credentials, project_id = _default.load_credentials_from_file(
135 AUTHORIZED_USER_CLOUD_SDK_FILE,
136 scopes=["https://www.google.com/calendar/feeds"],
137 )
138 assert isinstance(credentials, google.oauth2.credentials.Credentials)
139 assert project_id is None
140 assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
141
142
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700143def test_load_credentials_from_file_authorized_user_cloud_sdk_with_quota_project():
144 credentials, project_id = _default.load_credentials_from_file(
145 AUTHORIZED_USER_CLOUD_SDK_FILE, quota_project_id="project-foo"
146 )
147
148 assert isinstance(credentials, google.oauth2.credentials.Credentials)
149 assert project_id is None
150 assert credentials.quota_project_id == "project-foo"
151
152
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700153def test_load_credentials_from_file_service_account():
154 credentials, project_id = _default.load_credentials_from_file(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700155 assert isinstance(credentials, service_account.Credentials)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700156 assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700157
158
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700159def test_load_credentials_from_file_service_account_with_scopes():
160 credentials, project_id = _default.load_credentials_from_file(
161 SERVICE_ACCOUNT_FILE, scopes=["https://www.google.com/calendar/feeds"]
162 )
163 assert isinstance(credentials, service_account.Credentials)
164 assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
165 assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
166
167
Bu Sun Kimab2be5d2020-07-15 16:49:27 -0700168def test_load_credentials_from_file_service_account_with_quota_project():
169 credentials, project_id = _default.load_credentials_from_file(
170 SERVICE_ACCOUNT_FILE, quota_project_id="project-foo"
171 )
172 assert isinstance(credentials, service_account.Credentials)
173 assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
174 assert credentials.quota_project_id == "project-foo"
175
176
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700177def test_load_credentials_from_file_service_account_bad_format(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700178 filename = tmpdir.join("serivce_account_bad.json")
179 filename.write(json.dumps({"type": "service_account"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700180
181 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700182 _default.load_credentials_from_file(str(filename))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700183
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700184 assert excinfo.match(r"Failed to load service account")
185 assert excinfo.match(r"missing fields")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700186
187
188@mock.patch.dict(os.environ, {}, clear=True)
189def test__get_explicit_environ_credentials_no_env():
190 assert _default._get_explicit_environ_credentials() == (None, None)
191
192
193@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700194def test__get_explicit_environ_credentials(load, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700195 monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700196
197 credentials, project_id = _default._get_explicit_environ_credentials()
198
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700199 assert credentials is MOCK_CREDENTIALS
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700200 assert project_id is mock.sentinel.project_id
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700201 load.assert_called_with("filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700202
203
204@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700205def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700206 load.return_value = MOCK_CREDENTIALS, None
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700207 monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700208
209 credentials, project_id = _default._get_explicit_environ_credentials()
210
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700211 assert credentials is MOCK_CREDENTIALS
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700212 assert project_id is None
213
214
215@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800216@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700217 "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
218)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700219def test__get_gcloud_sdk_credentials(get_adc_path, load):
220 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700221
222 credentials, project_id = _default._get_gcloud_sdk_credentials()
223
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700224 assert credentials is MOCK_CREDENTIALS
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700225 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700226 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700227
228
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800229@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700230 "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
231)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700232def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700233 non_existent = tmpdir.join("non-existent")
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700234 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700235
236 credentials, project_id = _default._get_gcloud_sdk_credentials()
237
238 assert credentials is None
239 assert project_id is None
240
241
242@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700243 "google.auth._cloud_sdk.get_project_id",
244 return_value=mock.sentinel.project_id,
245 autospec=True,
246)
247@mock.patch("os.path.isfile", return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700248@LOAD_FILE_PATCH
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700249def test__get_gcloud_sdk_credentials_project_id(load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700250 # Don't return a project ID from load file, make the function check
251 # the Cloud SDK project.
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700252 load.return_value = MOCK_CREDENTIALS, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700253
254 credentials, project_id = _default._get_gcloud_sdk_credentials()
255
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700256 assert credentials == MOCK_CREDENTIALS
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700257 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700258 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700259
260
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700261@mock.patch("google.auth._cloud_sdk.get_project_id", return_value=None, autospec=True)
262@mock.patch("os.path.isfile", return_value=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700263@LOAD_FILE_PATCH
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700264def test__get_gcloud_sdk_credentials_no_project_id(load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700265 # Don't return a project ID from load file, make the function check
266 # the Cloud SDK project.
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700267 load.return_value = MOCK_CREDENTIALS, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700268
269 credentials, project_id = _default._get_gcloud_sdk_credentials()
270
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700271 assert credentials == MOCK_CREDENTIALS
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700272 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700273 assert get_project_id.called
274
275
276class _AppIdentityModule(object):
277 """The interface of the App Idenity app engine module.
278 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
279 /google.appengine.api.app_identity.app_identity
280 """
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700281
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700282 def get_application_id(self):
283 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700284
285
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700286@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700287def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700288 """Mocks the app_identity module for google.auth.app_engine."""
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700289 app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True)
290 monkeypatch.setattr(app_engine, "app_identity", app_identity_module)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700291 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700292
293
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700294def test__get_gae_credentials(app_identity):
295 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700296
297 credentials, project_id = _default._get_gae_credentials()
298
299 assert isinstance(credentials, app_engine.Credentials)
300 assert project_id == mock.sentinel.project
301
302
James Wilson6e0781b2018-12-20 20:38:52 -0500303def test__get_gae_credentials_no_app_engine():
304 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700305
306 with mock.patch.dict("sys.modules"):
307 sys.modules["google.auth.app_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500308 credentials, project_id = _default._get_gae_credentials()
309 assert credentials is None
310 assert project_id is None
311
312
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700313def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700314 assert _default._get_gae_credentials() == (None, None)
315
316
317@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700318 "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
319)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700320@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700321 "google.auth.compute_engine._metadata.get_project_id",
322 return_value="example-project",
323 autospec=True,
324)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700325def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700326 credentials, project_id = _default._get_gce_credentials()
327
328 assert isinstance(credentials, compute_engine.Credentials)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700329 assert project_id == "example-project"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700330
331
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800332@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700333 "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
334)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700335def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700336 credentials, project_id = _default._get_gce_credentials()
337
338 assert credentials is None
339 assert project_id is None
340
341
342@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700343 "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
344)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700345@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700346 "google.auth.compute_engine._metadata.get_project_id",
347 side_effect=exceptions.TransportError(),
348 autospec=True,
349)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700350def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700351 credentials, project_id = _default._get_gce_credentials()
352
353 assert isinstance(credentials, compute_engine.Credentials)
354 assert project_id is None
355
356
James Wilson6e0781b2018-12-20 20:38:52 -0500357def test__get_gce_credentials_no_compute_engine():
358 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700359
360 with mock.patch.dict("sys.modules"):
361 sys.modules["google.auth.compute_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500362 credentials, project_id = _default._get_gce_credentials()
363 assert credentials is None
364 assert project_id is None
365
366
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800367@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700368 "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
369)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700370def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700371 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700372 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700373
374
375@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700376 "google.auth._default._get_explicit_environ_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700377 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700378 autospec=True,
379)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700380def test_default_early_out(unused_get):
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700381 assert _default.default() == (MOCK_CREDENTIALS, mock.sentinel.project_id)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700382
383
384@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700385 "google.auth._default._get_explicit_environ_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700386 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700387 autospec=True,
388)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700389def test_default_explict_project_id(unused_get, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700390 monkeypatch.setenv(environment_vars.PROJECT, "explicit-env")
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700391 assert _default.default() == (MOCK_CREDENTIALS, "explicit-env")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700392
393
394@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700395 "google.auth._default._get_explicit_environ_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700396 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700397 autospec=True,
398)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700399def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700400 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, "explicit-env")
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700401 assert _default.default() == (MOCK_CREDENTIALS, "explicit-env")
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800402
403
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700404@mock.patch("logging.Logger.warning", autospec=True)
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800405@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700406 "google.auth._default._get_explicit_environ_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700407 return_value=(MOCK_CREDENTIALS, None),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700408 autospec=True,
409)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600410@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700411 "google.auth._default._get_gcloud_sdk_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700412 return_value=(MOCK_CREDENTIALS, None),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700413 autospec=True,
414)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600415@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700416 "google.auth._default._get_gae_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700417 return_value=(MOCK_CREDENTIALS, None),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700418 autospec=True,
419)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600420@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700421 "google.auth._default._get_gce_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700422 return_value=(MOCK_CREDENTIALS, None),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700423 autospec=True,
424)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600425def test_default_without_project_id(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700426 unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning
427):
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700428 assert _default.default() == (MOCK_CREDENTIALS, None)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600429 logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)
430
431
432@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700433 "google.auth._default._get_explicit_environ_credentials",
434 return_value=(None, None),
435 autospec=True,
436)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700437@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700438 "google.auth._default._get_gcloud_sdk_credentials",
439 return_value=(None, None),
440 autospec=True,
441)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700442@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700443 "google.auth._default._get_gae_credentials",
444 return_value=(None, None),
445 autospec=True,
446)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700447@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700448 "google.auth._default._get_gce_credentials",
449 return_value=(None, None),
450 autospec=True,
451)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700452def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
453 with pytest.raises(exceptions.DefaultCredentialsError):
454 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800455
456
457@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700458 "google.auth._default._get_explicit_environ_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700459 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700460 autospec=True,
461)
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700462@mock.patch(
463 "google.auth.credentials.with_scopes_if_required",
464 return_value=MOCK_CREDENTIALS,
465 autospec=True,
466)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600467def test_default_scoped(with_scopes, unused_get):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700468 scopes = ["one", "two"]
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800469
470 credentials, project_id = _default.default(scopes=scopes)
471
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700472 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800473 assert project_id == mock.sentinel.project_id
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700474 with_scopes.assert_called_once_with(MOCK_CREDENTIALS, scopes)
James Wilson6e0781b2018-12-20 20:38:52 -0500475
476
477@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700478 "google.auth._default._get_explicit_environ_credentials",
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700479 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700480 autospec=True,
481)
Bu Sun Kimab2be5d2020-07-15 16:49:27 -0700482def test_default_quota_project(with_quota_project):
483 credentials, project_id = _default.default(quota_project_id="project-foo")
484
485 MOCK_CREDENTIALS.with_quota_project.assert_called_once_with("project-foo")
486 assert project_id == mock.sentinel.project_id
487
488
489@mock.patch(
490 "google.auth._default._get_explicit_environ_credentials",
491 return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
492 autospec=True,
493)
James Wilson6e0781b2018-12-20 20:38:52 -0500494def test_default_no_app_engine_compute_engine_module(unused_get):
495 """
496 google.auth.compute_engine and google.auth.app_engine are both optional
497 to allow not including them when using this package. This verifies
498 that default fails gracefully if these modules are absent
499 """
500 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700501
502 with mock.patch.dict("sys.modules"):
503 sys.modules["google.auth.compute_engine"] = None
504 sys.modules["google.auth.app_engine"] = None
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700505 assert _default.default() == (MOCK_CREDENTIALS, mock.sentinel.project_id)