Class: RuleRegistry

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

Overview

This registry contains all the discovered rule classes that are to be used for inspection.

Historically this just kept the metadata around the rules that percolated into the violation reports. This is no longer true as the rule discovery logic is being split out to support multiple discover algorithms (i.e. rule repositories). The CustomRuleLoader asks the discovery delegate to the rule classes, and that discovery returns this object with the rule definitions, and the class itself that can be invoked to do an inspection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuleRegistry

Returns a new instance of RuleRegistry.



20
21
22
23
24
# File 'lib/cfn-nag/rule_registry.rb', line 20

def initialize
  @rules = []
  @duplicate_ids = []
  @rule_classes = Set.new
end

Instance Attribute Details

#duplicate_idsObject (readonly)

Returns the value of attribute duplicate_ids.



18
19
20
# File 'lib/cfn-nag/rule_registry.rb', line 18

def duplicate_ids
  @duplicate_ids
end

#rule_classesObject (readonly)

Returns the value of attribute rule_classes.



18
19
20
# File 'lib/cfn-nag/rule_registry.rb', line 18

def rule_classes
  @rule_classes
end

#rulesObject (readonly)

Returns the value of attribute rules.



18
19
20
# File 'lib/cfn-nag/rule_registry.rb', line 18

def rules
  @rules
end

Instance Method Details

#by_id(id) ⇒ Object



59
60
61
# File 'lib/cfn-nag/rule_registry.rb', line 59

def by_id(id)
  @rules.find { |rule| rule.id == id }
end

#definition(rule_class) ⇒ Object



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

def definition(rule_class)
  @rule_classes.add rule_class

  rule = rule_class.new

  existing_def = by_id rule.rule_id

  if existing_def.nil?
    rule_definition = RuleDefinition.new(
      id: rule.rule_id,
      type: rule.rule_type,
      message: rule.rule_text
    )
    add_rule rule_definition
  else
    @duplicate_ids << {
      id: rule.rule_id,
      new_message: rule.rule_text,
      registered_message: existing_def.message
    }
  end
end

#duplicate_ids?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cfn-nag/rule_registry.rb', line 26

def duplicate_ids?
  @duplicate_ids.count.positive?
end

#failingsObject



71
72
73
# File 'lib/cfn-nag/rule_registry.rb', line 71

def failings
  @rules.select { |rule| rule.type == RuleDefinition::FAILING_VIOLATION }
end

#idsObject



63
64
65
# File 'lib/cfn-nag/rule_registry.rb', line 63

def ids
  @rules.map(&:id)
end

#merge!(other_rule_registry) ⇒ Object



30
31
32
33
34
# File 'lib/cfn-nag/rule_registry.rb', line 30

def merge!(other_rule_registry)
  @rules += other_rule_registry.rules
  @duplicate_ids += other_rule_registry.duplicate_ids
  @rule_classes += other_rule_registry.rule_classes
end

#warningsObject



67
68
69
# File 'lib/cfn-nag/rule_registry.rb', line 67

def warnings
  @rules.select { |rule| rule.type == RuleDefinition::WARNING }
end