Class: CustomRuleLoader

Inherits:
Object show all
Includes:
Metadata
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

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#cfn_model_with_suppressed_resources_removed, #validate_cfn_nag_metadata

Constructor Details

#initialize(rule_directory: nil, allow_suppression: true, print_suppression: false, isolate_custom_rule_exceptions: false, rule_repository_definitions: []) ⇒ CustomRuleLoader

Returns a new instance of CustomRuleLoader.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cfn-nag/custom_rule_loader.rb', line 26

def initialize(rule_directory: nil,
               allow_suppression: true,
               print_suppression: false,
               isolate_custom_rule_exceptions: false,
               rule_repository_definitions: [])
  @rule_directory = rule_directory
  @allow_suppression = allow_suppression
  @print_suppression = print_suppression
  @isolate_custom_rule_exceptions = isolate_custom_rule_exceptions
  @rule_repository_definitions = rule_repository_definitions
  @registry = nil
end

Class Attribute Details

.rule_argumentsObject

Returns the value of attribute rule_arguments.



23
24
25
# File 'lib/cfn-nag/custom_rule_loader.rb', line 23

def rule_arguments
  @rule_arguments
end

Instance Method Details

#execute_custom_rules(cfn_model, rules_registry) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cfn-nag/custom_rule_loader.rb', line 55

def execute_custom_rules(cfn_model, rules_registry)
  if Logging.logger['log'].debug?
    Logging.logger['log'].debug "cfn_model: #{cfn_model}"
  end

  violations = []

  (cfn_model)

  filter_rule_classes cfn_model, violations, rules_registry

  violations
end

#rule_definitions(force_refresh: false) ⇒ Object

the first time this runs, it’s “expensive”. the core rules, the gem-based rules will load, and any other repos like “s3” will go the expensive route. after that, it’s cached so you can call it as many times as you like unless you force_refresh



44
45
46
47
48
49
50
51
52
53
# File 'lib/cfn-nag/custom_rule_loader.rb', line 44

def rule_definitions(force_refresh: false)
  if @registry.nil? || force_refresh
    @registry = FileBasedRuleRepo.new(@rule_directory).discover_rules
    @registry.merge! GemBasedRuleRepo.new.discover_rules

    @registry = RuleRepositoryLoader.new.merge(@registry, @rule_repository_definitions)
    @registry
  end
  @registry
end