-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vuln processing as distinct service (#16544)
create a distinct service to managed vulnerability processing
- Loading branch information
Showing
6 changed files
with
176 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# External Vulnerability Scans addon | ||
This addon moves vulnerability scans off of serving nodes and onto a scheduled task in AWS Eventbridge. | ||
This addon creates an additional ECS service that only runs a single task, responsible for vuln processing. It receives | ||
no web traffic. We utilize [current instance checks](https://fleetdm.com/docs/configuration/fleet-server-configuration#current-instance-checks) to make this happen. The advantages of this mechanism: | ||
|
||
1. dedicating processing power to vuln processing | ||
2. ensures task responsible for vuln processing isn't also trying to serve web traffic | ||
2. caching of vulnerability artifacts/dependencies | ||
|
||
|
||
Usage is simplified by using the output from the fleet byo-ecs module (../terraform/byo-vpc/byo-db/byo-ecs/README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,109 @@ | ||
data "aws_region" "current" {} | ||
|
||
resource "aws_cloudwatch_event_rule" "main" { | ||
schedule_expression = "rate(1 hour)" | ||
} | ||
|
||
data "aws_iam_policy_document" "assume_role" { | ||
statement { | ||
effect = "Allow" | ||
|
||
principals { | ||
type = "Service" | ||
identifiers = ["events.amazonaws.com", "ecs-tasks.amazonaws.com"] | ||
locals { | ||
environment = [ | ||
// specifically overriding disable schedule here because the output of this module sets this to true | ||
// and then we pull in the output of fleet ecs module | ||
for k, v in merge( | ||
var.fleet_config.extra_environment_variables, | ||
{ FLEET_VULNERABILITIES_DISABLE_SCHEDULE = "false"} | ||
) : { | ||
name = k | ||
value = v | ||
} | ||
|
||
actions = ["sts:AssumeRole"] | ||
} | ||
] | ||
secrets = [ | ||
for k, v in var.fleet_config.extra_secrets : { | ||
name = k | ||
valueFrom = v | ||
} | ||
] | ||
} | ||
|
||
resource "aws_iam_role" "ecs_events" { | ||
assume_role_policy = data.aws_iam_policy_document.assume_role.json | ||
} | ||
resource "aws_ecs_service" "fleet" { | ||
name = "${var.fleet_config.service.name}-vuln-processing" | ||
launch_type = "FARGATE" | ||
cluster = var.ecs_cluster | ||
task_definition = aws_ecs_task_definition.vuln-processing.arn | ||
desired_count = 1 | ||
deployment_minimum_healthy_percent = 100 | ||
deployment_maximum_percent = 200 | ||
|
||
data "aws_iam_policy_document" "ecs_events_run_task_with_any_role" { | ||
statement { | ||
effect = "Allow" | ||
actions = ["iam:PassRole"] | ||
resources = ["*"] | ||
lifecycle { | ||
ignore_changes = [desired_count] | ||
} | ||
|
||
statement { | ||
effect = "Allow" | ||
actions = ["ecs:RunTask"] | ||
resources = [replace(var.task_definition.arn, "/:\\d+$/", ":*")] | ||
condition { | ||
test = "ArnEquals" | ||
values = [var.ecs_cluster.cluster_arn] | ||
variable = "ecs:cluster" | ||
} | ||
network_configuration { | ||
subnets = var.subnets | ||
security_groups = var.security_groups | ||
} | ||
} | ||
resource "aws_iam_role_policy" "ecs_events_run_task_with_any_role" { | ||
role = aws_iam_role.ecs_events.id | ||
policy = data.aws_iam_policy_document.ecs_events_run_task_with_any_role.json | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "ecs_scheduled_task" { | ||
arn = var.ecs_cluster.cluster_arn | ||
rule = aws_cloudwatch_event_rule.main.name | ||
role_arn = aws_iam_role.ecs_events.arn | ||
|
||
ecs_target { | ||
task_count = 1 | ||
task_definition_arn = var.task_definition.arn | ||
launch_type = "FARGATE" | ||
network_configuration { | ||
subnets = var.ecs_service.network_configuration[0].subnets | ||
security_groups = var.ecs_service.network_configuration[0].security_groups | ||
} | ||
} | ||
resource "aws_ecs_task_definition" "vuln-processing" { | ||
family = "${var.fleet_config.family}-vuln-processing" | ||
cpu = var.vuln_processing_cpu | ||
memory = var.vuln_processing_memory | ||
execution_role_arn = var.execution_iam_role_arn | ||
task_role_arn = var.task_role_arn | ||
network_mode = "awsvpc" | ||
requires_compatibilities = ["FARGATE"] | ||
|
||
input = jsonencode({ | ||
containerOverrides = [ | ||
{ | ||
name = "fleet", | ||
command = ["fleet", "vuln_processing"] | ||
}, | ||
{ | ||
resourceRequirements = [ | ||
{ | ||
type = "VCPU", | ||
value = "1" | ||
}, | ||
{ | ||
type = "MEMORY", | ||
value = "4096" | ||
} | ||
] | ||
container_definitions = jsonencode(concat([ | ||
{ | ||
name = "fleet-vuln-processing" | ||
image = var.fleet_config.image | ||
essential = true | ||
networkMode = "awsvpc" | ||
secrets = [ | ||
{ | ||
name = "FLEET_MYSQL_PASSWORD" | ||
valueFrom = var.fleet_config.database.password_secret_arn | ||
} | ||
] | ||
ulimits = [ | ||
{ | ||
name = "nofile" | ||
softLimit = 999999 | ||
hardLimit = 999999 | ||
} | ||
], | ||
environment = concat([ | ||
{ | ||
name = "FLEET_MYSQL_USERNAME" | ||
value = var.fleet_config.database.user | ||
}, | ||
{ | ||
name = "FLEET_MYSQL_DATABASE" | ||
value = var.fleet_config.database.database | ||
}, | ||
{ | ||
name = "FLEET_MYSQL_ADDRESS" | ||
value = var.fleet_config.database.address | ||
}, | ||
{ | ||
name = "FLEET_REDIS_ADDRESS" | ||
value = var.fleet_config.redis.address | ||
}, | ||
{ | ||
name = "FLEET_REDIS_USE_TLS" | ||
value = tostring(var.fleet_config.redis.use_tls) | ||
}, | ||
{ | ||
name = "FLEET_SERVER_TLS" | ||
value = "false" | ||
}, | ||
], local.environment), | ||
logConfiguration = { | ||
logDriver = "awslogs" | ||
options = { | ||
awslogs-group = var.awslogs_config.group | ||
awslogs-region = var.awslogs_config.region == null ? data.aws_region.current.name : var.awslogs_config.region | ||
awslogs-stream-prefix = "${var.awslogs_config.prefix}-vuln-processing" | ||
} | ||
} | ||
] | ||
}) | ||
}] | ||
, var.fleet_config.sidecars)) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
output "extra_environment_variables" { | ||
value = { | ||
FLEET_VULNERABILITIES_DISABLE_SCHEDULE = "true" | ||
FLEET_VULNERABILITIES_DISABLE_SCHEDULE = "true" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,53 @@ | ||
variable "task_definition" { | ||
description = "The task definition resource that is created by the byo-ecs module" | ||
variable "ecs_cluster" { | ||
description = "The ecs cluster module that is created by the byo-db module" | ||
} | ||
|
||
variable "ecs_service" { | ||
description = "The ecs service resource that is created by the byo-ecs module" | ||
variable "fleet_config" { | ||
description = "The root Fleet config object" | ||
type = any | ||
} | ||
|
||
variable "ecs_cluster" { | ||
description = "The ecs cluster module that is created by the byo-db module" | ||
variable "awslogs_config" { | ||
type = object({ | ||
group = string | ||
region = string | ||
prefix = string | ||
}) | ||
} | ||
|
||
variable "subnets" { | ||
type = list(string) | ||
nullable = false | ||
} | ||
|
||
variable "security_groups" { | ||
type = list(string) | ||
nullable = false | ||
} | ||
|
||
|
||
variable "customer_prefix" { | ||
type = string | ||
default = "fleet" | ||
} | ||
|
||
variable "execution_iam_role_arn" { | ||
description = "The ARN of the fleet execution role, this is necessary to pass role from ecs events" | ||
} | ||
|
||
variable "task_role_arn" { | ||
description = "The ARN of the fleet task role, this is necessary to pass role from ecs events" | ||
} | ||
|
||
variable "vuln_processing_memory" { | ||
// note must conform to FARGATE breakpoints https://docs.aws.amazon.com/AmazonECS/latest/userguide/fargate-task-defs.html | ||
default = 4096 | ||
description = "The amount of memory to dedicate to the vuln processing command" | ||
} | ||
|
||
variable "vuln_processing_cpu" { | ||
// note must conform to FARGETE breakpoints https://docs.aws.amazon.com/AmazonECS/latest/userguide/fargate-task-defs.html | ||
default = 1024 | ||
description = "The amount of CPU to dedicate to the vuln processing command" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters