_default_async.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright 2020 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. """Application default credentials.
  15. Implements application default credentials and project ID detection.
  16. """
  17. import io
  18. import json
  19. import os
  20. from google.auth import _default
  21. from google.auth import environment_vars
  22. from google.auth import exceptions
  23. def load_credentials_from_file(filename, scopes=None, quota_project_id=None):
  24. """Loads Google credentials from a file.
  25. The credentials file must be a service account key or stored authorized
  26. user credentials.
  27. Args:
  28. filename (str): The full path to the credentials file.
  29. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  30. specified, the credentials will automatically be scoped if
  31. necessary
  32. quota_project_id (Optional[str]): The project ID used for
  33. quota and billing.
  34. Returns:
  35. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  36. credentials and the project ID. Authorized user credentials do not
  37. have the project ID information.
  38. Raises:
  39. google.auth.exceptions.DefaultCredentialsError: if the file is in the
  40. wrong format or is missing.
  41. """
  42. if not os.path.exists(filename):
  43. raise exceptions.DefaultCredentialsError(
  44. "File {} was not found.".format(filename)
  45. )
  46. with io.open(filename, "r") as file_obj:
  47. try:
  48. info = json.load(file_obj)
  49. except ValueError as caught_exc:
  50. new_exc = exceptions.DefaultCredentialsError(
  51. "File {} is not a valid json file.".format(filename), caught_exc
  52. )
  53. raise new_exc from caught_exc
  54. # The type key should indicate that the file is either a service account
  55. # credentials file or an authorized user credentials file.
  56. credential_type = info.get("type")
  57. if credential_type == _default._AUTHORIZED_USER_TYPE:
  58. from google.oauth2 import _credentials_async as credentials
  59. try:
  60. credentials = credentials.Credentials.from_authorized_user_info(
  61. info, scopes=scopes
  62. )
  63. except ValueError as caught_exc:
  64. msg = "Failed to load authorized user credentials from {}".format(filename)
  65. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  66. raise new_exc from caught_exc
  67. if quota_project_id:
  68. credentials = credentials.with_quota_project(quota_project_id)
  69. if not credentials.quota_project_id:
  70. _default._warn_about_problematic_credentials(credentials)
  71. return credentials, None
  72. elif credential_type == _default._SERVICE_ACCOUNT_TYPE:
  73. from google.oauth2 import _service_account_async as service_account
  74. try:
  75. credentials = service_account.Credentials.from_service_account_info(
  76. info, scopes=scopes
  77. ).with_quota_project(quota_project_id)
  78. except ValueError as caught_exc:
  79. msg = "Failed to load service account credentials from {}".format(filename)
  80. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  81. raise new_exc from caught_exc
  82. return credentials, info.get("project_id")
  83. else:
  84. raise exceptions.DefaultCredentialsError(
  85. "The file {file} does not have a valid type. "
  86. "Type is {type}, expected one of {valid_types}.".format(
  87. file=filename, type=credential_type, valid_types=_default._VALID_TYPES
  88. )
  89. )
  90. def _get_gcloud_sdk_credentials(quota_project_id=None):
  91. """Gets the credentials and project ID from the Cloud SDK."""
  92. from google.auth import _cloud_sdk
  93. # Check if application default credentials exist.
  94. credentials_filename = _cloud_sdk.get_application_default_credentials_path()
  95. if not os.path.isfile(credentials_filename):
  96. return None, None
  97. credentials, project_id = load_credentials_from_file(
  98. credentials_filename, quota_project_id=quota_project_id
  99. )
  100. if not project_id:
  101. project_id = _cloud_sdk.get_project_id()
  102. return credentials, project_id
  103. def _get_explicit_environ_credentials(quota_project_id=None):
  104. """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
  105. variable."""
  106. from google.auth import _cloud_sdk
  107. cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()
  108. explicit_file = os.environ.get(environment_vars.CREDENTIALS)
  109. if explicit_file is not None and explicit_file == cloud_sdk_adc_path:
  110. # Cloud sdk flow calls gcloud to fetch project id, so if the explicit
  111. # file path is cloud sdk credentials path, then we should fall back
  112. # to cloud sdk flow, otherwise project id cannot be obtained.
  113. return _get_gcloud_sdk_credentials(quota_project_id=quota_project_id)
  114. if explicit_file is not None:
  115. credentials, project_id = load_credentials_from_file(
  116. os.environ[environment_vars.CREDENTIALS], quota_project_id=quota_project_id
  117. )
  118. return credentials, project_id
  119. else:
  120. return None, None
  121. def _get_gae_credentials():
  122. """Gets Google App Engine App Identity credentials and project ID."""
  123. # While this library is normally bundled with app_engine, there are
  124. # some cases where it's not available, so we tolerate ImportError.
  125. return _default._get_gae_credentials()
  126. def _get_gce_credentials(request=None):
  127. """Gets credentials and project ID from the GCE Metadata Service."""
  128. # Ping requires a transport, but we want application default credentials
  129. # to require no arguments. So, we'll use the _http_client transport which
  130. # uses http.client. This is only acceptable because the metadata server
  131. # doesn't do SSL and never requires proxies.
  132. # While this library is normally bundled with compute_engine, there are
  133. # some cases where it's not available, so we tolerate ImportError.
  134. return _default._get_gce_credentials(request)
  135. def default_async(scopes=None, request=None, quota_project_id=None):
  136. """Gets the default credentials for the current environment.
  137. `Application Default Credentials`_ provides an easy way to obtain
  138. credentials to call Google APIs for server-to-server or local applications.
  139. This function acquires credentials from the environment in the following
  140. order:
  141. 1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
  142. to the path of a valid service account JSON private key file, then it is
  143. loaded and returned. The project ID returned is the project ID defined
  144. in the service account file if available (some older files do not
  145. contain project ID information).
  146. 2. If the `Google Cloud SDK`_ is installed and has application default
  147. credentials set they are loaded and returned.
  148. To enable application default credentials with the Cloud SDK run::
  149. gcloud auth application-default login
  150. If the Cloud SDK has an active project, the project ID is returned. The
  151. active project can be set using::
  152. gcloud config set project
  153. 3. If the application is running in the `App Engine standard environment`_
  154. (first generation) then the credentials and project ID from the
  155. `App Identity Service`_ are used.
  156. 4. If the application is running in `Compute Engine`_ or `Cloud Run`_ or
  157. the `App Engine flexible environment`_ or the `App Engine standard
  158. environment`_ (second generation) then the credentials and project ID
  159. are obtained from the `Metadata Service`_.
  160. 5. If no credentials are found,
  161. :class:`~google.auth.exceptions.DefaultCredentialsError` will be raised.
  162. .. _Application Default Credentials: https://developers.google.com\
  163. /identity/protocols/application-default-credentials
  164. .. _Google Cloud SDK: https://cloud.google.com/sdk
  165. .. _App Engine standard environment: https://cloud.google.com/appengine
  166. .. _App Identity Service: https://cloud.google.com/appengine/docs/python\
  167. /appidentity/
  168. .. _Compute Engine: https://cloud.google.com/compute
  169. .. _App Engine flexible environment: https://cloud.google.com\
  170. /appengine/flexible
  171. .. _Metadata Service: https://cloud.google.com/compute/docs\
  172. /storing-retrieving-metadata
  173. .. _Cloud Run: https://cloud.google.com/run
  174. Example::
  175. import google.auth
  176. credentials, project_id = google.auth.default()
  177. Args:
  178. scopes (Sequence[str]): The list of scopes for the credentials. If
  179. specified, the credentials will automatically be scoped if
  180. necessary.
  181. request (google.auth.transport.Request): An object used to make
  182. HTTP requests. This is used to detect whether the application
  183. is running on Compute Engine. If not specified, then it will
  184. use the standard library http client to make requests.
  185. quota_project_id (Optional[str]): The project ID used for
  186. quota and billing.
  187. Returns:
  188. Tuple[~google.auth.credentials.Credentials, Optional[str]]:
  189. the current environment's credentials and project ID. Project ID
  190. may be None, which indicates that the Project ID could not be
  191. ascertained from the environment.
  192. Raises:
  193. ~google.auth.exceptions.DefaultCredentialsError:
  194. If no credentials were found, or if the credentials found were
  195. invalid.
  196. """
  197. from google.auth._credentials_async import with_scopes_if_required
  198. from google.auth.credentials import CredentialsWithQuotaProject
  199. explicit_project_id = os.environ.get(
  200. environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)
  201. )
  202. checkers = (
  203. lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
  204. lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),
  205. _get_gae_credentials,
  206. lambda: _get_gce_credentials(request),
  207. )
  208. for checker in checkers:
  209. credentials, project_id = checker()
  210. if credentials is not None:
  211. credentials = with_scopes_if_required(credentials, scopes)
  212. if quota_project_id and isinstance(
  213. credentials, CredentialsWithQuotaProject
  214. ):
  215. credentials = credentials.with_quota_project(quota_project_id)
  216. effective_project_id = explicit_project_id or project_id
  217. if not effective_project_id:
  218. _default._LOGGER.warning(
  219. "No project ID could be determined. Consider running "
  220. "`gcloud config set project` or setting the %s "
  221. "environment variable",
  222. environment_vars.PROJECT,
  223. )
  224. return credentials, effective_project_id
  225. raise exceptions.DefaultCredentialsError(_default._CLOUD_SDK_MISSING_CREDENTIALS)