blob: 8054ac522cfd6e2a19644e2dea16d0cf32eda2aa [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
46def test__load_credentials_from_file_invalid_json(tmpdir):
47 jsonfile = tmpdir.join('invalid.json')
48 jsonfile.write('{')
49
50 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
51 _default._load_credentials_from_file(str(jsonfile))
52
53 assert excinfo.match(r'not a valid json file')
54
55
56def test__load_credentials_from_file_invalid_type(tmpdir):
57 jsonfile = tmpdir.join('invalid.json')
58 jsonfile.write(json.dumps({'type': 'not-a-real-type'}))
59
60 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
61 _default._load_credentials_from_file(str(jsonfile))
62
63 assert excinfo.match(r'does not have a valid type')
64
65
66def test__load_credentials_from_file_authorized_user():
67 credentials, project_id = _default._load_credentials_from_file(
68 AUTHORIZED_USER_FILE)
69 assert isinstance(credentials, google.oauth2.credentials.Credentials)
70 assert project_id is None
71
72
73def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
74 filename = tmpdir.join('authorized_user_bad.json')
75 filename.write(json.dumps({'type': 'authorized_user'}))
76
77 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
78 _default._load_credentials_from_file(str(filename))
79
80 assert excinfo.match(r'Failed to load authorized user')
81 assert excinfo.match(r'missing fields')
82
83
84def test__load_credentials_from_file_service_account():
85 credentials, project_id = _default._load_credentials_from_file(
86 SERVICE_ACCOUNT_FILE)
87 assert isinstance(credentials, service_account.Credentials)
88 assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id']
89
90
91def test__load_credentials_from_file_service_account_bad_format(tmpdir):
92 filename = tmpdir.join('serivce_account_bad.json')
93 filename.write(json.dumps({'type': 'service_account'}))
94
95 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
96 _default._load_credentials_from_file(str(filename))
97
98 assert excinfo.match(r'Failed to load service account')
99 assert excinfo.match(r'missing fields')
100
101
102@mock.patch.dict(os.environ, {}, clear=True)
103def test__get_explicit_environ_credentials_no_env():
104 assert _default._get_explicit_environ_credentials() == (None, None)
105
106
107@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700108def test__get_explicit_environ_credentials(load, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700109 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
110
111 credentials, project_id = _default._get_explicit_environ_credentials()
112
113 assert credentials is mock.sentinel.credentials
114 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700115 load.assert_called_with('filename')
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700116
117
118@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700119def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
120 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700121 monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename')
122
123 credentials, project_id = _default._get_explicit_environ_credentials()
124
125 assert credentials is mock.sentinel.credentials
126 assert project_id is None
127
128
129@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800130@mock.patch(
131 'google.auth._cloud_sdk.get_application_default_credentials_path',
132 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700133def test__get_gcloud_sdk_credentials(get_adc_path, load):
134 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700135
136 credentials, project_id = _default._get_gcloud_sdk_credentials()
137
138 assert credentials is mock.sentinel.credentials
139 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700140 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700141
142
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800143@mock.patch(
144 'google.auth._cloud_sdk.get_application_default_credentials_path',
145 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700146def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700147 non_existent = tmpdir.join('non-existent')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700148 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700149
150 credentials, project_id = _default._get_gcloud_sdk_credentials()
151
152 assert credentials is None
153 assert project_id is None
154
155
156@mock.patch(
157 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800158 return_value=mock.sentinel.project_id, autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700159@mock.patch('os.path.isfile', return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700160@LOAD_FILE_PATCH
161def test__get_gcloud_sdk_credentials_project_id(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700162 load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700163 # Don't return a project ID from load file, make the function check
164 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700165 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700166
167 credentials, project_id = _default._get_gcloud_sdk_credentials()
168
169 assert credentials == mock.sentinel.credentials
170 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700171 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700172
173
174@mock.patch(
175 'google.auth._cloud_sdk.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800176 return_value=None, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700177@mock.patch('os.path.isfile', return_value=True)
178@LOAD_FILE_PATCH
179def test__get_gcloud_sdk_credentials_no_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 is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700189 assert get_project_id.called
190
191
192class _AppIdentityModule(object):
193 """The interface of the App Idenity app engine module.
194 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
195 /google.appengine.api.app_identity.app_identity
196 """
197 def get_application_id(self):
198 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700199
200
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700201@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700202def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700203 """Mocks the app_identity module for google.auth.app_engine."""
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700204 app_identity_module = mock.create_autospec(
205 _AppIdentityModule, instance=True)
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700206 monkeypatch.setattr(
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700207 app_engine, 'app_identity', app_identity_module)
208 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700209
210
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700211def test__get_gae_credentials(app_identity):
212 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700213
214 credentials, project_id = _default._get_gae_credentials()
215
216 assert isinstance(credentials, app_engine.Credentials)
217 assert project_id == mock.sentinel.project
218
219
220def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700221 assert _default._get_gae_credentials() == (None, None)
222
223
224@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800225 'google.auth.compute_engine._metadata.ping', return_value=True,
226 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700227@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700228 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800229 return_value='example-project', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700230def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700231 credentials, project_id = _default._get_gce_credentials()
232
233 assert isinstance(credentials, compute_engine.Credentials)
234 assert project_id == 'example-project'
235
236
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800237@mock.patch(
238 'google.auth.compute_engine._metadata.ping', return_value=False,
239 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700240def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700241 credentials, project_id = _default._get_gce_credentials()
242
243 assert credentials is None
244 assert project_id is None
245
246
247@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800248 'google.auth.compute_engine._metadata.ping', return_value=True,
249 autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700250@mock.patch(
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700251 'google.auth.compute_engine._metadata.get_project_id',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800252 side_effect=exceptions.TransportError(), autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700253def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700254 credentials, project_id = _default._get_gce_credentials()
255
256 assert isinstance(credentials, compute_engine.Credentials)
257 assert project_id is None
258
259
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800260@mock.patch(
261 'google.auth.compute_engine._metadata.ping', return_value=False,
262 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700263def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700264 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700265 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700266
267
268@mock.patch(
269 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800270 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
271 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700272def test_default_early_out(unused_get):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700273 assert _default.default() == (
274 mock.sentinel.credentials, mock.sentinel.project_id)
275
276
277@mock.patch(
278 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800279 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
280 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700281def test_default_explict_project_id(unused_get, monkeypatch):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700282 monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
283 assert _default.default() == (
284 mock.sentinel.credentials, 'explicit-env')
285
286
287@mock.patch(
288 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800289 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
290 autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700291def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800292 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
293 assert _default.default() == (
294 mock.sentinel.credentials, 'explicit-env')
295
296
297@mock.patch(
298 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800299 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700300@mock.patch(
301 'google.auth._default._get_gcloud_sdk_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800302 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700303@mock.patch(
304 'google.auth._default._get_gae_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800305 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700306@mock.patch(
307 'google.auth._default._get_gce_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800308 return_value=(None, None), autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700309def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
310 with pytest.raises(exceptions.DefaultCredentialsError):
311 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800312
313
314@mock.patch(
315 'google.auth._default._get_explicit_environ_credentials',
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800316 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
317 autospec=True)
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800318@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800319 'google.auth.credentials.with_scopes_if_required', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700320def test_default_scoped(with_scopes, get):
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800321 scopes = ['one', 'two']
322
323 credentials, project_id = _default.default(scopes=scopes)
324
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700325 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800326 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700327 with_scopes.assert_called_once_with(
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800328 mock.sentinel.credentials, scopes)