Search and replace most attribute references in lib/

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-02-12 13:10:02 -05:00
parent 4f361bfc56
commit e5f764c9fc
8 changed files with 52 additions and 45 deletions

View file

@ -108,7 +108,7 @@ module Inspec
banner: 'one two:/output/file/path',
desc: 'Enable one or more output reporters: cli, documentation, html, progress, json, json-min, json-rspec, junit, yaml'
option :attrs, type: :array,
desc: 'Load attributes file (experimental)'
desc: 'Load one or more input files, a YAML file with values for the profile to use'
option :create_lockfile, type: :boolean,
desc: 'Write out a lockfile based on this execution (unless one already exists)'
option :backend_cache, type: :boolean,

View file

@ -216,7 +216,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
inspec exec /path/to/profile
```
Local single test (doesn't allow attributes or custom resources)
Local single test (doesn't allow inputs or custom resources)
```
inspec exec /path/to/a_test.rb
```

View file

@ -97,7 +97,7 @@ module Inspec
@profile_id = options[:id]
@profile_name = options[:profile_name]
@cache = options[:vendor_cache] || Cache.new
@attr_values = options[:attributes]
@input_values = options[:inputs]
@tests_collected = false
@libraries_loaded = false
@check_mode = options[:check_mode] || false
@ -120,14 +120,14 @@ module Inspec
@runner_context =
options[:profile_context] ||
Inspec::ProfileContext.for_profile(self, @backend, @attr_values)
Inspec::ProfileContext.for_profile(self, @backend, @input_values)
@supports_platform = metadata.supports_platform?(@backend)
@supports_runtime = metadata.supports_runtime?
register_metadata_attributes
register_metadata_inputs
end
def register_metadata_attributes
def register_metadata_inputs # TODO: deprecate
if metadata.params.key?(:attributes) && metadata.params[:attributes].is_a?(Array)
metadata.params[:attributes].each do |attribute|
attr_dup = attribute.dup
@ -297,12 +297,12 @@ module Inspec
group
end
# add information about the required attributes
if res[:attributes].nil? || res[:attributes].empty?
# convert to array for backwords compatability
res[:attributes] = []
# add information about the required inputs
if res[:inputs].nil? || res[:inputs].empty?
# convert to array for backwards compatability
res[:inputs] = []
else
res[:attributes] = res[:attributes].values.map(&:to_hash)
res[:inputs] = res[:inputs].values.map(&:to_hash)
end
res[:sha256] = sha256
res[:parent_profile] = parent_profile unless parent_profile.nil?
@ -530,7 +530,7 @@ module Inspec
backend: @backend,
parent_profile: name,
}
Inspec::DependencySet.from_lockfile(lockfile, config, { attributes: @attr_values })
Inspec::DependencySet.from_lockfile(lockfile, config, { inputs: @input_values })
end
# Calculate this profile's SHA256 checksum. Includes metadata, dependencies,
@ -595,7 +595,7 @@ module Inspec
f = load_rule_filepath(prefix, rule)
load_rule(rule, f, controls, groups)
end
params[:attributes] = @runner_context.attributes
params[:inputs] = @runner_context.inputs
params
end

View file

@ -12,13 +12,13 @@ require 'inspec/objects/input'
module Inspec
class ProfileContext
def self.for_profile(profile, backend, attributes)
def self.for_profile(profile, backend, inputs)
new(profile.name, backend, { 'profile' => profile,
'attributes' => attributes,
'inputs' => inputs,
'check_mode' => profile.check_mode })
end
attr_reader :attributes, :backend, :profile_name, :profile_id, :resource_registry
attr_reader :inputs, :backend, :profile_name, :profile_id, :resource_registry
attr_accessor :rules
def initialize(profile_id, backend, conf)
if backend.nil?
@ -35,7 +35,7 @@ module Inspec
@lib_subcontexts = []
@require_loader = ::Inspec::RequireLoader.new
Inspec::InputRegistry.register_profile_alias(@profile_id, @profile_name) if @profile_id != @profile_name
@attributes = Inspec::InputRegistry.list_inputs_for_profile(@profile_id)
@inputs = Inspec::InputRegistry.list_inputs_for_profile(@profile_id)
# A local resource registry that only contains resources defined
# in the transitive dependency tree of the loaded profile.
@resource_registry = Inspec::Resource.new_registry
@ -187,11 +187,11 @@ module Inspec
end
end
def register_attribute(name, options = {})
# we need to return an attribute object, to allow dermination of values
attribute = Inspec::InputRegistry.register_attribute(name, @profile_id, options)
attribute.value = @conf['attributes'][name] unless @conf['attributes'].nil? || @conf['attributes'][name].nil?
attribute.value
def register_input(name, options = {})
# we need to return an input object, to allow dermination of values
input = Inspec::InputRegistry.register_input(name, @profile_id, options)
input.value = @conf['inputs'][name] unless @conf['inputs'].nil? || @conf['inputs'][name].nil?
input.value
end
def set_header(field, val)

View file

@ -107,7 +107,7 @@ module Inspec::Reporters
copyright: p[:copyright],
copyright_email: p[:copyright_email],
supports: p[:supports],
attributes: p[:attributes],
attributes: p[:attributes], # TODO: rename field to inputs, see #3802
parent_profile: p[:parent_profile],
depends: p[:depends],
groups: profile_groups(p),

View file

@ -32,7 +32,13 @@ module Inspec
class Runner
extend Forwardable
attr_reader :backend, :rules, :attributes
attr_reader :backend, :rules, :inputs
def attributes
Inspec.deprecate(:rename_attributes_to_inputs, "Don't call runner.attributes, call runner.inputs")
inputs
end
def initialize(conf = {})
@rules = []
# If we were handed a Hash config (by audit cookbook or kitchen-inspec),
@ -51,10 +57,10 @@ module Inspec
RunnerRspec.new(@conf)
end
# list of profile attributes
@attributes = {}
# list of profile inputs
@inputs = {}
load_attributes(@conf)
load_inputs(@conf)
configure_transport
end
@ -95,7 +101,7 @@ module Inspec
@test_collector.add_profile(requirement.profile)
end
@attributes = profile.runner_context.attributes if @attributes.empty?
@inputs = profile.runner_context.inputs if @inputs.empty?
tests = profile.collect_tests
all_controls += tests unless tests.nil?
end
@ -143,25 +149,26 @@ module Inspec
@test_collector.exit_code
end
# determine all attributes before the execution, fetch data from secrets backend
def load_attributes(options)
# determine all inputs before the execution, fetch data from secrets backend
def load_inputs(options)
# TODO - rename :attributes and :attrs - these are both user-visible
options[:attributes] ||= {}
secrets_targets = options[:attrs]
return options[:attributes] if secrets_targets.nil?
secrets_targets.each do |target|
validate_attributes_file_readability!(target)
validate_inputs_file_readability!(target)
secrets = Inspec::SecretsBackend.resolve(target)
if secrets.nil?
raise Inspec::Exceptions::SecretsBackendNotFound,
"Cannot find parser for attributes file '#{target}'. " \
"Cannot find parser for inputs file '#{target}'. " \
'Check to make sure file has the appropriate extension.'
end
next if secrets.attributes.nil?
options[:attributes].merge!(secrets.attributes)
next if secrets.inputs.nil?
options[:attributes].merge!(secrets.inputs)
end
options[:attributes]
@ -173,7 +180,7 @@ module Inspec
#
# A target is a path or URL that points to a profile. Using this
# target we generate a Profile and a ProfileContext. The content
# (libraries, tests, and attributes) from the Profile are loaded
# (libraries, tests, and inputs) from the Profile are loaded
# into the ProfileContext.
#
# If the profile depends on other profiles, those profiles will be
@ -198,7 +205,7 @@ module Inspec
vendor_cache: @cache,
backend: @backend,
controls: @controls,
attributes: @conf[:attributes])
inputs: @conf[:attributes]) # TODO: read form :inputs here (user visible)
raise "Could not resolve #{target} to valid input." if profile.nil?
@target_profiles << profile if supports_profile?(profile)
end
@ -289,16 +296,16 @@ module Inspec
examples.each { |e| @test_collector.add_test(e, rule) }
end
def validate_attributes_file_readability!(target)
def validate_inputs_file_readability!(target)
unless File.exist?(target)
raise Inspec::Exceptions::InputsFileDoesNotExist,
"Cannot find attributes file '#{target}'. " \
"Cannot find input file '#{target}'. " \
'Check to make sure file exists.'
end
unless File.readable?(target)
raise Inspec::Exceptions::InputsFileNotReadable,
"Cannot read attributes file '#{target}'. " \
"Cannot read input file '#{target}'. " \
'Check to make sure file is readable.'
end

View file

@ -153,7 +153,7 @@ module Inspec
'type' => 'array',
'items' => CONTROL_GROUP,
},
'attributes' => {
'attributes' => { # TODO: rename to inputs, refs #3802
'type' => 'array',
# TODO: more detailed specification needed
},

View file

@ -6,7 +6,7 @@ module Secrets
class YAML < Inspec.secrets(1)
name 'yaml'
attr_reader :attributes
attr_reader :inputs
def self.resolve(target)
unless target.is_a?(String) && File.file?(target) && ['.yml', '.yaml'].include?(File.extname(target).downcase)
@ -17,14 +17,14 @@ module Secrets
# array of yaml file paths
def initialize(target)
@attributes = ::YAML.load_file(target)
@inputs = ::YAML.load_file(target)
if @attributes == false || !@attributes.is_a?(Hash)
if @inputs == false || !@inputs.is_a?(Hash)
Inspec::Log.warn("#{self.class} unable to parse #{target}: invalid YAML or contents is not a Hash")
@attributes = nil
@inputs = nil
end
rescue => e
raise "Error reading InSpec attributes: #{e}"
raise "Error reading InSpec inputs: #{e}"
end
end
end