Skip to content

Commit a24191f

Browse files
nadersipnsipahy
andauthored
Fix report csv (#127)
* Fix report csv * adding comment --------- Co-authored-by: Nader <[email protected]>
1 parent d4faecd commit a24191f

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

arch/templates/EvidenceCollectionComponents.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,15 @@ Resources:
235235
- "s3:GetObject"
236236
- "s3:CreateBucket"
237237
- "s3:ListBucket"
238+
- "s3:DeleteObject"
238239
Resource: !Sub "arn:aws:s3:::gc-fedclient-${AWS::AccountId}-${AWS::Region}/*"
239240
- Effect: Allow
240241
Action:
241242
- "s3:PutObject"
242243
- "s3:GetObject"
243244
- "s3:CreateBucket"
244245
- "s3:ListBucket"
246+
- "s3:DeleteObject"
245247
Resource: !Sub "arn:aws:s3:::gc-fedclient-${AWS::AccountId}-${AWS::Region}"
246248
- Effect: Allow
247249
Action:

src/lambda/aws_bucket_watcher/app.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ def lambda_handler(event, context):
1919
key = event["Records"][0]["s3"]["object"]["key"]
2020
s3_resource = boto3.resource("s3")
2121
copy_source = {"Bucket": bucket, "Key": key}
22+
23+
#print(key)
2224

2325
#Todo: Not sure why this exist, its not being used anywhere?
2426
#account_id = context.invoked_function_arn.split(":")[4]
2527
target_key = f"{org_id}/{key}"
2628
logger.info("Attempting to copy audit data to GC managed s3 bucket: %s", target_key)
27-
28-
s3_resource.Bucket(os.environ["target_bucket"]).Object(target_key).copy(
29-
copy_source, ExtraArgs={"ACL": "bucket-owner-full-control"}
30-
)
29+
30+
if key.startswith("chunks/") or key.startswith("state/"):
31+
pass
32+
else:
33+
s3_resource.Bucket(os.environ["target_bucket"]).Object(target_key).copy(
34+
copy_source, ExtraArgs={"ACL": "bucket-owner-full-control"}
35+
)

src/lambda/aws_compile_audit_report/app.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,32 @@ def create_boto3_clients():
6262

6363
def lambda_handler(event, context):
6464
logger.info("Lambda invocation started (structured).")
65-
65+
6666
clients = create_boto3_clients()
6767

68+
# Delete state file before proceeding
69+
try:
70+
s3 = boto3.client("s3")
71+
objects = s3.list_objects_v2(Bucket=config["SOURCE_TARGET_BUCKET"], Prefix="state")
72+
if 'Contents' in objects:
73+
for obj in objects['Contents']:
74+
s3.delete_object(Bucket=config["SOURCE_TARGET_BUCKET"], Key=obj["Key"])
75+
# logger.info(f"Delete complete{obj}")
76+
except Exception as e:
77+
logger.info("Failed to delete S3 states folder: %s", str(e))
78+
79+
# Delete Chunk files before proceeding
80+
81+
try:
82+
s3 = boto3.client("s3")
83+
objects = s3.list_objects_v2(Bucket=config["SOURCE_TARGET_BUCKET"], Prefix="chunks")
84+
if 'Contents' in objects:
85+
for obj in objects['Contents']:
86+
s3.delete_object(Bucket=config["SOURCE_TARGET_BUCKET"], Key=obj["Key"])
87+
# logger.info(f"Delete complete{obj}")
88+
except Exception as e:
89+
logger.info("Failed to delete S3 chunks folder: %s", str(e))
90+
6891
# Handle concurrency limit
6992
current_concurrency = event.get("current_concurrency", 1)
7093
if current_concurrency > config["MAX_CONCURRENCY"]:
@@ -149,6 +172,9 @@ def process_assessments(event, context, current_concurrency, clients):
149172
with open(chunk_file_local, "w", newline="") as csvfile:
150173
writer = csv.writer(csvfile)
151174
writer.writerow(OUTPUT_HEADER)
175+
# Log the creation of the chunk file
176+
logger.info(f"Created chunk file at {chunk_file_local}")
177+
152178

153179
all_evidence_pages = get_all_evidence_paginated(
154180
clients["auditmanager"], assessment_id, control_set_id, folder_id

0 commit comments

Comments
 (0)