_default.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. # 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. """Application default credentials.
  15. Implements application default credentials and project ID detection.
  16. """
  17. import io
  18. import json
  19. import logging
  20. import os
  21. import warnings
  22. import six
  23. from google.auth import environment_vars
  24. from google.auth import exceptions
  25. import google.auth.transport._http_client
  26. _LOGGER = logging.getLogger(__name__)
  27. # Valid types accepted for file-based credentials.
  28. _AUTHORIZED_USER_TYPE = "authorized_user"
  29. _SERVICE_ACCOUNT_TYPE = "service_account"
  30. _EXTERNAL_ACCOUNT_TYPE = "external_account"
  31. _VALID_TYPES = (_AUTHORIZED_USER_TYPE, _SERVICE_ACCOUNT_TYPE, _EXTERNAL_ACCOUNT_TYPE)
  32. # Help message when no credentials can be found.
  33. _HELP_MESSAGE = """\
  34. Could not automatically determine credentials. Please set {env} or \
  35. explicitly create credentials and re-run the application. For more \
  36. information, please see \
  37. https://cloud.google.com/docs/authentication/getting-started
  38. """.format(
  39. env=environment_vars.CREDENTIALS
  40. ).strip()
  41. # Warning when using Cloud SDK user credentials
  42. _CLOUD_SDK_CREDENTIALS_WARNING = """\
  43. Your application has authenticated using end user credentials from Google \
  44. Cloud SDK without a quota project. You might receive a "quota exceeded" \
  45. or "API not enabled" error. We recommend you rerun \
  46. `gcloud auth application-default login` and make sure a quota project is \
  47. added. Or you can use service accounts instead. For more information \
  48. about service accounts, see https://cloud.google.com/docs/authentication/"""
  49. def _warn_about_problematic_credentials(credentials):
  50. """Determines if the credentials are problematic.
  51. Credentials from the Cloud SDK that are associated with Cloud SDK's project
  52. are problematic because they may not have APIs enabled and have limited
  53. quota. If this is the case, warn about it.
  54. """
  55. from google.auth import _cloud_sdk
  56. if credentials.client_id == _cloud_sdk.CLOUD_SDK_CLIENT_ID:
  57. warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
  58. def load_credentials_from_file(
  59. filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
  60. ):
  61. """Loads Google credentials from a file.
  62. The credentials file must be a service account key, stored authorized
  63. user credentials or external account credentials.
  64. Args:
  65. filename (str): The full path to the credentials file.
  66. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  67. specified, the credentials will automatically be scoped if
  68. necessary
  69. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  70. Google client library. Use 'scopes' for user-defined scopes.
  71. quota_project_id (Optional[str]): The project ID used for
  72. quota and billing.
  73. request (Optional[google.auth.transport.Request]): An object used to make
  74. HTTP requests. This is used to determine the associated project ID
  75. for a workload identity pool resource (external account credentials).
  76. If not specified, then it will use a
  77. google.auth.transport.requests.Request client to make requests.
  78. Returns:
  79. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  80. credentials and the project ID. Authorized user credentials do not
  81. have the project ID information. External account credentials project
  82. IDs may not always be determined.
  83. Raises:
  84. google.auth.exceptions.DefaultCredentialsError: if the file is in the
  85. wrong format or is missing.
  86. """
  87. if not os.path.exists(filename):
  88. raise exceptions.DefaultCredentialsError(
  89. "File {} was not found.".format(filename)
  90. )
  91. with io.open(filename, "r") as file_obj:
  92. try:
  93. info = json.load(file_obj)
  94. except ValueError as caught_exc:
  95. new_exc = exceptions.DefaultCredentialsError(
  96. "File {} is not a valid json file.".format(filename), caught_exc
  97. )
  98. six.raise_from(new_exc, caught_exc)
  99. # The type key should indicate that the file is either a service account
  100. # credentials file or an authorized user credentials file.
  101. credential_type = info.get("type")
  102. if credential_type == _AUTHORIZED_USER_TYPE:
  103. from google.oauth2 import credentials
  104. try:
  105. credentials = credentials.Credentials.from_authorized_user_info(
  106. info, scopes=scopes
  107. )
  108. except ValueError as caught_exc:
  109. msg = "Failed to load authorized user credentials from {}".format(filename)
  110. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  111. six.raise_from(new_exc, caught_exc)
  112. if quota_project_id:
  113. credentials = credentials.with_quota_project(quota_project_id)
  114. if not credentials.quota_project_id:
  115. _warn_about_problematic_credentials(credentials)
  116. return credentials, None
  117. elif credential_type == _SERVICE_ACCOUNT_TYPE:
  118. from google.oauth2 import service_account
  119. try:
  120. credentials = service_account.Credentials.from_service_account_info(
  121. info, scopes=scopes, default_scopes=default_scopes
  122. )
  123. except ValueError as caught_exc:
  124. msg = "Failed to load service account credentials from {}".format(filename)
  125. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  126. six.raise_from(new_exc, caught_exc)
  127. if quota_project_id:
  128. credentials = credentials.with_quota_project(quota_project_id)
  129. return credentials, info.get("project_id")
  130. elif credential_type == _EXTERNAL_ACCOUNT_TYPE:
  131. credentials, project_id = _get_external_account_credentials(
  132. info,
  133. filename,
  134. scopes=scopes,
  135. default_scopes=default_scopes,
  136. request=request,
  137. )
  138. if quota_project_id:
  139. credentials = credentials.with_quota_project(quota_project_id)
  140. return credentials, project_id
  141. else:
  142. raise exceptions.DefaultCredentialsError(
  143. "The file {file} does not have a valid type. "
  144. "Type is {type}, expected one of {valid_types}.".format(
  145. file=filename, type=credential_type, valid_types=_VALID_TYPES
  146. )
  147. )
  148. def _get_gcloud_sdk_credentials():
  149. """Gets the credentials and project ID from the Cloud SDK."""
  150. from google.auth import _cloud_sdk
  151. _LOGGER.debug("Checking Cloud SDK credentials as part of auth process...")
  152. # Check if application default credentials exist.
  153. credentials_filename = _cloud_sdk.get_application_default_credentials_path()
  154. if not os.path.isfile(credentials_filename):
  155. _LOGGER.debug("Cloud SDK credentials not found on disk; not using them")
  156. return None, None
  157. credentials, project_id = load_credentials_from_file(credentials_filename)
  158. if not project_id:
  159. project_id = _cloud_sdk.get_project_id()
  160. return credentials, project_id
  161. def _get_explicit_environ_credentials():
  162. """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
  163. variable."""
  164. from google.auth import _cloud_sdk
  165. cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()
  166. explicit_file = os.environ.get(environment_vars.CREDENTIALS)
  167. _LOGGER.debug(
  168. "Checking %s for explicit credentials as part of auth process...", explicit_file
  169. )
  170. if explicit_file is not None and explicit_file == cloud_sdk_adc_path:
  171. # Cloud sdk flow calls gcloud to fetch project id, so if the explicit
  172. # file path is cloud sdk credentials path, then we should fall back
  173. # to cloud sdk flow, otherwise project id cannot be obtained.
  174. _LOGGER.debug(
  175. "Explicit credentials path %s is the same as Cloud SDK credentials path, fall back to Cloud SDK credentials flow...",
  176. explicit_file,
  177. )
  178. return _get_gcloud_sdk_credentials()
  179. if explicit_file is not None:
  180. credentials, project_id = load_credentials_from_file(
  181. os.environ[environment_vars.CREDENTIALS]
  182. )
  183. return credentials, project_id
  184. else:
  185. return None, None
  186. def _get_gae_credentials():
  187. """Gets Google App Engine App Identity credentials and project ID."""
  188. # If not GAE gen1, prefer the metadata service even if the GAE APIs are
  189. # available as per https://google.aip.dev/auth/4115.
  190. if os.environ.get(environment_vars.LEGACY_APPENGINE_RUNTIME) != "python27":
  191. return None, None
  192. # While this library is normally bundled with app_engine, there are
  193. # some cases where it's not available, so we tolerate ImportError.
  194. try:
  195. _LOGGER.debug("Checking for App Engine runtime as part of auth process...")
  196. import google.auth.app_engine as app_engine
  197. except ImportError:
  198. _LOGGER.warning("Import of App Engine auth library failed.")
  199. return None, None
  200. try:
  201. credentials = app_engine.Credentials()
  202. project_id = app_engine.get_project_id()
  203. return credentials, project_id
  204. except EnvironmentError:
  205. _LOGGER.debug(
  206. "No App Engine library was found so cannot authentication via App Engine Identity Credentials."
  207. )
  208. return None, None
  209. def _get_gce_credentials(request=None):
  210. """Gets credentials and project ID from the GCE Metadata Service."""
  211. # Ping requires a transport, but we want application default credentials
  212. # to require no arguments. So, we'll use the _http_client transport which
  213. # uses http.client. This is only acceptable because the metadata server
  214. # doesn't do SSL and never requires proxies.
  215. # While this library is normally bundled with compute_engine, there are
  216. # some cases where it's not available, so we tolerate ImportError.
  217. try:
  218. from google.auth import compute_engine
  219. from google.auth.compute_engine import _metadata
  220. except ImportError:
  221. _LOGGER.warning("Import of Compute Engine auth library failed.")
  222. return None, None
  223. if request is None:
  224. request = google.auth.transport._http_client.Request()
  225. if _metadata.ping(request=request):
  226. # Get the project ID.
  227. try:
  228. project_id = _metadata.get_project_id(request=request)
  229. except exceptions.TransportError:
  230. project_id = None
  231. return compute_engine.Credentials(), project_id
  232. else:
  233. _LOGGER.warning(
  234. "Authentication failed using Compute Engine authentication due to unavailable metadata server."
  235. )
  236. return None, None
  237. def _get_external_account_credentials(
  238. info, filename, scopes=None, default_scopes=None, request=None
  239. ):
  240. """Loads external account Credentials from the parsed external account info.
  241. The credentials information must correspond to a supported external account
  242. credentials.
  243. Args:
  244. info (Mapping[str, str]): The external account info in Google format.
  245. filename (str): The full path to the credentials file.
  246. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  247. specified, the credentials will automatically be scoped if
  248. necessary.
  249. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  250. Google client library. Use 'scopes' for user-defined scopes.
  251. request (Optional[google.auth.transport.Request]): An object used to make
  252. HTTP requests. This is used to determine the associated project ID
  253. for a workload identity pool resource (external account credentials).
  254. If not specified, then it will use a
  255. google.auth.transport.requests.Request client to make requests.
  256. Returns:
  257. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  258. credentials and the project ID. External account credentials project
  259. IDs may not always be determined.
  260. Raises:
  261. google.auth.exceptions.DefaultCredentialsError: if the info dictionary
  262. is in the wrong format or is missing required information.
  263. """
  264. # There are currently 2 types of external_account credentials.
  265. try:
  266. # Check if configuration corresponds to an AWS credentials.
  267. from google.auth import aws
  268. credentials = aws.Credentials.from_info(
  269. info, scopes=scopes, default_scopes=default_scopes
  270. )
  271. except ValueError:
  272. try:
  273. # Check if configuration corresponds to an Identity Pool credentials.
  274. from google.auth import identity_pool
  275. credentials = identity_pool.Credentials.from_info(
  276. info, scopes=scopes, default_scopes=default_scopes
  277. )
  278. except ValueError:
  279. # If the configuration is invalid or does not correspond to any
  280. # supported external_account credentials, raise an error.
  281. raise exceptions.DefaultCredentialsError(
  282. "Failed to load external account credentials from {}".format(filename)
  283. )
  284. if request is None:
  285. request = google.auth.transport.requests.Request()
  286. return credentials, credentials.get_project_id(request=request)
  287. def default(scopes=None, request=None, quota_project_id=None, default_scopes=None):
  288. """Gets the default credentials for the current environment.
  289. `Application Default Credentials`_ provides an easy way to obtain
  290. credentials to call Google APIs for server-to-server or local applications.
  291. This function acquires credentials from the environment in the following
  292. order:
  293. 1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
  294. to the path of a valid service account JSON private key file, then it is
  295. loaded and returned. The project ID returned is the project ID defined
  296. in the service account file if available (some older files do not
  297. contain project ID information).
  298. If the environment variable is set to the path of a valid external
  299. account JSON configuration file (workload identity federation), then the
  300. configuration file is used to determine and retrieve the external
  301. credentials from the current environment (AWS, Azure, etc).
  302. These will then be exchanged for Google access tokens via the Google STS
  303. endpoint.
  304. The project ID returned in this case is the one corresponding to the
  305. underlying workload identity pool resource if determinable.
  306. 2. If the `Google Cloud SDK`_ is installed and has application default
  307. credentials set they are loaded and returned.
  308. To enable application default credentials with the Cloud SDK run::
  309. gcloud auth application-default login
  310. If the Cloud SDK has an active project, the project ID is returned. The
  311. active project can be set using::
  312. gcloud config set project
  313. 3. If the application is running in the `App Engine standard environment`_
  314. (first generation) then the credentials and project ID from the
  315. `App Identity Service`_ are used.
  316. 4. If the application is running in `Compute Engine`_ or `Cloud Run`_ or
  317. the `App Engine flexible environment`_ or the `App Engine standard
  318. environment`_ (second generation) then the credentials and project ID
  319. are obtained from the `Metadata Service`_.
  320. 5. If no credentials are found,
  321. :class:`~google.auth.exceptions.DefaultCredentialsError` will be raised.
  322. .. _Application Default Credentials: https://developers.google.com\
  323. /identity/protocols/application-default-credentials
  324. .. _Google Cloud SDK: https://cloud.google.com/sdk
  325. .. _App Engine standard environment: https://cloud.google.com/appengine
  326. .. _App Identity Service: https://cloud.google.com/appengine/docs/python\
  327. /appidentity/
  328. .. _Compute Engine: https://cloud.google.com/compute
  329. .. _App Engine flexible environment: https://cloud.google.com\
  330. /appengine/flexible
  331. .. _Metadata Service: https://cloud.google.com/compute/docs\
  332. /storing-retrieving-metadata
  333. .. _Cloud Run: https://cloud.google.com/run
  334. Example::
  335. import google.auth
  336. credentials, project_id = google.auth.default()
  337. Args:
  338. scopes (Sequence[str]): The list of scopes for the credentials. If
  339. specified, the credentials will automatically be scoped if
  340. necessary.
  341. request (Optional[google.auth.transport.Request]): An object used to make
  342. HTTP requests. This is used to either detect whether the application
  343. is running on Compute Engine or to determine the associated project
  344. ID for a workload identity pool resource (external account
  345. credentials). If not specified, then it will either use the standard
  346. library http client to make requests for Compute Engine credentials
  347. or a google.auth.transport.requests.Request client for external
  348. account credentials.
  349. quota_project_id (Optional[str]): The project ID used for
  350. quota and billing.
  351. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  352. Google client library. Use 'scopes' for user-defined scopes.
  353. Returns:
  354. Tuple[~google.auth.credentials.Credentials, Optional[str]]:
  355. the current environment's credentials and project ID. Project ID
  356. may be None, which indicates that the Project ID could not be
  357. ascertained from the environment.
  358. Raises:
  359. ~google.auth.exceptions.DefaultCredentialsError:
  360. If no credentials were found, or if the credentials found were
  361. invalid.
  362. """
  363. from google.auth.credentials import with_scopes_if_required
  364. explicit_project_id = os.environ.get(
  365. environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)
  366. )
  367. checkers = (
  368. # Avoid passing scopes here to prevent passing scopes to user credentials.
  369. # with_scopes_if_required() below will ensure scopes/default scopes are
  370. # safely set on the returned credentials since requires_scopes will
  371. # guard against setting scopes on user credentials.
  372. _get_explicit_environ_credentials,
  373. _get_gcloud_sdk_credentials,
  374. _get_gae_credentials,
  375. lambda: _get_gce_credentials(request),
  376. )
  377. for checker in checkers:
  378. credentials, project_id = checker()
  379. if credentials is not None:
  380. credentials = with_scopes_if_required(
  381. credentials, scopes, default_scopes=default_scopes
  382. )
  383. # For external account credentials, scopes are required to determine
  384. # the project ID. Try to get the project ID again if not yet
  385. # determined.
  386. if not project_id and callable(
  387. getattr(credentials, "get_project_id", None)
  388. ):
  389. if request is None:
  390. request = google.auth.transport.requests.Request()
  391. project_id = credentials.get_project_id(request=request)
  392. if quota_project_id:
  393. credentials = credentials.with_quota_project(quota_project_id)
  394. effective_project_id = explicit_project_id or project_id
  395. if not effective_project_id:
  396. _LOGGER.warning(
  397. "No project ID could be determined. Consider running "
  398. "`gcloud config set project` or setting the %s "
  399. "environment variable",
  400. environment_vars.PROJECT,
  401. )
  402. return credentials, effective_project_id
  403. raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)