aws.py 29 KB

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