Skip to content

Commit 575a4b6

Browse files
nobodynatenobodynate
andauthored
fix: derive region from bucket in cfn__resource_injection (closes #484) (#498)
* fix: update mypy to ^1.0 to support Python 3.11+ typed-ast 1.4.3 (a mypy <0.900 dependency) fails to build on Python 3.11+ due to C23 keyword conflicts. mypy 1.0+ removed the typed-ast dependency. * fix: derive region from bucket in cfn__resource_injection (closes #484) Region is now resolved once from the bucket name immediately after bucket is known, and passed explicitly to attacker_sess, lambda_env, and s3_notifications_sess. This ensures all resources (Lambda, S3 notification, boto3 sessions) target the same region rather than falling back to us-east-1. Supersedes #497. * fix: add mypy config to suppress errors from stricter mypy 1.x defaults * fix: disable mypy 1.x error codes that surface pre-existing sqlalchemy stub issues * fix: pin botocore <1.32 to avoid moto 2.x incompatibility with newer checksum behavior * update lockfile --------- Co-authored-by: nobodynate <nate.wilson@rhinosecuritylabs.com>
1 parent 6e6c58f commit 575a4b6

File tree

3 files changed

+1684
-1053
lines changed

3 files changed

+1684
-1053
lines changed

pacu/modules/cfn__resource_injection/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,14 @@ def main(args, pacu_main: 'Main'):
181181
sess = get_aws_key_by_name(pacu_main, args.s3_access_key, 'us-east-1')
182182
bucket = get_bucket_name(sess.resource('s3'), lambda_dir)
183183

184+
region = get_region(bucket, pacu_main.get_regions('lambda'))
185+
attacker_sess = get_session_from_key_name(pacu_main, args.attacker_key, region)
186+
184187
deploy_key: 'AWSKey' = pacu_main.get_aws_key_by_alias(args.attacker_key)
185188
if not deploy_key:
186189
print(f"Did not find the key {args.attacker_key} in pacu, make sure to set this with `set_keys` first.")
187190

188-
env = lambda_env(pacu_main, bucket, deploy_key)
191+
env = lambda_env(deploy_key, region)
189192

190193
deploy_dir = (lambda_dir / bucket)
191194
if args.delete:
@@ -195,7 +198,6 @@ def main(args, pacu_main: 'Main'):
195198
s3_access_key: 'AWSKey' = pacu_main.get_aws_key_by_alias_from_db(args.s3_access_key)
196199
deploy_lambda(pacu_main, env, deploy_dir, bucket, principal, s3_access_key)
197200

198-
region = get_region(bucket, pacu_main.get_regions('lambda'))
199201
s3_notifications_sess = get_aws_key_by_name(pacu_main, s3_notifications_setup_key, region)
200202

201203
# No need to remove this on args.delete since we are deleting the lambda either way.
@@ -226,15 +228,15 @@ def main(args, pacu_main: 'Main'):
226228
return msg
227229

228230

229-
def lambda_env(pacu: 'Main', bucket: str, key: 'AWSKey'):
231+
def lambda_env(key: 'AWSKey', region: str):
230232
env = {
231233
"AWS_ACCESS_KEY_ID": key.access_key_id,
232234
"AWS_SECRET_ACCESS_KEY": key.secret_access_key,
233235
"PATH": os.environ["PATH"],
234236
}
235237
if key.session_token:
236238
env["AWS_SESSION_TOKEN"] = key.session_token
237-
env["AWS_DEFAULT_REGION"] = get_region(bucket, pacu.get_regions('lambda'))
239+
env["AWS_DEFAULT_REGION"] = region
238240
return env
239241

240242

0 commit comments

Comments
 (0)