Skip to content

Commit

Permalink
stelligent#438 Add rule that checks if RDS instance has a backup rete…
Browse files Browse the repository at this point in the history
…ntion period gr…

* Add rule that checks if RDS instance has a backup retention period greater than 0

* Update template to check for string values as well

* Refactor if statement to check for nil

* Remove blank lines

Co-authored-by: Daniel Pinheiro <[email protected]>
  • Loading branch information
dscpinheiro and dscpinheiro authored Apr 28, 2020
1 parent a7c4570 commit c481acc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/cfn-nag/custom_rules/RDSInstanceBackupRetentionPeriodRule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'cfn-nag/violation'
require_relative 'base'

class RDSInstanceBackupRetentionPeriodRule < BaseRule
def rule_text
'RDS instance should have backup retention period greater than 0'
end

def rule_type
Violation::WARNING
end

def rule_id
'W75'
end

def audit_impl(cfn_model)
rds_dbinstances = cfn_model.resources_by_type('AWS::RDS::DBInstance')

violating_rdsinstances = rds_dbinstances.select do |instance|
violating_period(instance.backupRetentionPeriod)
end

violating_rdsinstances.map(&:logical_resource_id)
end

def violating_period(backup_retention_period)
if backup_retention_period.nil?
false
end

if backup_retention_period.is_a?(Integer) || backup_retention_period.is_a?(String)
backup_retention_period.to_i == 0
else
false
end
end
end
46 changes: 46 additions & 0 deletions spec/custom_rules/RDSInstanceBackupRetentionPeriodRule_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'
require 'cfn-model'
require 'cfn-nag/custom_rules/RDSInstanceBackupRetentionPeriodRule'

describe RDSInstanceBackupRetentionPeriodRule do
context 'RDS instance with backup period set to zero' do
it 'returns offending logical resource id' do
cfn_model = CfnParser.new.parse read_test_template(
'yaml/rds_dbinstance/rds_dbinstance_with_backup_retention_period_set_to_zero.yaml'
)

actual_logical_resource_ids = RDSInstanceBackupRetentionPeriodRule.new.audit_impl cfn_model
expected_logical_resource_ids = %w[RDSDBInstanceWithZeroBackupRetentionPeriod RDSDBInstanceWithStringZeroBackupRetentionPeriod]

expect(actual_logical_resource_ids).to eq expected_logical_resource_ids
end
end

context 'RDS instance without backup period' do
it 'returns empty list' do
cfn_model = CfnParser.new.parse read_test_template(
'yaml/rds_dbinstance/rds_dbinstance_without_backup_retention_period.yaml'
)

# The default for the BackupRetentionPeriod property is 1 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html#cfn-rds-dbinstance-backupretentionperiod).

actual_logical_resource_ids = RDSInstanceBackupRetentionPeriodRule.new.audit_impl cfn_model
expected_logical_resource_ids = []

expect(actual_logical_resource_ids).to eq expected_logical_resource_ids
end
end

context 'RDS instance with backup period set to greather than zero' do
it 'returns empty list' do
cfn_model = CfnParser.new.parse read_test_template(
'yaml/rds_dbinstance/rds_dbinstance_with_backup_retention_period.yaml'
)

actual_logical_resource_ids = RDSInstanceBackupRetentionPeriodRule.new.audit_impl cfn_model
expected_logical_resource_ids = []

expect(actual_logical_resource_ids).to eq expected_logical_resource_ids
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
Resources:
RDSDBInstanceWithNonZeroBackupRetentionPeriod:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.m5.large
Engine: mysql
BackupRetentionPeriod: 5
RDSDBInstanceWithStringNonZeroBackupRetentionPeriod:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.m5.large
Engine: mysql
BackupRetentionPeriod: "5"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
Resources:
RDSDBInstanceWithoutBackupRetentionPeriod:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.m5.large
Engine: mysql
RDSDBInstanceWithZeroBackupRetentionPeriod:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.m5.large
Engine: mysql
BackupRetentionPeriod: 0
RDSDBInstanceWithStringZeroBackupRetentionPeriod:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.m5.large
Engine: mysql
BackupRetentionPeriod: "0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Resources:
RDSDBInstanceWithoutBackupRetentionPeriod:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.m5.large
Engine: mysql

0 comments on commit c481acc

Please sign in to comment.