external_account.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. """External Account Credentials.
  15. This module provides credentials that exchange workload identity pool external
  16. credentials for Google access tokens. This facilitates accessing Google Cloud
  17. Platform resources from on-prem and non-Google Cloud platforms (e.g. AWS,
  18. Microsoft Azure, OIDC identity providers), using native credentials retrieved
  19. from the current environment without the need to copy, save and manage
  20. long-lived service account credentials.
  21. Specifically, this is intended to use access tokens acquired using the GCP STS
  22. token exchange endpoint following the `OAuth 2.0 Token Exchange`_ spec.
  23. .. _OAuth 2.0 Token Exchange: https://tools.ietf.org/html/rfc8693
  24. """
  25. import abc
  26. import copy
  27. from dataclasses import dataclass
  28. import datetime
  29. import functools
  30. import io
  31. import json
  32. import re
  33. from google.auth import _helpers
  34. from google.auth import credentials
  35. from google.auth import exceptions
  36. from google.auth import impersonated_credentials
  37. from google.auth import metrics
  38. from google.oauth2 import sts
  39. from google.oauth2 import utils
  40. # External account JSON type identifier.
  41. _EXTERNAL_ACCOUNT_JSON_TYPE = "external_account"
  42. # The token exchange grant_type used for exchanging credentials.
  43. _STS_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange"
  44. # The token exchange requested_token_type. This is always an access_token.
  45. _STS_REQUESTED_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token"
  46. # Cloud resource manager URL used to retrieve project information.
  47. _CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.googleapis.com/v1/projects/"
  48. # Default Google sts token url.
  49. _DEFAULT_TOKEN_URL = "https://sts.{universe_domain}/v1/token"
  50. @dataclass
  51. class SupplierContext:
  52. """A context class that contains information about the requested third party credential that is passed
  53. to AWS security credential and subject token suppliers.
  54. Attributes:
  55. subject_token_type (str): The requested subject token type based on the Oauth2.0 token exchange spec.
  56. Expected values include::
  57. “urn:ietf:params:oauth:token-type:jwt”
  58. “urn:ietf:params:oauth:token-type:id-token”
  59. “urn:ietf:params:oauth:token-type:saml2”
  60. “urn:ietf:params:aws:token-type:aws4_request”
  61. audience (str): The requested audience for the subject token.
  62. """
  63. subject_token_type: str
  64. audience: str
  65. class Credentials(
  66. credentials.Scoped,
  67. credentials.CredentialsWithQuotaProject,
  68. credentials.CredentialsWithTokenUri,
  69. metaclass=abc.ABCMeta,
  70. ):
  71. """Base class for all external account credentials.
  72. This is used to instantiate Credentials for exchanging external account
  73. credentials for Google access token and authorizing requests to Google APIs.
  74. The base class implements the common logic for exchanging external account
  75. credentials for Google access tokens.
  76. """
  77. def __init__(
  78. self,
  79. audience,
  80. subject_token_type,
  81. token_url,
  82. credential_source,
  83. service_account_impersonation_url=None,
  84. service_account_impersonation_options=None,
  85. client_id=None,
  86. client_secret=None,
  87. token_info_url=None,
  88. quota_project_id=None,
  89. scopes=None,
  90. default_scopes=None,
  91. workforce_pool_user_project=None,
  92. universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN,
  93. trust_boundary=None,
  94. ):
  95. """Instantiates an external account credentials object.
  96. Args:
  97. audience (str): The STS audience field.
  98. subject_token_type (str): The subject token type based on the Oauth2.0 token exchange spec.
  99. Expected values include::
  100. “urn:ietf:params:oauth:token-type:jwt”
  101. “urn:ietf:params:oauth:token-type:id-token”
  102. “urn:ietf:params:oauth:token-type:saml2”
  103. “urn:ietf:params:aws:token-type:aws4_request”
  104. token_url (str): The STS endpoint URL.
  105. credential_source (Mapping): The credential source dictionary.
  106. service_account_impersonation_url (Optional[str]): The optional service account
  107. impersonation generateAccessToken URL.
  108. client_id (Optional[str]): The optional client ID.
  109. client_secret (Optional[str]): The optional client secret.
  110. token_info_url (str): The optional STS endpoint URL for token introspection.
  111. quota_project_id (Optional[str]): The optional quota project ID.
  112. scopes (Optional[Sequence[str]]): Optional scopes to request during the
  113. authorization grant.
  114. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  115. Google client library. Use 'scopes' for user-defined scopes.
  116. workforce_pool_user_project (Optona[str]): The optional workforce pool user
  117. project number when the credential corresponds to a workforce pool and not
  118. a workload identity pool. The underlying principal must still have
  119. serviceusage.services.use IAM permission to use the project for
  120. billing/quota.
  121. universe_domain (str): The universe domain. The default universe
  122. domain is googleapis.com.
  123. trust_boundary (str): String representation of trust boundary meta.
  124. Raises:
  125. google.auth.exceptions.RefreshError: If the generateAccessToken
  126. endpoint returned an error.
  127. """
  128. super(Credentials, self).__init__()
  129. self._audience = audience
  130. self._subject_token_type = subject_token_type
  131. self._universe_domain = universe_domain
  132. self._token_url = token_url
  133. if self._token_url == _DEFAULT_TOKEN_URL:
  134. self._token_url = self._token_url.replace(
  135. "{universe_domain}", self._universe_domain
  136. )
  137. self._token_info_url = token_info_url
  138. self._credential_source = credential_source
  139. self._service_account_impersonation_url = service_account_impersonation_url
  140. self._service_account_impersonation_options = (
  141. service_account_impersonation_options or {}
  142. )
  143. self._client_id = client_id
  144. self._client_secret = client_secret
  145. self._quota_project_id = quota_project_id
  146. self._scopes = scopes
  147. self._default_scopes = default_scopes
  148. self._workforce_pool_user_project = workforce_pool_user_project
  149. self._trust_boundary = {
  150. "locations": [],
  151. "encoded_locations": "0x0",
  152. } # expose a placeholder trust boundary value.
  153. if self._client_id:
  154. self._client_auth = utils.ClientAuthentication(
  155. utils.ClientAuthType.basic, self._client_id, self._client_secret
  156. )
  157. else:
  158. self._client_auth = None
  159. self._sts_client = sts.Client(self._token_url, self._client_auth)
  160. self._metrics_options = self._create_default_metrics_options()
  161. self._impersonated_credentials = None
  162. self._project_id = None
  163. self._supplier_context = SupplierContext(
  164. self._subject_token_type, self._audience
  165. )
  166. if not self.is_workforce_pool and self._workforce_pool_user_project:
  167. # Workload identity pools do not support workforce pool user projects.
  168. raise exceptions.InvalidValue(
  169. "workforce_pool_user_project should not be set for non-workforce pool "
  170. "credentials"
  171. )
  172. @property
  173. def info(self):
  174. """Generates the dictionary representation of the current credentials.
  175. Returns:
  176. Mapping: The dictionary representation of the credentials. This is the
  177. reverse of "from_info" defined on the subclasses of this class. It is
  178. useful for serializing the current credentials so it can deserialized
  179. later.
  180. """
  181. config_info = self._constructor_args()
  182. config_info.update(
  183. type=_EXTERNAL_ACCOUNT_JSON_TYPE,
  184. service_account_impersonation=config_info.pop(
  185. "service_account_impersonation_options", None
  186. ),
  187. )
  188. config_info.pop("scopes", None)
  189. config_info.pop("default_scopes", None)
  190. return {key: value for key, value in config_info.items() if value is not None}
  191. def _constructor_args(self):
  192. args = {
  193. "audience": self._audience,
  194. "subject_token_type": self._subject_token_type,
  195. "token_url": self._token_url,
  196. "token_info_url": self._token_info_url,
  197. "service_account_impersonation_url": self._service_account_impersonation_url,
  198. "service_account_impersonation_options": copy.deepcopy(
  199. self._service_account_impersonation_options
  200. )
  201. or None,
  202. "credential_source": copy.deepcopy(self._credential_source),
  203. "quota_project_id": self._quota_project_id,
  204. "client_id": self._client_id,
  205. "client_secret": self._client_secret,
  206. "workforce_pool_user_project": self._workforce_pool_user_project,
  207. "scopes": self._scopes,
  208. "default_scopes": self._default_scopes,
  209. "universe_domain": self._universe_domain,
  210. }
  211. if not self.is_workforce_pool:
  212. args.pop("workforce_pool_user_project")
  213. return args
  214. @property
  215. def service_account_email(self):
  216. """Returns the service account email if service account impersonation is used.
  217. Returns:
  218. Optional[str]: The service account email if impersonation is used. Otherwise
  219. None is returned.
  220. """
  221. if self._service_account_impersonation_url:
  222. # Parse email from URL. The formal looks as follows:
  223. # https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/name@project-id.iam.gserviceaccount.com:generateAccessToken
  224. url = self._service_account_impersonation_url
  225. start_index = url.rfind("/")
  226. end_index = url.find(":generateAccessToken")
  227. if start_index != -1 and end_index != -1 and start_index < end_index:
  228. start_index = start_index + 1
  229. return url[start_index:end_index]
  230. return None
  231. @property
  232. def is_user(self):
  233. """Returns whether the credentials represent a user (True) or workload (False).
  234. Workloads behave similarly to service accounts. Currently workloads will use
  235. service account impersonation but will eventually not require impersonation.
  236. As a result, this property is more reliable than the service account email
  237. property in determining if the credentials represent a user or workload.
  238. Returns:
  239. bool: True if the credentials represent a user. False if they represent a
  240. workload.
  241. """
  242. # If service account impersonation is used, the credentials will always represent a
  243. # service account.
  244. if self._service_account_impersonation_url:
  245. return False
  246. return self.is_workforce_pool
  247. @property
  248. def is_workforce_pool(self):
  249. """Returns whether the credentials represent a workforce pool (True) or
  250. workload (False) based on the credentials' audience.
  251. This will also return True for impersonated workforce pool credentials.
  252. Returns:
  253. bool: True if the credentials represent a workforce pool. False if they
  254. represent a workload.
  255. """
  256. # Workforce pools representing users have the following audience format:
  257. # //iam.googleapis.com/locations/$location/workforcePools/$poolId/providers/$providerId
  258. p = re.compile(r"//iam\.googleapis\.com/locations/[^/]+/workforcePools/")
  259. return p.match(self._audience or "") is not None
  260. @property
  261. def requires_scopes(self):
  262. """Checks if the credentials requires scopes.
  263. Returns:
  264. bool: True if there are no scopes set otherwise False.
  265. """
  266. return not self._scopes and not self._default_scopes
  267. @property
  268. def project_number(self):
  269. """Optional[str]: The project number corresponding to the workload identity pool."""
  270. # STS audience pattern:
  271. # //iam.googleapis.com/projects/$PROJECT_NUMBER/locations/...
  272. components = self._audience.split("/")
  273. try:
  274. project_index = components.index("projects")
  275. if project_index + 1 < len(components):
  276. return components[project_index + 1] or None
  277. except ValueError:
  278. return None
  279. @property
  280. def token_info_url(self):
  281. """Optional[str]: The STS token introspection endpoint."""
  282. return self._token_info_url
  283. @_helpers.copy_docstring(credentials.Scoped)
  284. def with_scopes(self, scopes, default_scopes=None):
  285. kwargs = self._constructor_args()
  286. kwargs.update(scopes=scopes, default_scopes=default_scopes)
  287. scoped = self.__class__(**kwargs)
  288. scoped._metrics_options = self._metrics_options
  289. return scoped
  290. @abc.abstractmethod
  291. def retrieve_subject_token(self, request):
  292. """Retrieves the subject token using the credential_source object.
  293. Args:
  294. request (google.auth.transport.Request): A callable used to make
  295. HTTP requests.
  296. Returns:
  297. str: The retrieved subject token.
  298. """
  299. # pylint: disable=missing-raises-doc
  300. # (pylint doesn't recognize that this is abstract)
  301. raise NotImplementedError("retrieve_subject_token must be implemented")
  302. def get_project_id(self, request):
  303. """Retrieves the project ID corresponding to the workload identity or workforce pool.
  304. For workforce pool credentials, it returns the project ID corresponding to
  305. the workforce_pool_user_project.
  306. When not determinable, None is returned.
  307. This is introduced to support the current pattern of using the Auth library:
  308. credentials, project_id = google.auth.default()
  309. The resource may not have permission (resourcemanager.projects.get) to
  310. call this API or the required scopes may not be selected:
  311. https://cloud.google.com/resource-manager/reference/rest/v1/projects/get#authorization-scopes
  312. Args:
  313. request (google.auth.transport.Request): A callable used to make
  314. HTTP requests.
  315. Returns:
  316. Optional[str]: The project ID corresponding to the workload identity pool
  317. or workforce pool if determinable.
  318. """
  319. if self._project_id:
  320. # If already retrieved, return the cached project ID value.
  321. return self._project_id
  322. scopes = self._scopes if self._scopes is not None else self._default_scopes
  323. # Scopes are required in order to retrieve a valid access token.
  324. project_number = self.project_number or self._workforce_pool_user_project
  325. if project_number and scopes:
  326. headers = {}
  327. url = _CLOUD_RESOURCE_MANAGER + project_number
  328. self.before_request(request, "GET", url, headers)
  329. response = request(url=url, method="GET", headers=headers)
  330. response_body = (
  331. response.data.decode("utf-8")
  332. if hasattr(response.data, "decode")
  333. else response.data
  334. )
  335. response_data = json.loads(response_body)
  336. if response.status == 200:
  337. # Cache result as this field is immutable.
  338. self._project_id = response_data.get("projectId")
  339. return self._project_id
  340. return None
  341. @_helpers.copy_docstring(credentials.Credentials)
  342. def refresh(self, request):
  343. scopes = self._scopes if self._scopes is not None else self._default_scopes
  344. # Inject client certificate into request.
  345. if self._mtls_required():
  346. request = functools.partial(
  347. request, cert=self._get_mtls_cert_and_key_paths()
  348. )
  349. if self._should_initialize_impersonated_credentials():
  350. self._impersonated_credentials = self._initialize_impersonated_credentials()
  351. if self._impersonated_credentials:
  352. self._impersonated_credentials.refresh(request)
  353. self.token = self._impersonated_credentials.token
  354. self.expiry = self._impersonated_credentials.expiry
  355. else:
  356. now = _helpers.utcnow()
  357. additional_options = None
  358. # Do not pass workforce_pool_user_project when client authentication
  359. # is used. The client ID is sufficient for determining the user project.
  360. if self._workforce_pool_user_project and not self._client_id:
  361. additional_options = {"userProject": self._workforce_pool_user_project}
  362. additional_headers = {
  363. metrics.API_CLIENT_HEADER: metrics.byoid_metrics_header(
  364. self._metrics_options
  365. )
  366. }
  367. response_data = self._sts_client.exchange_token(
  368. request=request,
  369. grant_type=_STS_GRANT_TYPE,
  370. subject_token=self.retrieve_subject_token(request),
  371. subject_token_type=self._subject_token_type,
  372. audience=self._audience,
  373. scopes=scopes,
  374. requested_token_type=_STS_REQUESTED_TOKEN_TYPE,
  375. additional_options=additional_options,
  376. additional_headers=additional_headers,
  377. )
  378. self.token = response_data.get("access_token")
  379. expires_in = response_data.get("expires_in")
  380. # Some services do not respect the OAUTH2.0 RFC and send expires_in as a
  381. # JSON String.
  382. if isinstance(expires_in, str):
  383. expires_in = int(expires_in)
  384. lifetime = datetime.timedelta(seconds=expires_in)
  385. self.expiry = now + lifetime
  386. @_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
  387. def with_quota_project(self, quota_project_id):
  388. # Return copy of instance with the provided quota project ID.
  389. kwargs = self._constructor_args()
  390. kwargs.update(quota_project_id=quota_project_id)
  391. new_cred = self.__class__(**kwargs)
  392. new_cred._metrics_options = self._metrics_options
  393. return new_cred
  394. @_helpers.copy_docstring(credentials.CredentialsWithTokenUri)
  395. def with_token_uri(self, token_uri):
  396. kwargs = self._constructor_args()
  397. kwargs.update(token_url=token_uri)
  398. new_cred = self.__class__(**kwargs)
  399. new_cred._metrics_options = self._metrics_options
  400. return new_cred
  401. @_helpers.copy_docstring(credentials.CredentialsWithUniverseDomain)
  402. def with_universe_domain(self, universe_domain):
  403. kwargs = self._constructor_args()
  404. kwargs.update(universe_domain=universe_domain)
  405. new_cred = self.__class__(**kwargs)
  406. new_cred._metrics_options = self._metrics_options
  407. return new_cred
  408. def _should_initialize_impersonated_credentials(self):
  409. return (
  410. self._service_account_impersonation_url is not None
  411. and self._impersonated_credentials is None
  412. )
  413. def _initialize_impersonated_credentials(self):
  414. """Generates an impersonated credentials.
  415. For more details, see `projects.serviceAccounts.generateAccessToken`_.
  416. .. _projects.serviceAccounts.generateAccessToken: https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateAccessToken
  417. Returns:
  418. impersonated_credentials.Credential: The impersonated credentials
  419. object.
  420. Raises:
  421. google.auth.exceptions.RefreshError: If the generateAccessToken
  422. endpoint returned an error.
  423. """
  424. # Return copy of instance with no service account impersonation.
  425. kwargs = self._constructor_args()
  426. kwargs.update(
  427. service_account_impersonation_url=None,
  428. service_account_impersonation_options={},
  429. )
  430. source_credentials = self.__class__(**kwargs)
  431. source_credentials._metrics_options = self._metrics_options
  432. # Determine target_principal.
  433. target_principal = self.service_account_email
  434. if not target_principal:
  435. raise exceptions.RefreshError(
  436. "Unable to determine target principal from service account impersonation URL."
  437. )
  438. scopes = self._scopes if self._scopes is not None else self._default_scopes
  439. # Initialize and return impersonated credentials.
  440. return impersonated_credentials.Credentials(
  441. source_credentials=source_credentials,
  442. target_principal=target_principal,
  443. target_scopes=scopes,
  444. quota_project_id=self._quota_project_id,
  445. iam_endpoint_override=self._service_account_impersonation_url,
  446. lifetime=self._service_account_impersonation_options.get(
  447. "token_lifetime_seconds"
  448. ),
  449. )
  450. def _create_default_metrics_options(self):
  451. metrics_options = {}
  452. if self._service_account_impersonation_url:
  453. metrics_options["sa-impersonation"] = "true"
  454. else:
  455. metrics_options["sa-impersonation"] = "false"
  456. if self._service_account_impersonation_options.get("token_lifetime_seconds"):
  457. metrics_options["config-lifetime"] = "true"
  458. else:
  459. metrics_options["config-lifetime"] = "false"
  460. return metrics_options
  461. def _mtls_required(self):
  462. """Returns a boolean representing whether the current credential is configured
  463. for mTLS and should add a certificate to the outgoing calls to the sts and service
  464. account impersonation endpoint.
  465. Returns:
  466. bool: True if the credential is configured for mTLS, False if it is not.
  467. """
  468. return False
  469. def _get_mtls_cert_and_key_paths(self):
  470. """Gets the file locations for a certificate and private key file
  471. to be used for configuring mTLS for the sts and service account
  472. impersonation calls. Currently only expected to return a value when using
  473. X509 workload identity federation.
  474. Returns:
  475. Tuple[str, str]: The cert and key file locations as strings in a tuple.
  476. Raises:
  477. NotImplementedError: When the current credential is not configured for
  478. mTLS.
  479. """
  480. raise NotImplementedError(
  481. "_get_mtls_cert_and_key_location must be implemented."
  482. )
  483. @classmethod
  484. def from_info(cls, info, **kwargs):
  485. """Creates a Credentials instance from parsed external account info.
  486. Args:
  487. info (Mapping[str, str]): The external account info in Google
  488. format.
  489. kwargs: Additional arguments to pass to the constructor.
  490. Returns:
  491. google.auth.identity_pool.Credentials: The constructed
  492. credentials.
  493. Raises:
  494. InvalidValue: For invalid parameters.
  495. """
  496. return cls(
  497. audience=info.get("audience"),
  498. subject_token_type=info.get("subject_token_type"),
  499. token_url=info.get("token_url"),
  500. token_info_url=info.get("token_info_url"),
  501. service_account_impersonation_url=info.get(
  502. "service_account_impersonation_url"
  503. ),
  504. service_account_impersonation_options=info.get(
  505. "service_account_impersonation"
  506. )
  507. or {},
  508. client_id=info.get("client_id"),
  509. client_secret=info.get("client_secret"),
  510. credential_source=info.get("credential_source"),
  511. quota_project_id=info.get("quota_project_id"),
  512. workforce_pool_user_project=info.get("workforce_pool_user_project"),
  513. universe_domain=info.get(
  514. "universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN
  515. ),
  516. **kwargs
  517. )
  518. @classmethod
  519. def from_file(cls, filename, **kwargs):
  520. """Creates a Credentials instance from an external account json file.
  521. Args:
  522. filename (str): The path to the external account json file.
  523. kwargs: Additional arguments to pass to the constructor.
  524. Returns:
  525. google.auth.identity_pool.Credentials: The constructed
  526. credentials.
  527. """
  528. with io.open(filename, "r", encoding="utf-8") as json_file:
  529. data = json.load(json_file)
  530. return cls.from_info(data, **kwargs)