Class: Inspec::InputRegistry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/inspec/input_registry.rb,
lib/inspec/errors.rb

Overview

The InputRegistry’s responsibilities include:

- maintaining a list of Input objects that are bound to profiles
- assisting in the lookup and creation of Inputs

Defined Under Namespace

Classes: Error, InputLookupError, ProfileLookupError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputRegistry

Returns a new instance of InputRegistry.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/inspec/input_registry.rb', line 23

def initialize
  # Keyed on String profile_name => Hash of String input_name => Input object
  @inputs_by_profile = {}

  # this is a list of optional profile name overrides set in the inspec.yml
  @profile_aliases = {}

  # Upon creation, activate all input plugins
  activators = Inspec::Plugin::V2::Registry.instance.find_activators(plugin_type: :input)

  @plugins = activators.map do |activator|
    activator.activate!
    activator.implementation_class.new
  end
end

Instance Attribute Details

#inputs_by_profileObject (readonly)

Returns the value of attribute inputs_by_profile.



16
17
18
# File 'lib/inspec/input_registry.rb', line 16

def inputs_by_profile
  @inputs_by_profile
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



16
17
18
# File 'lib/inspec/input_registry.rb', line 16

def plugins
  @plugins
end

#profile_aliasesObject (readonly)

Returns the value of attribute profile_aliases.



16
17
18
# File 'lib/inspec/input_registry.rb', line 16

def profile_aliases
  @profile_aliases
end

Instance Method Details

#__resetObject

Used in testing



257
258
259
260
# File 'lib/inspec/input_registry.rb', line 257

def __reset
  @inputs_by_profile = {}
  @profile_aliases = {}
end

#bind_profile_inputs(profile_name, sources = {}) ⇒ Object

This method is called by the Profile as soon as it has enough context to allow binding inputs to it.



126
127
128
129
130
131
132
133
134
135
# File 'lib/inspec/input_registry.rb', line 126

def bind_profile_inputs(profile_name, sources = {})
  inputs_by_profile[profile_name] ||= {}

  # In a more perfect world, we could let the core plugins choose
  # self-determine what to do; but as-is, the APIs that call this
  # are a bit over-constrained.
  (profile_name, sources[:profile_metadata])
  bind_inputs_from_input_files(profile_name, sources[:cli_input_files])
  bind_inputs_from_runner_api(profile_name, sources[:runner_api])
end

#find_or_register_input(input_name, profile_name, options = {}) ⇒ Object

————————————————————-#

Support for Individual Inputs

————————————————————-#



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/inspec/input_registry.rb', line 66

def find_or_register_input(input_name, profile_name, options = {})
  if profile_alias?(profile_name) && !profile_aliases[profile_name].nil?
    alias_name = profile_name
    profile_name = profile_aliases[profile_name]
    handle_late_arriving_alias(alias_name, profile_name) if profile_known?(alias_name)
  end

  # Find or create the input
  inputs_by_profile[profile_name] ||= {}
  if inputs_by_profile[profile_name].key?(input_name)
    inputs_by_profile[profile_name][input_name].update(options)
  else
    inputs_by_profile[profile_name][input_name] = Inspec::Input.new(input_name, options)
    poll_plugins_for_update(profile_name, input_name)
  end

  inputs_by_profile[profile_name][input_name]
end

#handle_late_arriving_alias(alias_name, profile_name) ⇒ Object

It is possible for a wrapper profile to create an input in metadata, referring to the child profile by an alias that has not yet been registered. The registry will then store the inputs under the alias, as if the alias were a true profile. If that happens and the child profile also mentions the input, we will need to move some things - all inputs should be stored under the true profile name, and no inputs should be stored under the alias.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/inspec/input_registry.rb', line 106

def handle_late_arriving_alias(alias_name, profile_name)
  inputs_by_profile[profile_name] ||= {}
  inputs_by_profile[alias_name].each do |input_name, input_from_alias|
    # Move the inpuut, or if it exists, merge events
    existing = inputs_by_profile[profile_name][input_name]
    if existing
      existing.events.concat(input_from_alias.events)
    else
      inputs_by_profile[profile_name][input_name] = input_from_alias
    end
  end
  # Finally, delete the (now copied-out) entry for the alias
  inputs_by_profile.delete(alias_name)
end

#list_inputs_for_profile(profile) ⇒ Object

Returns an Hash, name => Input that have actually been mentioned



48
49
50
51
# File 'lib/inspec/input_registry.rb', line 48

def list_inputs_for_profile(profile)
  inputs_by_profile[profile] = {} unless profile_known?(profile)
  inputs_by_profile[profile]
end

#list_potential_input_names_for_profile(profile_name) ⇒ Object

Returns an Array of input names. This includes input names that plugins may be able to fetch, but have not actually been mentioned in the control code.



56
57
58
59
60
# File 'lib/inspec/input_registry.rb', line 56

def list_potential_input_names_for_profile(profile_name)
  input_names_from_dsl = inputs_by_profile[profile_name].keys
  input_names_from_plugins = plugins.map { |plugin| plugin.list_inputs(profile_name) }
  (input_names_from_dsl + input_names_from_plugins).flatten.uniq
end

#poll_plugins_for_update(profile_name, input_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/inspec/input_registry.rb', line 85

def poll_plugins_for_update(profile_name, input_name)
  plugins.each do |plugin|
    response = plugin.fetch(profile_name, input_name)
    evt = Inspec::Input::Event.new(
      action: :fetch,
      provider: plugin.class.plugin_name,
      priority: plugin.default_priority,
      hit: !response.nil?
    )
    evt.value = response unless response.nil?
    inputs_by_profile[profile_name][input_name].events << evt
  end
end

#register_profile_alias(name, alias_name) ⇒ Object

————————————————————-#

Support for Profiles

————————————————————-#



43
44
45
# File 'lib/inspec/input_registry.rb', line 43

def register_profile_alias(name, alias_name)
  @profile_aliases[name] = alias_name
end