1515ED_USER = ED_PASS = None
1616EDL_USER_TOKEN : dict [str , str ] = {}
1717HARMONY_CLIENT : Client | None = None
18+ AWS_ACCOUNT_ID : str | None = None
1819
1920HARMONY_SHOULD_VALIDATE_AUTH = os .environ .get ('HARMONY_SHOULD_VALIDATE_AUTH' , default = 'False' ).upper () == 'TRUE'
2021
2122
23+ def get_aws_account_id () -> str :
24+ """
25+ Get and cache the current AWS account ID via STS.
26+
27+ Returns
28+ -------
29+ str
30+ The AWS account ID for the current caller identity
31+ """
32+ global AWS_ACCOUNT_ID # pylint: disable=W0603
33+ if not AWS_ACCOUNT_ID :
34+ sts_client = boto3 .client ('sts' )
35+ AWS_ACCOUNT_ID = sts_client .get_caller_identity ()['Account' ]
36+ return AWS_ACCOUNT_ID
37+
38+
2239def get_edl_creds () -> tuple [str , str ]:
2340 """
2441 Get EDL username and password from SSM.
@@ -215,7 +232,8 @@ def upload_string_as_object(bucket_name: str, key_name: str, object_content: str
215232 s3_client .put_object (
216233 Body = object_content .encode (),
217234 Bucket = bucket_name ,
218- Key = key_name
235+ Key = key_name ,
236+ ExpectedBucketOwner = get_aws_account_id ()
219237 )
220238 return f's3://{ bucket_name } /{ key_name } '
221239
@@ -235,6 +253,7 @@ def upload_object(
235253 Key = key ,
236254 Body = body_content ,
237255 ContentType = content_type ,
256+ ExpectedBucketOwner = get_aws_account_id ()
238257 )
239258 return f's3://{ bucket } /{ key } '
240259
@@ -258,7 +277,8 @@ def upload_to_s3(filepath: pathlib.Path, bucket_name: str, object_key: str):
258277 s3 uri of new object
259278 """
260279 s3_client = boto3 .client ('s3' )
261- s3_client .upload_file (str (filepath ), bucket_name , object_key )
280+ s3_client .upload_file (str (filepath ), bucket_name , object_key ,
281+ ExtraArgs = {'ExpectedBucketOwner' : get_aws_account_id ()})
262282
263283 return f's3://{ bucket_name } /{ object_key } '
264284
0 commit comments