Skip to content

Commit 6896d23

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

File tree

1 file changed

+31
-8
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/rules/sra_bedrock_check_guardrail_encryption

1 file changed

+31
-8
lines changed

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
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+
"""
110
from typing import Any
211
import boto3
312
import json
@@ -17,9 +26,17 @@
1726
bedrock_client = boto3.client('bedrock', region_name=AWS_REGION)
1827
config_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

Comments
 (0)