Skip to content

Commit b5ba4b6

Browse files
committed
fix multiple accounts for eval job
1 parent effa7b7 commit b5ba4b6

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# Define the AWS Config rule parameters
3030
RULE_NAME = "sra-bedrock-check-eval-job-bucket"
3131
SERVICE_NAME = "bedrock.amazonaws.com"
32+
BUCKET_NAME = ""
3233

3334

3435
def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa: U100, CCR001, C901
@@ -41,21 +42,32 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa:
4142
Returns:
4243
tuple[str, str]: The compliance status and annotation
4344
"""
45+
global BUCKET_NAME
4446
LOGGER.info(f"Evaluate Compliance Event: {event}")
4547
# Initialize AWS clients
4648
s3 = boto3.client("s3")
47-
49+
sts = boto3.client("sts")
50+
account = sts.get_caller_identity().get("Account")
4851
# Get rule parameters
4952
params = ast.literal_eval(event["ruleParameters"])
5053
LOGGER.info(f"Parameters: {params}")
51-
bucket_name = params.get("BucketName", "")
54+
LOGGER.info(f"Account: {account}")
55+
buckets = params.get("Buckets", {account: ""})
56+
LOGGER.info(f"Buckets: {buckets}")
57+
buckets = ast.literal_eval(buckets)
58+
bucket_name = buckets.get(account, "")
59+
LOGGER.info(f"Bucket Name: {bucket_name}")
60+
BUCKET_NAME = bucket_name
61+
5262
check_retention = params.get("CheckRetention", "true").lower() != "false"
5363
check_encryption = params.get("CheckEncryption", "true").lower() != "false"
5464
check_logging = params.get("CheckLogging", "true").lower() != "false"
5565
check_object_locking = params.get("CheckObjectLocking", "true").lower() != "false"
5666
check_versioning = params.get("CheckVersioning", "true").lower() != "false"
5767

5868
# Check if the bucket exists
69+
if bucket_name == "":
70+
return build_evaluation("NOT_APPLICABLE", "No bucket name provided")
5971
if not check_bucket_exists(bucket_name):
6072
return build_evaluation("NOT_APPLICABLE", f"Bucket {bucket_name} does not exist or is not accessible")
6173

@@ -64,6 +76,7 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa:
6476

6577
# Check retention
6678
if check_retention:
79+
LOGGER.info(f"Checking retention policy for bucket {bucket_name}")
6780
try:
6881
retention = s3.get_bucket_lifecycle_configuration(Bucket=bucket_name)
6982
if not any(rule.get("Expiration") for rule in retention.get("Rules", [])):
@@ -75,6 +88,7 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa:
7588

7689
# Check encryption
7790
if check_encryption:
91+
LOGGER.info(f"Checking encryption for bucket {bucket_name}")
7892
try:
7993
encryption = s3.get_bucket_encryption(Bucket=bucket_name)
8094
if "ServerSideEncryptionConfiguration" not in encryption:
@@ -86,13 +100,15 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa:
86100

87101
# Check logging
88102
if check_logging:
103+
LOGGER.info(f"Checking logging for bucket {bucket_name}")
89104
logging = s3.get_bucket_logging(Bucket=bucket_name)
90105
if "LoggingEnabled" not in logging:
91106
compliance_type = "NON_COMPLIANT"
92107
annotation.append("Server access logging not enabled")
93108

94109
# Check object locking
95110
if check_object_locking:
111+
LOGGER.info(f"Checking object locking for bucket {bucket_name}")
96112
try:
97113
object_locking = s3.get_object_lock_configuration(Bucket=bucket_name)
98114
if "ObjectLockConfiguration" not in object_locking:
@@ -104,6 +120,7 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa:
104120

105121
# Check versioning
106122
if check_versioning:
123+
LOGGER.info(f"Checking versioning for bucket {bucket_name}")
107124
versioning = s3.get_bucket_versioning(Bucket=bucket_name)
108125
if versioning.get("Status") != "Enabled":
109126
compliance_type = "NON_COMPLIANT"
@@ -157,12 +174,11 @@ def lambda_handler(event: dict, context: Any) -> None:
157174
LOGGER.info(f"Lambda Handler Event: {event}")
158175
evaluation = evaluate_compliance(event, context)
159176
config = boto3.client("config")
160-
params = ast.literal_eval(event["ruleParameters"])
161177
config.put_evaluations(
162178
Evaluations=[
163179
{
164180
"ComplianceResourceType": "AWS::S3::Bucket",
165-
"ComplianceResourceId": params.get("BucketName"),
181+
"ComplianceResourceId": BUCKET_NAME,
166182
"ComplianceType": evaluation["ComplianceType"], # type: ignore
167183
"Annotation": evaluation["Annotation"], # type: ignore
168184
"OrderingTimestamp": evaluation["OrderingTimestamp"], # type: ignore

aws_sra_examples/solutions/genai/bedrock_org/templates/sra-bedrock-org-main.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ Parameters:
9696

9797
pBedrockModelEvalBucketRuleParams:
9898
Type: String
99-
Default: '{"deploy": "true", "accounts": ["444455556666"], "regions": ["us-west-2"], "input_params": {"BucketName": "model-invocation-log-bucket-444455556666-us-west-2"}}'
99+
Default: '{"deploy": "true", "accounts": ["444455556666"], "regions": ["us-west-2"], "input_params": {"Buckets": {"444455556666": "model-invocation-log-bucket-444455556666"},"CheckRetention": "true", "CheckEncryption": "true", "CheckLogging": "true", "CheckObjectLocking": "true", "CheckVersioning": "true"}}'
100100
Description: Bedrock Model Evaluation Job Config Rule Parameters
101-
AllowedPattern: ^\{"deploy"\s*:\s*"(true|false)",\s*"accounts"\s*:\s*\[((?:"[0-9]+"(?:\s*,\s*)?)*)\],\s*"regions"\s*:\s*\[((?:"[a-z0-9-]+"(?:\s*,\s*)?)*)\],\s*"input_params"\s*:\s*(\{\s*(?:"BucketName"\s*:\s*"([a-zA-Z0-9-]*)"\s*)?})\}$
101+
AllowedPattern: ^\{"deploy"\s*:\s*"(true|false)",\s*"accounts"\s*:\s*\[((?:"[0-9]+"(?:\s*,\s*)?)*)\],\s*"regions"\s*:\s*\[((?:"[a-z0-9-]+"(?:\s*,\s*)?)*)\],\s*"input_params"\s*:\s*(\{\s*(?:"Buckets"\s*:\s*(\{\s*"[0-9]+"\s*:\s*"[a-zA-Z0-9-]*"\s*)?},\s*"CheckRetention"\s*:\s*"(true|false)",\s*"CheckEncryption"\s*:\s*"(true|false)",\s*"CheckLogging"\s*:\s*"(true|false)",\s*"CheckObjectLocking"\s*:\s*"(true|false)",\s*"CheckVersioning"\s*:\s*"(true|false)"\s*)}})$
102102
ConstraintDescription:
103103
"Must be a valid JSON string containing: 'deploy' (true/false), 'accounts' (array of account numbers),
104104
'regions' (array of region names), and 'input_params' object (can be empty or contain 'BucketName'). Arrays can be empty.

0 commit comments

Comments
 (0)