Class: Inspec::Config

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/inspec/config.rb

Defined Under Namespace

Classes: Defaults

Constant Summary collapse

GENERIC_CREDENTIALS =

These are options that apply to any transport

%w{
  backend
  logger
  sudo
  sudo_password
  sudo_command
  sudo_options
  shell
  shell_options
  shell_command
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_opts = {}, cfg_io = nil, command_name = nil) ⇒ Config

This gets called when the first config is created.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inspec/config.rb', line 48

def initialize(cli_opts = {}, cfg_io = nil, command_name = nil)
  @command_name = command_name || (ARGV.empty? ? nil : ARGV[0].to_sym)
  @defaults = Defaults.for_command(@command_name)

  @cli_opts = cli_opts.dup
  cfg_io = resolve_cfg_io(@cli_opts, cfg_io)
  @cfg_file_contents = read_cfg_file_io(cfg_io)

  @merged_options = merge_options
  @final_options = finalize_options
  self.class.cached = self
end

Instance Attribute Details

#final_optionsObject (readonly)

Returns the value of attribute final_options.



30
31
32
# File 'lib/inspec/config.rb', line 30

def final_options
  @final_options
end

Class Method Details

.cachedObject

Use this to get a cached version of the config. This prevents you from being required to pass it around everywhere.



39
40
41
# File 'lib/inspec/config.rb', line 39

def self.cached
  @cached_config
end

.cached=(cfg) ⇒ Object



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

def self.cached=(cfg)
  @cached_config ||= cfg
end

.mock(opts = {}) ⇒ Object

This makes it easy to make a config with a mock backend.



33
34
35
# File 'lib/inspec/config.rb', line 33

def self.mock(opts = {})
  Inspec::Config.new({ backend: :mock }.merge(opts), StringIO.new("{}"))
end

Instance Method Details

#diagnoseObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/inspec/config.rb', line 61

def diagnose
  return unless self[:diagnose]

  puts "InSpec version: #{Inspec::VERSION}"
  puts "Train version: #{Train::VERSION}"
  puts "Command line configuration:"
  pp @cli_opts
  puts "JSON configuration file:"
  pp @cfg_file_contents
  puts "Merged configuration:"
  pp @merged_options
  puts
end

#telemetry_optionsHash

return all telemetry options from config

Returns:



77
78
79
# File 'lib/inspec/config.rb', line 77

def telemetry_options
  final_options.select { |key, _| key.include?("telemetry") }
end

#unpack_train_credentialsObject

Returns a Hash with Symbol keys as follows:

backend: machine name of the Train transport needed
If present, any of the GENERIC_CREDENTIALS.
All other keys are specific to the backend.

The credentials are gleaned from:

* the Train transport defaults. Train handles this on transport creation,
    so this method doesn't load defaults.
* individual InSpec CLI options (which in many cases may have the
    transport name prefixed, which is stripped before being added
    to the creds hash)
* the --target CLI option, which is interpreted:
   - as a transport://credset format, which looks up the creds in
     the config file in the credentials section
   - as an arbitrary URI, which is parsed by Train.unpack_target_from_uri


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/inspec/config.rb', line 101

def unpack_train_credentials
  # Internally, use indifferent access while we build the creds
  credentials = Thor::CoreExt::HashWithIndifferentAccess.new({})

  # Helper methods prefixed with _utc_ (Unpack Train Credentials)

  credentials.merge!(_utc_generic_credentials)

  _utc_determine_backend(credentials)
  transport_name = credentials[:backend].to_s

  _utc_merge_credset(credentials, transport_name)
  _utc_merge_transport_options(credentials, transport_name)

  # Convert to all-Symbol keys
  credentials.each_with_object({}) do |(option, value), creds|
    creds[option.to_sym] = value
    creds
  end
end