11from datetime import datetime , timezone
22import hashlib
3+ import traceback
34from typing import Tuple
45import urllib .parse
56
@@ -42,6 +43,7 @@ def get_access_token(headers: Headers) -> Tuple[str, str]:
4243 (str, str): the user's access token or "" if not found, and the user's ID if the token is
4344 a client_credentials token
4445 """
46+ # TODO unit tests for this function
4547 auth_header = headers .get ("authorization" )
4648 if not auth_header :
4749 return "" , ""
@@ -51,13 +53,15 @@ def get_access_token(headers: Headers) -> Tuple[str, str]:
5153 raise HTTPException (HTTP_401_UNAUTHORIZED , err_msg )
5254 try :
5355 if "Credential=" in auth_header : # format 1 (see docstring)
54- access_key_id = auth_header .split ("Credential=" )[1 ].split ("/" )[0 ]
56+ access_token = auth_header .split ("Credential=" )[1 ].split ("/" )[0 ]
57+ user_id = None
5558 else : # format 2 (see docstring)
5659 access_key_id = auth_header .split ("AWS " )[1 ]
5760 access_key_id = ":" .join (access_key_id .split (":" )[:- 1 ])
58- access_token , user_id = access_key_id .split (";userId=" )
61+ access_token , user_id = access_key_id .split (";userId=" )
5962 return access_token , user_id
6063 except Exception as e :
64+ traceback .print_exc ()
6165 logger .error (
6266 f"Unexpected format; unable to extract access token from authorization header: { e } "
6367 )
@@ -257,11 +261,17 @@ async def s3_endpoint(path: str, request: Request):
257261 logger .error (f"Error from AWS: { response .status_code } { response .text } " )
258262
259263 # return the response from AWS S3.
260- # mask the details of 403 errors from the end user: authentication is done internally by this
264+ # - mask the details of 403 errors from the end user: authentication is done internally by this
261265 # function, so 403 errors are internal service errors
262- resp_contents = response .content if response .status_code != 403 else None
266+ # - return all the headers from the AWS response, except `x-amz-bucket-region` which for some
267+ # reason causes this error for tasks ran through Nextflow: `The AWS Access Key Id you provided
268+ # does not exist in our records`
263269 return Response (
264- content = resp_contents ,
270+ content = (
271+ response .content if response .status_code != HTTP_403_FORBIDDEN else None
272+ ),
265273 status_code = response .status_code ,
266- headers = response .headers ,
274+ headers = {
275+ k : v for k , v in response .headers .items () if k != "x-amz-bucket-region"
276+ },
267277 )
0 commit comments