Added support for bucket policy - #241
Merged
Merged
Conversation
akhil-sumologic
commented
Jul 28, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds S3 bucket policy update support intended to safely append log-delivery statements (CloudTrail/ALB/ELB) without overwriting existing bucket policies.
Changes:
- Introduces a shared
_apply_bucket_policyhelper to append policy statements bySidwith basic retry handling. - Refactors ALB/ELB/VPC log-enablement flows to call the shared bucket-policy helper.
- Adds a new CloudFormation custom resource
Custom::AddBucketPolicyto manage bucket policy statements by service type.
Comments suppressed due to low confidence (2)
sumologic-app-utils/src/awsresource.py:1406
_add_policyassumesexisting_policy["Statement"]is always a list, but IAM-style policies can legally have a single statement object. Normalizing to a list avoids iterating over dict keys and generating incorrectexisting_sids.
existing_sids = {s.get("Sid") for s in existing_policy["Statement"] if s.get("Sid")}
sumologic-app-utils/src/awsresource.py:1438
- Like
_add_policy,_remove_policyassumesStatementis always a list; normalize to a list before filtering to avoid iterating a dict (and to ensure the resulting policy JSON is valid).
existing_policy["Statement"] = [
s for s in existing_policy["Statement"] if s.get("Sid") not in our_sids
]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (4)
sumologic-app-utils/src/awsresource.py:532
existing_policy["Statement"]is assumed to be a list of dicts, but IAM/S3 policies allowStatementto be either a single dict or a list. If a bucket has a single-statement policy, this comprehension/append path will raise (e.g., iterating a dict yields keys, and strings don’t have.get). NormalizeStatementto a list before iterating/appending.
existing_sids = {s.get("Sid") for s in existing_policy["Statement"] if s.get("Sid")}
for stmt in statements:
if stmt.get("Sid") not in existing_sids:
existing_policy["Statement"].append(stmt)
try:
sumologic-app-utils/src/awsresource.py:1411
- Same
Statementshape issue here:_add_policyassumesexisting_policy["Statement"]is a list of dicts, but it can be a single dict. This will break SID detection andappendwhenStatementis not a list. Normalize before iterating/appending.
existing_sids = {s.get("Sid") for s in existing_policy["Statement"] if s.get("Sid")}
added = []
for stmt in expected_stmts:
if stmt["Sid"] not in existing_sids:
existing_policy["Statement"].append(stmt)
added.append(stmt["Sid"])
sumologic-app-utils/src/awsresource.py:1427
- The post-write verification also assumes
verify["Statement"]is a list; if it is a dict,{s.get("Sid") for s in verify["Statement"]}will raise. Normalize the verificationStatementvalue too.
verify = json.loads(s3.get_bucket_policy(Bucket=bucket_name)["Policy"])
current_sids = {s.get("Sid") for s in verify["Statement"]}
if not expected_sids.issubset(current_sids):
sumologic-app-utils/src/awsresource.py:1447
_remove_policyassumesexisting_policy["Statement"]is a list of dicts, but it can be a single dict. In that case the filter comprehension will iterate keys and fail on.get. NormalizeStatementto a list before filtering.
existing_policy["Statement"] = [
s for s in existing_policy["Statement"] if s.get("Sid") not in our_sids
]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.