identity_pool.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # Copyright 2020 Google LLC
  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. """Identity Pool Credentials.
  15. This module provides credentials to access Google Cloud resources from on-prem
  16. or non-Google Cloud platforms which support external credentials (e.g. OIDC ID
  17. tokens) retrieved from local file locations or local servers. This includes
  18. Microsoft Azure and OIDC identity providers (e.g. K8s workloads registered with
  19. Hub with Hub workload identity enabled).
  20. These credentials are recommended over the use of service account credentials
  21. in on-prem/non-Google Cloud platforms as they do not involve the management of
  22. long-live service account private keys.
  23. Identity Pool Credentials are initialized using external_account
  24. arguments which are typically loaded from an external credentials file or
  25. an external credentials URL.
  26. This module also provides a definition for an abstract subject token supplier.
  27. This supplier can be implemented to return a valid OIDC or SAML2.0 subject token
  28. and used to create Identity Pool credentials. The credentials will then call the
  29. supplier instead of using pre-defined methods such as reading a local file or
  30. calling a URL.
  31. """
  32. try:
  33. from collections.abc import Mapping
  34. # Python 2.7 compatibility
  35. except ImportError: # pragma: NO COVER
  36. from collections import Mapping # type: ignore
  37. import abc
  38. import json
  39. import os
  40. from typing import NamedTuple
  41. from google.auth import _helpers
  42. from google.auth import exceptions
  43. from google.auth import external_account
  44. from google.auth.transport import _mtls_helper
  45. class SubjectTokenSupplier(metaclass=abc.ABCMeta):
  46. """Base class for subject token suppliers. This can be implemented with custom logic to retrieve
  47. a subject token to exchange for a Google Cloud access token when using Workload or
  48. Workforce Identity Federation. The identity pool credential does not cache the subject token,
  49. so caching logic should be added in the implementation.
  50. """
  51. @abc.abstractmethod
  52. def get_subject_token(self, context, request):
  53. """Returns the requested subject token. The subject token must be valid.
  54. .. warning: This is not cached by the calling Google credential, so caching logic should be implemented in the supplier.
  55. Args:
  56. context (google.auth.externalaccount.SupplierContext): The context object
  57. containing information about the requested audience and subject token type.
  58. request (google.auth.transport.Request): The object used to make
  59. HTTP requests.
  60. Raises:
  61. google.auth.exceptions.RefreshError: If an error is encountered during
  62. subject token retrieval logic.
  63. Returns:
  64. str: The requested subject token string.
  65. """
  66. raise NotImplementedError("")
  67. class _TokenContent(NamedTuple):
  68. """Models the token content response from file and url internal suppliers.
  69. Attributes:
  70. content (str): The string content of the file or URL response.
  71. location (str): The location the content was retrieved from. This will either be a file location or a URL.
  72. """
  73. content: str
  74. location: str
  75. class _FileSupplier(SubjectTokenSupplier):
  76. """ Internal implementation of subject token supplier which supports reading a subject token from a file."""
  77. def __init__(self, path, format_type, subject_token_field_name):
  78. self._path = path
  79. self._format_type = format_type
  80. self._subject_token_field_name = subject_token_field_name
  81. @_helpers.copy_docstring(SubjectTokenSupplier)
  82. def get_subject_token(self, context, request):
  83. if not os.path.exists(self._path):
  84. raise exceptions.RefreshError("File '{}' was not found.".format(self._path))
  85. with open(self._path, "r", encoding="utf-8") as file_obj:
  86. token_content = _TokenContent(file_obj.read(), self._path)
  87. return _parse_token_data(
  88. token_content, self._format_type, self._subject_token_field_name
  89. )
  90. class _UrlSupplier(SubjectTokenSupplier):
  91. """ Internal implementation of subject token supplier which supports retrieving a subject token by calling a URL endpoint."""
  92. def __init__(self, url, format_type, subject_token_field_name, headers):
  93. self._url = url
  94. self._format_type = format_type
  95. self._subject_token_field_name = subject_token_field_name
  96. self._headers = headers
  97. @_helpers.copy_docstring(SubjectTokenSupplier)
  98. def get_subject_token(self, context, request):
  99. response = request(url=self._url, method="GET", headers=self._headers)
  100. # support both string and bytes type response.data
  101. response_body = (
  102. response.data.decode("utf-8")
  103. if hasattr(response.data, "decode")
  104. else response.data
  105. )
  106. if response.status != 200:
  107. raise exceptions.RefreshError(
  108. "Unable to retrieve Identity Pool subject token", response_body
  109. )
  110. token_content = _TokenContent(response_body, self._url)
  111. return _parse_token_data(
  112. token_content, self._format_type, self._subject_token_field_name
  113. )
  114. class _X509Supplier(SubjectTokenSupplier):
  115. """Internal supplier for X509 workload credentials. This class is used internally and always returns an empty string as the subject token."""
  116. @_helpers.copy_docstring(SubjectTokenSupplier)
  117. def get_subject_token(self, context, request):
  118. return ""
  119. def _parse_token_data(token_content, format_type="text", subject_token_field_name=None):
  120. if format_type == "text":
  121. token = token_content.content
  122. else:
  123. try:
  124. # Parse file content as JSON.
  125. response_data = json.loads(token_content.content)
  126. # Get the subject_token.
  127. token = response_data[subject_token_field_name]
  128. except (KeyError, ValueError):
  129. raise exceptions.RefreshError(
  130. "Unable to parse subject_token from JSON file '{}' using key '{}'".format(
  131. token_content.location, subject_token_field_name
  132. )
  133. )
  134. if not token:
  135. raise exceptions.RefreshError(
  136. "Missing subject_token in the credential_source file"
  137. )
  138. return token
  139. class Credentials(external_account.Credentials):
  140. """External account credentials sourced from files and URLs."""
  141. def __init__(
  142. self,
  143. audience,
  144. subject_token_type,
  145. token_url=external_account._DEFAULT_TOKEN_URL,
  146. credential_source=None,
  147. subject_token_supplier=None,
  148. *args,
  149. **kwargs
  150. ):
  151. """Instantiates an external account credentials object from a file/URL.
  152. Args:
  153. audience (str): The STS audience field.
  154. subject_token_type (str): The subject token type based on the Oauth2.0 token exchange spec.
  155. Expected values include::
  156. “urn:ietf:params:oauth:token-type:jwt”
  157. “urn:ietf:params:oauth:token-type:id-token”
  158. “urn:ietf:params:oauth:token-type:saml2”
  159. token_url (Optional [str]): The STS endpoint URL. If not provided, will default to "https://sts.googleapis.com/v1/token".
  160. credential_source (Optional [Mapping]): The credential source dictionary used to
  161. provide instructions on how to retrieve external credential to be
  162. exchanged for Google access tokens. Either a credential source or
  163. a subject token supplier must be provided.
  164. Example credential_source for url-sourced credential::
  165. {
  166. "url": "http://www.example.com",
  167. "format": {
  168. "type": "json",
  169. "subject_token_field_name": "access_token",
  170. },
  171. "headers": {"foo": "bar"},
  172. }
  173. Example credential_source for file-sourced credential::
  174. {
  175. "file": "/path/to/token/file.txt"
  176. }
  177. subject_token_supplier (Optional [SubjectTokenSupplier]): Optional subject token supplier.
  178. This will be called to supply a valid subject token which will then
  179. be exchanged for Google access tokens. Either a subject token supplier
  180. or a credential source must be provided.
  181. args (List): Optional positional arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  182. kwargs (Mapping): Optional keyword arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  183. Raises:
  184. google.auth.exceptions.RefreshError: If an error is encountered during
  185. access token retrieval logic.
  186. ValueError: For invalid parameters.
  187. .. note:: Typically one of the helper constructors
  188. :meth:`from_file` or
  189. :meth:`from_info` are used instead of calling the constructor directly.
  190. """
  191. super(Credentials, self).__init__(
  192. audience=audience,
  193. subject_token_type=subject_token_type,
  194. token_url=token_url,
  195. credential_source=credential_source,
  196. *args,
  197. **kwargs
  198. )
  199. if credential_source is None and subject_token_supplier is None:
  200. raise exceptions.InvalidValue(
  201. "A valid credential source or a subject token supplier must be provided."
  202. )
  203. if credential_source is not None and subject_token_supplier is not None:
  204. raise exceptions.InvalidValue(
  205. "Identity pool credential cannot have both a credential source and a subject token supplier."
  206. )
  207. if subject_token_supplier is not None:
  208. self._subject_token_supplier = subject_token_supplier
  209. self._credential_source_file = None
  210. self._credential_source_url = None
  211. self._credential_source_certificate = None
  212. else:
  213. if not isinstance(credential_source, Mapping):
  214. self._credential_source_executable = None
  215. raise exceptions.MalformedError(
  216. "Invalid credential_source. The credential_source is not a dict."
  217. )
  218. self._credential_source_file = credential_source.get("file")
  219. self._credential_source_url = credential_source.get("url")
  220. self._credential_source_certificate = credential_source.get("certificate")
  221. # environment_id is only supported in AWS or dedicated future external
  222. # account credentials.
  223. if "environment_id" in credential_source:
  224. raise exceptions.MalformedError(
  225. "Invalid Identity Pool credential_source field 'environment_id'"
  226. )
  227. # check that only one of file, url, or certificate are provided.
  228. self._validate_single_source()
  229. if self._credential_source_certificate:
  230. self._validate_certificate_config()
  231. else:
  232. self._validate_file_or_url_config(credential_source)
  233. if self._credential_source_file:
  234. self._subject_token_supplier = _FileSupplier(
  235. self._credential_source_file,
  236. self._credential_source_format_type,
  237. self._credential_source_field_name,
  238. )
  239. elif self._credential_source_url:
  240. self._subject_token_supplier = _UrlSupplier(
  241. self._credential_source_url,
  242. self._credential_source_format_type,
  243. self._credential_source_field_name,
  244. self._credential_source_headers,
  245. )
  246. else: # self._credential_source_certificate
  247. self._subject_token_supplier = _X509Supplier()
  248. @_helpers.copy_docstring(external_account.Credentials)
  249. def retrieve_subject_token(self, request):
  250. return self._subject_token_supplier.get_subject_token(
  251. self._supplier_context, request
  252. )
  253. def _get_mtls_cert_and_key_paths(self):
  254. if self._credential_source_certificate is None:
  255. raise exceptions.RefreshError(
  256. 'The credential is not configured to use mtls requests. The credential should include a "certificate" section in the credential source.'
  257. )
  258. else:
  259. return _mtls_helper._get_workload_cert_and_key_paths(
  260. self._certificate_config_location
  261. )
  262. def _mtls_required(self):
  263. return self._credential_source_certificate is not None
  264. def _create_default_metrics_options(self):
  265. metrics_options = super(Credentials, self)._create_default_metrics_options()
  266. # Check that credential source is a dict before checking for credential type. This check needs to be done
  267. # here because the external_account credential constructor needs to pass the metrics options to the
  268. # impersonated credential object before the identity_pool credentials are validated.
  269. if isinstance(self._credential_source, Mapping):
  270. if self._credential_source.get("file"):
  271. metrics_options["source"] = "file"
  272. elif self._credential_source.get("url"):
  273. metrics_options["source"] = "url"
  274. else:
  275. metrics_options["source"] = "x509"
  276. else:
  277. metrics_options["source"] = "programmatic"
  278. return metrics_options
  279. def _has_custom_supplier(self):
  280. return self._credential_source is None
  281. def _constructor_args(self):
  282. args = super(Credentials, self)._constructor_args()
  283. # If a custom supplier was used, append it to the args dict.
  284. if self._has_custom_supplier():
  285. args.update({"subject_token_supplier": self._subject_token_supplier})
  286. return args
  287. def _validate_certificate_config(self):
  288. self._certificate_config_location = self._credential_source_certificate.get(
  289. "certificate_config_location"
  290. )
  291. use_default = self._credential_source_certificate.get(
  292. "use_default_certificate_config"
  293. )
  294. if self._certificate_config_location and use_default:
  295. raise exceptions.MalformedError(
  296. "Invalid certificate configuration, certificate_config_location cannot be specified when use_default_certificate_config = true."
  297. )
  298. if not self._certificate_config_location and not use_default:
  299. raise exceptions.MalformedError(
  300. "Invalid certificate configuration, use_default_certificate_config should be true if no certificate_config_location is provided."
  301. )
  302. def _validate_file_or_url_config(self, credential_source):
  303. self._credential_source_headers = credential_source.get("headers")
  304. credential_source_format = credential_source.get("format", {})
  305. # Get credential_source format type. When not provided, this
  306. # defaults to text.
  307. self._credential_source_format_type = (
  308. credential_source_format.get("type") or "text"
  309. )
  310. if self._credential_source_format_type not in ["text", "json"]:
  311. raise exceptions.MalformedError(
  312. "Invalid credential_source format '{}'".format(
  313. self._credential_source_format_type
  314. )
  315. )
  316. # For JSON types, get the required subject_token field name.
  317. if self._credential_source_format_type == "json":
  318. self._credential_source_field_name = credential_source_format.get(
  319. "subject_token_field_name"
  320. )
  321. if self._credential_source_field_name is None:
  322. raise exceptions.MalformedError(
  323. "Missing subject_token_field_name for JSON credential_source format"
  324. )
  325. else:
  326. self._credential_source_field_name = None
  327. def _validate_single_source(self):
  328. credential_sources = [
  329. self._credential_source_file,
  330. self._credential_source_url,
  331. self._credential_source_certificate,
  332. ]
  333. valid_credential_sources = list(
  334. filter(lambda source: source is not None, credential_sources)
  335. )
  336. if len(valid_credential_sources) > 1:
  337. raise exceptions.MalformedError(
  338. "Ambiguous credential_source. 'file', 'url', and 'certificate' are mutually exclusive.."
  339. )
  340. if len(valid_credential_sources) != 1:
  341. raise exceptions.MalformedError(
  342. "Missing credential_source. A 'file', 'url', or 'certificate' must be provided."
  343. )
  344. @classmethod
  345. def from_info(cls, info, **kwargs):
  346. """Creates an Identity Pool Credentials instance from parsed external account info.
  347. Args:
  348. info (Mapping[str, str]): The Identity Pool external account info in Google
  349. format.
  350. kwargs: Additional arguments to pass to the constructor.
  351. Returns:
  352. google.auth.identity_pool.Credentials: The constructed
  353. credentials.
  354. Raises:
  355. ValueError: For invalid parameters.
  356. """
  357. subject_token_supplier = info.get("subject_token_supplier")
  358. kwargs.update({"subject_token_supplier": subject_token_supplier})
  359. return super(Credentials, cls).from_info(info, **kwargs)
  360. @classmethod
  361. def from_file(cls, filename, **kwargs):
  362. """Creates an IdentityPool Credentials instance from an external account json file.
  363. Args:
  364. filename (str): The path to the IdentityPool external account json file.
  365. kwargs: Additional arguments to pass to the constructor.
  366. Returns:
  367. google.auth.identity_pool.Credentials: The constructed
  368. credentials.
  369. """
  370. return super(Credentials, cls).from_file(filename, **kwargs)