-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CloudWatchLogs LogGroup should specify RetentionInDays to expire the …
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cfn-nag/violation' | ||
require 'cfn-nag/util/truthy' | ||
require_relative 'base' | ||
|
||
class LogsLogGroupRetentionRule < BaseRule | ||
def rule_text | ||
'CloudWatchLogs LogGroup should specify RetentionInDays to expire the log data' | ||
end | ||
|
||
def rule_type | ||
Violation::WARNING | ||
end | ||
|
||
def rule_id | ||
'W86' | ||
end | ||
|
||
def audit_impl(cfn_model) | ||
violating_groups = cfn_model.resources_by_type('AWS::Logs::LogGroup').select do |group| | ||
group.retentionInDays.nil? | ||
end | ||
|
||
violating_groups.map(&:logical_resource_id) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'spec_helper' | ||
require 'cfn-model' | ||
require 'cfn-nag/custom_rules/LogsLogGroupRetentionRule' | ||
|
||
describe LogsLogGroupRetentionRule do | ||
context 'CloudWatchLogs LogGroup without retention' do | ||
it 'returns offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template( | ||
'yaml/cloudwatchlogs_loggroup/cloudwatchlogs_loggroup_without_retention.yaml' | ||
) | ||
|
||
actual_logical_resource_ids = LogsLogGroupRetentionRule.new.audit_impl cfn_model | ||
expected_logical_resource_ids = %w[CWLogGroup] | ||
|
||
expect(actual_logical_resource_ids).to eq expected_logical_resource_ids | ||
end | ||
end | ||
|
||
context 'CloudWatchLogs LogGroup with retention' do | ||
it 'returns empty list' do | ||
cfn_model = CfnParser.new.parse read_test_template( | ||
'yaml/cloudwatchlogs_loggroup/cloudwatchlogs_loggroup_with_retention.yaml' | ||
) | ||
|
||
actual_logical_resource_ids = LogsLogGroupRetentionRule.new.audit_impl cfn_model | ||
expected_logical_resource_ids = %w[] | ||
|
||
expect(actual_logical_resource_ids).to eq expected_logical_resource_ids | ||
end | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
spec/test_templates/yaml/cloudwatchlogs_loggroup/cloudwatchlogs_loggroup_with_retention.yaml
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
Resources: | ||
CWLogGroup: | ||
Type: AWS::Logs::LogGroup | ||
Properties: | ||
RetentionInDays: 7 |
5 changes: 5 additions & 0 deletions
5
...est_templates/yaml/cloudwatchlogs_loggroup/cloudwatchlogs_loggroup_without_retention.yaml
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
Resources: | ||
CWLogGroup: | ||
Type: AWS::Logs::LogGroup | ||
Properties: |