bp-check/services/ecs.py

220 lines
8.6 KiB
Python
Raw Normal View History

2024-08-05 02:30:34 +00:00
from models import RuleCheckResult
import boto3
2024-08-10 05:49:34 +00:00
client = boto3.client("ecs")
2024-08-05 02:30:34 +00:00
def ecs_awsvpc_networking_enabled():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
if task_definition.get("networkMode") == "awsvpc":
compliant_resources.append(task_definition["taskDefinitionArn"])
else:
non_compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_containers_nonprivileged():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:50:51 +00:00
if container.get("privileged"):
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_containers_readonly_access():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:51:28 +00:00
if not container.get("readonlyRootFilesystem"):
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_container_insights_enabled():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
2024-08-10 08:51:28 +00:00
clusters = client.describe_clusters(include=["SETTINGS"])["clusters"]
2024-08-10 05:49:34 +00:00
2024-08-10 08:51:28 +00:00
for cluster in clusters:
container_insights_setting = [setting for setting in cluster["settings"] if setting["name"] == "containerInsights"]
2024-08-10 05:49:34 +00:00
2024-08-10 08:51:28 +00:00
if container_insights_setting and container_insights_setting[0]["value"] == "enabled":
compliant_resources.append(cluster["clusterArn"])
else:
non_compliant_resources.append(cluster["clusterArn"])
2024-08-10 05:49:34 +00:00
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_fargate_latest_platform_version():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
cluster_arns = client.list_clusters()["clusterArns"]
for cluster_arn in cluster_arns:
2024-08-10 08:51:28 +00:00
service_arns = client.list_services(cluster=cluster_arn, launchType="FARGATE")["serviceArns"]
services = client.describe_services(cluster=cluster_arn, services=service_arns)["services"]
2024-08-10 05:49:34 +00:00
for service in services:
2024-08-10 08:51:28 +00:00
if service["platformVersion"] == "LATEST":
2024-08-10 05:49:34 +00:00
compliant_resources.append(service["serviceArn"])
2024-08-10 08:51:28 +00:00
else:
non_compliant_resources.append(service["serviceArn"])
2024-08-10 05:49:34 +00:00
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_task_definition_log_configuration():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:50:51 +00:00
if "logConfiguration" not in container:
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_task_definition_memory_hard_limit():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:50:51 +00:00
if "memory" not in container:
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecs_task_definition_nonroot_user():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
if container.get("user") in [None, "root"]:
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)