pluggable.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. # Copyright 2022 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. """Pluggable Credentials.
  15. Pluggable Credentials are initialized using external_account arguments which
  16. are typically loaded from third-party executables. Unlike other
  17. credentials that can be initialized with a list of explicit arguments, secrets
  18. or credentials, external account clients use the environment and hints/guidelines
  19. provided by the external_account JSON file to retrieve credentials and exchange
  20. them for Google access tokens.
  21. Example credential_source for pluggable credential:
  22. {
  23. "executable": {
  24. "command": "/path/to/get/credentials.sh --arg1=value1 --arg2=value2",
  25. "timeout_millis": 5000,
  26. "output_file": "/path/to/generated/cached/credentials"
  27. }
  28. }
  29. """
  30. try:
  31. from collections.abc import Mapping
  32. # Python 2.7 compatibility
  33. except ImportError: # pragma: NO COVER
  34. from collections import Mapping
  35. import json
  36. import os
  37. import subprocess
  38. import sys
  39. import time
  40. from google.auth import _helpers
  41. from google.auth import exceptions
  42. from google.auth import external_account
  43. # The max supported executable spec version.
  44. EXECUTABLE_SUPPORTED_MAX_VERSION = 1
  45. EXECUTABLE_TIMEOUT_MILLIS_DEFAULT = 30 * 1000 # 30 seconds
  46. EXECUTABLE_TIMEOUT_MILLIS_LOWER_BOUND = 5 * 1000 # 5 seconds
  47. EXECUTABLE_TIMEOUT_MILLIS_UPPER_BOUND = 120 * 1000 # 2 minutes
  48. EXECUTABLE_INTERACTIVE_TIMEOUT_MILLIS_LOWER_BOUND = 30 * 1000 # 30 seconds
  49. EXECUTABLE_INTERACTIVE_TIMEOUT_MILLIS_UPPER_BOUND = 30 * 60 * 1000 # 30 minutes
  50. class Credentials(external_account.Credentials):
  51. """External account credentials sourced from executables."""
  52. def __init__(
  53. self,
  54. audience,
  55. subject_token_type,
  56. token_url,
  57. credential_source,
  58. *args,
  59. **kwargs
  60. ):
  61. """Instantiates an external account credentials object from a executables.
  62. Args:
  63. audience (str): The STS audience field.
  64. subject_token_type (str): The subject token type.
  65. token_url (str): The STS endpoint URL.
  66. credential_source (Mapping): The credential source dictionary used to
  67. provide instructions on how to retrieve external credential to be
  68. exchanged for Google access tokens.
  69. Example credential_source for pluggable credential:
  70. {
  71. "executable": {
  72. "command": "/path/to/get/credentials.sh --arg1=value1 --arg2=value2",
  73. "timeout_millis": 5000,
  74. "output_file": "/path/to/generated/cached/credentials"
  75. }
  76. }
  77. args (List): Optional positional arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  78. kwargs (Mapping): Optional keyword arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  79. Raises:
  80. google.auth.exceptions.RefreshError: If an error is encountered during
  81. access token retrieval logic.
  82. ValueError: For invalid parameters.
  83. .. note:: Typically one of the helper constructors
  84. :meth:`from_file` or
  85. :meth:`from_info` are used instead of calling the constructor directly.
  86. """
  87. self.interactive = kwargs.pop("interactive", False)
  88. super(Credentials, self).__init__(
  89. audience=audience,
  90. subject_token_type=subject_token_type,
  91. token_url=token_url,
  92. credential_source=credential_source,
  93. *args,
  94. **kwargs
  95. )
  96. if not isinstance(credential_source, Mapping):
  97. self._credential_source_executable = None
  98. raise ValueError(
  99. "Missing credential_source. The credential_source is not a dict."
  100. )
  101. self._credential_source_executable = credential_source.get("executable")
  102. if not self._credential_source_executable:
  103. raise ValueError(
  104. "Missing credential_source. An 'executable' must be provided."
  105. )
  106. self._credential_source_executable_command = self._credential_source_executable.get(
  107. "command"
  108. )
  109. self._credential_source_executable_timeout_millis = self._credential_source_executable.get(
  110. "timeout_millis"
  111. )
  112. self._credential_source_executable_interactive_timeout_millis = self._credential_source_executable.get(
  113. "interactive_timeout_millis"
  114. )
  115. self._credential_source_executable_output_file = self._credential_source_executable.get(
  116. "output_file"
  117. )
  118. # Dummy value. This variable is only used via injection, not exposed to ctor
  119. self._tokeninfo_username = ""
  120. if not self._credential_source_executable_command:
  121. raise ValueError(
  122. "Missing command field. Executable command must be provided."
  123. )
  124. if not self._credential_source_executable_timeout_millis:
  125. self._credential_source_executable_timeout_millis = (
  126. EXECUTABLE_TIMEOUT_MILLIS_DEFAULT
  127. )
  128. elif (
  129. self._credential_source_executable_timeout_millis
  130. < EXECUTABLE_TIMEOUT_MILLIS_LOWER_BOUND
  131. or self._credential_source_executable_timeout_millis
  132. > EXECUTABLE_TIMEOUT_MILLIS_UPPER_BOUND
  133. ):
  134. raise ValueError("Timeout must be between 5 and 120 seconds.")
  135. if self._credential_source_executable_interactive_timeout_millis:
  136. if (
  137. self._credential_source_executable_interactive_timeout_millis
  138. < EXECUTABLE_INTERACTIVE_TIMEOUT_MILLIS_LOWER_BOUND
  139. or self._credential_source_executable_interactive_timeout_millis
  140. > EXECUTABLE_INTERACTIVE_TIMEOUT_MILLIS_UPPER_BOUND
  141. ):
  142. raise ValueError(
  143. "Interactive timeout must be between 30 seconds and 30 minutes."
  144. )
  145. @_helpers.copy_docstring(external_account.Credentials)
  146. def retrieve_subject_token(self, request):
  147. self._validate_running_mode()
  148. # Check output file.
  149. if self._credential_source_executable_output_file is not None:
  150. try:
  151. with open(
  152. self._credential_source_executable_output_file, encoding="utf-8"
  153. ) as output_file:
  154. response = json.load(output_file)
  155. except Exception:
  156. pass
  157. else:
  158. try:
  159. # If the cached response is expired, _parse_subject_token will raise an error which will be ignored and we will call the executable again.
  160. subject_token = self._parse_subject_token(response)
  161. if (
  162. "expiration_time" not in response
  163. ): # Always treat missing expiration_time as expired and proceed to executable run.
  164. raise exceptions.RefreshError
  165. except ValueError:
  166. raise
  167. except exceptions.RefreshError:
  168. pass
  169. else:
  170. return subject_token
  171. if not _helpers.is_python_3():
  172. raise exceptions.RefreshError(
  173. "Pluggable auth is only supported for python 3.6+"
  174. )
  175. # Inject env vars.
  176. env = os.environ.copy()
  177. self._inject_env_variables(env)
  178. env["GOOGLE_EXTERNAL_ACCOUNT_REVOKE"] = "0"
  179. # Run executable.
  180. exe_timeout = (
  181. self._credential_source_executable_interactive_timeout_millis / 1000
  182. if self.interactive
  183. else self._credential_source_executable_timeout_millis / 1000
  184. )
  185. exe_stdin = sys.stdin if self.interactive else None
  186. exe_stdout = sys.stdout if self.interactive else subprocess.PIPE
  187. exe_stderr = sys.stdout if self.interactive else subprocess.STDOUT
  188. result = subprocess.run(
  189. self._credential_source_executable_command.split(),
  190. timeout=exe_timeout,
  191. stdin=exe_stdin,
  192. stdout=exe_stdout,
  193. stderr=exe_stderr,
  194. env=env,
  195. )
  196. if result.returncode != 0:
  197. raise exceptions.RefreshError(
  198. "Executable exited with non-zero return code {}. Error: {}".format(
  199. result.returncode, result.stdout
  200. )
  201. )
  202. # Handle executable output.
  203. response = json.loads(result.stdout.decode("utf-8")) if result.stdout else None
  204. if not response and self._credential_source_executable_output_file is not None:
  205. response = json.load(
  206. open(self._credential_source_executable_output_file, encoding="utf-8")
  207. )
  208. subject_token = self._parse_subject_token(response)
  209. return subject_token
  210. def revoke(self, request):
  211. """Revokes the subject token using the credential_source object.
  212. Args:
  213. request (google.auth.transport.Request): A callable used to make
  214. HTTP requests.
  215. Raises:
  216. google.auth.exceptions.RefreshError: If the executable revocation
  217. not properly executed.
  218. """
  219. if not self.interactive:
  220. raise ValueError("Revoke is only enabled under interactive mode.")
  221. self._validate_running_mode()
  222. if not _helpers.is_python_3():
  223. raise exceptions.RefreshError(
  224. "Pluggable auth is only supported for python 3.6+"
  225. )
  226. # Inject variables
  227. env = os.environ.copy()
  228. self._inject_env_variables(env)
  229. env["GOOGLE_EXTERNAL_ACCOUNT_REVOKE"] = "1"
  230. # Run executable
  231. result = subprocess.run(
  232. self._credential_source_executable_command.split(),
  233. timeout=self._credential_source_executable_interactive_timeout_millis
  234. / 1000,
  235. stdout=subprocess.PIPE,
  236. stderr=subprocess.STDOUT,
  237. env=env,
  238. )
  239. if result.returncode != 0:
  240. raise exceptions.RefreshError(
  241. "Auth revoke failed on executable. Exit with non-zero return code {}. Error: {}".format(
  242. result.returncode, result.stdout
  243. )
  244. )
  245. response = json.loads(result.stdout.decode("utf-8"))
  246. self._validate_revoke_response(response)
  247. @property
  248. def external_account_id(self):
  249. """Returns the external account identifier.
  250. When service account impersonation is used the identifier is the service
  251. account email.
  252. Without service account impersonation, this returns None, unless it is
  253. being used by the Google Cloud CLI which populates this field.
  254. """
  255. return self.service_account_email or self._tokeninfo_username
  256. @classmethod
  257. def from_info(cls, info, **kwargs):
  258. """Creates a Pluggable Credentials instance from parsed external account info.
  259. Args:
  260. info (Mapping[str, str]): The Pluggable external account info in Google
  261. format.
  262. kwargs: Additional arguments to pass to the constructor.
  263. Returns:
  264. google.auth.pluggable.Credentials: The constructed
  265. credentials.
  266. Raises:
  267. ValueError: For invalid parameters.
  268. """
  269. return super(Credentials, cls).from_info(info, **kwargs)
  270. @classmethod
  271. def from_file(cls, filename, **kwargs):
  272. """Creates an Pluggable Credentials instance from an external account json file.
  273. Args:
  274. filename (str): The path to the Pluggable external account json file.
  275. kwargs: Additional arguments to pass to the constructor.
  276. Returns:
  277. google.auth.pluggable.Credentials: The constructed
  278. credentials.
  279. """
  280. return super(Credentials, cls).from_file(filename, **kwargs)
  281. def _inject_env_variables(self, env):
  282. env["GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE"] = self._audience
  283. env["GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE"] = self._subject_token_type
  284. env["GOOGLE_EXTERNAL_ACCOUNT_ID"] = self.external_account_id
  285. env["GOOGLE_EXTERNAL_ACCOUNT_INTERACTIVE"] = "1" if self.interactive else "0"
  286. if self._service_account_impersonation_url is not None:
  287. env[
  288. "GOOGLE_EXTERNAL_ACCOUNT_IMPERSONATED_EMAIL"
  289. ] = self.service_account_email
  290. if self._credential_source_executable_output_file is not None:
  291. env[
  292. "GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE"
  293. ] = self._credential_source_executable_output_file
  294. def _parse_subject_token(self, response):
  295. self._validate_response_schema(response)
  296. if not response["success"]:
  297. if "code" not in response or "message" not in response:
  298. raise ValueError(
  299. "Error code and message fields are required in the response."
  300. )
  301. raise exceptions.RefreshError(
  302. "Executable returned unsuccessful response: code: {}, message: {}.".format(
  303. response["code"], response["message"]
  304. )
  305. )
  306. if "expiration_time" in response and response["expiration_time"] < time.time():
  307. raise exceptions.RefreshError(
  308. "The token returned by the executable is expired."
  309. )
  310. if "token_type" not in response:
  311. raise ValueError("The executable response is missing the token_type field.")
  312. if (
  313. response["token_type"] == "urn:ietf:params:oauth:token-type:jwt"
  314. or response["token_type"] == "urn:ietf:params:oauth:token-type:id_token"
  315. ): # OIDC
  316. return response["id_token"]
  317. elif response["token_type"] == "urn:ietf:params:oauth:token-type:saml2": # SAML
  318. return response["saml_response"]
  319. else:
  320. raise exceptions.RefreshError("Executable returned unsupported token type.")
  321. def _validate_revoke_response(self, response):
  322. self._validate_response_schema(response)
  323. if not response["success"]:
  324. raise exceptions.RefreshError("Revoke failed with unsuccessful response.")
  325. def _validate_response_schema(self, response):
  326. if "version" not in response:
  327. raise ValueError("The executable response is missing the version field.")
  328. if response["version"] > EXECUTABLE_SUPPORTED_MAX_VERSION:
  329. raise exceptions.RefreshError(
  330. "Executable returned unsupported version {}.".format(
  331. response["version"]
  332. )
  333. )
  334. if "success" not in response:
  335. raise ValueError("The executable response is missing the success field.")
  336. def _validate_running_mode(self):
  337. env_allow_executables = os.environ.get(
  338. "GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"
  339. )
  340. if env_allow_executables != "1":
  341. raise ValueError(
  342. "Executables need to be explicitly allowed (set GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES to '1') to run."
  343. )
  344. if self.interactive and not self._credential_source_executable_output_file:
  345. raise ValueError(
  346. "An output_file must be specified in the credential configuration for interactive mode."
  347. )
  348. if (
  349. self.interactive
  350. and not self._credential_source_executable_interactive_timeout_millis
  351. ):
  352. raise ValueError(
  353. "Interactive mode cannot run without an interactive timeout."
  354. )
  355. if self.interactive and not self.is_workforce_pool:
  356. raise ValueError("Interactive mode is only enabled for workforce pool.")