Ensure a log metric filter and alarm exist for changes to network gateways

Error: A log metric filter and alarm does not exist for changes to network gateways

Bridgecrew Policy ID: BC_AWS_MONITORING_12
Severity: HIGH

A log metric filter and alarm does not exist for changes to network gateways

Description

Real-time monitoring of API calls can be achieved by directing CloudTrail Logs to CloudWatch Logs and establishing corresponding metric filters and alarms. Network gateways are required to send/receive traffic to a destination outside of a VPC. Monitoring changes to network gateways will help ensure that all ingress/egress traffic traverses the VPC border via a controlled path.

We recommend you establish a log metric filter and alarm to detect changes to network gateways.

Fix - Runtime

Procedure

To ensure that there is at least one active multi-region CloudTrail with prescribed metric filters and alarms configured, follow these steps and commands:

  1. Identify the log group name configured for use with active multi-region CloudTrail:
    List all CloudTrails:
   aws cloudtrail describe-trails

Identify Multi region Cloudtrails: Trails with IsMultiRegionTrail set to TRUE.
From value associated with CloudWatchLogsLogGroupArn note
<cloudtrail_log_group_name>
Example: for CloudWatchLogsLogGroupArn that looks like:

arn:aws:logs:<region>:<aws_account_number>:log-group:NewGroup:*

<cloudtrail_log_group_name> would be NewGroup

Ensure Identified Multi region CloudTrail is active

aws cloudtrail get-trail-status --name <Name of a Multi-region CloudTrail>

Ensure IsLogging is set to TRUE.
Ensure identified Multi-region Cloudtrail captures all Management Events:

aws cloudtrail get-event-selectors --trail-name <trailname shown in describetrails>

Ensure there is at least one Event Selector for a Trail with IncludeManagementEvents set to true and ReadWriteType set to All

  1. Get a list of all associated metric filters for this <cloudtrail_log_group_name>:
aws logs describe-metric-filters --log-group-name
<cloudtrail_log_group_name>
  1. Ensure the output from the above command contains the following:
filterPattern: { ($.eventName = CreateCustomerGateway) || 
($.eventName = DeleteCustomerGateway) || ($.eventName = AttachInternetGateway) ||
($.eventName = CreateInternetGateway) || ($.eventName = DeleteInternetGateway) || 
($.eventName = DetachInternetGateway) }
  1. Note the <network_gw_changes_metric> value associated with the filterPattern found in Step 3.

  2. Get a list of CloudWatch alarms and filter on the <network_gw_changes_metric> captured in Step 4.

aws cloudwatch describe-alarms 
--query 'MetricAlarms[?MetricName==<network_gw_changes_metric>]'
  1. Note the AlarmActions value. This will provide the SNS topic ARN value.

  2. Ensure there is at least one active subscriber to the SNS topic.

aws sns list-subscriptions-by-topic --topic-arn <sns_topic_arn> 

At least one subscription should have SubscriptionArn with valid aws ARN.
Example of valid SubscriptionArn:

arn:aws:sns:<region>:<aws_account_number>:<SnsTopicName>:<SubscriptionID> 

Perform the following to setup the metric filter, alarm, SNS topic, and subscription.

  1. Create a metric filter based on filter pattern provided which checks for network gateways changes and the <cloudtrail_log_group_name> taken from Audit Step 1.
aws logs put-metric-filter 
--log-group-name <cloudtrail_log_group_name> 
--filter-name <network_gw_changes_metric>
--metric-transformationsmetricName= <network_gw_changes_metric>,
        metricNamespace='CISBenchmark',metricValue=1 
--filter-pattern '{($.eventName = CreateCustomerGateway) || DeleteCustomerGateway) || 
($.eventName = ($.eventName = CreateInternetGateway) || DeleteInternetGateway) || 
($.eventName = ($.eventName = AttachInternetGateway) || 
($.eventName = DetachInternetGateway) }'

📘

Note

You can choose your own metric Name and metric Namespace strings. Using the same metric Namespace for all Foundations Benchmark metrics will group them together.

  1. Create an SNS topic that the alarm will notify.
aws sns create-topic --name <sns_topic_name>

📘

Note

You can execute this command once and then re-use the same topic for all monitoring alarms.

  1. Create an SNS subscription to the topic created in Step 9.
aws sns subscribe 
--topic-arn <sns_topic_arn> 
--protocol <protocol_for_sns> 
--notification-endpoint <sns_subscription_endpoints>

📘

Note

You can execute this command once and then re-use the SNS subscription for all monitoring alarms.

  1. Create an alarm that is associated with the CloudWatch Logs Metric Filter created in Step 1 and an SNS topic created in Step 9.
aws cloudwatch put-metric-alarm 
--alarm-name <network_gw_changes_alarm>
-metric-name <network_gw_changes_metric>
--statistic Sum 
--period 300 
--threshold 1 
--comparison-operator GreaterThanOrEqualToThreshold 
--evaluationperiods 1 
--namespace 'CISBenchmark' 
--alarm-actions <sns_topic_arn>

Fix - Buildtime

Resource: aws_cloudwatch_log_metric_filter, aws_cloudwatch_metric_alarm

resource "aws_sns_topic" "trail-unauthorised" {
  name="Unauthorised"
  kms_master_key_id = "alias/aws/sns"
}

resource "aws_sns_topic_subscription" "sms" {
  topic_arn = aws_sns_topic.trail-unauthorised.arn
  protocol  = "sms"
  endpoint=var.endpoint
}

resource "aws_cloudwatch_metric_alarm" "gateway" {
  alarm_name          = "gateway_changes_alarm"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = 1
  metric_name         = "gateway_changes_metric"
  namespace           = "CISBenchmark"
  period              = 300
  statistic           = "Sum"
  threshold           = 1
  alarm_actions       = [aws_sns_topic.trail-unauthorised.arn]
}

resource "aws_cloudwatch_log_metric_filter" "gateway" {
  name           = "gateway_changes_metric"
  pattern        = <<PATTERN
{ ($.eventName = CreateCustomerGateway) ||
($.eventName = DeleteCustomerGateway) || ($.eventName = AttachInternetGateway) ||
($.eventName = CreateInternetGateway) || ($.eventName = DeleteInternetGateway) ||
($.eventName = DetachInternetGateway) }
PATTERN
  log_group_name = var.log_group_name
  metric_transformation {
    name      = "gateway_changes_metric"
    namespace = "CISBenchmark"
    value     = "1"
  }
}

Did this page help you?