@@ -279,32 +279,36 @@ def get_boto_credentials(self, *_, **__) -> Credentials | None:
279279 def get_aws_credentials (self , * _ , ** __ ) -> AwsCredentials | None :
280280 return self .util_creds
281281
282- def sign_request (self , request : AWSRequest ):
283- # Generate a proper-looking authorization header that matches what the real signer would produce
282+ def sign_request (self , request : AWSRequest ) -> None :
283+ """
284+ Fake replacement for botocore SigV4Auth.add_auth that produces the same
285+ *static* parts of the Authorization header (everything before
286+ `Signature=`).
287+ """
288+ # Add the headers a real signer would inject
284289 utc_now = datetime .datetime .utcnow ()
285290 amz_date = utc_now .strftime ("%Y%m%dT%H%M%SZ" )
286- date_string = utc_now .strftime ("%Y%m%d" )
291+ date_stamp = utc_now .strftime ("%Y%m%d" )
287292
288- # Add the same headers that the real signer would add
289- request . headers . add_header ( "X-Amz-Date" , amz_date )
290- request .headers . add_header ( "X-Amz-Security-Token" , self .util_creds .token )
293+ request . headers [ "X-Amz-Date" ] = amz_date
294+ if self . util_creds . token :
295+ request .headers [ "X-Amz-Security-Token" ] = self .util_creds .token
291296
292- # Generate signed headers list that matches what the real signer would include
293- header_keys = []
294- for key in sorted (request .headers .keys (), key = str .lower ):
295- header_keys .append (key .lower ())
297+ # Host header is already set by the test; add it if a future test forgets
298+ if "Host" not in request .headers :
299+ request .headers ["Host" ] = urlparse (request .url ).netloc
296300
297- signed_headers = ";" . join ( header_keys )
298- credential_scope = f" { date_string } / { self . region } /sts/aws4_request"
301+ # Build the signed-headers list
302+ signed_headers = ";" . join ( sorted ( h . lower () for h in request . headers . keys ()))
299303
300- authorization = (
301- f"AWS4-HMAC-SHA256 "
304+ credential_scope = f"{ date_stamp } /{ self .region } /sts/aws4_request"
305+
306+ request .headers ["Authorization" ] = (
307+ "AWS4-HMAC-SHA256 "
302308 f"Credential={ self .util_creds .access_key } /{ credential_scope } , "
303309 f"SignedHeaders={ signed_headers } , Signature=<sig>"
304310 )
305311
306- request .headers .add_header ("Authorization" , authorization )
307-
308312 def __enter__ (self ):
309313 # Preserve existing env and then set creds/region for util fallback
310314 self ._old_env = {
0 commit comments