blob: 68c4fb035144bce836dd271bd67c4c2c4ba1a09a [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
36SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
37
38with open(SERVICE_ACCOUNT_FILE) as fh:
39 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
40
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070041LOAD_FILE_PATCH = mock.patch(
42 'google.auth._default._load_credentials_from_file', return_value=(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080043 mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070044
45
weitaiting6e86c932017-08-12 03:26:59 +080046def test__load_credentials_from_missing_file():
47 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
48 _default._load_credentials_from_file('')
49
50 assert excinfo.match(r'not found')
51
52
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070053def test__load_credentials_from_file_invalid_json(tmpdir):
54 jsonfile = tmpdir.join('invalid.json')
55 jsonfile.write('{')
56
57 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
58 _default._load_credentials_from_file(str(jsonfile))
59
60 assert excinfo.match(r'not a valid json file')
61
62
63def test__load_credentials_from_file_invalid_type(tmpdir):
64 jsonfile = tmpdir.join('invalid.json')
65 jsonfile.write(json.dumps({'type': 'not-a-real-type'}))
66
67 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
68 _default._load_credentials_from_file(str(jsonfile))
69
70 assert excinfo.match(r'does not have a valid type')
71
72
73def test__load_credentials_from_file_authorized_user():
74 credentials, project_id = _default._load_credentials_from_file(
75 AUTHORIZED_USER_FILE)
76 assert isinstance(credentials, google.oauth2.credentials.Credentials)
77 assert project_id is None
78
79
80def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
81 filename = tmpdir.join('authorized_user_bad.json')
82 filename.write(json.dumps({'type': 'authorized_user'}))
83
84 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
85 _default._load_credentials_from_file(str(filename))
86
87 assert excinfo.match(r'Failed to load authorized user')
88 assert excinfo.match(r'missing fields')
89
90
91def test__load_credentials_from_file_service_account():
92 credentials, project_id = _default._load_credentials_from_file(
93 SERVICE_ACCOUNT_FILE)
94 assert isinstance(credentials, service_account.Credentials)
95 assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id']
96
97
98def test__load_credentials_from_file_service_account_bad_format(tmpdir):
99 filename = tmpdir.join('serivce_account_bad.json')
100 filename.write(json.dumps({'type': 'service_account'}))
101
102 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
103 _default._load_credentials_from_file(str(filename))
104
105 assert excinfo.match(r'Failed to load service account')
106 assert excinfo.match(r'missing fields')
107
108
109@mock.patch.dict(os.environ, {}, clear=True)
110def test__get_explicit_environ_credentials_no_env():
111 assert _default._get_explicit_environ_credentials() == (None, None)
112
113
114@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700115def test__get_explicit_environ_credentials(load, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700116 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
117
118 credentials, project_id = _default._get_explicit_environ_credentials()
119
120 assert credentials is mock.sentinel.credentials
121 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700122 load.assert_called_with('filename')
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700123
124
125@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700126def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
127 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700128 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
129
130 credentials, project_id = _default._get_explicit_environ_credentials()
131
132 assert credentials is mock.sentinel.credentials
133 assert project_id is None
134
135
136@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800137@mock.patch(
138 'google.auth._cloud_sdk.get_application_default_credentials_path',
139 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700140def test__get_gcloud_sdk_credentials(get_adc_path, load):
141 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700142
143 credentials, project_id = _default._get_gcloud_sdk_credentials()
144
145 assert credentials is mock.sentinel.credentials
146 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700147 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700148
149
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800150@mock.patch(
151 'google.auth._cloud_sdk.get_application_default_credentials_path',
152 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700153def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700154 non_existent = tmpdir.join('non-existent')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700155 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700156
157 credentials, project_id = _default._get_gcloud_sdk_credentials()
158
159 assert credentials is None
160 assert project_id is None
161
162
163@mock.patch(
164 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800165 return_value=mock.sentinel.project_id, autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700166@mock.patch('os.path.isfile', return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700167@LOAD_FILE_PATCH
168def test__get_gcloud_sdk_credentials_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700169 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700170 # Don't return a project ID from load file, make the function check
171 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700172 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700173
174 credentials, project_id = _default._get_gcloud_sdk_credentials()
175
176 assert credentials == mock.sentinel.credentials
177 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700178 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700179
180
181@mock.patch(
182 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800183 return_value=None, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700184@mock.patch('os.path.isfile', return_value=True)
185@LOAD_FILE_PATCH
186def test__get_gcloud_sdk_credentials_no_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700187 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700188 # Don't return a project ID from load file, make the function check
189 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700190 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700191
192 credentials, project_id = _default._get_gcloud_sdk_credentials()
193
194 assert credentials == mock.sentinel.credentials
195 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700196 assert get_project_id.called
197
198
199class _AppIdentityModule(object):
200 """The interface of the App Idenity app engine module.
201 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
202 /google.appengine.api.app_identity.app_identity
203 """
204 def get_application_id(self):
205 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700206
207
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700208@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700209def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700210 """Mocks the app_identity module for google.auth.app_engine."""
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700211 app_identity_module = mock.create_autospec(
212 _AppIdentityModule, instance=True)
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700213 monkeypatch.setattr(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700214 app_engine, 'app_identity', app_identity_module)
215 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700216
217
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700218def test__get_gae_credentials(app_identity):
219 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700220
221 credentials, project_id = _default._get_gae_credentials()
222
223 assert isinstance(credentials, app_engine.Credentials)
224 assert project_id == mock.sentinel.project
225
226
227def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700228 assert _default._get_gae_credentials() == (None, None)
229
230
231@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800232 'google.auth.compute_engine._metadata.ping', return_value=True,
233 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700234@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700235 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800236 return_value='example-project', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700237def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700238 credentials, project_id = _default._get_gce_credentials()
239
240 assert isinstance(credentials, compute_engine.Credentials)
241 assert project_id == 'example-project'
242
243
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800244@mock.patch(
245 'google.auth.compute_engine._metadata.ping', return_value=False,
246 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700247def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700248 credentials, project_id = _default._get_gce_credentials()
249
250 assert credentials is None
251 assert project_id is None
252
253
254@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800255 'google.auth.compute_engine._metadata.ping', return_value=True,
256 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700257@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700258 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800259 side_effect=exceptions.TransportError(), autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700260def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700261 credentials, project_id = _default._get_gce_credentials()
262
263 assert isinstance(credentials, compute_engine.Credentials)
264 assert project_id is None
265
266
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800267@mock.patch(
268 'google.auth.compute_engine._metadata.ping', return_value=False,
269 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700270def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700271 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700272 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700273
274
275@mock.patch(
276 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800277 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
278 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700279def test_default_early_out(unused_get):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700280 assert _default.default() == (
281 mock.sentinel.credentials, mock.sentinel.project_id)
282
283
284@mock.patch(
285 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800286 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
287 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700288def test_default_explict_project_id(unused_get, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700289 monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
290 assert _default.default() == (
291 mock.sentinel.credentials, 'explicit-env')
292
293
294@mock.patch(
295 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800296 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
297 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700298def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800299 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
300 assert _default.default() == (
301 mock.sentinel.credentials, 'explicit-env')
302
303
304@mock.patch(
Jacob Hayes15af07b2017-12-13 14:09:47 -0600305 'logging.Logger.warning',
306 autospec=True)
307@mock.patch(
308 'google.auth._default._get_explicit_environ_credentials',
309 return_value=(mock.sentinel.credentials, None), autospec=True)
310@mock.patch(
311 'google.auth._default._get_gcloud_sdk_credentials',
312 return_value=(mock.sentinel.credentials, None), autospec=True)
313@mock.patch(
314 'google.auth._default._get_gae_credentials',
315 return_value=(mock.sentinel.credentials, None), autospec=True)
316@mock.patch(
317 'google.auth._default._get_gce_credentials',
318 return_value=(mock.sentinel.credentials, None), autospec=True)
319def test_default_without_project_id(
320 unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning):
321 assert _default.default() == (
322 mock.sentinel.credentials, None)
323 logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)
324
325
326@mock.patch(
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800327 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800328 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700329@mock.patch(
330 'google.auth._default._get_gcloud_sdk_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800331 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700332@mock.patch(
333 'google.auth._default._get_gae_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800334 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700335@mock.patch(
336 'google.auth._default._get_gce_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800337 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700338def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
339 with pytest.raises(exceptions.DefaultCredentialsError):
340 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800341
342
343@mock.patch(
344 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800345 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
346 autospec=True)
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800347@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800348 'google.auth.credentials.with_scopes_if_required', autospec=True)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600349def test_default_scoped(with_scopes, unused_get):
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800350 scopes = ['one', 'two']
351
352 credentials, project_id = _default.default(scopes=scopes)
353
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700354 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800355 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700356 with_scopes.assert_called_once_with(
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800357 mock.sentinel.credentials, scopes)