blob: 090cea688929f574281f3a76344c54939b80a966 [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')
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070088
89
90def test__load_credentials_from_file_service_account():
91 credentials, project_id = _default._load_credentials_from_file(
92 SERVICE_ACCOUNT_FILE)
93 assert isinstance(credentials, service_account.Credentials)
94 assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id']
95
96
97def test__load_credentials_from_file_service_account_bad_format(tmpdir):
98 filename = tmpdir.join('serivce_account_bad.json')
99 filename.write(json.dumps({'type': 'service_account'}))
100
101 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
102 _default._load_credentials_from_file(str(filename))
103
104 assert excinfo.match(r'Failed to load service account')
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700105
106
107@mock.patch.dict(os.environ, {}, clear=True)
108def test__get_explicit_environ_credentials_no_env():
109 assert _default._get_explicit_environ_credentials() == (None, None)
110
111
112@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700113def test__get_explicit_environ_credentials(load, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700114 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
115
116 credentials, project_id = _default._get_explicit_environ_credentials()
117
118 assert credentials is mock.sentinel.credentials
119 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700120 load.assert_called_with('filename')
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700121
122
123@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700124def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
125 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700126 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
127
128 credentials, project_id = _default._get_explicit_environ_credentials()
129
130 assert credentials is mock.sentinel.credentials
131 assert project_id is None
132
133
134@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800135@mock.patch(
136 'google.auth._cloud_sdk.get_application_default_credentials_path',
137 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700138def test__get_gcloud_sdk_credentials(get_adc_path, load):
139 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700140
141 credentials, project_id = _default._get_gcloud_sdk_credentials()
142
143 assert credentials is mock.sentinel.credentials
144 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700145 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700146
147
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_non_existent(get_adc_path, tmpdir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700152 non_existent = tmpdir.join('non-existent')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700153 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700154
155 credentials, project_id = _default._get_gcloud_sdk_credentials()
156
157 assert credentials is None
158 assert project_id is None
159
160
161@mock.patch(
162 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800163 return_value=mock.sentinel.project_id, autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700164@mock.patch('os.path.isfile', return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700165@LOAD_FILE_PATCH
166def test__get_gcloud_sdk_credentials_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700167 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700168 # Don't return a project ID from load file, make the function check
169 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700170 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700171
172 credentials, project_id = _default._get_gcloud_sdk_credentials()
173
174 assert credentials == mock.sentinel.credentials
175 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700176 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700177
178
179@mock.patch(
180 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800181 return_value=None, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700182@mock.patch('os.path.isfile', return_value=True)
183@LOAD_FILE_PATCH
184def test__get_gcloud_sdk_credentials_no_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700185 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700186 # Don't return a project ID from load file, make the function check
187 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700188 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700189
190 credentials, project_id = _default._get_gcloud_sdk_credentials()
191
192 assert credentials == mock.sentinel.credentials
193 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700194 assert get_project_id.called
195
196
197class _AppIdentityModule(object):
198 """The interface of the App Idenity app engine module.
199 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
200 /google.appengine.api.app_identity.app_identity
201 """
202 def get_application_id(self):
203 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700204
205
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700206@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700207def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700208 """Mocks the app_identity module for google.auth.app_engine."""
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700209 app_identity_module = mock.create_autospec(
210 _AppIdentityModule, instance=True)
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700211 monkeypatch.setattr(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700212 app_engine, 'app_identity', app_identity_module)
213 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700214
215
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700216def test__get_gae_credentials(app_identity):
217 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700218
219 credentials, project_id = _default._get_gae_credentials()
220
221 assert isinstance(credentials, app_engine.Credentials)
222 assert project_id == mock.sentinel.project
223
224
225def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700226 assert _default._get_gae_credentials() == (None, None)
227
228
229@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800230 'google.auth.compute_engine._metadata.ping', return_value=True,
231 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700232@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700233 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800234 return_value='example-project', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700235def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700236 credentials, project_id = _default._get_gce_credentials()
237
238 assert isinstance(credentials, compute_engine.Credentials)
239 assert project_id == 'example-project'
240
241
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800242@mock.patch(
243 'google.auth.compute_engine._metadata.ping', return_value=False,
244 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700245def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700246 credentials, project_id = _default._get_gce_credentials()
247
248 assert credentials is None
249 assert project_id is None
250
251
252@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800253 'google.auth.compute_engine._metadata.ping', return_value=True,
254 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700255@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700256 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800257 side_effect=exceptions.TransportError(), autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700258def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700259 credentials, project_id = _default._get_gce_credentials()
260
261 assert isinstance(credentials, compute_engine.Credentials)
262 assert project_id is None
263
264
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800265@mock.patch(
266 'google.auth.compute_engine._metadata.ping', return_value=False,
267 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700268def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700269 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700270 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700271
272
273@mock.patch(
274 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800275 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
276 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700277def test_default_early_out(unused_get):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700278 assert _default.default() == (
279 mock.sentinel.credentials, mock.sentinel.project_id)
280
281
282@mock.patch(
283 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800284 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
285 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700286def test_default_explict_project_id(unused_get, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700287 monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
288 assert _default.default() == (
289 mock.sentinel.credentials, 'explicit-env')
290
291
292@mock.patch(
293 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800294 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
295 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700296def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800297 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
298 assert _default.default() == (
299 mock.sentinel.credentials, 'explicit-env')
300
301
302@mock.patch(
303 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800304 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700305@mock.patch(
306 'google.auth._default._get_gcloud_sdk_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800307 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700308@mock.patch(
309 'google.auth._default._get_gae_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800310 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700311@mock.patch(
312 'google.auth._default._get_gce_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800313 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700314def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
315 with pytest.raises(exceptions.DefaultCredentialsError):
316 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800317
318
319@mock.patch(
320 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800321 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
322 autospec=True)
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800323@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800324 'google.auth.credentials.with_scopes_if_required', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700325def test_default_scoped(with_scopes, get):
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800326 scopes = ['one', 'two']
327
328 credentials, project_id = _default.default(scopes=scopes)
329
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700330 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800331 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700332 with_scopes.assert_called_once_with(
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800333 mock.sentinel.credentials, scopes)