Method: Inspec::Profile#initialize

Defined in:
lib/inspec/profile.rb

#initialize(source_reader, options = {}) ⇒ Profile

rubocop:disable Metrics/AbcSize



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/inspec/profile.rb', line 83

def initialize(source_reader, options = {})
  @source_reader = source_reader
  @target = options[:target]
  @logger = options[:logger] || Logger.new(nil)
  @locked_dependencies = options[:dependencies]
  @controls = options[:controls] || []
  @writable = options[:writable] || false
  @profile_id = options[:id]
  @profile_name = options[:profile_name]
  @cache = options[:vendor_cache] || Cache.new
  @input_values = options[:inputs]
  @tests_collected = false
  @libraries_loaded = false
  @check_mode = options[:check_mode] || false
  @parent_profile = options[:parent_profile]
  @legacy_profile_path = options[:profiles_path] || false
  .finalize(@source_reader., @profile_id, options)

  # if a backend has already been created, clone it so each profile has its own unique backend object
  # otherwise, create a new backend object
  #
  # This is necessary since we store the RuntimeProfile on the backend object. If a user runs `inspec exec`
  # with multiple profiles, only the RuntimeProfile for the last-loaded profile will be available if
  # we share the backend between profiles.
  #
  # This will cause issues if a profile attempts to load a file via `inspec.profile.file`
  train_options = options.reject { |k, _| k == "target" } # See https://github.com/chef/inspec/pull/1646
  @backend = options[:backend].nil? ? Inspec::Backend.create(Inspec::Config.new(train_options)) : options[:backend].dup
  @runtime_profile = RuntimeProfile.new(self)
  @backend.profile = @runtime_profile

  # The AttributeRegistry is in charge of keeping track of inputs;
  # it is the single source of truth. Now that we have a profile object,
  # we can create any inputs that were provided by various mechanisms.
  options[:runner_conf] ||= Inspec::Config.cached

  # Catch legacy CLI input option usage
  if options[:runner_conf].key?(:attrs)
    Inspec.deprecate(:rename_attributes_to_inputs, "Use --input-file on the command line instead of --attrs.")
    options[:runner_conf][:input_file] = options[:runner_conf].delete(:attrs)
  elsif options[:runner_conf].key?(:input_files)
    # The kitchen-inspec docs say to use plural. Our CLI and internal expectations are singular.
    options[:runner_conf][:input_file] = options[:runner_conf].delete(:input_files)
  end

  # Catch legacy kitchen-inspec input usage
  if options[:runner_conf].key?(:attributes)
    Inspec.deprecate(:rename_attributes_to_inputs, "Use :inputs in your kitchen.yml verifier config instead of :attributes.")
    options[:runner_conf][:inputs] = options[:runner_conf].delete(:attributes)
  end

  Inspec::InputRegistry.bind_profile_inputs(
    # Every input only exists in the context of a profile
    .params[:name], # TODO: test this with profile aliasing
    # Remaining args are possible sources of inputs
    cli_input_files: options[:runner_conf][:input_file], # From CLI --input-file
    profile_metadata: ,
    runner_api: options[:runner_conf][:inputs], # This is the route the audit_cookbook and kitchen-inspec take
    cli_input_arg: options[:runner_conf][:input] # The --input name=value CLI option
  )

  @runner_context =
    options[:profile_context] ||
    Inspec::ProfileContext.for_profile(self, @backend)

  @supports_platform = .supports_platform?(@backend)
  @supports_runtime = .supports_runtime?
end