Skip to content

Commit ec20c30

Browse files
committed
fix flake8 issues in config rules
1 parent ec522c0 commit ec20c30

File tree

1 file changed

+52
-12
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/rules/sra_bedrock_check_eval_job_bucket

1 file changed

+52
-12
lines changed

aws_sra_examples/solutions/genai/bedrock_org/lambda/rules/sra_bedrock_check_eval_job_bucket/app.py

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
"""Config rule to check the eval job S3 bucket for Bedrock environemts.
2+
3+
Version: 1.0
4+
5+
Config rule for SRA in the repo, https://github.com/aws-samples/aws-security-reference-architecture-examples
6+
7+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
8+
SPDX-License-Identifier: MIT-0
9+
"""
110
from typing import Any
211
import boto3
3-
import json
412
from botocore.exceptions import ClientError
513
from datetime import datetime
614
import logging
7-
import os # maybe not needed for logging
15+
import os
816
import ast
917

1018
# Set to True to get the lambda to assume the Role attached on the Config Service (useful for cross-account).
@@ -22,11 +30,19 @@
2230
SERVICE_NAME = "bedrock.amazonaws.com"
2331

2432

25-
def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]:
33+
def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa: U100, CCR001, C901
34+
"""Evaluate the S3 bucket for the compliance.
35+
36+
Args:
37+
event (dict): The AWS Config event
38+
context (Any): The AWS Lambda context
39+
40+
Returns:
41+
tuple[str, str]: The compliance status and annotation
42+
"""
2643
LOGGER.info(f"Evaluate Compliance Event: {event}")
2744
# Initialize AWS clients
2845
s3 = boto3.client('s3')
29-
config = boto3.client('config')
3046

3147
# Get rule parameters
3248
params = ast.literal_eval(event['ruleParameters'])
@@ -39,9 +55,6 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]:
3955
check_versioning = params.get('CheckVersioning', 'true').lower() != 'false'
4056

4157
# Check if the bucket exists
42-
# try:
43-
# s3.head_bucket(Bucket=bucket_name)
44-
# except ClientError as e:
4558
if not check_bucket_exists(bucket_name):
4659
return build_evaluation('NOT_APPLICABLE', f"Bucket {bucket_name} does not exist or is not accessible")
4760

@@ -98,25 +111,52 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]:
98111
annotation_str = '; '.join(annotation) if annotation else "All checked features are compliant"
99112
return build_evaluation(compliance_type, annotation_str)
100113

114+
101115
def check_bucket_exists(bucket_name: str) -> Any:
116+
"""Check if the bucket exists and is accessible.
117+
118+
Args:
119+
bucket_name (str): The name of the bucket to check
120+
121+
Returns:
122+
Any: True if the bucket exists and is accessible, False otherwise
123+
"""
102124
s3 = boto3.client('s3')
103125
try:
104126
response = s3.list_buckets()
105127
buckets = [bucket['Name'] for bucket in response['Buckets']]
106128
return bucket_name in buckets
107129
except ClientError as e:
108-
print(f"An error occurred: {e}")
130+
LOGGER.info(f"An error occurred: {e}")
109131
return False
110132

133+
111134
def build_evaluation(compliance_type: str, annotation: str) -> Any:
135+
"""Build the evaluation compliance type and annotation.
136+
137+
Args:
138+
compliance_type (str): The compliance type
139+
annotation (str): the annotation
140+
141+
Returns:
142+
Any: The evaluation compliance type and annotation
143+
"""
112144
LOGGER.info(f"Build Evaluation Compliance Type: {compliance_type} Annotation: {annotation}")
113145
return {
114146
'ComplianceType': compliance_type,
115147
'Annotation': annotation,
116148
'OrderingTimestamp': datetime.now().isoformat()
117149
}
118150

151+
119152
def lambda_handler(event: dict, context: Any) -> None:
153+
"""Lambda handler.
154+
155+
Args:
156+
event (dict): The AWS Config event
157+
context (Any): The AWS Lambda context
158+
"""
159+
LOGGER.info(f"Lambda Handler Context: {context}")
120160
LOGGER.info(f"Lambda Handler Event: {event}")
121161
evaluation = evaluate_compliance(event, context)
122162
config = boto3.client('config')
@@ -126,10 +166,10 @@ def lambda_handler(event: dict, context: Any) -> None:
126166
{
127167
'ComplianceResourceType': 'AWS::S3::Bucket',
128168
'ComplianceResourceId': params.get('BucketName'),
129-
'ComplianceType': evaluation['ComplianceType'], # type: ignore
130-
'Annotation': evaluation['Annotation'], # type: ignore
131-
'OrderingTimestamp': evaluation['OrderingTimestamp'] # type: ignore
169+
'ComplianceType': evaluation['ComplianceType'], # type: ignore
170+
'Annotation': evaluation['Annotation'], # type: ignore
171+
'OrderingTimestamp': evaluation['OrderingTimestamp'] # type: ignore
132172
}
133173
],
134174
ResultToken=event['resultToken']
135-
)
175+
)

0 commit comments

Comments
 (0)