bp-check/services/security_hub.py

32 lines
966 B
Python
Raw Permalink Normal View History

2024-08-14 01:05:06 +00:00
from models import RuleCheckResult, RuleChecker
2024-08-05 02:30:34 +00:00
import boto3
2024-08-14 01:05:06 +00:00
class SecurityHubRuleChecker(RuleChecker):
def __init__(self):
self.client = boto3.client("securityhub")
self.sts_client = boto3.client("sts")
2024-08-12 04:06:13 +00:00
2024-08-14 01:05:06 +00:00
def securityhub_enabled(self):
compliant_resources = []
non_compliant_resources = []
aws_account_id = self.sts_client.get_caller_identity()["Account"]
2024-08-05 02:30:34 +00:00
2024-08-14 01:05:06 +00:00
try:
hub = self.client.describe_hub()
compliant_resources.append(aws_account_id)
except Exception as e:
if e.__class__.__name__ == "InvalidAccessException":
non_compliant_resources.append(aws_account_id)
else:
raise e
2024-08-05 02:30:34 +00:00
2024-08-14 01:05:06 +00:00
return RuleCheckResult(
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
)
2024-08-12 04:06:13 +00:00
2024-08-14 01:05:06 +00:00
rule_checker = SecurityHubRuleChecker