pluggable.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 # type: ignore
  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. google.auth.exceptions.InvalidValue: For invalid parameters.
  83. google.auth.exceptions.MalformedError: For invalid parameters.
  84. .. note:: Typically one of the helper constructors
  85. :meth:`from_file` or
  86. :meth:`from_info` are used instead of calling the constructor directly.
  87. """
  88. self.interactive = kwargs.pop("interactive", False)
  89. super(Credentials, self).__init__(
  90. audience=audience,
  91. subject_token_type=subject_token_type,
  92. token_url=token_url,
  93. credential_source=credential_source,
  94. *args,
  95. **kwargs
  96. )
  97. if not isinstance(credential_source, Mapping):
  98. self._credential_source_executable = None
  99. raise exceptions.MalformedError(
  100. "Missing credential_source. The credential_source is not a dict."
  101. )
  102. self._credential_source_executable = credential_source.get("executable")
  103. if not self._credential_source_executable:
  104. raise exceptions.MalformedError(
  105. "Missing credential_source. An 'executable' must be provided."
  106. )
  107. self._credential_source_executable_command = self._credential_source_executable.get(
  108. "command"
  109. )
  110. self._credential_source_executable_timeout_millis = self._credential_source_executable.get(
  111. "timeout_millis"
  112. )
  113. self._credential_source_executable_interactive_timeout_millis = self._credential_source_executable.get(
  114. "interactive_timeout_millis"
  115. )
  116. self._credential_source_executable_output_file = self._credential_source_executable.get(
  117. "output_file"
  118. )
  119. # Dummy value. This variable is only used via injection, not exposed to ctor
  120. self._tokeninfo_username = ""
  121. if not self._credential_source_executable_command:
  122. raise exceptions.MalformedError(
  123. "Missing command field. Executable command must be provided."
  124. )
  125. if not self._credential_source_executable_timeout_millis:
  126. self._credential_source_executable_timeout_millis = (
  127. EXECUTABLE_TIMEOUT_MILLIS_DEFAULT
  128. )
  129. elif (
  130. self._credential_source_executable_timeout_millis
  131. < EXECUTABLE_TIMEOUT_MILLIS_LOWER_BOUND
  132. or self._credential_source_executable_timeout_millis
  133. > EXECUTABLE_TIMEOUT_MILLIS_UPPER_BOUND
  134. ):
  135. raise exceptions.InvalidValue("Timeout must be between 5 and 120 seconds.")
  136. if self._credential_source_executable_interactive_timeout_millis:
  137. if (
  138. self._credential_source_executable_interactive_timeout_millis
  139. < EXECUTABLE_INTERACTIVE_TIMEOUT_MILLIS_LOWER_BOUND
  140. or self._credential_source_executable_interactive_timeout_millis
  141. > EXECUTABLE_INTERACTIVE_TIMEOUT_MILLIS_UPPER_BOUND
  142. ):
  143. raise exceptions.InvalidValue(
  144. "Interactive timeout must be between 30 seconds and 30 minutes."
  145. )
  146. @_helpers.copy_docstring(external_account.Credentials)
  147. def retrieve_subject_token(self, request):
  148. self._validate_running_mode()
  149. # Check output file.
  150. if self._credential_source_executable_output_file is not None:
  151. try:
  152. with open(
  153. self._credential_source_executable_output_file, encoding="utf-8"
  154. ) as output_file:
  155. response = json.load(output_file)
  156. except Exception:
  157. pass
  158. else:
  159. try:
  160. # If the cached response is expired, _parse_subject_token will raise an error which will be ignored and we will call the executable again.
  161. subject_token = self._parse_subject_token(response)
  162. if (
  163. "expiration_time" not in response
  164. ): # Always treat missing expiration_time as expired and proceed to executable run.
  165. raise exceptions.RefreshError
  166. except (exceptions.MalformedError, exceptions.InvalidValue):
  167. raise
  168. except exceptions.RefreshError:
  169. pass
  170. else:
  171. return subject_token
  172. if not _helpers.is_python_3():
  173. raise exceptions.RefreshError(
  174. "Pluggable auth is only supported for python 3.7+"
  175. )
  176. # Inject env vars.
  177. env = os.environ.copy()
  178. self._inject_env_variables(env)
  179. env["GOOGLE_EXTERNAL_ACCOUNT_REVOKE"] = "0"
  180. # Run executable.
  181. exe_timeout = (
  182. self._credential_source_executable_interactive_timeout_millis / 1000
  183. if self.interactive
  184. else self._credential_source_executable_timeout_millis / 1000
  185. )
  186. exe_stdin = sys.stdin if self.interactive else None
  187. exe_stdout = sys.stdout if self.interactive else subprocess.PIPE
  188. exe_stderr = sys.stdout if self.interactive else subprocess.STDOUT
  189. result = subprocess.run(
  190. self._credential_source_executable_command.split(),
  191. timeout=exe_timeout,
  192. stdin=exe_stdin,
  193. stdout=exe_stdout,
  194. stderr=exe_stderr,
  195. env=env,
  196. )
  197. if result.returncode != 0:
  198. raise exceptions.RefreshError(
  199. "Executable exited with non-zero return code {}. Error: {}".format(
  200. result.returncode, result.stdout
  201. )
  202. )
  203. # Handle executable output.
  204. response = json.loads(result.stdout.decode("utf-8")) if result.stdout else None
  205. if not response and self._credential_source_executable_output_file is not None:
  206. response = json.load(
  207. open(self._credential_source_executable_output_file, encoding="utf-8")
  208. )
  209. subject_token = self._parse_subject_token(response)
  210. return subject_token
  211. def revoke(self, request):
  212. """Revokes the subject token using the credential_source object.
  213. Args:
  214. request (google.auth.transport.Request): A callable used to make
  215. HTTP requests.
  216. Raises:
  217. google.auth.exceptions.RefreshError: If the executable revocation
  218. not properly executed.
  219. """
  220. if not self.interactive:
  221. raise exceptions.InvalidValue(
  222. "Revoke is only enabled under interactive mode."
  223. )
  224. self._validate_running_mode()
  225. if not _helpers.is_python_3():
  226. raise exceptions.RefreshError(
  227. "Pluggable auth is only supported for python 3.7+"
  228. )
  229. # Inject variables
  230. env = os.environ.copy()
  231. self._inject_env_variables(env)
  232. env["GOOGLE_EXTERNAL_ACCOUNT_REVOKE"] = "1"
  233. # Run executable
  234. result = subprocess.run(
  235. self._credential_source_executable_command.split(),
  236. timeout=self._credential_source_executable_interactive_timeout_millis
  237. / 1000,
  238. stdout=subprocess.PIPE,
  239. stderr=subprocess.STDOUT,
  240. env=env,
  241. )
  242. if result.returncode != 0:
  243. raise exceptions.RefreshError(
  244. "Auth revoke failed on executable. Exit with non-zero return code {}. Error: {}".format(
  245. result.returncode, result.stdout
  246. )
  247. )
  248. response = json.loads(result.stdout.decode("utf-8"))
  249. self._validate_revoke_response(response)
  250. @property
  251. def external_account_id(self):
  252. """Returns the external account identifier.
  253. When service account impersonation is used the identifier is the service
  254. account email.
  255. Without service account impersonation, this returns None, unless it is
  256. being used by the Google Cloud CLI which populates this field.
  257. """
  258. return self.service_account_email or self._tokeninfo_username
  259. @classmethod
  260. def from_info(cls, info, **kwargs):
  261. """Creates a Pluggable Credentials instance from parsed external account info.
  262. Args:
  263. info (Mapping[str, str]): The Pluggable external account info in Google
  264. format.
  265. kwargs: Additional arguments to pass to the constructor.
  266. Returns:
  267. google.auth.pluggable.Credentials: The constructed
  268. credentials.
  269. Raises:
  270. google.auth.exceptions.InvalidValue: For invalid parameters.
  271. google.auth.exceptions.MalformedError: For invalid parameters.
  272. """
  273. return super(Credentials, cls).from_info(info, **kwargs)
  274. @classmethod
  275. def from_file(cls, filename, **kwargs):
  276. """Creates an Pluggable Credentials instance from an external account json file.
  277. Args:
  278. filename (str): The path to the Pluggable external account json file.
  279. kwargs: Additional arguments to pass to the constructor.
  280. Returns:
  281. google.auth.pluggable.Credentials: The constructed
  282. credentials.
  283. """
  284. return super(Credentials, cls).from_file(filename, **kwargs)
  285. def _inject_env_variables(self, env):
  286. env["GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE"] = self._audience
  287. env["GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE"] = self._subject_token_type
  288. env["GOOGLE_EXTERNAL_ACCOUNT_ID"] = self.external_account_id
  289. env["GOOGLE_EXTERNAL_ACCOUNT_INTERACTIVE"] = "1" if self.interactive else "0"
  290. if self._service_account_impersonation_url is not None:
  291. env[
  292. "GOOGLE_EXTERNAL_ACCOUNT_IMPERSONATED_EMAIL"
  293. ] = self.service_account_email
  294. if self._credential_source_executable_output_file is not None:
  295. env[
  296. "GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE"
  297. ] = self._credential_source_executable_output_file
  298. def _parse_subject_token(self, response):
  299. self._validate_response_schema(response)
  300. if not response["success"]:
  301. if "code" not in response or "message" not in response:
  302. raise exceptions.MalformedError(
  303. "Error code and message fields are required in the response."
  304. )
  305. raise exceptions.RefreshError(
  306. "Executable returned unsuccessful response: code: {}, message: {}.".format(
  307. response["code"], response["message"]
  308. )
  309. )
  310. if "expiration_time" in response and response["expiration_time"] < time.time():
  311. raise exceptions.RefreshError(
  312. "The token returned by the executable is expired."
  313. )
  314. if "token_type" not in response:
  315. raise exceptions.MalformedError(
  316. "The executable response is missing the token_type field."
  317. )
  318. if (
  319. response["token_type"] == "urn:ietf:params:oauth:token-type:jwt"
  320. or response["token_type"] == "urn:ietf:params:oauth:token-type:id_token"
  321. ): # OIDC
  322. return response["id_token"]
  323. elif response["token_type"] == "urn:ietf:params:oauth:token-type:saml2": # SAML
  324. return response["saml_response"]
  325. else:
  326. raise exceptions.RefreshError("Executable returned unsupported token type.")
  327. def _validate_revoke_response(self, response):
  328. self._validate_response_schema(response)
  329. if not response["success"]:
  330. raise exceptions.RefreshError("Revoke failed with unsuccessful response.")
  331. def _validate_response_schema(self, response):
  332. if "version" not in response:
  333. raise exceptions.MalformedError(
  334. "The executable response is missing the version field."
  335. )
  336. if response["version"] > EXECUTABLE_SUPPORTED_MAX_VERSION:
  337. raise exceptions.RefreshError(
  338. "Executable returned unsupported version {}.".format(
  339. response["version"]
  340. )
  341. )
  342. if "success" not in response:
  343. raise exceptions.MalformedError(
  344. "The executable response is missing the success field."
  345. )
  346. def _validate_running_mode(self):
  347. env_allow_executables = os.environ.get(
  348. "GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"
  349. )
  350. if env_allow_executables != "1":
  351. raise exceptions.MalformedError(
  352. "Executables need to be explicitly allowed (set GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES to '1') to run."
  353. )
  354. if self.interactive and not self._credential_source_executable_output_file:
  355. raise exceptions.MalformedError(
  356. "An output_file must be specified in the credential configuration for interactive mode."
  357. )
  358. if (
  359. self.interactive
  360. and not self._credential_source_executable_interactive_timeout_millis
  361. ):
  362. raise exceptions.InvalidOperation(
  363. "Interactive mode cannot run without an interactive timeout."
  364. )
  365. if self.interactive and not self.is_workforce_pool:
  366. raise exceptions.InvalidValue(
  367. "Interactive mode is only enabled for workforce pool."
  368. )
  369. def _create_default_metrics_options(self):
  370. metrics_options = super(Credentials, self)._create_default_metrics_options()
  371. metrics_options["source"] = "executable"
  372. return metrics_options