identity_pool.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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
  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. class SubjectTokenSupplier(metaclass=abc.ABCMeta):
  45. """Base class for subject token suppliers. This can be implemented with custom logic to retrieve
  46. a subject token to exchange for a Google Cloud access token when using Workload or
  47. Workforce Identity Federation. The identity pool credential does not cache the subject token,
  48. so caching logic should be added in the implementation.
  49. """
  50. @abc.abstractmethod
  51. def get_subject_token(self, context, request):
  52. """Returns the requested subject token. The subject token must be valid.
  53. .. warning: This is not cached by the calling Google credential, so caching logic should be implemented in the supplier.
  54. Args:
  55. context (google.auth.externalaccount.SupplierContext): The context object
  56. containing information about the requested audience and subject token type.
  57. request (google.auth.transport.Request): The object used to make
  58. HTTP requests.
  59. Raises:
  60. google.auth.exceptions.RefreshError: If an error is encountered during
  61. subject token retrieval logic.
  62. Returns:
  63. str: The requested subject token string.
  64. """
  65. raise NotImplementedError("")
  66. class _TokenContent(NamedTuple):
  67. """Models the token content response from file and url internal suppliers.
  68. Attributes:
  69. content (str): The string content of the file or URL response.
  70. location (str): The location the content was retrieved from. This will either be a file location or a URL.
  71. """
  72. content: str
  73. location: str
  74. class _FileSupplier(SubjectTokenSupplier):
  75. """ Internal implementation of subject token supplier which supports reading a subject token from a file."""
  76. def __init__(self, path, format_type, subject_token_field_name):
  77. self._path = path
  78. self._format_type = format_type
  79. self._subject_token_field_name = subject_token_field_name
  80. @_helpers.copy_docstring(SubjectTokenSupplier)
  81. def get_subject_token(self, context, request):
  82. if not os.path.exists(self._path):
  83. raise exceptions.RefreshError("File '{}' was not found.".format(self._path))
  84. with open(self._path, "r", encoding="utf-8") as file_obj:
  85. token_content = _TokenContent(file_obj.read(), self._path)
  86. return _parse_token_data(
  87. token_content, self._format_type, self._subject_token_field_name
  88. )
  89. class _UrlSupplier(SubjectTokenSupplier):
  90. """ Internal implementation of subject token supplier which supports retrieving a subject token by calling a URL endpoint."""
  91. def __init__(self, url, format_type, subject_token_field_name, headers):
  92. self._url = url
  93. self._format_type = format_type
  94. self._subject_token_field_name = subject_token_field_name
  95. self._headers = headers
  96. @_helpers.copy_docstring(SubjectTokenSupplier)
  97. def get_subject_token(self, context, request):
  98. response = request(url=self._url, method="GET", headers=self._headers)
  99. # support both string and bytes type response.data
  100. response_body = (
  101. response.data.decode("utf-8")
  102. if hasattr(response.data, "decode")
  103. else response.data
  104. )
  105. if response.status != 200:
  106. raise exceptions.RefreshError(
  107. "Unable to retrieve Identity Pool subject token", response_body
  108. )
  109. token_content = _TokenContent(response_body, self._url)
  110. return _parse_token_data(
  111. token_content, self._format_type, self._subject_token_field_name
  112. )
  113. def _parse_token_data(token_content, format_type="text", subject_token_field_name=None):
  114. if format_type == "text":
  115. token = token_content.content
  116. else:
  117. try:
  118. # Parse file content as JSON.
  119. response_data = json.loads(token_content.content)
  120. # Get the subject_token.
  121. token = response_data[subject_token_field_name]
  122. except (KeyError, ValueError):
  123. raise exceptions.RefreshError(
  124. "Unable to parse subject_token from JSON file '{}' using key '{}'".format(
  125. token_content.location, subject_token_field_name
  126. )
  127. )
  128. if not token:
  129. raise exceptions.RefreshError(
  130. "Missing subject_token in the credential_source file"
  131. )
  132. return token
  133. class Credentials(external_account.Credentials):
  134. """External account credentials sourced from files and URLs."""
  135. def __init__(
  136. self,
  137. audience,
  138. subject_token_type,
  139. token_url=external_account._DEFAULT_TOKEN_URL,
  140. credential_source=None,
  141. subject_token_supplier=None,
  142. *args,
  143. **kwargs
  144. ):
  145. """Instantiates an external account credentials object from a file/URL.
  146. Args:
  147. audience (str): The STS audience field.
  148. subject_token_type (str): The subject token type based on the Oauth2.0 token exchange spec.
  149. Expected values include::
  150. “urn:ietf:params:oauth:token-type:jwt”
  151. “urn:ietf:params:oauth:token-type:id-token”
  152. “urn:ietf:params:oauth:token-type:saml2”
  153. token_url (Optional [str]): The STS endpoint URL. If not provided, will default to "https://sts.googleapis.com/v1/token".
  154. credential_source (Optional [Mapping]): The credential source dictionary used to
  155. provide instructions on how to retrieve external credential to be
  156. exchanged for Google access tokens. Either a credential source or
  157. a subject token supplier must be provided.
  158. Example credential_source for url-sourced credential::
  159. {
  160. "url": "http://www.example.com",
  161. "format": {
  162. "type": "json",
  163. "subject_token_field_name": "access_token",
  164. },
  165. "headers": {"foo": "bar"},
  166. }
  167. Example credential_source for file-sourced credential::
  168. {
  169. "file": "/path/to/token/file.txt"
  170. }
  171. subject_token_supplier (Optional [SubjectTokenSupplier]): Optional subject token supplier.
  172. This will be called to supply a valid subject token which will then
  173. be exchanged for Google access tokens. Either a subject token supplier
  174. or a credential source must be provided.
  175. args (List): Optional positional arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  176. kwargs (Mapping): Optional keyword arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  177. Raises:
  178. google.auth.exceptions.RefreshError: If an error is encountered during
  179. access token retrieval logic.
  180. ValueError: For invalid parameters.
  181. .. note:: Typically one of the helper constructors
  182. :meth:`from_file` or
  183. :meth:`from_info` are used instead of calling the constructor directly.
  184. """
  185. super(Credentials, self).__init__(
  186. audience=audience,
  187. subject_token_type=subject_token_type,
  188. token_url=token_url,
  189. credential_source=credential_source,
  190. *args,
  191. **kwargs
  192. )
  193. if credential_source is None and subject_token_supplier is None:
  194. raise exceptions.InvalidValue(
  195. "A valid credential source or a subject token supplier must be provided."
  196. )
  197. if credential_source is not None and subject_token_supplier is not None:
  198. raise exceptions.InvalidValue(
  199. "Identity pool credential cannot have both a credential source and a subject token supplier."
  200. )
  201. if subject_token_supplier is not None:
  202. self._subject_token_supplier = subject_token_supplier
  203. self._credential_source_file = None
  204. self._credential_source_url = None
  205. else:
  206. if not isinstance(credential_source, Mapping):
  207. self._credential_source_executable = None
  208. raise exceptions.MalformedError(
  209. "Invalid credential_source. The credential_source is not a dict."
  210. )
  211. self._credential_source_file = credential_source.get("file")
  212. self._credential_source_url = credential_source.get("url")
  213. self._credential_source_headers = credential_source.get("headers")
  214. credential_source_format = credential_source.get("format", {})
  215. # Get credential_source format type. When not provided, this
  216. # defaults to text.
  217. self._credential_source_format_type = (
  218. credential_source_format.get("type") or "text"
  219. )
  220. # environment_id is only supported in AWS or dedicated future external
  221. # account credentials.
  222. if "environment_id" in credential_source:
  223. raise exceptions.MalformedError(
  224. "Invalid Identity Pool credential_source field 'environment_id'"
  225. )
  226. if self._credential_source_format_type not in ["text", "json"]:
  227. raise exceptions.MalformedError(
  228. "Invalid credential_source format '{}'".format(
  229. self._credential_source_format_type
  230. )
  231. )
  232. # For JSON types, get the required subject_token field name.
  233. if self._credential_source_format_type == "json":
  234. self._credential_source_field_name = credential_source_format.get(
  235. "subject_token_field_name"
  236. )
  237. if self._credential_source_field_name is None:
  238. raise exceptions.MalformedError(
  239. "Missing subject_token_field_name for JSON credential_source format"
  240. )
  241. else:
  242. self._credential_source_field_name = None
  243. if self._credential_source_file and self._credential_source_url:
  244. raise exceptions.MalformedError(
  245. "Ambiguous credential_source. 'file' is mutually exclusive with 'url'."
  246. )
  247. if not self._credential_source_file and not self._credential_source_url:
  248. raise exceptions.MalformedError(
  249. "Missing credential_source. A 'file' or 'url' must be provided."
  250. )
  251. if self._credential_source_file:
  252. self._subject_token_supplier = _FileSupplier(
  253. self._credential_source_file,
  254. self._credential_source_format_type,
  255. self._credential_source_field_name,
  256. )
  257. else:
  258. self._subject_token_supplier = _UrlSupplier(
  259. self._credential_source_url,
  260. self._credential_source_format_type,
  261. self._credential_source_field_name,
  262. self._credential_source_headers,
  263. )
  264. @_helpers.copy_docstring(external_account.Credentials)
  265. def retrieve_subject_token(self, request):
  266. return self._subject_token_supplier.get_subject_token(
  267. self._supplier_context, request
  268. )
  269. def _create_default_metrics_options(self):
  270. metrics_options = super(Credentials, self)._create_default_metrics_options()
  271. # Check that credential source is a dict before checking for file vs url. This check needs to be done
  272. # here because the external_account credential constructor needs to pass the metrics options to the
  273. # impersonated credential object before the identity_pool credentials are validated.
  274. if isinstance(self._credential_source, Mapping):
  275. if self._credential_source.get("file"):
  276. metrics_options["source"] = "file"
  277. else:
  278. metrics_options["source"] = "url"
  279. else:
  280. metrics_options["source"] = "programmatic"
  281. return metrics_options
  282. def _has_custom_supplier(self):
  283. return self._credential_source is None
  284. def _constructor_args(self):
  285. args = super(Credentials, self)._constructor_args()
  286. # If a custom supplier was used, append it to the args dict.
  287. if self._has_custom_supplier():
  288. args.update({"subject_token_supplier": self._subject_token_supplier})
  289. return args
  290. @classmethod
  291. def from_info(cls, info, **kwargs):
  292. """Creates an Identity Pool Credentials instance from parsed external account info.
  293. Args:
  294. info (Mapping[str, str]): The Identity Pool external account info in Google
  295. format.
  296. kwargs: Additional arguments to pass to the constructor.
  297. Returns:
  298. google.auth.identity_pool.Credentials: The constructed
  299. credentials.
  300. Raises:
  301. ValueError: For invalid parameters.
  302. """
  303. subject_token_supplier = info.get("subject_token_supplier")
  304. kwargs.update({"subject_token_supplier": subject_token_supplier})
  305. return super(Credentials, cls).from_info(info, **kwargs)
  306. @classmethod
  307. def from_file(cls, filename, **kwargs):
  308. """Creates an IdentityPool Credentials instance from an external account json file.
  309. Args:
  310. filename (str): The path to the IdentityPool external account json file.
  311. kwargs: Additional arguments to pass to the constructor.
  312. Returns:
  313. google.auth.identity_pool.Credentials: The constructed
  314. credentials.
  315. """
  316. return super(Credentials, cls).from_file(filename, **kwargs)