1+ """Config rule to check the guardrail encryption 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+ """
110from typing import Any
211import boto3
312import json
1726bedrock_client = boto3 .client ('bedrock' , region_name = AWS_REGION )
1827config_client = boto3 .client ('config' , region_name = AWS_REGION )
1928
20- def evaluate_compliance (rule_parameters : dict ) -> tuple [str , str ]:
21- """Evaluates if Bedrock guardrails are encrypted with a KMS key"""
22-
29+
30+ def evaluate_compliance (rule_parameters : dict ) -> tuple [str , str ]: # noqa: CFQ004
31+ """Evaluate if Bedrock guardrails are encrypted with a KMS key.
32+
33+ Args:
34+ rule_parameters (dict): The rule parameters
35+
36+ Returns:
37+ tuple[str, str]: The compliance type and annotation
38+ """
39+ LOGGER .info (f"Rule parameters: { json .dumps (rule_parameters )} " )
2340 try :
2441 response = bedrock_client .list_guardrails ()
2542 guardrails = response .get ('guardrails' , [])
@@ -32,28 +49,34 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]:
3249 guardrail_id = guardrail ['id' ]
3350 guardrail_name = guardrail ['name' ]
3451 guardrail_detail = bedrock_client .get_guardrail (guardrailIdentifier = guardrail_id )
35-
52+
3653 if 'kmsKeyArn' not in guardrail_detail :
3754 unencrypted_guardrails .append (guardrail_name )
3855
3956 if unencrypted_guardrails :
4057 return 'NON_COMPLIANT' , f"The following Bedrock guardrails are not encrypted with a KMS key: { ', ' .join (unencrypted_guardrails )} "
41- else :
42- return 'COMPLIANT' , "All Bedrock guardrails are encrypted with a KMS key"
58+ return 'COMPLIANT' , "All Bedrock guardrails are encrypted with a KMS key"
4359
4460 except Exception as e :
4561 LOGGER .error (f"Error evaluating Bedrock guardrails encryption: { str (e )} " )
4662 return 'ERROR' , f"Error evaluating compliance: { str (e )} "
4763
48- def lambda_handler (event : dict , context : Any ) -> None :
64+
65+ def lambda_handler (event : dict , context : Any ) -> None : # noqa: U100
66+ """Lambda handler.
67+
68+ Args:
69+ event (dict): Lambda event object
70+ context (Any): Lambda context object
71+ """
4972 LOGGER .info ('Evaluating compliance for AWS Config rule' )
5073 LOGGER .info (f"Event: { json .dumps (event )} " )
5174
5275 invoking_event = json .loads (event ['invokingEvent' ])
5376 rule_parameters = json .loads (event ['ruleParameters' ]) if 'ruleParameters' in event else {}
5477
5578 compliance_type , annotation = evaluate_compliance (rule_parameters )
56-
79+
5780 evaluation = {
5881 'ComplianceResourceType' : 'AWS::::Account' ,
5982 'ComplianceResourceId' : event ['accountId' ],
0 commit comments