external_account.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. import datetime
  28. import json
  29. import re
  30. import six
  31. from google.auth import _helpers
  32. from google.auth import credentials
  33. from google.auth import exceptions
  34. from google.auth import impersonated_credentials
  35. from google.oauth2 import sts
  36. from google.oauth2 import utils
  37. # External account JSON type identifier.
  38. _EXTERNAL_ACCOUNT_JSON_TYPE = "external_account"
  39. # The token exchange grant_type used for exchanging credentials.
  40. _STS_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange"
  41. # The token exchange requested_token_type. This is always an access_token.
  42. _STS_REQUESTED_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token"
  43. # Cloud resource manager URL used to retrieve project information.
  44. _CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.googleapis.com/v1/projects/"
  45. @six.add_metaclass(abc.ABCMeta)
  46. class Credentials(credentials.Scoped, credentials.CredentialsWithQuotaProject):
  47. """Base class for all external account credentials.
  48. This is used to instantiate Credentials for exchanging external account
  49. credentials for Google access token and authorizing requests to Google APIs.
  50. The base class implements the common logic for exchanging external account
  51. credentials for Google access tokens.
  52. """
  53. def __init__(
  54. self,
  55. audience,
  56. subject_token_type,
  57. token_url,
  58. credential_source,
  59. service_account_impersonation_url=None,
  60. client_id=None,
  61. client_secret=None,
  62. quota_project_id=None,
  63. scopes=None,
  64. default_scopes=None,
  65. ):
  66. """Instantiates an external account credentials object.
  67. Args:
  68. audience (str): The STS audience field.
  69. subject_token_type (str): The subject token type.
  70. token_url (str): The STS endpoint URL.
  71. credential_source (Mapping): The credential source dictionary.
  72. service_account_impersonation_url (Optional[str]): The optional service account
  73. impersonation generateAccessToken URL.
  74. client_id (Optional[str]): The optional client ID.
  75. client_secret (Optional[str]): The optional client secret.
  76. quota_project_id (Optional[str]): The optional quota project ID.
  77. scopes (Optional[Sequence[str]]): Optional scopes to request during the
  78. authorization grant.
  79. default_scopes (Optional[Sequence[str]]): Default scopes passed by a
  80. Google client library. Use 'scopes' for user-defined scopes.
  81. Raises:
  82. google.auth.exceptions.RefreshError: If the generateAccessToken
  83. endpoint returned an error.
  84. """
  85. super(Credentials, self).__init__()
  86. self._audience = audience
  87. self._subject_token_type = subject_token_type
  88. self._token_url = token_url
  89. self._credential_source = credential_source
  90. self._service_account_impersonation_url = service_account_impersonation_url
  91. self._client_id = client_id
  92. self._client_secret = client_secret
  93. self._quota_project_id = quota_project_id
  94. self._scopes = scopes
  95. self._default_scopes = default_scopes
  96. if self._client_id:
  97. self._client_auth = utils.ClientAuthentication(
  98. utils.ClientAuthType.basic, self._client_id, self._client_secret
  99. )
  100. else:
  101. self._client_auth = None
  102. self._sts_client = sts.Client(self._token_url, self._client_auth)
  103. if self._service_account_impersonation_url:
  104. self._impersonated_credentials = self._initialize_impersonated_credentials()
  105. else:
  106. self._impersonated_credentials = None
  107. self._project_id = None
  108. @property
  109. def info(self):
  110. """Generates the dictionary representation of the current credentials.
  111. Returns:
  112. Mapping: The dictionary representation of the credentials. This is the
  113. reverse of "from_info" defined on the subclasses of this class. It is
  114. useful for serializing the current credentials so it can deserialized
  115. later.
  116. """
  117. config_info = {
  118. "type": _EXTERNAL_ACCOUNT_JSON_TYPE,
  119. "audience": self._audience,
  120. "subject_token_type": self._subject_token_type,
  121. "token_url": self._token_url,
  122. "service_account_impersonation_url": self._service_account_impersonation_url,
  123. "credential_source": copy.deepcopy(self._credential_source),
  124. "quota_project_id": self._quota_project_id,
  125. "client_id": self._client_id,
  126. "client_secret": self._client_secret,
  127. }
  128. return {key: value for key, value in config_info.items() if value is not None}
  129. @property
  130. def service_account_email(self):
  131. """Returns the service account email if service account impersonation is used.
  132. Returns:
  133. Optional[str]: The service account email if impersonation is used. Otherwise
  134. None is returned.
  135. """
  136. if self._service_account_impersonation_url:
  137. # Parse email from URL. The formal looks as follows:
  138. # https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/name@project-id.iam.gserviceaccount.com:generateAccessToken
  139. url = self._service_account_impersonation_url
  140. start_index = url.rfind("/")
  141. end_index = url.find(":generateAccessToken")
  142. if start_index != -1 and end_index != -1 and start_index < end_index:
  143. start_index = start_index + 1
  144. return url[start_index:end_index]
  145. return None
  146. @property
  147. def is_user(self):
  148. """Returns whether the credentials represent a user (True) or workload (False).
  149. Workloads behave similarly to service accounts. Currently workloads will use
  150. service account impersonation but will eventually not require impersonation.
  151. As a result, this property is more reliable than the service account email
  152. property in determining if the credentials represent a user or workload.
  153. Returns:
  154. bool: True if the credentials represent a user. False if they represent a
  155. workload.
  156. """
  157. # If service account impersonation is used, the credentials will always represent a
  158. # service account.
  159. if self._service_account_impersonation_url:
  160. return False
  161. # Workforce pools representing users have the following audience format:
  162. # //iam.googleapis.com/locations/$location/workforcePools/$poolId/providers/$providerId
  163. p = re.compile(r"//iam\.googleapis\.com/locations/[^/]+/workforcePools/")
  164. if p.match(self._audience):
  165. return True
  166. return False
  167. @property
  168. def requires_scopes(self):
  169. """Checks if the credentials requires scopes.
  170. Returns:
  171. bool: True if there are no scopes set otherwise False.
  172. """
  173. return not self._scopes and not self._default_scopes
  174. @property
  175. def project_number(self):
  176. """Optional[str]: The project number corresponding to the workload identity pool."""
  177. # STS audience pattern:
  178. # //iam.googleapis.com/projects/$PROJECT_NUMBER/locations/...
  179. components = self._audience.split("/")
  180. try:
  181. project_index = components.index("projects")
  182. if project_index + 1 < len(components):
  183. return components[project_index + 1] or None
  184. except ValueError:
  185. return None
  186. @_helpers.copy_docstring(credentials.Scoped)
  187. def with_scopes(self, scopes, default_scopes=None):
  188. return self.__class__(
  189. audience=self._audience,
  190. subject_token_type=self._subject_token_type,
  191. token_url=self._token_url,
  192. credential_source=self._credential_source,
  193. service_account_impersonation_url=self._service_account_impersonation_url,
  194. client_id=self._client_id,
  195. client_secret=self._client_secret,
  196. quota_project_id=self._quota_project_id,
  197. scopes=scopes,
  198. default_scopes=default_scopes,
  199. )
  200. @abc.abstractmethod
  201. def retrieve_subject_token(self, request):
  202. """Retrieves the subject token using the credential_source object.
  203. Args:
  204. request (google.auth.transport.Request): A callable used to make
  205. HTTP requests.
  206. Returns:
  207. str: The retrieved subject token.
  208. """
  209. # pylint: disable=missing-raises-doc
  210. # (pylint doesn't recognize that this is abstract)
  211. raise NotImplementedError("retrieve_subject_token must be implemented")
  212. def get_project_id(self, request):
  213. """Retrieves the project ID corresponding to the workload identity pool.
  214. When not determinable, None is returned.
  215. This is introduced to support the current pattern of using the Auth library:
  216. credentials, project_id = google.auth.default()
  217. The resource may not have permission (resourcemanager.projects.get) to
  218. call this API or the required scopes may not be selected:
  219. https://cloud.google.com/resource-manager/reference/rest/v1/projects/get#authorization-scopes
  220. Args:
  221. request (google.auth.transport.Request): A callable used to make
  222. HTTP requests.
  223. Returns:
  224. Optional[str]: The project ID corresponding to the workload identity pool
  225. if determinable.
  226. """
  227. if self._project_id:
  228. # If already retrieved, return the cached project ID value.
  229. return self._project_id
  230. scopes = self._scopes if self._scopes is not None else self._default_scopes
  231. # Scopes are required in order to retrieve a valid access token.
  232. if self.project_number and scopes:
  233. headers = {}
  234. url = _CLOUD_RESOURCE_MANAGER + self.project_number
  235. self.before_request(request, "GET", url, headers)
  236. response = request(url=url, method="GET", headers=headers)
  237. response_body = (
  238. response.data.decode("utf-8")
  239. if hasattr(response.data, "decode")
  240. else response.data
  241. )
  242. response_data = json.loads(response_body)
  243. if response.status == 200:
  244. # Cache result as this field is immutable.
  245. self._project_id = response_data.get("projectId")
  246. return self._project_id
  247. return None
  248. @_helpers.copy_docstring(credentials.Credentials)
  249. def refresh(self, request):
  250. scopes = self._scopes if self._scopes is not None else self._default_scopes
  251. if self._impersonated_credentials:
  252. self._impersonated_credentials.refresh(request)
  253. self.token = self._impersonated_credentials.token
  254. self.expiry = self._impersonated_credentials.expiry
  255. else:
  256. now = _helpers.utcnow()
  257. response_data = self._sts_client.exchange_token(
  258. request=request,
  259. grant_type=_STS_GRANT_TYPE,
  260. subject_token=self.retrieve_subject_token(request),
  261. subject_token_type=self._subject_token_type,
  262. audience=self._audience,
  263. scopes=scopes,
  264. requested_token_type=_STS_REQUESTED_TOKEN_TYPE,
  265. )
  266. self.token = response_data.get("access_token")
  267. lifetime = datetime.timedelta(seconds=response_data.get("expires_in"))
  268. self.expiry = now + lifetime
  269. @_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
  270. def with_quota_project(self, quota_project_id):
  271. # Return copy of instance with the provided quota project ID.
  272. return self.__class__(
  273. audience=self._audience,
  274. subject_token_type=self._subject_token_type,
  275. token_url=self._token_url,
  276. credential_source=self._credential_source,
  277. service_account_impersonation_url=self._service_account_impersonation_url,
  278. client_id=self._client_id,
  279. client_secret=self._client_secret,
  280. quota_project_id=quota_project_id,
  281. scopes=self._scopes,
  282. default_scopes=self._default_scopes,
  283. )
  284. def _initialize_impersonated_credentials(self):
  285. """Generates an impersonated credentials.
  286. For more details, see `projects.serviceAccounts.generateAccessToken`_.
  287. .. _projects.serviceAccounts.generateAccessToken: https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateAccessToken
  288. Returns:
  289. impersonated_credentials.Credential: The impersonated credentials
  290. object.
  291. Raises:
  292. google.auth.exceptions.RefreshError: If the generateAccessToken
  293. endpoint returned an error.
  294. """
  295. # Return copy of instance with no service account impersonation.
  296. source_credentials = self.__class__(
  297. audience=self._audience,
  298. subject_token_type=self._subject_token_type,
  299. token_url=self._token_url,
  300. credential_source=self._credential_source,
  301. service_account_impersonation_url=None,
  302. client_id=self._client_id,
  303. client_secret=self._client_secret,
  304. quota_project_id=self._quota_project_id,
  305. scopes=self._scopes,
  306. default_scopes=self._default_scopes,
  307. )
  308. # Determine target_principal.
  309. target_principal = self.service_account_email
  310. if not target_principal:
  311. raise exceptions.RefreshError(
  312. "Unable to determine target principal from service account impersonation URL."
  313. )
  314. scopes = self._scopes if self._scopes is not None else self._default_scopes
  315. # Initialize and return impersonated credentials.
  316. return impersonated_credentials.Credentials(
  317. source_credentials=source_credentials,
  318. target_principal=target_principal,
  319. target_scopes=scopes,
  320. quota_project_id=self._quota_project_id,
  321. iam_endpoint_override=self._service_account_impersonation_url,
  322. )