Skip to content

Commit 37cd351

Browse files
zollmanphobologic
authored andcommitted
Use default ACL for uploaded lambda code (#682)
* Use default ACL for uploaded lambda code The "Authenticated-Read" ACL, currently set on all uploads, allows your code to be read by all S3 users. Default behavior should be to use the permissions implied by the bucket policy, i.e. "private". Organizations that do not grant SetObjectAcl permissions (for fear of data loss) will block this call. * Add config option to make default uploads private Per PR#682, although the default can be changed to 'private', we should allow users to set 'authenticated-read' if they desire. Adds a new configuration option, payload_acl, to define this.
1 parent fe0086c commit 37cd351

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

stacker/hooks/aws_lambda.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def _head_object(s3_conn, bucket, key):
194194
raise
195195

196196

197-
def _upload_code(s3_conn, bucket, prefix, name, contents, content_hash):
197+
def _upload_code(s3_conn, bucket, prefix, name, contents, content_hash,
198+
payload_acl):
198199
"""Upload a ZIP file to S3 for use by Lambda.
199200
200201
The key used for the upload will be unique based on the checksum of the
@@ -210,6 +211,8 @@ def _upload_code(s3_conn, bucket, prefix, name, contents, content_hash):
210211
construct a key name for the uploaded file.
211212
contents (str): byte string with the content of the file upload.
212213
content_hash (str): md5 hash of the contents to be uploaded.
214+
payload_acl (str): The canned S3 object ACL to be applied to the
215+
uploaded payload
213216
214217
Returns:
215218
troposphere.awslambda.Code: CloudFormation Lambda Code object,
@@ -229,7 +232,7 @@ def _upload_code(s3_conn, bucket, prefix, name, contents, content_hash):
229232
logger.info('lambda: uploading object %s', key)
230233
s3_conn.put_object(Bucket=bucket, Key=key, Body=contents,
231234
ContentType='application/zip',
232-
ACL='authenticated-read')
235+
ACL=payload_acl)
233236

234237
return Code(S3Bucket=bucket, S3Key=key)
235238

@@ -269,7 +272,8 @@ def _check_pattern_list(patterns, key, default=None):
269272
'list of strings'.format(key))
270273

271274

272-
def _upload_function(s3_conn, bucket, prefix, name, options, follow_symlinks):
275+
def _upload_function(s3_conn, bucket, prefix, name, options, follow_symlinks,
276+
payload_acl):
273277
"""Builds a Lambda payload from user configuration and uploads it to S3.
274278
275279
Args:
@@ -292,6 +296,8 @@ def _upload_function(s3_conn, bucket, prefix, name, options, follow_symlinks):
292296
file patterns to exclude from the payload (optional).
293297
follow_symlinks (bool): If true, symlinks will be included in the
294298
resulting zip file
299+
payload_acl (str): The canned S3 object ACL to be applied to the
300+
uploaded payload
295301
296302
Returns:
297303
troposphere.awslambda.Code: CloudFormation AWS Lambda Code object,
@@ -326,7 +332,7 @@ def _upload_function(s3_conn, bucket, prefix, name, options, follow_symlinks):
326332
follow_symlinks)
327333

328334
return _upload_code(s3_conn, bucket, prefix, name, zip_contents,
329-
content_hash)
335+
content_hash, payload_acl)
330336

331337

332338
def select_bucket_region(custom_bucket, hook_region, stacker_bucket_region,
@@ -385,6 +391,8 @@ def upload_lambda_functions(context, provider, **kwargs):
385391
zip name.
386392
follow_symlinks (bool, optional): Will determine if symlinks should
387393
be followed and included with the zip artifact. Default: False
394+
payload_acl (str, optional): The canned S3 object ACL to be applied to
395+
the uploaded payload. Default: private
388396
functions (dict):
389397
Configurations of desired payloads to build. Keys correspond to
390398
function names, used to derive key names for the payload. Each
@@ -438,6 +446,7 @@ def upload_lambda_functions(context, provider, **kwargs):
438446
bucket: custom-bucket
439447
follow_symlinks: true
440448
prefix: cloudformation-custom-resources/
449+
payload_acl: authenticated-read
441450
functions:
442451
MyFunction:
443452
path: ./lambda_functions
@@ -494,6 +503,10 @@ def create_template(self):
494503
if not isinstance(follow_symlinks, bool):
495504
raise ValueError('follow_symlinks option must be a boolean')
496505

506+
# Check for S3 object acl. Valid values from:
507+
# https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
508+
payload_acl = kwargs.get('payload_acl', 'private')
509+
497510
# Always use the global client for s3
498511
session = get_session(bucket_region)
499512
s3_client = session.client('s3')
@@ -505,6 +518,6 @@ def create_template(self):
505518
results = {}
506519
for name, options in kwargs['functions'].items():
507520
results[name] = _upload_function(s3_client, bucket_name, prefix, name,
508-
options, follow_symlinks)
521+
options, follow_symlinks, payload_acl)
509522

510523
return results

0 commit comments

Comments
 (0)