Rename attributes -> inputs in profile metadata files (#4017)

Rename attributes -> inputs in profile metadata files
This commit is contained in:
Clinton Wolfe 2019-06-13 14:01:17 -04:00 committed by GitHub
commit 6c02822f90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 22 deletions

View file

@ -11,6 +11,11 @@
"comment": "See #3853",
"prefix": "The 'attribute' DSL keyword is being replaced by 'input' - please use it instead."
},
"attrs_rename_in_metadata": {
"action": "ignore",
"comment": "See 3854",
"prefix": "Inputs should be specified by using the 'inputs' key in profile metadata, not 'attributes'."
},
"aws_resources_in_resource_pack": {
"comment": "See #3822",
"action": "warn",

View file

@ -205,14 +205,31 @@ module Inspec
def bind_inputs_from_metadata(profile_name, profile_metadata_obj)
# TODO: move this into a core plugin
# TODO: add deprecation stuff
return if profile_metadata_obj.nil? # Metadata files are technically optional
if profile_metadata_obj.params.key?(:attributes) && profile_metadata_obj.params[:attributes].is_a?(Array)
profile_metadata_obj.params[:attributes].each do |input_orig|
if profile_metadata_obj.params.key?(:inputs)
raw_inputs = profile_metadata_obj.params[:inputs]
elsif profile_metadata_obj.params.key?(:attributes)
Inspec.deprecate(:attrs_rename_in_metadata, "Profile: '#{profile_name}'.")
raw_inputs = profile_metadata_obj.params[:attributes]
else
return
end
unless raw_inputs.is_a?(Array)
Inspec::Log.warn "Inputs must be defined as an Array in metadata files. Skipping definition from #{profile_name}."
return
end
raw_inputs.each { |i| handle_raw_input_from_metadata(i, profile_name) }
end
def handle_raw_input_from_metadata(input_orig, profile_name)
input_options = input_orig.dup
input_name = input_options.delete(:name)
input_options.merge!({ priority: 30, provider: :profile_metadata, file: File.join(profile_name, "inspec.yml") })
input_options[:provider] = :profile_metadata
input_options[:file] = File.join(profile_name, "inspec.yml")
input_options[:priority] ||= 30
evt = Inspec::Input.infer_event(input_options)
# Profile metadata may set inputs in other profiles by naming them.
@ -221,15 +238,13 @@ module Inspec
# Override priority to force this to win. Allow user to set their own priority.
evt.priority = input_orig[:priority] || 35
end
find_or_register_input(input_name,
find_or_register_input(
input_name,
profile_name,
type: input_options[:type],
required: input_options[:required],
event: evt)
end
elsif profile_metadata_obj.params.key?(:attributes)
Inspec::Log.warn "Inputs must be defined as an Array. Skipping current definition."
end
event: evt
)
end
#-------------------------------------------------------------#

View file

@ -80,11 +80,26 @@ describe "inputs" do
end
describe "run profile with metadata inputs" do
it "works when using the new 'inputs' key" do
cmd = "exec #{inputs_profiles_path}/metadata-basic"
result = run_inspec_process(cmd, json: true)
result.must_have_all_controls_passing
result.stderr.must_be_empty
end
it "works when using the legacy 'attributes' key" do
cmd = "exec #{inputs_profiles_path}/metadata-legacy"
result = run_inspec_process(cmd, json: true)
result.must_have_all_controls_passing
# Will eventually issue deprecation warning
end
it "does not error when inputs are empty" do
cmd = "exec "
cmd += File.join(inputs_profiles_path, "metadata-empty")
result = run_inspec_process(cmd, json: true)
result.stderr.must_include "WARN: Inputs must be defined as an Array. Skipping current definition."
result.stderr.must_include "WARN: Inputs must be defined as an Array in metadata files. Skipping definition from profile-with-empty-attributes."
assert_exit_code 0, result
end

View file

@ -0,0 +1,5 @@
control 'test_control_01' do
describe attribute('test_01') do
it { should cmp 'test_value_01' }
end
end

View file

@ -0,0 +1,11 @@
name: metadata_basic
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: A profile with a single simple input defined in metadata
version: 0.1.0
inputs:
- name: test_01
value: test_value_01

View file

@ -0,0 +1,5 @@
control 'test_control_01' do
describe attribute('test_01') do
it { should cmp 'test_value_01' }
end
end

View file

@ -0,0 +1,12 @@
name: metadata_legacy
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: A profile with a single simple input defined in metadata, using the attributes key
version: 0.1.0
attributes: # Use legacy "attributes" key
- name: test_01
value: test_value_01