blob: d7d537c82b3e9f37461c676a7670f788d7fdb88b [file] [log] [blame]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -07001# Copyright 2016 Google Inc.
2#
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
24from google.auth import environment_vars
25from google.auth import exceptions
26from google.oauth2 import service_account
27import google.oauth2.credentials
28
29
30DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
31AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
32
33with open(AUTHORIZED_USER_FILE) as fh:
34 AUTHORIZED_USER_FILE_DATA = json.load(fh)
35
Thea Flowersa8d93482018-05-31 14:52:06 -070036AUTHORIZED_USER_CLOUD_SDK_FILE = os.path.join(
37 DATA_DIR, 'authorized_user_cloud_sdk.json')
38
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070039SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
40
41with open(SERVICE_ACCOUNT_FILE) as fh:
42 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
43
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070044LOAD_FILE_PATCH = mock.patch(
45 'google.auth._default._load_credentials_from_file', return_value=(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080046 mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070047
48
weitaiting6e86c932017-08-12 03:26:59 +080049def test__load_credentials_from_missing_file():
50 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
51 _default._load_credentials_from_file('')
52
53 assert excinfo.match(r'not found')
54
55
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070056def test__load_credentials_from_file_invalid_json(tmpdir):
57 jsonfile = tmpdir.join('invalid.json')
58 jsonfile.write('{')
59
60 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
61 _default._load_credentials_from_file(str(jsonfile))
62
63 assert excinfo.match(r'not a valid json file')
64
65
66def test__load_credentials_from_file_invalid_type(tmpdir):
67 jsonfile = tmpdir.join('invalid.json')
68 jsonfile.write(json.dumps({'type': 'not-a-real-type'}))
69
70 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
71 _default._load_credentials_from_file(str(jsonfile))
72
73 assert excinfo.match(r'does not have a valid type')
74
75
76def test__load_credentials_from_file_authorized_user():
77 credentials, project_id = _default._load_credentials_from_file(
78 AUTHORIZED_USER_FILE)
79 assert isinstance(credentials, google.oauth2.credentials.Credentials)
80 assert project_id is None
81
82
83def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
84 filename = tmpdir.join('authorized_user_bad.json')
85 filename.write(json.dumps({'type': 'authorized_user'}))
86
87 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
88 _default._load_credentials_from_file(str(filename))
89
90 assert excinfo.match(r'Failed to load authorized user')
91 assert excinfo.match(r'missing fields')
92
93
Thea Flowersa8d93482018-05-31 14:52:06 -070094def test__load_credentials_from_file_authorized_user_cloud_sdk():
95 with pytest.warns(UserWarning, matches='Cloud SDK'):
96 credentials, project_id = _default._load_credentials_from_file(
97 AUTHORIZED_USER_CLOUD_SDK_FILE)
98 assert isinstance(credentials, google.oauth2.credentials.Credentials)
99 assert project_id is None
100
101
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700102def test__load_credentials_from_file_service_account():
103 credentials, project_id = _default._load_credentials_from_file(
104 SERVICE_ACCOUNT_FILE)
105 assert isinstance(credentials, service_account.Credentials)
106 assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id']
107
108
109def test__load_credentials_from_file_service_account_bad_format(tmpdir):
110 filename = tmpdir.join('serivce_account_bad.json')
111 filename.write(json.dumps({'type': 'service_account'}))
112
113 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
114 _default._load_credentials_from_file(str(filename))
115
116 assert excinfo.match(r'Failed to load service account')
117 assert excinfo.match(r'missing fields')
118
119
120@mock.patch.dict(os.environ, {}, clear=True)
121def test__get_explicit_environ_credentials_no_env():
122 assert _default._get_explicit_environ_credentials() == (None, None)
123
124
125@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700126def test__get_explicit_environ_credentials(load, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700127 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
128
129 credentials, project_id = _default._get_explicit_environ_credentials()
130
131 assert credentials is mock.sentinel.credentials
132 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700133 load.assert_called_with('filename')
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700134
135
136@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700137def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
138 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700139 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
140
141 credentials, project_id = _default._get_explicit_environ_credentials()
142
143 assert credentials is mock.sentinel.credentials
144 assert project_id is None
145
146
147@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800148@mock.patch(
149 'google.auth._cloud_sdk.get_application_default_credentials_path',
150 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700151def test__get_gcloud_sdk_credentials(get_adc_path, load):
152 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700153
154 credentials, project_id = _default._get_gcloud_sdk_credentials()
155
156 assert credentials is mock.sentinel.credentials
157 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700158 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700159
160
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800161@mock.patch(
162 'google.auth._cloud_sdk.get_application_default_credentials_path',
163 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700164def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700165 non_existent = tmpdir.join('non-existent')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700166 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700167
168 credentials, project_id = _default._get_gcloud_sdk_credentials()
169
170 assert credentials is None
171 assert project_id is None
172
173
174@mock.patch(
175 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800176 return_value=mock.sentinel.project_id, autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700177@mock.patch('os.path.isfile', return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700178@LOAD_FILE_PATCH
179def test__get_gcloud_sdk_credentials_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700180 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700181 # Don't return a project ID from load file, make the function check
182 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700183 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700184
185 credentials, project_id = _default._get_gcloud_sdk_credentials()
186
187 assert credentials == mock.sentinel.credentials
188 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700189 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700190
191
192@mock.patch(
193 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800194 return_value=None, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700195@mock.patch('os.path.isfile', return_value=True)
196@LOAD_FILE_PATCH
197def test__get_gcloud_sdk_credentials_no_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700198 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700199 # Don't return a project ID from load file, make the function check
200 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700201 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700202
203 credentials, project_id = _default._get_gcloud_sdk_credentials()
204
205 assert credentials == mock.sentinel.credentials
206 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700207 assert get_project_id.called
208
209
210class _AppIdentityModule(object):
211 """The interface of the App Idenity app engine module.
212 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
213 /google.appengine.api.app_identity.app_identity
214 """
215 def get_application_id(self):
216 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700217
218
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700219@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700220def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700221 """Mocks the app_identity module for google.auth.app_engine."""
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700222 app_identity_module = mock.create_autospec(
223 _AppIdentityModule, instance=True)
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700224 monkeypatch.setattr(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700225 app_engine, 'app_identity', app_identity_module)
226 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700227
228
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700229def test__get_gae_credentials(app_identity):
230 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700231
232 credentials, project_id = _default._get_gae_credentials()
233
234 assert isinstance(credentials, app_engine.Credentials)
235 assert project_id == mock.sentinel.project
236
237
238def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700239 assert _default._get_gae_credentials() == (None, None)
240
241
242@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800243 'google.auth.compute_engine._metadata.ping', return_value=True,
244 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700245@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700246 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800247 return_value='example-project', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700248def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700249 credentials, project_id = _default._get_gce_credentials()
250
251 assert isinstance(credentials, compute_engine.Credentials)
252 assert project_id == 'example-project'
253
254
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800255@mock.patch(
256 'google.auth.compute_engine._metadata.ping', return_value=False,
257 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700258def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700259 credentials, project_id = _default._get_gce_credentials()
260
261 assert credentials is None
262 assert project_id is None
263
264
265@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800266 'google.auth.compute_engine._metadata.ping', return_value=True,
267 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700268@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700269 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800270 side_effect=exceptions.TransportError(), autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700271def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700272 credentials, project_id = _default._get_gce_credentials()
273
274 assert isinstance(credentials, compute_engine.Credentials)
275 assert project_id is None
276
277
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800278@mock.patch(
279 'google.auth.compute_engine._metadata.ping', return_value=False,
280 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700281def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700282 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700283 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700284
285
286@mock.patch(
287 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800288 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
289 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700290def test_default_early_out(unused_get):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700291 assert _default.default() == (
292 mock.sentinel.credentials, mock.sentinel.project_id)
293
294
295@mock.patch(
296 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800297 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
298 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700299def test_default_explict_project_id(unused_get, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700300 monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
301 assert _default.default() == (
302 mock.sentinel.credentials, 'explicit-env')
303
304
305@mock.patch(
306 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800307 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
308 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700309def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800310 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
311 assert _default.default() == (
312 mock.sentinel.credentials, 'explicit-env')
313
314
315@mock.patch(
Jacob Hayes15af07b2017-12-13 14:09:47 -0600316 'logging.Logger.warning',
317 autospec=True)
318@mock.patch(
319 'google.auth._default._get_explicit_environ_credentials',
320 return_value=(mock.sentinel.credentials, None), autospec=True)
321@mock.patch(
322 'google.auth._default._get_gcloud_sdk_credentials',
323 return_value=(mock.sentinel.credentials, None), autospec=True)
324@mock.patch(
325 'google.auth._default._get_gae_credentials',
326 return_value=(mock.sentinel.credentials, None), autospec=True)
327@mock.patch(
328 'google.auth._default._get_gce_credentials',
329 return_value=(mock.sentinel.credentials, None), autospec=True)
330def test_default_without_project_id(
331 unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning):
332 assert _default.default() == (
333 mock.sentinel.credentials, None)
334 logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)
335
336
337@mock.patch(
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800338 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800339 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700340@mock.patch(
341 'google.auth._default._get_gcloud_sdk_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800342 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700343@mock.patch(
344 'google.auth._default._get_gae_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800345 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700346@mock.patch(
347 'google.auth._default._get_gce_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800348 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700349def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
350 with pytest.raises(exceptions.DefaultCredentialsError):
351 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800352
353
354@mock.patch(
355 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800356 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
357 autospec=True)
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800358@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800359 'google.auth.credentials.with_scopes_if_required', autospec=True)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600360def test_default_scoped(with_scopes, unused_get):
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800361 scopes = ['one', 'two']
362
363 credentials, project_id = _default.default(scopes=scopes)
364
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700365 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800366 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700367 with_scopes.assert_called_once_with(
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800368 mock.sentinel.credentials, scopes)