Class: CustomRuleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn-nag/custom_rule_loader.rb

Overview

This object can discover the internal and custom user-provided rules and apply these rules to a CfnModel object

Instance Method Summary collapse

Constructor Details

#initialize(rule_directory: nil) ⇒ CustomRuleLoader

Returns a new instance of CustomRuleLoader.



12
13
14
15
# File 'lib/cfn-nag/custom_rule_loader.rb', line 12

def initialize(rule_directory: nil)
  @rule_directory = rule_directory
  validate_extra_rule_directory rule_directory
end

Instance Method Details

#execute_custom_rules(cfn_model) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cfn-nag/custom_rule_loader.rb', line 37

def execute_custom_rules(cfn_model)
  Logging.logger['log'].debug "cfn_model: #{cfn_model}"

  violations = []

  discover_rule_classes(@rule_directory).each do |rule_class|
    audit_result = rule_class.new.audit(cfn_model)
    violations << audit_result unless audit_result.nil?
  end

  discover_jmespath_filenames(@rule_directory).each do |jmespath_file|
    evaluator = JmesPathEvaluator.new cfn_model
    evaluator.instance_eval do
      eval IO.read jmespath_file
    end
    violations +=  evaluator.violations
  end
  violations
end

#rule_definitionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cfn-nag/custom_rule_loader.rb', line 17

def rule_definitions
  rule_registry = RuleRegistry.new

  discover_rule_classes(@rule_directory).each do |rule_class|
    rule = rule_class.new
    rule_registry.definition(id: rule.rule_id,
                             type: rule.rule_type,
                             message: rule.rule_text)
  end

  discover_jmespath_filenames(@rule_directory).each do |jmespath_file|
    evaluator = JmesPathDiscovery.new rule_registry
    evaluator.instance_eval do
      eval IO.read jmespath_file
    end
  end

  rule_registry
end