aws.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. """AWS Credentials and AWS Signature V4 Request Signer.
  15. This module provides credentials to access Google Cloud resources from Amazon
  16. Web Services (AWS) workloads. These credentials are recommended over the
  17. use of service account credentials in AWS as they do not involve the management
  18. of long-live service account private keys.
  19. AWS Credentials are initialized using external_account arguments which are
  20. typically loaded from the external credentials JSON file.
  21. This module also provides a definition for an abstract AWS security credentials supplier.
  22. This supplier can be implemented to return valid AWS security credentials and an AWS region
  23. and used to create AWS credentials. The credentials will then call the
  24. supplier instead of using pre-defined methods such as calling the EC2 metadata endpoints.
  25. This module also provides a basic implementation of the
  26. `AWS Signature Version 4`_ request signing algorithm.
  27. AWS Credentials use serialized signed requests to the
  28. `AWS STS GetCallerIdentity`_ API that can be exchanged for Google access tokens
  29. via the GCP STS endpoint.
  30. .. _AWS Signature Version 4: https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
  31. .. _AWS STS GetCallerIdentity: https://docs.aws.amazon.com/STS/latest/APIReference/API_GetCallerIdentity.html
  32. """
  33. import abc
  34. from dataclasses import dataclass
  35. import hashlib
  36. import hmac
  37. import http.client as http_client
  38. import json
  39. import os
  40. import posixpath
  41. import re
  42. from typing import Optional
  43. import urllib
  44. from urllib.parse import urljoin
  45. from google.auth import _helpers
  46. from google.auth import environment_vars
  47. from google.auth import exceptions
  48. from google.auth import external_account
  49. # AWS Signature Version 4 signing algorithm identifier.
  50. _AWS_ALGORITHM = "AWS4-HMAC-SHA256"
  51. # The termination string for the AWS credential scope value as defined in
  52. # https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
  53. _AWS_REQUEST_TYPE = "aws4_request"
  54. # The AWS authorization header name for the security session token if available.
  55. _AWS_SECURITY_TOKEN_HEADER = "x-amz-security-token"
  56. # The AWS authorization header name for the auto-generated date.
  57. _AWS_DATE_HEADER = "x-amz-date"
  58. # The default AWS regional credential verification URL.
  59. _DEFAULT_AWS_REGIONAL_CREDENTIAL_VERIFICATION_URL = (
  60. "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"
  61. )
  62. # IMDSV2 session token lifetime. This is set to a low value because the session token is used immediately.
  63. _IMDSV2_SESSION_TOKEN_TTL_SECONDS = "300"
  64. class RequestSigner(object):
  65. """Implements an AWS request signer based on the AWS Signature Version 4 signing
  66. process.
  67. https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
  68. """
  69. def __init__(self, region_name):
  70. """Instantiates an AWS request signer used to compute authenticated signed
  71. requests to AWS APIs based on the AWS Signature Version 4 signing process.
  72. Args:
  73. region_name (str): The AWS region to use.
  74. """
  75. self._region_name = region_name
  76. def get_request_options(
  77. self,
  78. aws_security_credentials,
  79. url,
  80. method,
  81. request_payload="",
  82. additional_headers={},
  83. ):
  84. """Generates the signed request for the provided HTTP request for calling
  85. an AWS API. This follows the steps described at:
  86. https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
  87. Args:
  88. aws_security_credentials (AWSSecurityCredentials): The AWS security credentials.
  89. url (str): The AWS service URL containing the canonical URI and
  90. query string.
  91. method (str): The HTTP method used to call this API.
  92. request_payload (Optional[str]): The optional request payload if
  93. available.
  94. additional_headers (Optional[Mapping[str, str]]): The optional
  95. additional headers needed for the requested AWS API.
  96. Returns:
  97. Mapping[str, str]: The AWS signed request dictionary object.
  98. """
  99. additional_headers = additional_headers or {}
  100. uri = urllib.parse.urlparse(url)
  101. # Normalize the URL path. This is needed for the canonical_uri.
  102. # os.path.normpath can't be used since it normalizes "/" paths
  103. # to "\\" in Windows OS.
  104. normalized_uri = urllib.parse.urlparse(
  105. urljoin(url, posixpath.normpath(uri.path))
  106. )
  107. # Validate provided URL.
  108. if not uri.hostname or uri.scheme != "https":
  109. raise exceptions.InvalidResource("Invalid AWS service URL")
  110. header_map = _generate_authentication_header_map(
  111. host=uri.hostname,
  112. canonical_uri=normalized_uri.path or "/",
  113. canonical_querystring=_get_canonical_querystring(uri.query),
  114. method=method,
  115. region=self._region_name,
  116. aws_security_credentials=aws_security_credentials,
  117. request_payload=request_payload,
  118. additional_headers=additional_headers,
  119. )
  120. headers = {
  121. "Authorization": header_map.get("authorization_header"),
  122. "host": uri.hostname,
  123. }
  124. # Add x-amz-date if available.
  125. if "amz_date" in header_map:
  126. headers[_AWS_DATE_HEADER] = header_map.get("amz_date")
  127. # Append additional optional headers, eg. X-Amz-Target, Content-Type, etc.
  128. for key in additional_headers:
  129. headers[key] = additional_headers[key]
  130. # Add session token if available.
  131. if aws_security_credentials.session_token is not None:
  132. headers[_AWS_SECURITY_TOKEN_HEADER] = aws_security_credentials.session_token
  133. signed_request = {"url": url, "method": method, "headers": headers}
  134. if request_payload:
  135. signed_request["data"] = request_payload
  136. return signed_request
  137. def _get_canonical_querystring(query):
  138. """Generates the canonical query string given a raw query string.
  139. Logic is based on
  140. https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
  141. Args:
  142. query (str): The raw query string.
  143. Returns:
  144. str: The canonical query string.
  145. """
  146. # Parse raw query string.
  147. querystring = urllib.parse.parse_qs(query)
  148. querystring_encoded_map = {}
  149. for key in querystring:
  150. quote_key = urllib.parse.quote(key, safe="-_.~")
  151. # URI encode key.
  152. querystring_encoded_map[quote_key] = []
  153. for item in querystring[key]:
  154. # For each key, URI encode all values for that key.
  155. querystring_encoded_map[quote_key].append(
  156. urllib.parse.quote(item, safe="-_.~")
  157. )
  158. # Sort values for each key.
  159. querystring_encoded_map[quote_key].sort()
  160. # Sort keys.
  161. sorted_keys = list(querystring_encoded_map.keys())
  162. sorted_keys.sort()
  163. # Reconstruct the query string. Preserve keys with multiple values.
  164. querystring_encoded_pairs = []
  165. for key in sorted_keys:
  166. for item in querystring_encoded_map[key]:
  167. querystring_encoded_pairs.append("{}={}".format(key, item))
  168. return "&".join(querystring_encoded_pairs)
  169. def _sign(key, msg):
  170. """Creates the HMAC-SHA256 hash of the provided message using the provided
  171. key.
  172. Args:
  173. key (str): The HMAC-SHA256 key to use.
  174. msg (str): The message to hash.
  175. Returns:
  176. str: The computed hash bytes.
  177. """
  178. return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
  179. def _get_signing_key(key, date_stamp, region_name, service_name):
  180. """Calculates the signing key used to calculate the signature for
  181. AWS Signature Version 4 based on:
  182. https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
  183. Args:
  184. key (str): The AWS secret access key.
  185. date_stamp (str): The '%Y%m%d' date format.
  186. region_name (str): The AWS region.
  187. service_name (str): The AWS service name, eg. sts.
  188. Returns:
  189. str: The signing key bytes.
  190. """
  191. k_date = _sign(("AWS4" + key).encode("utf-8"), date_stamp)
  192. k_region = _sign(k_date, region_name)
  193. k_service = _sign(k_region, service_name)
  194. k_signing = _sign(k_service, "aws4_request")
  195. return k_signing
  196. def _generate_authentication_header_map(
  197. host,
  198. canonical_uri,
  199. canonical_querystring,
  200. method,
  201. region,
  202. aws_security_credentials,
  203. request_payload="",
  204. additional_headers={},
  205. ):
  206. """Generates the authentication header map needed for generating the AWS
  207. Signature Version 4 signed request.
  208. Args:
  209. host (str): The AWS service URL hostname.
  210. canonical_uri (str): The AWS service URL path name.
  211. canonical_querystring (str): The AWS service URL query string.
  212. method (str): The HTTP method used to call this API.
  213. region (str): The AWS region.
  214. aws_security_credentials (AWSSecurityCredentials): The AWS security credentials.
  215. request_payload (Optional[str]): The optional request payload if
  216. available.
  217. additional_headers (Optional[Mapping[str, str]]): The optional
  218. additional headers needed for the requested AWS API.
  219. Returns:
  220. Mapping[str, str]: The AWS authentication header dictionary object.
  221. This contains the x-amz-date and authorization header information.
  222. """
  223. # iam.amazonaws.com host => iam service.
  224. # sts.us-east-2.amazonaws.com host => sts service.
  225. service_name = host.split(".")[0]
  226. current_time = _helpers.utcnow()
  227. amz_date = current_time.strftime("%Y%m%dT%H%M%SZ")
  228. date_stamp = current_time.strftime("%Y%m%d")
  229. # Change all additional headers to be lower case.
  230. full_headers = {}
  231. for key in additional_headers:
  232. full_headers[key.lower()] = additional_headers[key]
  233. # Add AWS session token if available.
  234. if aws_security_credentials.session_token is not None:
  235. full_headers[
  236. _AWS_SECURITY_TOKEN_HEADER
  237. ] = aws_security_credentials.session_token
  238. # Required headers
  239. full_headers["host"] = host
  240. # Do not use generated x-amz-date if the date header is provided.
  241. # Previously the date was not fixed with x-amz- and could be provided
  242. # manually.
  243. # https://github.com/boto/botocore/blob/879f8440a4e9ace5d3cf145ce8b3d5e5ffb892ef/tests/unit/auth/aws4_testsuite/get-header-value-trim.req
  244. if "date" not in full_headers:
  245. full_headers[_AWS_DATE_HEADER] = amz_date
  246. # Header keys need to be sorted alphabetically.
  247. canonical_headers = ""
  248. header_keys = list(full_headers.keys())
  249. header_keys.sort()
  250. for key in header_keys:
  251. canonical_headers = "{}{}:{}\n".format(
  252. canonical_headers, key, full_headers[key]
  253. )
  254. signed_headers = ";".join(header_keys)
  255. payload_hash = hashlib.sha256((request_payload or "").encode("utf-8")).hexdigest()
  256. # https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
  257. canonical_request = "{}\n{}\n{}\n{}\n{}\n{}".format(
  258. method,
  259. canonical_uri,
  260. canonical_querystring,
  261. canonical_headers,
  262. signed_headers,
  263. payload_hash,
  264. )
  265. credential_scope = "{}/{}/{}/{}".format(
  266. date_stamp, region, service_name, _AWS_REQUEST_TYPE
  267. )
  268. # https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
  269. string_to_sign = "{}\n{}\n{}\n{}".format(
  270. _AWS_ALGORITHM,
  271. amz_date,
  272. credential_scope,
  273. hashlib.sha256(canonical_request.encode("utf-8")).hexdigest(),
  274. )
  275. # https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
  276. signing_key = _get_signing_key(
  277. aws_security_credentials.secret_access_key, date_stamp, region, service_name
  278. )
  279. signature = hmac.new(
  280. signing_key, string_to_sign.encode("utf-8"), hashlib.sha256
  281. ).hexdigest()
  282. # https://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html
  283. authorization_header = "{} Credential={}/{}, SignedHeaders={}, Signature={}".format(
  284. _AWS_ALGORITHM,
  285. aws_security_credentials.access_key_id,
  286. credential_scope,
  287. signed_headers,
  288. signature,
  289. )
  290. authentication_header = {"authorization_header": authorization_header}
  291. # Do not use generated x-amz-date if the date header is provided.
  292. if "date" not in full_headers:
  293. authentication_header["amz_date"] = amz_date
  294. return authentication_header
  295. @dataclass
  296. class AwsSecurityCredentials:
  297. """A class that models AWS security credentials with an optional session token.
  298. Attributes:
  299. access_key_id (str): The AWS security credentials access key id.
  300. secret_access_key (str): The AWS security credentials secret access key.
  301. session_token (Optional[str]): The optional AWS security credentials session token. This should be set when using temporary credentials.
  302. """
  303. access_key_id: str
  304. secret_access_key: str
  305. session_token: Optional[str] = None
  306. class AwsSecurityCredentialsSupplier(metaclass=abc.ABCMeta):
  307. """Base class for AWS security credential suppliers. This can be implemented with custom logic to retrieve
  308. AWS security credentials to exchange for a Google Cloud access token. The AWS external account credential does
  309. not cache the AWS security credentials, so caching logic should be added in the implementation.
  310. """
  311. @abc.abstractmethod
  312. def get_aws_security_credentials(self, context, request):
  313. """Returns the AWS security credentials for the requested context.
  314. .. warning: This is not cached by the calling Google credential, so caching logic should be implemented in the supplier.
  315. Args:
  316. context (google.auth.externalaccount.SupplierContext): The context object
  317. containing information about the requested audience and subject token type.
  318. request (google.auth.transport.Request): The object used to make
  319. HTTP requests.
  320. Raises:
  321. google.auth.exceptions.RefreshError: If an error is encountered during
  322. security credential retrieval logic.
  323. Returns:
  324. AwsSecurityCredentials: The requested AWS security credentials.
  325. """
  326. raise NotImplementedError("")
  327. @abc.abstractmethod
  328. def get_aws_region(self, context, request):
  329. """Returns the AWS region for the requested context.
  330. Args:
  331. context (google.auth.externalaccount.SupplierContext): The context object
  332. containing information about the requested audience and subject token type.
  333. request (google.auth.transport.Request): The object used to make
  334. HTTP requests.
  335. Raises:
  336. google.auth.exceptions.RefreshError: If an error is encountered during
  337. region retrieval logic.
  338. Returns:
  339. str: The AWS region.
  340. """
  341. raise NotImplementedError("")
  342. class _DefaultAwsSecurityCredentialsSupplier(AwsSecurityCredentialsSupplier):
  343. """Default implementation of AWS security credentials supplier. Supports retrieving
  344. credentials and region via EC2 metadata endpoints and environment variables.
  345. """
  346. def __init__(self, credential_source):
  347. self._region_url = credential_source.get("region_url")
  348. self._security_credentials_url = credential_source.get("url")
  349. self._imdsv2_session_token_url = credential_source.get(
  350. "imdsv2_session_token_url"
  351. )
  352. @_helpers.copy_docstring(AwsSecurityCredentialsSupplier)
  353. def get_aws_security_credentials(self, context, request):
  354. # Check environment variables for permanent credentials first.
  355. # https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html
  356. env_aws_access_key_id = os.environ.get(environment_vars.AWS_ACCESS_KEY_ID)
  357. env_aws_secret_access_key = os.environ.get(
  358. environment_vars.AWS_SECRET_ACCESS_KEY
  359. )
  360. # This is normally not available for permanent credentials.
  361. env_aws_session_token = os.environ.get(environment_vars.AWS_SESSION_TOKEN)
  362. if env_aws_access_key_id and env_aws_secret_access_key:
  363. return AwsSecurityCredentials(
  364. env_aws_access_key_id, env_aws_secret_access_key, env_aws_session_token
  365. )
  366. imdsv2_session_token = self._get_imdsv2_session_token(request)
  367. role_name = self._get_metadata_role_name(request, imdsv2_session_token)
  368. # Get security credentials.
  369. credentials = self._get_metadata_security_credentials(
  370. request, role_name, imdsv2_session_token
  371. )
  372. return AwsSecurityCredentials(
  373. credentials.get("AccessKeyId"),
  374. credentials.get("SecretAccessKey"),
  375. credentials.get("Token"),
  376. )
  377. @_helpers.copy_docstring(AwsSecurityCredentialsSupplier)
  378. def get_aws_region(self, context, request):
  379. # The AWS metadata server is not available in some AWS environments
  380. # such as AWS lambda. Instead, it is available via environment
  381. # variable.
  382. env_aws_region = os.environ.get(environment_vars.AWS_REGION)
  383. if env_aws_region is not None:
  384. return env_aws_region
  385. env_aws_region = os.environ.get(environment_vars.AWS_DEFAULT_REGION)
  386. if env_aws_region is not None:
  387. return env_aws_region
  388. if not self._region_url:
  389. raise exceptions.RefreshError("Unable to determine AWS region")
  390. headers = None
  391. imdsv2_session_token = self._get_imdsv2_session_token(request)
  392. if imdsv2_session_token is not None:
  393. headers = {"X-aws-ec2-metadata-token": imdsv2_session_token}
  394. response = request(url=self._region_url, method="GET", headers=headers)
  395. # Support both string and bytes type response.data.
  396. response_body = (
  397. response.data.decode("utf-8")
  398. if hasattr(response.data, "decode")
  399. else response.data
  400. )
  401. if response.status != http_client.OK:
  402. raise exceptions.RefreshError(
  403. "Unable to retrieve AWS region: {}".format(response_body)
  404. )
  405. # This endpoint will return the region in format: us-east-2b.
  406. # Only the us-east-2 part should be used.
  407. return response_body[:-1]
  408. def _get_imdsv2_session_token(self, request):
  409. if request is not None and self._imdsv2_session_token_url is not None:
  410. headers = {
  411. "X-aws-ec2-metadata-token-ttl-seconds": _IMDSV2_SESSION_TOKEN_TTL_SECONDS
  412. }
  413. imdsv2_session_token_response = request(
  414. url=self._imdsv2_session_token_url, method="PUT", headers=headers
  415. )
  416. if imdsv2_session_token_response.status != http_client.OK:
  417. raise exceptions.RefreshError(
  418. "Unable to retrieve AWS Session Token: {}".format(
  419. imdsv2_session_token_response.data
  420. )
  421. )
  422. return imdsv2_session_token_response.data
  423. else:
  424. return None
  425. def _get_metadata_security_credentials(
  426. self, request, role_name, imdsv2_session_token
  427. ):
  428. """Retrieves the AWS security credentials required for signing AWS
  429. requests from the AWS metadata server.
  430. Args:
  431. request (google.auth.transport.Request): A callable used to make
  432. HTTP requests.
  433. role_name (str): The AWS role name required by the AWS metadata
  434. server security_credentials endpoint in order to return the
  435. credentials.
  436. imdsv2_session_token (str): The AWS IMDSv2 session token to be added as a
  437. header in the requests to AWS metadata endpoint.
  438. Returns:
  439. Mapping[str, str]: The AWS metadata server security credentials
  440. response.
  441. Raises:
  442. google.auth.exceptions.RefreshError: If an error occurs while
  443. retrieving the AWS security credentials.
  444. """
  445. headers = {"Content-Type": "application/json"}
  446. if imdsv2_session_token is not None:
  447. headers["X-aws-ec2-metadata-token"] = imdsv2_session_token
  448. response = request(
  449. url="{}/{}".format(self._security_credentials_url, role_name),
  450. method="GET",
  451. headers=headers,
  452. )
  453. # support both string and bytes type response.data
  454. response_body = (
  455. response.data.decode("utf-8")
  456. if hasattr(response.data, "decode")
  457. else response.data
  458. )
  459. if response.status != http_client.OK:
  460. raise exceptions.RefreshError(
  461. "Unable to retrieve AWS security credentials: {}".format(response_body)
  462. )
  463. credentials_response = json.loads(response_body)
  464. return credentials_response
  465. def _get_metadata_role_name(self, request, imdsv2_session_token):
  466. """Retrieves the AWS role currently attached to the current AWS
  467. workload by querying the AWS metadata server. This is needed for the
  468. AWS metadata server security credentials endpoint in order to retrieve
  469. the AWS security credentials needed to sign requests to AWS APIs.
  470. Args:
  471. request (google.auth.transport.Request): A callable used to make
  472. HTTP requests.
  473. imdsv2_session_token (str): The AWS IMDSv2 session token to be added as a
  474. header in the requests to AWS metadata endpoint.
  475. Returns:
  476. str: The AWS role name.
  477. Raises:
  478. google.auth.exceptions.RefreshError: If an error occurs while
  479. retrieving the AWS role name.
  480. """
  481. if self._security_credentials_url is None:
  482. raise exceptions.RefreshError(
  483. "Unable to determine the AWS metadata server security credentials endpoint"
  484. )
  485. headers = None
  486. if imdsv2_session_token is not None:
  487. headers = {"X-aws-ec2-metadata-token": imdsv2_session_token}
  488. response = request(
  489. url=self._security_credentials_url, method="GET", headers=headers
  490. )
  491. # support both string and bytes type response.data
  492. response_body = (
  493. response.data.decode("utf-8")
  494. if hasattr(response.data, "decode")
  495. else response.data
  496. )
  497. if response.status != http_client.OK:
  498. raise exceptions.RefreshError(
  499. "Unable to retrieve AWS role name {}".format(response_body)
  500. )
  501. return response_body
  502. class Credentials(external_account.Credentials):
  503. """AWS external account credentials.
  504. This is used to exchange serialized AWS signature v4 signed requests to
  505. AWS STS GetCallerIdentity service for Google access tokens.
  506. """
  507. def __init__(
  508. self,
  509. audience,
  510. subject_token_type,
  511. token_url=external_account._DEFAULT_TOKEN_URL,
  512. credential_source=None,
  513. aws_security_credentials_supplier=None,
  514. *args,
  515. **kwargs
  516. ):
  517. """Instantiates an AWS workload external account credentials object.
  518. Args:
  519. audience (str): The STS audience field.
  520. subject_token_type (str): The subject token type based on the Oauth2.0 token exchange spec.
  521. Expected values include::
  522. “urn:ietf:params:aws:token-type:aws4_request”
  523. token_url (Optional [str]): The STS endpoint URL. If not provided, will default to "https://sts.googleapis.com/v1/token".
  524. credential_source (Optional [Mapping]): The credential source dictionary used
  525. to provide instructions on how to retrieve external credential to be exchanged for Google access tokens.
  526. Either a credential source or an AWS security credentials supplier must be provided.
  527. Example credential_source for AWS credential::
  528. {
  529. "environment_id": "aws1",
  530. "regional_cred_verification_url": "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
  531. "region_url": "http://169.254.169.254/latest/meta-data/placement/availability-zone",
  532. "url": "http://169.254.169.254/latest/meta-data/iam/security-credentials",
  533. imdsv2_session_token_url": "http://169.254.169.254/latest/api/token"
  534. }
  535. aws_security_credentials_supplier (Optional [AwsSecurityCredentialsSupplier]): Optional AWS security credentials supplier.
  536. This will be called to supply valid AWS security credentails which will then
  537. be exchanged for Google access tokens. Either an AWS security credentials supplier
  538. or a credential source must be provided.
  539. args (List): Optional positional arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  540. kwargs (Mapping): Optional keyword arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
  541. Raises:
  542. google.auth.exceptions.RefreshError: If an error is encountered during
  543. access token retrieval logic.
  544. ValueError: For invalid parameters.
  545. .. note:: Typically one of the helper constructors
  546. :meth:`from_file` or
  547. :meth:`from_info` are used instead of calling the constructor directly.
  548. """
  549. super(Credentials, self).__init__(
  550. audience=audience,
  551. subject_token_type=subject_token_type,
  552. token_url=token_url,
  553. credential_source=credential_source,
  554. *args,
  555. **kwargs
  556. )
  557. if credential_source is None and aws_security_credentials_supplier is None:
  558. raise exceptions.InvalidValue(
  559. "A valid credential source or AWS security credentials supplier must be provided."
  560. )
  561. if (
  562. credential_source is not None
  563. and aws_security_credentials_supplier is not None
  564. ):
  565. raise exceptions.InvalidValue(
  566. "AWS credential cannot have both a credential source and an AWS security credentials supplier."
  567. )
  568. if aws_security_credentials_supplier:
  569. self._aws_security_credentials_supplier = aws_security_credentials_supplier
  570. # The regional cred verification URL would normally be provided through the credential source. So set it to the default one here.
  571. self._cred_verification_url = (
  572. _DEFAULT_AWS_REGIONAL_CREDENTIAL_VERIFICATION_URL
  573. )
  574. else:
  575. environment_id = credential_source.get("environment_id") or ""
  576. self._aws_security_credentials_supplier = _DefaultAwsSecurityCredentialsSupplier(
  577. credential_source
  578. )
  579. self._cred_verification_url = credential_source.get(
  580. "regional_cred_verification_url"
  581. )
  582. # Get the environment ID, i.e. "aws1". Currently, only one version supported (1).
  583. matches = re.match(r"^(aws)([\d]+)$", environment_id)
  584. if matches:
  585. env_id, env_version = matches.groups()
  586. else:
  587. env_id, env_version = (None, None)
  588. if env_id != "aws" or self._cred_verification_url is None:
  589. raise exceptions.InvalidResource(
  590. "No valid AWS 'credential_source' provided"
  591. )
  592. elif env_version is None or int(env_version) != 1:
  593. raise exceptions.InvalidValue(
  594. "aws version '{}' is not supported in the current build.".format(
  595. env_version
  596. )
  597. )
  598. self._target_resource = audience
  599. self._request_signer = None
  600. def retrieve_subject_token(self, request):
  601. """Retrieves the subject token using the credential_source object.
  602. The subject token is a serialized `AWS GetCallerIdentity signed request`_.
  603. The logic is summarized as:
  604. Retrieve the AWS region from the AWS_REGION or AWS_DEFAULT_REGION
  605. environment variable or from the AWS metadata server availability-zone
  606. if not found in the environment variable.
  607. Check AWS credentials in environment variables. If not found, retrieve
  608. from the AWS metadata server security-credentials endpoint.
  609. When retrieving AWS credentials from the metadata server
  610. security-credentials endpoint, the AWS role needs to be determined by
  611. calling the security-credentials endpoint without any argument. Then the
  612. credentials can be retrieved via: security-credentials/role_name
  613. Generate the signed request to AWS STS GetCallerIdentity action.
  614. Inject x-goog-cloud-target-resource into header and serialize the
  615. signed request. This will be the subject-token to pass to GCP STS.
  616. .. _AWS GetCallerIdentity signed request:
  617. https://cloud.google.com/iam/docs/access-resources-aws#exchange-token
  618. Args:
  619. request (google.auth.transport.Request): A callable used to make
  620. HTTP requests.
  621. Returns:
  622. str: The retrieved subject token.
  623. """
  624. # Initialize the request signer if not yet initialized after determining
  625. # the current AWS region.
  626. if self._request_signer is None:
  627. self._region = self._aws_security_credentials_supplier.get_aws_region(
  628. self._supplier_context, request
  629. )
  630. self._request_signer = RequestSigner(self._region)
  631. # Retrieve the AWS security credentials needed to generate the signed
  632. # request.
  633. aws_security_credentials = self._aws_security_credentials_supplier.get_aws_security_credentials(
  634. self._supplier_context, request
  635. )
  636. # Generate the signed request to AWS STS GetCallerIdentity API.
  637. # Use the required regional endpoint. Otherwise, the request will fail.
  638. request_options = self._request_signer.get_request_options(
  639. aws_security_credentials,
  640. self._cred_verification_url.replace("{region}", self._region),
  641. "POST",
  642. )
  643. # The GCP STS endpoint expects the headers to be formatted as:
  644. # [
  645. # {key: 'x-amz-date', value: '...'},
  646. # {key: 'Authorization', value: '...'},
  647. # ...
  648. # ]
  649. # And then serialized as:
  650. # quote(json.dumps({
  651. # url: '...',
  652. # method: 'POST',
  653. # headers: [{key: 'x-amz-date', value: '...'}, ...]
  654. # }))
  655. request_headers = request_options.get("headers")
  656. # The full, canonical resource name of the workload identity pool
  657. # provider, with or without the HTTPS prefix.
  658. # Including this header as part of the signature is recommended to
  659. # ensure data integrity.
  660. request_headers["x-goog-cloud-target-resource"] = self._target_resource
  661. # Serialize AWS signed request.
  662. aws_signed_req = {}
  663. aws_signed_req["url"] = request_options.get("url")
  664. aws_signed_req["method"] = request_options.get("method")
  665. aws_signed_req["headers"] = []
  666. # Reformat header to GCP STS expected format.
  667. for key in request_headers.keys():
  668. aws_signed_req["headers"].append(
  669. {"key": key, "value": request_headers[key]}
  670. )
  671. return urllib.parse.quote(
  672. json.dumps(aws_signed_req, separators=(",", ":"), sort_keys=True)
  673. )
  674. def _create_default_metrics_options(self):
  675. metrics_options = super(Credentials, self)._create_default_metrics_options()
  676. metrics_options["source"] = "aws"
  677. if self._has_custom_supplier():
  678. metrics_options["source"] = "programmatic"
  679. return metrics_options
  680. def _has_custom_supplier(self):
  681. return self._credential_source is None
  682. def _constructor_args(self):
  683. args = super(Credentials, self)._constructor_args()
  684. # If a custom supplier was used, append it to the args dict.
  685. if self._has_custom_supplier():
  686. args.update(
  687. {
  688. "aws_security_credentials_supplier": self._aws_security_credentials_supplier
  689. }
  690. )
  691. return args
  692. @classmethod
  693. def from_info(cls, info, **kwargs):
  694. """Creates an AWS Credentials instance from parsed external account info.
  695. Args:
  696. info (Mapping[str, str]): The AWS external account info in Google
  697. format.
  698. kwargs: Additional arguments to pass to the constructor.
  699. Returns:
  700. google.auth.aws.Credentials: The constructed credentials.
  701. Raises:
  702. ValueError: For invalid parameters.
  703. """
  704. aws_security_credentials_supplier = info.get(
  705. "aws_security_credentials_supplier"
  706. )
  707. kwargs.update(
  708. {"aws_security_credentials_supplier": aws_security_credentials_supplier}
  709. )
  710. return super(Credentials, cls).from_info(info, **kwargs)
  711. @classmethod
  712. def from_file(cls, filename, **kwargs):
  713. """Creates an AWS Credentials instance from an external account json file.
  714. Args:
  715. filename (str): The path to the AWS external account json file.
  716. kwargs: Additional arguments to pass to the constructor.
  717. Returns:
  718. google.auth.aws.Credentials: The constructed credentials.
  719. """
  720. return super(Credentials, cls).from_file(filename, **kwargs)