C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 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 | |
| 15 | import json |
| 16 | import os |
| 17 | |
| 18 | import mock |
| 19 | import pytest |
| 20 | |
| 21 | from google.auth import _default |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 22 | from google.auth import app_engine |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 23 | from google.auth import compute_engine |
| 24 | from google.auth import environment_vars |
| 25 | from google.auth import exceptions |
| 26 | from google.oauth2 import service_account |
| 27 | import google.oauth2.credentials |
| 28 | |
| 29 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 30 | DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
| 31 | AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 32 | |
| 33 | with open(AUTHORIZED_USER_FILE) as fh: |
| 34 | AUTHORIZED_USER_FILE_DATA = json.load(fh) |
| 35 | |
Thea Flowers | a8d9348 | 2018-05-31 14:52:06 -0700 | [diff] [blame] | 36 | AUTHORIZED_USER_CLOUD_SDK_FILE = os.path.join( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 37 | DATA_DIR, "authorized_user_cloud_sdk.json" |
| 38 | ) |
Thea Flowers | a8d9348 | 2018-05-31 14:52:06 -0700 | [diff] [blame] | 39 | |
arithmetic1728 | f30b45a | 2020-06-17 23:36:04 -0700 | [diff] [blame] | 40 | AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE = os.path.join( |
| 41 | DATA_DIR, "authorized_user_cloud_sdk_with_quota_project_id.json" |
| 42 | ) |
| 43 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 44 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 45 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 46 | CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, "client_secrets.json") |
| 47 | |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 48 | with open(SERVICE_ACCOUNT_FILE) as fh: |
| 49 | SERVICE_ACCOUNT_FILE_DATA = json.load(fh) |
| 50 | |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 51 | LOAD_FILE_PATCH = mock.patch( |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 52 | "google.auth._default.load_credentials_from_file", |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 53 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
| 54 | autospec=True, |
| 55 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 56 | |
| 57 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 58 | def test_load_credentials_from_missing_file(): |
weitaiting | 6e86c93 | 2017-08-12 03:26:59 +0800 | [diff] [blame] | 59 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 60 | _default.load_credentials_from_file("") |
weitaiting | 6e86c93 | 2017-08-12 03:26:59 +0800 | [diff] [blame] | 61 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 62 | assert excinfo.match(r"not found") |
weitaiting | 6e86c93 | 2017-08-12 03:26:59 +0800 | [diff] [blame] | 63 | |
| 64 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 65 | def test_load_credentials_from_file_invalid_json(tmpdir): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 66 | jsonfile = tmpdir.join("invalid.json") |
| 67 | jsonfile.write("{") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 68 | |
| 69 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 70 | _default.load_credentials_from_file(str(jsonfile)) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 71 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 72 | assert excinfo.match(r"not a valid json file") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 73 | |
| 74 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 75 | def test_load_credentials_from_file_invalid_type(tmpdir): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 76 | jsonfile = tmpdir.join("invalid.json") |
| 77 | jsonfile.write(json.dumps({"type": "not-a-real-type"})) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 78 | |
| 79 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 80 | _default.load_credentials_from_file(str(jsonfile)) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 81 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 82 | assert excinfo.match(r"does not have a valid type") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 83 | |
| 84 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 85 | def test_load_credentials_from_file_authorized_user(): |
| 86 | credentials, project_id = _default.load_credentials_from_file(AUTHORIZED_USER_FILE) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 87 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 88 | assert project_id is None |
| 89 | |
| 90 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 91 | def test_load_credentials_from_file_no_type(tmpdir): |
| 92 | # use the client_secrets.json, which is valid json but not a |
| 93 | # loadable credentials type |
| 94 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 95 | _default.load_credentials_from_file(CLIENT_SECRETS_FILE) |
| 96 | |
| 97 | assert excinfo.match(r"does not have a valid type") |
| 98 | assert excinfo.match(r"Type is None") |
| 99 | |
| 100 | |
| 101 | def test_load_credentials_from_file_authorized_user_bad_format(tmpdir): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 102 | filename = tmpdir.join("authorized_user_bad.json") |
| 103 | filename.write(json.dumps({"type": "authorized_user"})) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 104 | |
| 105 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 106 | _default.load_credentials_from_file(str(filename)) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 107 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 108 | assert excinfo.match(r"Failed to load authorized user") |
| 109 | assert excinfo.match(r"missing fields") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 110 | |
| 111 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 112 | def test_load_credentials_from_file_authorized_user_cloud_sdk(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 113 | with pytest.warns(UserWarning, match="Cloud SDK"): |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 114 | credentials, project_id = _default.load_credentials_from_file( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 115 | AUTHORIZED_USER_CLOUD_SDK_FILE |
| 116 | ) |
Thea Flowers | a8d9348 | 2018-05-31 14:52:06 -0700 | [diff] [blame] | 117 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 118 | assert project_id is None |
| 119 | |
arithmetic1728 | f30b45a | 2020-06-17 23:36:04 -0700 | [diff] [blame] | 120 | # No warning if the json file has quota project id. |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 121 | credentials, project_id = _default.load_credentials_from_file( |
arithmetic1728 | f30b45a | 2020-06-17 23:36:04 -0700 | [diff] [blame] | 122 | AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE |
| 123 | ) |
| 124 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 125 | assert project_id is None |
| 126 | |
Thea Flowers | a8d9348 | 2018-05-31 14:52:06 -0700 | [diff] [blame] | 127 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 128 | def test_load_credentials_from_file_authorized_user_cloud_sdk_with_scopes(): |
| 129 | with pytest.warns(UserWarning, match="Cloud SDK"): |
| 130 | credentials, project_id = _default.load_credentials_from_file( |
| 131 | AUTHORIZED_USER_CLOUD_SDK_FILE, |
| 132 | scopes=["https://www.google.com/calendar/feeds"], |
| 133 | ) |
| 134 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 135 | assert project_id is None |
| 136 | assert credentials.scopes == ["https://www.google.com/calendar/feeds"] |
| 137 | |
| 138 | |
| 139 | def test_load_credentials_from_file_service_account(): |
| 140 | credentials, project_id = _default.load_credentials_from_file(SERVICE_ACCOUNT_FILE) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 141 | assert isinstance(credentials, service_account.Credentials) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 142 | assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"] |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 143 | |
| 144 | |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 145 | def test_load_credentials_from_file_service_account_with_scopes(): |
| 146 | credentials, project_id = _default.load_credentials_from_file( |
| 147 | SERVICE_ACCOUNT_FILE, scopes=["https://www.google.com/calendar/feeds"] |
| 148 | ) |
| 149 | assert isinstance(credentials, service_account.Credentials) |
| 150 | assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"] |
| 151 | assert credentials.scopes == ["https://www.google.com/calendar/feeds"] |
| 152 | |
| 153 | |
| 154 | def test_load_credentials_from_file_service_account_bad_format(tmpdir): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 155 | filename = tmpdir.join("serivce_account_bad.json") |
| 156 | filename.write(json.dumps({"type": "service_account"})) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 157 | |
| 158 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
Bu Sun Kim | 15d5fa9 | 2020-06-18 14:05:40 -0700 | [diff] [blame^] | 159 | _default.load_credentials_from_file(str(filename)) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 160 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 161 | assert excinfo.match(r"Failed to load service account") |
| 162 | assert excinfo.match(r"missing fields") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 163 | |
| 164 | |
| 165 | @mock.patch.dict(os.environ, {}, clear=True) |
| 166 | def test__get_explicit_environ_credentials_no_env(): |
| 167 | assert _default._get_explicit_environ_credentials() == (None, None) |
| 168 | |
| 169 | |
| 170 | @LOAD_FILE_PATCH |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 171 | def test__get_explicit_environ_credentials(load, monkeypatch): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 172 | monkeypatch.setenv(environment_vars.CREDENTIALS, "filename") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 173 | |
| 174 | credentials, project_id = _default._get_explicit_environ_credentials() |
| 175 | |
| 176 | assert credentials is mock.sentinel.credentials |
| 177 | assert project_id is mock.sentinel.project_id |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 178 | load.assert_called_with("filename") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 179 | |
| 180 | |
| 181 | @LOAD_FILE_PATCH |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 182 | def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch): |
| 183 | load.return_value = mock.sentinel.credentials, None |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 184 | monkeypatch.setenv(environment_vars.CREDENTIALS, "filename") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 185 | |
| 186 | credentials, project_id = _default._get_explicit_environ_credentials() |
| 187 | |
| 188 | assert credentials is mock.sentinel.credentials |
| 189 | assert project_id is None |
| 190 | |
| 191 | |
| 192 | @LOAD_FILE_PATCH |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 193 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 194 | "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True |
| 195 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 196 | def test__get_gcloud_sdk_credentials(get_adc_path, load): |
| 197 | get_adc_path.return_value = SERVICE_ACCOUNT_FILE |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 198 | |
| 199 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 200 | |
| 201 | assert credentials is mock.sentinel.credentials |
| 202 | assert project_id is mock.sentinel.project_id |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 203 | load.assert_called_with(SERVICE_ACCOUNT_FILE) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 204 | |
| 205 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 206 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 207 | "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True |
| 208 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 209 | def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 210 | non_existent = tmpdir.join("non-existent") |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 211 | get_adc_path.return_value = str(non_existent) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 212 | |
| 213 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 214 | |
| 215 | assert credentials is None |
| 216 | assert project_id is None |
| 217 | |
| 218 | |
| 219 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 220 | "google.auth._cloud_sdk.get_project_id", |
| 221 | return_value=mock.sentinel.project_id, |
| 222 | autospec=True, |
| 223 | ) |
| 224 | @mock.patch("os.path.isfile", return_value=True, autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 225 | @LOAD_FILE_PATCH |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 226 | def test__get_gcloud_sdk_credentials_project_id(load, unused_isfile, get_project_id): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 227 | # Don't return a project ID from load file, make the function check |
| 228 | # the Cloud SDK project. |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 229 | load.return_value = mock.sentinel.credentials, None |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 230 | |
| 231 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 232 | |
| 233 | assert credentials == mock.sentinel.credentials |
| 234 | assert project_id == mock.sentinel.project_id |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 235 | assert get_project_id.called |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 236 | |
| 237 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 238 | @mock.patch("google.auth._cloud_sdk.get_project_id", return_value=None, autospec=True) |
| 239 | @mock.patch("os.path.isfile", return_value=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 240 | @LOAD_FILE_PATCH |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 241 | def test__get_gcloud_sdk_credentials_no_project_id(load, unused_isfile, get_project_id): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 242 | # Don't return a project ID from load file, make the function check |
| 243 | # the Cloud SDK project. |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 244 | load.return_value = mock.sentinel.credentials, None |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 245 | |
| 246 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 247 | |
| 248 | assert credentials == mock.sentinel.credentials |
| 249 | assert project_id is None |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 250 | assert get_project_id.called |
| 251 | |
| 252 | |
| 253 | class _AppIdentityModule(object): |
| 254 | """The interface of the App Idenity app engine module. |
| 255 | See https://cloud.google.com/appengine/docs/standard/python/refdocs\ |
| 256 | /google.appengine.api.app_identity.app_identity |
| 257 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 258 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 259 | def get_application_id(self): |
| 260 | raise NotImplementedError() |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 261 | |
| 262 | |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 263 | @pytest.fixture |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 264 | def app_identity(monkeypatch): |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 265 | """Mocks the app_identity module for google.auth.app_engine.""" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 266 | app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True) |
| 267 | monkeypatch.setattr(app_engine, "app_identity", app_identity_module) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 268 | yield app_identity_module |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 269 | |
| 270 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 271 | def test__get_gae_credentials(app_identity): |
| 272 | app_identity.get_application_id.return_value = mock.sentinel.project |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 273 | |
| 274 | credentials, project_id = _default._get_gae_credentials() |
| 275 | |
| 276 | assert isinstance(credentials, app_engine.Credentials) |
| 277 | assert project_id == mock.sentinel.project |
| 278 | |
| 279 | |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 280 | def test__get_gae_credentials_no_app_engine(): |
| 281 | import sys |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 282 | |
| 283 | with mock.patch.dict("sys.modules"): |
| 284 | sys.modules["google.auth.app_engine"] = None |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 285 | credentials, project_id = _default._get_gae_credentials() |
| 286 | assert credentials is None |
| 287 | assert project_id is None |
| 288 | |
| 289 | |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 290 | def test__get_gae_credentials_no_apis(): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 291 | assert _default._get_gae_credentials() == (None, None) |
| 292 | |
| 293 | |
| 294 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 295 | "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True |
| 296 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 297 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 298 | "google.auth.compute_engine._metadata.get_project_id", |
| 299 | return_value="example-project", |
| 300 | autospec=True, |
| 301 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 302 | def test__get_gce_credentials(unused_get, unused_ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 303 | credentials, project_id = _default._get_gce_credentials() |
| 304 | |
| 305 | assert isinstance(credentials, compute_engine.Credentials) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 306 | assert project_id == "example-project" |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 307 | |
| 308 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 309 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 310 | "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True |
| 311 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 312 | def test__get_gce_credentials_no_ping(unused_ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 313 | credentials, project_id = _default._get_gce_credentials() |
| 314 | |
| 315 | assert credentials is None |
| 316 | assert project_id is None |
| 317 | |
| 318 | |
| 319 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 320 | "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True |
| 321 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 322 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 323 | "google.auth.compute_engine._metadata.get_project_id", |
| 324 | side_effect=exceptions.TransportError(), |
| 325 | autospec=True, |
| 326 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 327 | def test__get_gce_credentials_no_project_id(unused_get, unused_ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 328 | credentials, project_id = _default._get_gce_credentials() |
| 329 | |
| 330 | assert isinstance(credentials, compute_engine.Credentials) |
| 331 | assert project_id is None |
| 332 | |
| 333 | |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 334 | def test__get_gce_credentials_no_compute_engine(): |
| 335 | import sys |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 336 | |
| 337 | with mock.patch.dict("sys.modules"): |
| 338 | sys.modules["google.auth.compute_engine"] = None |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 339 | credentials, project_id = _default._get_gce_credentials() |
| 340 | assert credentials is None |
| 341 | assert project_id is None |
| 342 | |
| 343 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 344 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 345 | "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True |
| 346 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 347 | def test__get_gce_credentials_explicit_request(ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 348 | _default._get_gce_credentials(mock.sentinel.request) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 349 | ping.assert_called_with(request=mock.sentinel.request) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 350 | |
| 351 | |
| 352 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 353 | "google.auth._default._get_explicit_environ_credentials", |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 354 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 355 | autospec=True, |
| 356 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 357 | def test_default_early_out(unused_get): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 358 | assert _default.default() == (mock.sentinel.credentials, mock.sentinel.project_id) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 359 | |
| 360 | |
| 361 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 362 | "google.auth._default._get_explicit_environ_credentials", |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 363 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 364 | autospec=True, |
| 365 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 366 | def test_default_explict_project_id(unused_get, monkeypatch): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 367 | monkeypatch.setenv(environment_vars.PROJECT, "explicit-env") |
| 368 | assert _default.default() == (mock.sentinel.credentials, "explicit-env") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 369 | |
| 370 | |
| 371 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 372 | "google.auth._default._get_explicit_environ_credentials", |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 373 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 374 | autospec=True, |
| 375 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 376 | def test_default_explict_legacy_project_id(unused_get, monkeypatch): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 377 | monkeypatch.setenv(environment_vars.LEGACY_PROJECT, "explicit-env") |
| 378 | assert _default.default() == (mock.sentinel.credentials, "explicit-env") |
Jon Wayne Parrott | ce37cba | 2016-11-07 16:41:42 -0800 | [diff] [blame] | 379 | |
| 380 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 381 | @mock.patch("logging.Logger.warning", autospec=True) |
Jon Wayne Parrott | ce37cba | 2016-11-07 16:41:42 -0800 | [diff] [blame] | 382 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 383 | "google.auth._default._get_explicit_environ_credentials", |
| 384 | return_value=(mock.sentinel.credentials, None), |
| 385 | autospec=True, |
| 386 | ) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 387 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 388 | "google.auth._default._get_gcloud_sdk_credentials", |
| 389 | return_value=(mock.sentinel.credentials, None), |
| 390 | autospec=True, |
| 391 | ) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 392 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 393 | "google.auth._default._get_gae_credentials", |
| 394 | return_value=(mock.sentinel.credentials, None), |
| 395 | autospec=True, |
| 396 | ) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 397 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 398 | "google.auth._default._get_gce_credentials", |
| 399 | return_value=(mock.sentinel.credentials, None), |
| 400 | autospec=True, |
| 401 | ) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 402 | def test_default_without_project_id( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 403 | unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning |
| 404 | ): |
| 405 | assert _default.default() == (mock.sentinel.credentials, None) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 406 | logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY) |
| 407 | |
| 408 | |
| 409 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 410 | "google.auth._default._get_explicit_environ_credentials", |
| 411 | return_value=(None, None), |
| 412 | autospec=True, |
| 413 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 414 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 415 | "google.auth._default._get_gcloud_sdk_credentials", |
| 416 | return_value=(None, None), |
| 417 | autospec=True, |
| 418 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 419 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 420 | "google.auth._default._get_gae_credentials", |
| 421 | return_value=(None, None), |
| 422 | autospec=True, |
| 423 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 424 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 425 | "google.auth._default._get_gce_credentials", |
| 426 | return_value=(None, None), |
| 427 | autospec=True, |
| 428 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 429 | def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit): |
| 430 | with pytest.raises(exceptions.DefaultCredentialsError): |
| 431 | assert _default.default() |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 432 | |
| 433 | |
| 434 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 435 | "google.auth._default._get_explicit_environ_credentials", |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 436 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 437 | autospec=True, |
| 438 | ) |
| 439 | @mock.patch("google.auth.credentials.with_scopes_if_required", autospec=True) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 440 | def test_default_scoped(with_scopes, unused_get): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 441 | scopes = ["one", "two"] |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 442 | |
| 443 | credentials, project_id = _default.default(scopes=scopes) |
| 444 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 445 | assert credentials == with_scopes.return_value |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 446 | assert project_id == mock.sentinel.project_id |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 447 | with_scopes.assert_called_once_with(mock.sentinel.credentials, scopes) |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 448 | |
| 449 | |
| 450 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 451 | "google.auth._default._get_explicit_environ_credentials", |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 452 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 453 | autospec=True, |
| 454 | ) |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 455 | def test_default_no_app_engine_compute_engine_module(unused_get): |
| 456 | """ |
| 457 | google.auth.compute_engine and google.auth.app_engine are both optional |
| 458 | to allow not including them when using this package. This verifies |
| 459 | that default fails gracefully if these modules are absent |
| 460 | """ |
| 461 | import sys |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 462 | |
| 463 | with mock.patch.dict("sys.modules"): |
| 464 | sys.modules["google.auth.compute_engine"] = None |
| 465 | sys.modules["google.auth.app_engine"] = None |
James Wilson | 6e0781b | 2018-12-20 20:38:52 -0500 | [diff] [blame] | 466 | assert _default.default() == ( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 467 | mock.sentinel.credentials, |
| 468 | mock.sentinel.project_id, |
| 469 | ) |