Skip to content

Commit 8ddd836

Browse files
committed
update error handing for rules
1 parent 83e9284 commit 8ddd836

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def evaluate_compliance(rule_parameters):
5858

5959
except Exception as e:
6060
LOGGER.error(f"Error evaluating Bedrock Model Invocation Logging configuration: {str(e)}")
61-
return 'ERROR', f"Error evaluating compliance: {str(e)}"
61+
return 'INSUFFICIENT_DATA', f"Error evaluating compliance: {str(e)}"
6262

6363
def lambda_handler(event, context):
6464
LOGGER.info('Evaluating compliance for AWS Config rule')

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44
import logging
5+
import botocore
56

67
# Setup Default Logger
78
LOGGER = logging.getLogger(__name__)
@@ -33,10 +34,11 @@ def evaluate_compliance(rule_parameters):
3334
logging_config = response.get('loggingConfig', {})
3435

3536
s3_config = logging_config.get('s3Config', {})
36-
s3_enabled = s3_config.get('enabled', False)
37-
bucket_name = s3_config.get('s3BucketName')
37+
LOGGER.info(f"Bedrock Model Invocation S3 config: {s3_config}")
38+
bucket_name = s3_config.get('bucketName', "")
39+
LOGGER.info(f"Bedrock Model Invocation S3 bucketName: {bucket_name}")
3840

39-
if not s3_enabled or not bucket_name:
41+
if not s3_config or not bucket_name:
4042
return 'NON_COMPLIANT', "S3 logging is not enabled for Bedrock Model Invocation Logging"
4143

4244
# Check S3 bucket configurations
@@ -57,15 +59,31 @@ def evaluate_compliance(rule_parameters):
5759
if 'LoggingEnabled' not in logging:
5860
issues.append("server access logging not enabled")
5961

60-
if check_object_locking:
61-
object_lock = s3_client.get_object_lock_configuration(Bucket=bucket_name)
62-
if 'ObjectLockConfiguration' not in object_lock:
63-
issues.append("object locking not enabled")
64-
6562
if check_versioning:
6663
versioning = s3_client.get_bucket_versioning(Bucket=bucket_name)
6764
if versioning.get('Status') != 'Enabled':
6865
issues.append("versioning not enabled")
66+
try:
67+
if check_object_locking:
68+
object_lock = s3_client.get_object_lock_configuration(Bucket=bucket_name)
69+
if 'ObjectLockConfiguration' not in object_lock:
70+
issues.append("object locking not enabled")
71+
except botocore.exceptions.ClientError as error:
72+
error_code = error.response['Error']['Code']
73+
if error_code == "ObjectLockConfigurationNotFoundError":
74+
LOGGER.info(f"Object Lock is not enabled for S3 bucket: {bucket_name}")
75+
issues.append("object locking not enabled")
76+
else:
77+
LOGGER.info(f"Error evaluating Object Lock configuration: {str(error)}")
78+
return 'INSUFFICIENT_DATA', f"Error evaluating Object Lock configuration: {str(error)}"
79+
# except Exception as error:
80+
# error_code = type(error).__name__
81+
# if error_code == "ObjectLockConfigurationNotFoundError":
82+
# LOGGER.info(f"Object Lock is not enabled for S3 bucket: {bucket_name}")
83+
# return 'NON_COMPLIANT', f"Object Lock is not enabled for S3 bucket: {bucket_name}"
84+
# else:
85+
# LOGGER.error(f"Error evaluating Object Lock configuration: {str(error)}")
86+
# return 'INSUFFICIENT_DATA', f"Error evaluating Object Lock configuration: {str(error)}"
6987

7088
if issues:
7189
return 'NON_COMPLIANT', f"S3 logging enabled but {', '.join(issues)}"
@@ -74,7 +92,7 @@ def evaluate_compliance(rule_parameters):
7492

7593
except Exception as e:
7694
LOGGER.error(f"Error evaluating Bedrock Model Invocation Logging configuration: {str(e)}")
77-
return 'ERROR', f"Error evaluating compliance: {str(e)}"
95+
return 'INSUFFICIENT_DATA', f"Error evaluating compliance: {str(e)}"
7896

7997
def lambda_handler(event, context):
8098
LOGGER.info('Evaluating compliance for AWS Config rule')
@@ -100,5 +118,7 @@ def lambda_handler(event, context):
100118
Evaluations=[evaluation],
101119
ResultToken=event['resultToken']
102120
)
121+
# ^^^ [ERROR] ValidationException: An error occurred (ValidationException) when calling the PutEvaluations operation:
122+
# 1 validation error detected: Value 'ERROR' at 'evaluations.1.member.complianceType' failed to satisfy constraint: Member must satisfy enum value set: [INSUFFICIENT_DATA, NON_COMPLIANT, NOT_APPLICABLE, COMPLIANT]
103123

104124
LOGGER.info("Compliance evaluation complete.")

aws_sra_examples/solutions/genai/bedrock_org/lambda/src/sra_config_lambda_iam_permissions.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181
"Sid": "AllowGetBucketConf",
8282
"Effect": "Allow",
8383
"Action": [
84-
"s3:GetBucketLifecycleConfiguration",
85-
"s3:GetBucketEncryption",
84+
"s3:GetBucketObjectLockConfiguration",
85+
"s3:GetLifecycleConfiguration",
86+
"s3:GetEncryptionConfiguration",
8687
"s3:GetBucketLogging",
87-
"s3:GetObjectLockConfiguration",
8888
"s3:GetBucketVersioning"
8989
],
9090
"Resource": "arn:aws:s3:::*"

0 commit comments

Comments
 (0)