@@ -43,73 +43,73 @@ def evaluate_compliance(event: dict, context: Any) -> tuple[str, str]: # noqa:
4343 """
4444 LOGGER .info (f"Evaluate Compliance Event: { event } " )
4545 # Initialize AWS clients
46- s3 = boto3 .client ('s3' )
46+ s3 = boto3 .client ("s3" )
4747
4848 # Get rule parameters
49- params = ast .literal_eval (event [' ruleParameters' ])
49+ params = ast .literal_eval (event [" ruleParameters" ])
5050 LOGGER .info (f"Parameters: { params } " )
51- bucket_name = params .get (' BucketName' , '' )
52- check_retention = params .get (' CheckRetention' , ' true' ).lower () != ' false'
53- check_encryption = params .get (' CheckEncryption' , ' true' ).lower () != ' false'
54- check_logging = params .get (' CheckLogging' , ' true' ).lower () != ' false'
55- check_object_locking = params .get (' CheckObjectLocking' , ' true' ).lower () != ' false'
56- check_versioning = params .get (' CheckVersioning' , ' true' ).lower () != ' false'
51+ bucket_name = params .get (" BucketName" , "" )
52+ check_retention = params .get (" CheckRetention" , " true" ).lower () != " false"
53+ check_encryption = params .get (" CheckEncryption" , " true" ).lower () != " false"
54+ check_logging = params .get (" CheckLogging" , " true" ).lower () != " false"
55+ check_object_locking = params .get (" CheckObjectLocking" , " true" ).lower () != " false"
56+ check_versioning = params .get (" CheckVersioning" , " true" ).lower () != " false"
5757
5858 # Check if the bucket exists
5959 if not check_bucket_exists (bucket_name ):
60- return build_evaluation (' NOT_APPLICABLE' , f"Bucket { bucket_name } does not exist or is not accessible" )
60+ return build_evaluation (" NOT_APPLICABLE" , f"Bucket { bucket_name } does not exist or is not accessible" )
6161
62- compliance_type = ' COMPLIANT'
62+ compliance_type = " COMPLIANT"
6363 annotation = []
6464
6565 # Check retention
6666 if check_retention :
6767 try :
6868 retention = s3 .get_bucket_lifecycle_configuration (Bucket = bucket_name )
69- if not any (rule .get (' Expiration' ) for rule in retention .get (' Rules' , [])):
70- compliance_type = ' NON_COMPLIANT'
69+ if not any (rule .get (" Expiration" ) for rule in retention .get (" Rules" , [])):
70+ compliance_type = " NON_COMPLIANT"
7171 annotation .append ("Retention policy not set" )
7272 except ClientError :
73- compliance_type = ' NON_COMPLIANT'
73+ compliance_type = " NON_COMPLIANT"
7474 annotation .append ("Retention policy not set" )
7575
7676 # Check encryption
7777 if check_encryption :
7878 try :
7979 encryption = s3 .get_bucket_encryption (Bucket = bucket_name )
80- if ' ServerSideEncryptionConfiguration' not in encryption :
81- compliance_type = ' NON_COMPLIANT'
80+ if " ServerSideEncryptionConfiguration" not in encryption :
81+ compliance_type = " NON_COMPLIANT"
8282 annotation .append ("KMS CMK encryption not enabled" )
8383 except ClientError :
84- compliance_type = ' NON_COMPLIANT'
84+ compliance_type = " NON_COMPLIANT"
8585 annotation .append ("KMS CMK encryption not enabled" )
8686
8787 # Check logging
8888 if check_logging :
8989 logging = s3 .get_bucket_logging (Bucket = bucket_name )
90- if ' LoggingEnabled' not in logging :
91- compliance_type = ' NON_COMPLIANT'
90+ if " LoggingEnabled" not in logging :
91+ compliance_type = " NON_COMPLIANT"
9292 annotation .append ("Server access logging not enabled" )
9393
9494 # Check object locking
9595 if check_object_locking :
9696 try :
9797 object_locking = s3 .get_object_lock_configuration (Bucket = bucket_name )
98- if ' ObjectLockConfiguration' not in object_locking :
99- compliance_type = ' NON_COMPLIANT'
98+ if " ObjectLockConfiguration" not in object_locking :
99+ compliance_type = " NON_COMPLIANT"
100100 annotation .append ("Object locking not enabled" )
101101 except ClientError :
102- compliance_type = ' NON_COMPLIANT'
102+ compliance_type = " NON_COMPLIANT"
103103 annotation .append ("Object locking not enabled" )
104104
105105 # Check versioning
106106 if check_versioning :
107107 versioning = s3 .get_bucket_versioning (Bucket = bucket_name )
108- if versioning .get (' Status' ) != ' Enabled' :
109- compliance_type = ' NON_COMPLIANT'
108+ if versioning .get (" Status" ) != " Enabled" :
109+ compliance_type = " NON_COMPLIANT"
110110 annotation .append ("Versioning not enabled" )
111111
112- annotation_str = '; ' .join (annotation ) if annotation else "All checked features are compliant"
112+ annotation_str = "; " .join (annotation ) if annotation else "All checked features are compliant"
113113 return build_evaluation (compliance_type , annotation_str )
114114
115115
@@ -122,10 +122,10 @@ def check_bucket_exists(bucket_name: str) -> Any:
122122 Returns:
123123 Any: True if the bucket exists and is accessible, False otherwise
124124 """
125- s3 = boto3 .client ('s3' )
125+ s3 = boto3 .client ("s3" )
126126 try :
127127 response = s3 .list_buckets ()
128- buckets = [bucket [' Name' ] for bucket in response [' Buckets' ]]
128+ buckets = [bucket [" Name" ] for bucket in response [" Buckets" ]]
129129 return bucket_name in buckets
130130 except ClientError as e :
131131 LOGGER .info (f"An error occurred: { e } " )
@@ -143,11 +143,7 @@ def build_evaluation(compliance_type: str, annotation: str) -> Any:
143143 Any: The evaluation compliance type and annotation
144144 """
145145 LOGGER .info (f"Build Evaluation Compliance Type: { compliance_type } Annotation: { annotation } " )
146- return {
147- 'ComplianceType' : compliance_type ,
148- 'Annotation' : annotation ,
149- 'OrderingTimestamp' : datetime .now ().isoformat ()
150- }
146+ return {"ComplianceType" : compliance_type , "Annotation" : annotation , "OrderingTimestamp" : datetime .now ().isoformat ()}
151147
152148
153149def lambda_handler (event : dict , context : Any ) -> None :
@@ -160,17 +156,17 @@ def lambda_handler(event: dict, context: Any) -> None:
160156 LOGGER .info (f"Lambda Handler Context: { context } " )
161157 LOGGER .info (f"Lambda Handler Event: { event } " )
162158 evaluation = evaluate_compliance (event , context )
163- config = boto3 .client (' config' )
164- params = ast .literal_eval (event [' ruleParameters' ])
159+ config = boto3 .client (" config" )
160+ params = ast .literal_eval (event [" ruleParameters" ])
165161 config .put_evaluations (
166162 Evaluations = [
167163 {
168- ' ComplianceResourceType' : ' AWS::S3::Bucket' ,
169- ' ComplianceResourceId' : params .get (' BucketName' ),
170- ' ComplianceType' : evaluation [' ComplianceType' ], # type: ignore
171- ' Annotation' : evaluation [' Annotation' ], # type: ignore
172- ' OrderingTimestamp' : evaluation [' OrderingTimestamp' ] # type: ignore
164+ " ComplianceResourceType" : " AWS::S3::Bucket" ,
165+ " ComplianceResourceId" : params .get (" BucketName" ),
166+ " ComplianceType" : evaluation [" ComplianceType" ], # type: ignore
167+ " Annotation" : evaluation [" Annotation" ], # type: ignore
168+ " OrderingTimestamp" : evaluation [" OrderingTimestamp" ], # type: ignore
173169 }
174170 ],
175- ResultToken = event [' resultToken' ]
171+ ResultToken = event [" resultToken" ],
176172 )
0 commit comments