Skip to content

Commit

Permalink
DynamoDB table should have backup enabled (#495)
Browse files Browse the repository at this point in the history
* #463

* Improve descripton

* Fix rubocop violation
  • Loading branch information
pethers authored Nov 25, 2020
1 parent 66beea9 commit d9e40ed
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/cfn-nag/custom_rules/DynamoDBBackupRule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

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

class DynamoDBBackupRule < BaseRule
def rule_text
'DynamoDB table should have backup enabled, should be set using PointInTimeRecoveryEnabled'
end

def rule_type
Violation::WARNING
end

def rule_id
'W78'
end

def audit_impl(cfn_model)
violating_ddb_tables = cfn_model.resources_by_type('AWS::DynamoDB::Table').select do |table|
table.pointInTimeRecoverySpecification.nil? ||
!truthy?(table.pointInTimeRecoverySpecification['PointInTimeRecoveryEnabled'].to_s)
end

violating_ddb_tables.map(&:logical_resource_id)
end
end
31 changes: 31 additions & 0 deletions spec/custom_rules/DynamoDBBackupRule_spec.rb
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/DynamoDBBackupRule'

describe DynamoDBBackupRule do
context 'dynamodb table without backup' do
it 'returns offending logical resource id' do
cfn_model = CfnParser.new.parse read_test_template(
'yaml/dynamodb/dynamodb_table_with_no_backup_enabled.yaml'
)

actual_logical_resource_ids = DynamoDBBackupRule.new.audit_impl cfn_model
expected_logical_resource_ids = %w[DynamodbNoBackupEnabled DynamodbNoBackupEnabled2]

expect(actual_logical_resource_ids).to eq expected_logical_resource_ids
end
end

context 'dynamodb table with backup enabled' do
it 'returns empty list' do
cfn_model = CfnParser.new.parse read_test_template(
'yaml/dynamodb/dynamodb_table_with_backup_enabled.yaml'
)

actual_logical_resource_ids = DynamoDBBackupRule.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:
DynamodbBackupEnabled:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
- KeySchema
TableName: String
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: True
SSESpecification:
KMSMasterKeyId: String
SSEEnabled: True
SSEType: String
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
Resources:
DynamodbNoBackupEnabled:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
- KeySchema
TableName: String
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: False
SSESpecification:
KMSMasterKeyId: String
SSEEnabled: True
SSEType: String

DynamodbNoBackupEnabled2:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
- KeySchema
TableName: String
SSESpecification:
KMSMasterKeyId: String
SSEEnabled: True
SSEType: String

0 comments on commit d9e40ed

Please sign in to comment.