blob: d63dcee3bcb62c7e3efc8a5a912ab3d891fac0c4 [file] [log] [blame]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -07001# Copyright 2015 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
15"""Application default credentials.
16
17Implements application default credentials and project ID detection.
18"""
19
20import io
21import json
22import logging
23import os
24
Danny Hermes895e3692017-11-09 11:35:57 -080025import six
26
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070027from google.auth import environment_vars
28from google.auth import exceptions
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070029import google.auth.transport._http_client
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070030
31_LOGGER = logging.getLogger(__name__)
32
33# Valid types accepted for file-based credentials.
34_AUTHORIZED_USER_TYPE = 'authorized_user'
35_SERVICE_ACCOUNT_TYPE = 'service_account'
36_VALID_TYPES = (_AUTHORIZED_USER_TYPE, _SERVICE_ACCOUNT_TYPE)
37
38# Help message when no credentials can be found.
39_HELP_MESSAGE = """
40Could not automatically determine credentials. Please set {env} or
41explicitly create credential and re-run the application. For more
42information, please see
43https://developers.google.com/accounts/docs/application-default-credentials.
44""".format(env=environment_vars.CREDENTIALS).strip()
45
46
47def _load_credentials_from_file(filename):
48 """Loads credentials from a file.
49
50 The credentials file must be a service account key or stored authorized
51 user credentials.
52
53 Args:
54 filename (str): The full path to the credentials file.
55
56 Returns:
57 Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
58 credentials and the project ID. Authorized user credentials do not
59 have the project ID information.
60
61 Raises:
62 google.auth.exceptions.DefaultCredentialsError: if the file is in the
weitaiting6e86c932017-08-12 03:26:59 +080063 wrong format or is missing.
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070064 """
weitaiting6e86c932017-08-12 03:26:59 +080065 if not os.path.exists(filename):
66 raise exceptions.DefaultCredentialsError(
67 'File {} was not found.'.format(filename))
68
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070069 with io.open(filename, 'r') as file_obj:
70 try:
71 info = json.load(file_obj)
Danny Hermes895e3692017-11-09 11:35:57 -080072 except ValueError as caught_exc:
73 new_exc = exceptions.DefaultCredentialsError(
Danny Hermes0a93e872017-11-09 12:18:58 -080074 'File {} is not a valid json file.'.format(filename),
75 caught_exc)
Danny Hermes895e3692017-11-09 11:35:57 -080076 six.raise_from(new_exc, caught_exc)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070077
78 # The type key should indicate that the file is either a service account
79 # credentials file or an authorized user credentials file.
80 credential_type = info.get('type')
81
82 if credential_type == _AUTHORIZED_USER_TYPE:
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -080083 from google.auth import _cloud_sdk
84
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070085 try:
86 credentials = _cloud_sdk.load_authorized_user_credentials(info)
Danny Hermes895e3692017-11-09 11:35:57 -080087 except ValueError as caught_exc:
Danny Hermes0a93e872017-11-09 12:18:58 -080088 msg = 'Failed to load authorized user credentials from {}'.format(
89 filename)
90 new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
Danny Hermes895e3692017-11-09 11:35:57 -080091 six.raise_from(new_exc, caught_exc)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070092 # Authorized user credentials do not contain the project ID.
93 return credentials, None
94
95 elif credential_type == _SERVICE_ACCOUNT_TYPE:
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -080096 from google.oauth2 import service_account
97
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070098 try:
99 credentials = (
100 service_account.Credentials.from_service_account_info(info))
Danny Hermes895e3692017-11-09 11:35:57 -0800101 except ValueError as caught_exc:
Danny Hermes0a93e872017-11-09 12:18:58 -0800102 msg = 'Failed to load service account credentials from {}'.format(
103 filename)
104 new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
Danny Hermes895e3692017-11-09 11:35:57 -0800105 six.raise_from(new_exc, caught_exc)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700106 return credentials, info.get('project_id')
107
108 else:
109 raise exceptions.DefaultCredentialsError(
110 'The file {file} does not have a valid type. '
111 'Type is {type}, expected one of {valid_types}.'.format(
112 file=filename, type=credential_type, valid_types=_VALID_TYPES))
113
114
115def _get_gcloud_sdk_credentials():
116 """Gets the credentials and project ID from the Cloud SDK."""
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -0800117 from google.auth import _cloud_sdk
118
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700119 # Check if application default credentials exist.
120 credentials_filename = (
121 _cloud_sdk.get_application_default_credentials_path())
122
123 if not os.path.isfile(credentials_filename):
124 return None, None
125
126 credentials, project_id = _load_credentials_from_file(
127 credentials_filename)
128
129 if not project_id:
130 project_id = _cloud_sdk.get_project_id()
131
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700132 return credentials, project_id
133
134
135def _get_explicit_environ_credentials():
136 """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
137 variable."""
138 explicit_file = os.environ.get(environment_vars.CREDENTIALS)
139
140 if explicit_file is not None:
141 credentials, project_id = _load_credentials_from_file(
142 os.environ[environment_vars.CREDENTIALS])
143
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700144 return credentials, project_id
145
146 else:
147 return None, None
148
149
150def _get_gae_credentials():
151 """Gets Google App Engine App Identity credentials and project ID."""
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -0800152 from google.auth import app_engine
153
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700154 try:
155 credentials = app_engine.Credentials()
156 project_id = app_engine.get_project_id()
157 return credentials, project_id
158 except EnvironmentError:
159 return None, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700160
161
162def _get_gce_credentials(request=None):
163 """Gets credentials and project ID from the GCE Metadata Service."""
164 # Ping requires a transport, but we want application default credentials
165 # to require no arguments. So, we'll use the _http_client transport which
166 # uses http.client. This is only acceptable because the metadata server
167 # doesn't do SSL and never requires proxies.
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -0800168 from google.auth import compute_engine
169 from google.auth.compute_engine import _metadata
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700170
171 if request is None:
172 request = google.auth.transport._http_client.Request()
173
174 if _metadata.ping(request=request):
175 # Get the project ID.
176 try:
Jon Wayne Parrott5b03ba12016-10-24 13:51:26 -0700177 project_id = _metadata.get_project_id(request=request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700178 except exceptions.TransportError:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700179 project_id = None
180
181 return compute_engine.Credentials(), project_id
182 else:
183 return None, None
184
185
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800186def default(scopes=None, request=None):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700187 """Gets the default credentials for the current environment.
188
189 `Application Default Credentials`_ provides an easy way to obtain
190 credentials to call Google APIs for server-to-server or local applications.
191 This function acquires credentials from the environment in the following
192 order:
193
194 1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
195 to the path of a valid service account JSON private key file, then it is
196 loaded and returned. The project ID returned is the project ID defined
197 in the service account file if available (some older files do not
198 contain project ID information).
199 2. If the `Google Cloud SDK`_ is installed and has application default
200 credentials set they are loaded and returned.
201
202 To enable application default credentials with the Cloud SDK run::
203
204 gcloud auth application-default login
205
206 If the Cloud SDK has an active project, the project ID is returned. The
207 active project can be set using::
208
209 gcloud config set project
210
211 3. If the application is running in the `App Engine standard environment`_
212 then the credentials and project ID from the `App Identity Service`_
213 are used.
214 4. If the application is running in `Compute Engine`_ or the
215 `App Engine flexible environment`_ then the credentials and project ID
216 are obtained from the `Metadata Service`_.
217 5. If no credentials are found,
218 :class:`~google.auth.exceptions.DefaultCredentialsError` will be raised.
219
220 .. _Application Default Credentials: https://developers.google.com\
221 /identity/protocols/application-default-credentials
222 .. _Google Cloud SDK: https://cloud.google.com/sdk
223 .. _App Engine standard environment: https://cloud.google.com/appengine
224 .. _App Identity Service: https://cloud.google.com/appengine/docs/python\
225 /appidentity/
226 .. _Compute Engine: https://cloud.google.com/compute
227 .. _App Engine flexible environment: https://cloud.google.com\
228 /appengine/flexible
229 .. _Metadata Service: https://cloud.google.com/compute/docs\
230 /storing-retrieving-metadata
231
232 Example::
233
234 import google.auth
235
236 credentials, project_id = google.auth.default()
237
238 Args:
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800239 scopes (Sequence[str]): The list of scopes for the credentials. If
240 specified, the credentials will automatically be scoped if
241 necessary.
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700242 request (google.auth.transport.Request): An object used to make
243 HTTP requests. This is used to detect whether the application
244 is running on Compute Engine. If not specified, then it will
245 use the standard library http client to make requests.
246
247 Returns:
248 Tuple[~google.auth.credentials.Credentials, Optional[str]]:
249 the current environment's credentials and project ID. Project ID
250 may be None, which indicates that the Project ID could not be
251 ascertained from the environment.
252
253 Raises:
254 ~google.auth.exceptions.DefaultCredentialsError:
255 If no credentials were found, or if the credentials found were
256 invalid.
257 """
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -0800258 from google.auth.credentials import with_scopes_if_required
259
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800260 explicit_project_id = os.environ.get(
261 environment_vars.PROJECT,
262 os.environ.get(environment_vars.LEGACY_PROJECT))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700263
264 checkers = (
265 _get_explicit_environ_credentials,
266 _get_gcloud_sdk_credentials,
267 _get_gae_credentials,
268 lambda: _get_gce_credentials(request))
269
270 for checker in checkers:
271 credentials, project_id = checker()
272 if credentials is not None:
Jon Wayne Parrott6dca98c2016-12-01 15:34:59 -0800273 credentials = with_scopes_if_required(credentials, scopes)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600274 effective_project_id = explicit_project_id or project_id
275 if not effective_project_id:
276 _LOGGER.warning(
277 'No project ID could be determined. Consider running '
278 '`gcloud config set project` or setting the %s '
279 'environment variable',
280 environment_vars.PROJECT)
281 return credentials, effective_project_id
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700282
283 raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)