_default.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. from google.auth import environment_vars
  23. from google.auth import exceptions
  24. import google.auth.transport._http_client
  25. _LOGGER = logging.getLogger(__name__)
  26. # Valid types accepted for file-based credentials.
  27. _AUTHORIZED_USER_TYPE = "authorized_user"
  28. _SERVICE_ACCOUNT_TYPE = "service_account"
  29. _EXTERNAL_ACCOUNT_TYPE = "external_account"
  30. _EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE = "external_account_authorized_user"
  31. _IMPERSONATED_SERVICE_ACCOUNT_TYPE = "impersonated_service_account"
  32. _GDCH_SERVICE_ACCOUNT_TYPE = "gdch_service_account"
  33. _VALID_TYPES = (
  34. _AUTHORIZED_USER_TYPE,
  35. _SERVICE_ACCOUNT_TYPE,
  36. _EXTERNAL_ACCOUNT_TYPE,
  37. _EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE,
  38. _IMPERSONATED_SERVICE_ACCOUNT_TYPE,
  39. _GDCH_SERVICE_ACCOUNT_TYPE,
  40. )
  41. # Help message when no credentials can be found.
  42. _CLOUD_SDK_MISSING_CREDENTIALS = """\
  43. Your default credentials were not found. To set up Application Default Credentials, \
  44. see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.\
  45. """
  46. # Warning when using Cloud SDK user credentials
  47. _CLOUD_SDK_CREDENTIALS_WARNING = """\
  48. Your application has authenticated using end user credentials from Google \
  49. Cloud SDK without a quota project. You might receive a "quota exceeded" \
  50. or "API not enabled" error. See the following page for troubleshooting: \
  51. https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds. \
  52. """
  53. # The subject token type used for AWS external_account credentials.
  54. _AWS_SUBJECT_TOKEN_TYPE = "urn:ietf:params:aws:token-type:aws4_request"
  55. def _warn_about_problematic_credentials(credentials):
  56. """Determines if the credentials are problematic.
  57. Credentials from the Cloud SDK that are associated with Cloud SDK's project
  58. are problematic because they may not have APIs enabled and have limited
  59. quota. If this is the case, warn about it.
  60. """
  61. from google.auth import _cloud_sdk
  62. if credentials.client_id == _cloud_sdk.CLOUD_SDK_CLIENT_ID:
  63. warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
  64. def load_credentials_from_file(
  65. filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
  66. ):
  67. """Loads Google credentials from a file.
  68. The credentials file must be a service account key, stored authorized
  69. user credentials, external account credentials, or impersonated service
  70. account credentials.
  71. .. warning::
  72. Important: If you accept a credential configuration (credential JSON/File/Stream)
  73. from an external source for authentication to Google Cloud Platform, you must
  74. validate it before providing it to any Google API or client library. Providing an
  75. unvalidated credential configuration to Google APIs or libraries can compromise
  76. the security of your systems and data. For more information, refer to
  77. `Validate credential configurations from external sources`_.
  78. .. _Validate credential configurations from external sources:
  79. https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
  80. Args:
  81. filename (str): The full path to the credentials file.
  82. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  83. specified, the credentials will automatically be scoped if
  84. necessary
  85. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  86. Google client library. Use 'scopes' for user-defined scopes.
  87. quota_project_id (Optional[str]): The project ID used for
  88. quota and billing.
  89. request (Optional[google.auth.transport.Request]): An object used to make
  90. HTTP requests. This is used to determine the associated project ID
  91. for a workload identity pool resource (external account credentials).
  92. If not specified, then it will use a
  93. google.auth.transport.requests.Request client to make requests.
  94. Returns:
  95. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  96. credentials and the project ID. Authorized user credentials do not
  97. have the project ID information. External account credentials project
  98. IDs may not always be determined.
  99. Raises:
  100. google.auth.exceptions.DefaultCredentialsError: if the file is in the
  101. wrong format or is missing.
  102. """
  103. if not os.path.exists(filename):
  104. raise exceptions.DefaultCredentialsError(
  105. "File {} was not found.".format(filename)
  106. )
  107. with io.open(filename, "r") as file_obj:
  108. try:
  109. info = json.load(file_obj)
  110. except ValueError as caught_exc:
  111. new_exc = exceptions.DefaultCredentialsError(
  112. "File {} is not a valid json file.".format(filename), caught_exc
  113. )
  114. raise new_exc from caught_exc
  115. return _load_credentials_from_info(
  116. filename, info, scopes, default_scopes, quota_project_id, request
  117. )
  118. def load_credentials_from_dict(
  119. info, scopes=None, default_scopes=None, quota_project_id=None, request=None
  120. ):
  121. """Loads Google credentials from a dict.
  122. The credentials file must be a service account key, stored authorized
  123. user credentials, external account credentials, or impersonated service
  124. account credentials.
  125. .. warning::
  126. Important: If you accept a credential configuration (credential JSON/File/Stream)
  127. from an external source for authentication to Google Cloud Platform, you must
  128. validate it before providing it to any Google API or client library. Providing an
  129. unvalidated credential configuration to Google APIs or libraries can compromise
  130. the security of your systems and data. For more information, refer to
  131. `Validate credential configurations from external sources`_.
  132. .. _Validate credential configurations from external sources:
  133. https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
  134. Args:
  135. info (Dict[str, Any]): A dict object containing the credentials
  136. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  137. specified, the credentials will automatically be scoped if
  138. necessary
  139. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  140. Google client library. Use 'scopes' for user-defined scopes.
  141. quota_project_id (Optional[str]): The project ID used for
  142. quota and billing.
  143. request (Optional[google.auth.transport.Request]): An object used to make
  144. HTTP requests. This is used to determine the associated project ID
  145. for a workload identity pool resource (external account credentials).
  146. If not specified, then it will use a
  147. google.auth.transport.requests.Request client to make requests.
  148. Returns:
  149. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  150. credentials and the project ID. Authorized user credentials do not
  151. have the project ID information. External account credentials project
  152. IDs may not always be determined.
  153. Raises:
  154. google.auth.exceptions.DefaultCredentialsError: if the file is in the
  155. wrong format or is missing.
  156. """
  157. if not isinstance(info, dict):
  158. raise exceptions.DefaultCredentialsError(
  159. "info object was of type {} but dict type was expected.".format(type(info))
  160. )
  161. return _load_credentials_from_info(
  162. "dict object", info, scopes, default_scopes, quota_project_id, request
  163. )
  164. def _load_credentials_from_info(
  165. filename, info, scopes, default_scopes, quota_project_id, request
  166. ):
  167. from google.auth.credentials import CredentialsWithQuotaProject
  168. credential_type = info.get("type")
  169. if credential_type == _AUTHORIZED_USER_TYPE:
  170. credentials, project_id = _get_authorized_user_credentials(
  171. filename, info, scopes
  172. )
  173. elif credential_type == _SERVICE_ACCOUNT_TYPE:
  174. credentials, project_id = _get_service_account_credentials(
  175. filename, info, scopes, default_scopes
  176. )
  177. elif credential_type == _EXTERNAL_ACCOUNT_TYPE:
  178. credentials, project_id = _get_external_account_credentials(
  179. info,
  180. filename,
  181. scopes=scopes,
  182. default_scopes=default_scopes,
  183. request=request,
  184. )
  185. elif credential_type == _EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE:
  186. credentials, project_id = _get_external_account_authorized_user_credentials(
  187. filename, info, request
  188. )
  189. elif credential_type == _IMPERSONATED_SERVICE_ACCOUNT_TYPE:
  190. credentials, project_id = _get_impersonated_service_account_credentials(
  191. filename, info, scopes
  192. )
  193. elif credential_type == _GDCH_SERVICE_ACCOUNT_TYPE:
  194. credentials, project_id = _get_gdch_service_account_credentials(filename, info)
  195. else:
  196. raise exceptions.DefaultCredentialsError(
  197. "The file {file} does not have a valid type. "
  198. "Type is {type}, expected one of {valid_types}.".format(
  199. file=filename, type=credential_type, valid_types=_VALID_TYPES
  200. )
  201. )
  202. if isinstance(credentials, CredentialsWithQuotaProject):
  203. credentials = _apply_quota_project_id(credentials, quota_project_id)
  204. return credentials, project_id
  205. def _get_gcloud_sdk_credentials(quota_project_id=None):
  206. """Gets the credentials and project ID from the Cloud SDK."""
  207. from google.auth import _cloud_sdk
  208. _LOGGER.debug("Checking Cloud SDK credentials as part of auth process...")
  209. # Check if application default credentials exist.
  210. credentials_filename = _cloud_sdk.get_application_default_credentials_path()
  211. if not os.path.isfile(credentials_filename):
  212. _LOGGER.debug("Cloud SDK credentials not found on disk; not using them")
  213. return None, None
  214. credentials, project_id = load_credentials_from_file(
  215. credentials_filename, quota_project_id=quota_project_id
  216. )
  217. credentials._cred_file_path = credentials_filename
  218. if not project_id:
  219. project_id = _cloud_sdk.get_project_id()
  220. return credentials, project_id
  221. def _get_explicit_environ_credentials(quota_project_id=None):
  222. """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
  223. variable."""
  224. from google.auth import _cloud_sdk
  225. cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()
  226. explicit_file = os.environ.get(environment_vars.CREDENTIALS)
  227. _LOGGER.debug(
  228. "Checking %s for explicit credentials as part of auth process...", explicit_file
  229. )
  230. if explicit_file is not None and explicit_file == cloud_sdk_adc_path:
  231. # Cloud sdk flow calls gcloud to fetch project id, so if the explicit
  232. # file path is cloud sdk credentials path, then we should fall back
  233. # to cloud sdk flow, otherwise project id cannot be obtained.
  234. _LOGGER.debug(
  235. "Explicit credentials path %s is the same as Cloud SDK credentials path, fall back to Cloud SDK credentials flow...",
  236. explicit_file,
  237. )
  238. return _get_gcloud_sdk_credentials(quota_project_id=quota_project_id)
  239. if explicit_file is not None:
  240. credentials, project_id = load_credentials_from_file(
  241. os.environ[environment_vars.CREDENTIALS], quota_project_id=quota_project_id
  242. )
  243. credentials._cred_file_path = f"{explicit_file} file via the GOOGLE_APPLICATION_CREDENTIALS environment variable"
  244. return credentials, project_id
  245. else:
  246. return None, None
  247. def _get_gae_credentials():
  248. """Gets Google App Engine App Identity credentials and project ID."""
  249. # If not GAE gen1, prefer the metadata service even if the GAE APIs are
  250. # available as per https://google.aip.dev/auth/4115.
  251. if os.environ.get(environment_vars.LEGACY_APPENGINE_RUNTIME) != "python27":
  252. return None, None
  253. # While this library is normally bundled with app_engine, there are
  254. # some cases where it's not available, so we tolerate ImportError.
  255. try:
  256. _LOGGER.debug("Checking for App Engine runtime as part of auth process...")
  257. import google.auth.app_engine as app_engine
  258. except ImportError:
  259. _LOGGER.warning("Import of App Engine auth library failed.")
  260. return None, None
  261. try:
  262. credentials = app_engine.Credentials()
  263. project_id = app_engine.get_project_id()
  264. return credentials, project_id
  265. except EnvironmentError:
  266. _LOGGER.debug(
  267. "No App Engine library was found so cannot authentication via App Engine Identity Credentials."
  268. )
  269. return None, None
  270. def _get_gce_credentials(request=None, quota_project_id=None):
  271. """Gets credentials and project ID from the GCE Metadata Service."""
  272. # Ping requires a transport, but we want application default credentials
  273. # to require no arguments. So, we'll use the _http_client transport which
  274. # uses http.client. This is only acceptable because the metadata server
  275. # doesn't do SSL and never requires proxies.
  276. # While this library is normally bundled with compute_engine, there are
  277. # some cases where it's not available, so we tolerate ImportError.
  278. try:
  279. from google.auth import compute_engine
  280. from google.auth.compute_engine import _metadata
  281. except ImportError:
  282. _LOGGER.warning("Import of Compute Engine auth library failed.")
  283. return None, None
  284. if request is None:
  285. request = google.auth.transport._http_client.Request()
  286. if _metadata.is_on_gce(request=request):
  287. # Get the project ID.
  288. try:
  289. project_id = _metadata.get_project_id(request=request)
  290. except exceptions.TransportError:
  291. project_id = None
  292. cred = compute_engine.Credentials()
  293. cred = _apply_quota_project_id(cred, quota_project_id)
  294. return cred, project_id
  295. else:
  296. _LOGGER.warning(
  297. "Authentication failed using Compute Engine authentication due to unavailable metadata server."
  298. )
  299. return None, None
  300. def _get_external_account_credentials(
  301. info, filename, scopes=None, default_scopes=None, request=None
  302. ):
  303. """Loads external account Credentials from the parsed external account info.
  304. The credentials information must correspond to a supported external account
  305. credentials.
  306. Args:
  307. info (Mapping[str, str]): The external account info in Google format.
  308. filename (str): The full path to the credentials file.
  309. scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
  310. specified, the credentials will automatically be scoped if
  311. necessary.
  312. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  313. Google client library. Use 'scopes' for user-defined scopes.
  314. request (Optional[google.auth.transport.Request]): An object used to make
  315. HTTP requests. This is used to determine the associated project ID
  316. for a workload identity pool resource (external account credentials).
  317. If not specified, then it will use a
  318. google.auth.transport.requests.Request client to make requests.
  319. Returns:
  320. Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
  321. credentials and the project ID. External account credentials project
  322. IDs may not always be determined.
  323. Raises:
  324. google.auth.exceptions.DefaultCredentialsError: if the info dictionary
  325. is in the wrong format or is missing required information.
  326. """
  327. # There are currently 3 types of external_account credentials.
  328. if info.get("subject_token_type") == _AWS_SUBJECT_TOKEN_TYPE:
  329. # Check if configuration corresponds to an AWS credentials.
  330. from google.auth import aws
  331. credentials = aws.Credentials.from_info(
  332. info, scopes=scopes, default_scopes=default_scopes
  333. )
  334. elif (
  335. info.get("credential_source") is not None
  336. and info.get("credential_source").get("executable") is not None
  337. ):
  338. from google.auth import pluggable
  339. credentials = pluggable.Credentials.from_info(
  340. info, scopes=scopes, default_scopes=default_scopes
  341. )
  342. else:
  343. try:
  344. # Check if configuration corresponds to an Identity Pool credentials.
  345. from google.auth import identity_pool
  346. credentials = identity_pool.Credentials.from_info(
  347. info, scopes=scopes, default_scopes=default_scopes
  348. )
  349. except ValueError:
  350. # If the configuration is invalid or does not correspond to any
  351. # supported external_account credentials, raise an error.
  352. raise exceptions.DefaultCredentialsError(
  353. "Failed to load external account credentials from {}".format(filename)
  354. )
  355. if request is None:
  356. import google.auth.transport.requests
  357. request = google.auth.transport.requests.Request()
  358. return credentials, credentials.get_project_id(request=request)
  359. def _get_external_account_authorized_user_credentials(
  360. filename, info, scopes=None, default_scopes=None, request=None
  361. ):
  362. try:
  363. from google.auth import external_account_authorized_user
  364. credentials = external_account_authorized_user.Credentials.from_info(info)
  365. except ValueError:
  366. raise exceptions.DefaultCredentialsError(
  367. "Failed to load external account authorized user credentials from {}".format(
  368. filename
  369. )
  370. )
  371. return credentials, None
  372. def _get_authorized_user_credentials(filename, info, scopes=None):
  373. from google.oauth2 import credentials
  374. try:
  375. credentials = credentials.Credentials.from_authorized_user_info(
  376. info, scopes=scopes
  377. )
  378. except ValueError as caught_exc:
  379. msg = "Failed to load authorized user credentials from {}".format(filename)
  380. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  381. raise new_exc from caught_exc
  382. return credentials, None
  383. def _get_service_account_credentials(filename, info, scopes=None, default_scopes=None):
  384. from google.oauth2 import service_account
  385. try:
  386. credentials = service_account.Credentials.from_service_account_info(
  387. info, scopes=scopes, default_scopes=default_scopes
  388. )
  389. except ValueError as caught_exc:
  390. msg = "Failed to load service account credentials from {}".format(filename)
  391. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  392. raise new_exc from caught_exc
  393. return credentials, info.get("project_id")
  394. def _get_impersonated_service_account_credentials(filename, info, scopes):
  395. from google.auth import impersonated_credentials
  396. try:
  397. source_credentials_info = info.get("source_credentials")
  398. source_credentials_type = source_credentials_info.get("type")
  399. if source_credentials_type == _AUTHORIZED_USER_TYPE:
  400. source_credentials, _ = _get_authorized_user_credentials(
  401. filename, source_credentials_info
  402. )
  403. elif source_credentials_type == _SERVICE_ACCOUNT_TYPE:
  404. source_credentials, _ = _get_service_account_credentials(
  405. filename, source_credentials_info
  406. )
  407. elif source_credentials_type == _EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE:
  408. source_credentials, _ = _get_external_account_authorized_user_credentials(
  409. filename, source_credentials_info
  410. )
  411. else:
  412. raise exceptions.InvalidType(
  413. "source credential of type {} is not supported.".format(
  414. source_credentials_type
  415. )
  416. )
  417. impersonation_url = info.get("service_account_impersonation_url")
  418. start_index = impersonation_url.rfind("/")
  419. end_index = impersonation_url.find(":generateAccessToken")
  420. if start_index == -1 or end_index == -1 or start_index > end_index:
  421. raise exceptions.InvalidValue(
  422. "Cannot extract target principal from {}".format(impersonation_url)
  423. )
  424. target_principal = impersonation_url[start_index + 1 : end_index]
  425. delegates = info.get("delegates")
  426. quota_project_id = info.get("quota_project_id")
  427. credentials = impersonated_credentials.Credentials(
  428. source_credentials,
  429. target_principal,
  430. scopes,
  431. delegates,
  432. quota_project_id=quota_project_id,
  433. )
  434. except ValueError as caught_exc:
  435. msg = "Failed to load impersonated service account credentials from {}".format(
  436. filename
  437. )
  438. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  439. raise new_exc from caught_exc
  440. return credentials, None
  441. def _get_gdch_service_account_credentials(filename, info):
  442. from google.oauth2 import gdch_credentials
  443. try:
  444. credentials = gdch_credentials.ServiceAccountCredentials.from_service_account_info(
  445. info
  446. )
  447. except ValueError as caught_exc:
  448. msg = "Failed to load GDCH service account credentials from {}".format(filename)
  449. new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
  450. raise new_exc from caught_exc
  451. return credentials, info.get("project")
  452. def get_api_key_credentials(key):
  453. """Return credentials with the given API key."""
  454. from google.auth import api_key
  455. return api_key.Credentials(key)
  456. def _apply_quota_project_id(credentials, quota_project_id):
  457. if quota_project_id:
  458. credentials = credentials.with_quota_project(quota_project_id)
  459. else:
  460. credentials = credentials.with_quota_project_from_environment()
  461. from google.oauth2 import credentials as authorized_user_credentials
  462. if isinstance(credentials, authorized_user_credentials.Credentials) and (
  463. not credentials.quota_project_id
  464. ):
  465. _warn_about_problematic_credentials(credentials)
  466. return credentials
  467. def default(scopes=None, request=None, quota_project_id=None, default_scopes=None):
  468. """Gets the default credentials for the current environment.
  469. `Application Default Credentials`_ provides an easy way to obtain
  470. credentials to call Google APIs for server-to-server or local applications.
  471. This function acquires credentials from the environment in the following
  472. order:
  473. 1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
  474. to the path of a valid service account JSON private key file, then it is
  475. loaded and returned. The project ID returned is the project ID defined
  476. in the service account file if available (some older files do not
  477. contain project ID information).
  478. If the environment variable is set to the path of a valid external
  479. account JSON configuration file (workload identity federation), then the
  480. configuration file is used to determine and retrieve the external
  481. credentials from the current environment (AWS, Azure, etc).
  482. These will then be exchanged for Google access tokens via the Google STS
  483. endpoint.
  484. The project ID returned in this case is the one corresponding to the
  485. underlying workload identity pool resource if determinable.
  486. If the environment variable is set to the path of a valid GDCH service
  487. account JSON file (`Google Distributed Cloud Hosted`_), then a GDCH
  488. credential will be returned. The project ID returned is the project
  489. specified in the JSON file.
  490. 2. If the `Google Cloud SDK`_ is installed and has application default
  491. credentials set they are loaded and returned.
  492. To enable application default credentials with the Cloud SDK run::
  493. gcloud auth application-default login
  494. If the Cloud SDK has an active project, the project ID is returned. The
  495. active project can be set using::
  496. gcloud config set project
  497. 3. If the application is running in the `App Engine standard environment`_
  498. (first generation) then the credentials and project ID from the
  499. `App Identity Service`_ are used.
  500. 4. If the application is running in `Compute Engine`_ or `Cloud Run`_ or
  501. the `App Engine flexible environment`_ or the `App Engine standard
  502. environment`_ (second generation) then the credentials and project ID
  503. are obtained from the `Metadata Service`_.
  504. 5. If no credentials are found,
  505. :class:`~google.auth.exceptions.DefaultCredentialsError` will be raised.
  506. .. _Application Default Credentials: https://developers.google.com\
  507. /identity/protocols/application-default-credentials
  508. .. _Google Cloud SDK: https://cloud.google.com/sdk
  509. .. _App Engine standard environment: https://cloud.google.com/appengine
  510. .. _App Identity Service: https://cloud.google.com/appengine/docs/python\
  511. /appidentity/
  512. .. _Compute Engine: https://cloud.google.com/compute
  513. .. _App Engine flexible environment: https://cloud.google.com\
  514. /appengine/flexible
  515. .. _Metadata Service: https://cloud.google.com/compute/docs\
  516. /storing-retrieving-metadata
  517. .. _Cloud Run: https://cloud.google.com/run
  518. .. _Google Distributed Cloud Hosted: https://cloud.google.com/blog/topics\
  519. /hybrid-cloud/announcing-google-distributed-cloud-edge-and-hosted
  520. Example::
  521. import google.auth
  522. credentials, project_id = google.auth.default()
  523. Args:
  524. scopes (Sequence[str]): The list of scopes for the credentials. If
  525. specified, the credentials will automatically be scoped if
  526. necessary.
  527. request (Optional[google.auth.transport.Request]): An object used to make
  528. HTTP requests. This is used to either detect whether the application
  529. is running on Compute Engine or to determine the associated project
  530. ID for a workload identity pool resource (external account
  531. credentials). If not specified, then it will either use the standard
  532. library http client to make requests for Compute Engine credentials
  533. or a google.auth.transport.requests.Request client for external
  534. account credentials.
  535. quota_project_id (Optional[str]): The project ID used for
  536. quota and billing.
  537. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  538. Google client library. Use 'scopes' for user-defined scopes.
  539. Returns:
  540. Tuple[~google.auth.credentials.Credentials, Optional[str]]:
  541. the current environment's credentials and project ID. Project ID
  542. may be None, which indicates that the Project ID could not be
  543. ascertained from the environment.
  544. Raises:
  545. ~google.auth.exceptions.DefaultCredentialsError:
  546. If no credentials were found, or if the credentials found were
  547. invalid.
  548. """
  549. from google.auth.credentials import with_scopes_if_required
  550. from google.auth.credentials import CredentialsWithQuotaProject
  551. explicit_project_id = os.environ.get(
  552. environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)
  553. )
  554. checkers = (
  555. # Avoid passing scopes here to prevent passing scopes to user credentials.
  556. # with_scopes_if_required() below will ensure scopes/default scopes are
  557. # safely set on the returned credentials since requires_scopes will
  558. # guard against setting scopes on user credentials.
  559. lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
  560. lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),
  561. _get_gae_credentials,
  562. lambda: _get_gce_credentials(request, quota_project_id=quota_project_id),
  563. )
  564. for checker in checkers:
  565. credentials, project_id = checker()
  566. if credentials is not None:
  567. credentials = with_scopes_if_required(
  568. credentials, scopes, default_scopes=default_scopes
  569. )
  570. effective_project_id = explicit_project_id or project_id
  571. # For external account credentials, scopes are required to determine
  572. # the project ID. Try to get the project ID again if not yet
  573. # determined.
  574. if not effective_project_id and callable(
  575. getattr(credentials, "get_project_id", None)
  576. ):
  577. if request is None:
  578. import google.auth.transport.requests
  579. request = google.auth.transport.requests.Request()
  580. effective_project_id = credentials.get_project_id(request=request)
  581. if quota_project_id and isinstance(
  582. credentials, CredentialsWithQuotaProject
  583. ):
  584. credentials = credentials.with_quota_project(quota_project_id)
  585. if not effective_project_id:
  586. _LOGGER.warning(
  587. "No project ID could be determined. Consider running "
  588. "`gcloud config set project` or setting the %s "
  589. "environment variable",
  590. environment_vars.PROJECT,
  591. )
  592. return credentials, effective_project_id
  593. raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)