🙆♀️
ECRのスキャン結果をSlackに送信する
やりたいこと
僕はECRのスキャン設定を以下のようにすることが多いです。
そして、スキャンした結果を見て、対応が必要な場合は随時対応しています。
ECRスキャンは多少なりとも時間がかかるのでECRにPushした後に、結果を楽に知りたいなと思い、Slackに連携することにしました。
Terraformの設定
ECR側
resource "aws_ecr_registry_scanning_configuration" "this" {
scan_type = "ENHANCED"
rule {
scan_frequency = "CONTINUOUS_SCAN"
repository_filter {
filter = "*"
filter_type = "WILDCARD"
}
}
}
SNS・EventBridge・Chatbot連携側
resource "aws_cloudwatch_event_target" "ecr_scanner" {
rule = aws_cloudwatch_event_rule.ecr_scanner.name
target_id = "SendECRScannerToSNS"
arn = aws_sns_topic.ecr_scanner.arn
input_transformer {
input_paths = {
"account" = "$.account"
"detail_type" = "$.detail-type"
"repo_name" = "$.detail.repository-name"
"image_digest" = "$.detail.image-digest"
"image_tag" = "$.detail.image-tags[0]"
"counts_critical" = "$.detail.finding-severity-counts.CRITICAL"
"counts_high" = "$.detail.finding-severity-counts.HIGH"
"counts_medium" = "$.detail.finding-severity-counts.MEDIUM"
"counts_total" = "$.detail.finding-severity-counts.TOTAL"
}
input_template = <<EOF
{
"version": "1.0",
"source": "custom",
"content": {
"description": ":information_source: *ECR Image Scan Complete | <account>*\n*Scanned Repo Name:* `<repo_name>`,\n*Image Tag:* <image_tag>,\n*CRITICAL:* <counts_critical> 件,\n*HIGH:* <counts_high> 件,\n*TOTAL:* <counts_total> 件"
}
}
EOF
}
}
resource "aws_cloudwatch_event_rule" "ecr_scanner" {
name = "ecr_scanner"
description = "ECR Scanner to monitor ECR Image Scan events"
event_pattern = jsonencode({
"source" : ["aws.inspector2"],
"detail-type" : ["Inspector2 Scan"]
})
}
resource "aws_sns_topic" "ecr_scanner" {
name = "ecr_scanner"
kms_master_key_id = aws_kms_alias.chatbot.name
}
resource "aws_sns_topic_policy" "ecr_scanner" {
arn = aws_sns_topic.ecr_scanner.arn
policy = templatefile("${path.module}/files/sns_topic_policy.json.tpl", {
sns_topic_arn = aws_sns_topic.ecr_scanner.arn
})
}
resource "aws_chatbot_slack_channel_configuration" "ecr_scanner" {
configuration_name = "Slack-Alert"
iam_role_arn = aws_iam_role.chatbot.arn
slack_channel_id = var.slack_channel_id
slack_team_id = var.slack_team_id
sns_topic_arns = [aws_sns_topic.ecr_scanner.arn]
}
送信結果
これで、ECRのスキャン結果をSlackで即座に把握できるようになりました。
詳細な内容はInspectorの画面で確認するようにしていますが、CRITICAL や HIGH の脆弱性が出た場合にはすぐに気付ける体制が整いました。
セキュリティ対策としても、これで一段階強化できたと感じています!
Discussion