-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Hopefully closes #211
- Loading branch information
Jesse Adams
authored and
Eric Kascic
committed
Jun 5, 2019
1 parent
81b7ca4
commit 233609c
Showing
10 changed files
with
154 additions
and
65 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
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
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
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
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
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
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
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cfn-nag/violation' | ||
require_relative 'base' | ||
require 'cfn-nag/util/truthy.rb' | ||
|
||
class BooleanBaseRule < BaseRule | ||
def resource_type | ||
raise 'must implement in subclass' | ||
end | ||
|
||
def boolean_property | ||
raise 'must implement in subclass' | ||
end | ||
|
||
def audit_impl(cfn_model) | ||
resources = cfn_model.resources_by_type(resource_type) | ||
|
||
violating_resources = resources.select do |resource| | ||
not_truthy?(resource.send(boolean_property)) | ||
end | ||
|
||
violating_resources.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
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,82 @@ | ||
require 'spec_helper' | ||
require 'cfn-model' | ||
require 'cfn-nag/custom_rules/boolean_base_rule' | ||
|
||
describe BooleanBaseRule do | ||
describe '#audit' do | ||
it 'raises an error when properties are not set' do | ||
expect do | ||
BooleanBaseRule.new.audit_impl nil | ||
end.to raise_error 'must implement in subclass' | ||
end | ||
|
||
it 'returns violation when boolean value is false' do | ||
base_rule = BooleanBaseRule.new | ||
base_rule.instance_eval do | ||
def rule_id | ||
'F3333' | ||
end | ||
|
||
def rule_type | ||
Violation::FAILING_VIOLATION | ||
end | ||
|
||
def rule_text | ||
'This is an epic fail!' | ||
end | ||
|
||
def resource_type | ||
'AWS::EFS::FileSystem' | ||
end | ||
|
||
def boolean_property | ||
:encrypted | ||
end | ||
end | ||
|
||
expect(base_rule).to receive(:boolean_property).and_return(:encrypted) | ||
expect(base_rule).to receive(:resource_type).and_return('AWS::EFS::FileSystem') | ||
|
||
cfn_model = CfnParser.new.parse read_test_template 'json/efs/filesystem_with_encryption_false.json' | ||
|
||
expected_violation = Violation.new(id: 'F3333', | ||
type: Violation::FAILING_VIOLATION, | ||
message: 'This is an epic fail!', | ||
logical_resource_ids: %w[filesystem]) | ||
|
||
expect(base_rule.audit(cfn_model)).to eq expected_violation | ||
end | ||
|
||
it 'returns no violation when boolean value is true' do | ||
base_rule = BooleanBaseRule.new | ||
base_rule.instance_eval do | ||
def rule_id | ||
'F3333' | ||
end | ||
|
||
def rule_type | ||
Violation::FAILING_VIOLATION | ||
end | ||
|
||
def rule_text | ||
'This is an epic fail!' | ||
end | ||
|
||
def resource_type | ||
'AWS::EFS::FileSystem' | ||
end | ||
|
||
def boolean_property | ||
:encrypted | ||
end | ||
end | ||
|
||
expect(base_rule).to receive(:boolean_property).and_return(:encrypted) | ||
expect(base_rule).to receive(:resource_type).and_return('AWS::EFS::FileSystem') | ||
|
||
cfn_model = CfnParser.new.parse read_test_template 'json/efs/filesystem_with_encryption.json' | ||
|
||
expect(base_rule.audit(cfn_model)).to be nil | ||
end | ||
end | ||
end |