From dea323fa52c6785110cfd847ac41ae5740f23258 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Wed, 1 May 2019 22:58:40 -0400 Subject: [PATCH 1/6] Test fixtures and failing functional test for input rename Signed-off-by: Clinton Wolfe --- test/functional/inputs_test.rb | 17 ++++++++++++++++- .../controls/metadata_controls.rb | 5 +++++ .../profiles/inputs/metadata-basic/inspec.yml | 11 +++++++++++ .../controls/metadata_controls.rb | 5 +++++ .../profiles/inputs/metadata-legacy/inspec.yml | 12 ++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/unit/mock/profiles/inputs/metadata-basic/controls/metadata_controls.rb create mode 100644 test/unit/mock/profiles/inputs/metadata-basic/inspec.yml create mode 100644 test/unit/mock/profiles/inputs/metadata-legacy/controls/metadata_controls.rb create mode 100644 test/unit/mock/profiles/inputs/metadata-legacy/inspec.yml diff --git a/test/functional/inputs_test.rb b/test/functional/inputs_test.rb index 954bc5838..8772373c1 100644 --- a/test/functional/inputs_test.rb +++ b/test/functional/inputs_test.rb @@ -79,7 +79,22 @@ describe "inputs" do end end - describe "run profile with metadata inputs" do + 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") diff --git a/test/unit/mock/profiles/inputs/metadata-basic/controls/metadata_controls.rb b/test/unit/mock/profiles/inputs/metadata-basic/controls/metadata_controls.rb new file mode 100644 index 000000000..d2bdcc5b4 --- /dev/null +++ b/test/unit/mock/profiles/inputs/metadata-basic/controls/metadata_controls.rb @@ -0,0 +1,5 @@ +control 'test_control_01' do + describe attribute('test_01') do + it { should cmp 'test_value_01' } + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/inputs/metadata-basic/inspec.yml b/test/unit/mock/profiles/inputs/metadata-basic/inspec.yml new file mode 100644 index 000000000..358cceb65 --- /dev/null +++ b/test/unit/mock/profiles/inputs/metadata-basic/inspec.yml @@ -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 \ No newline at end of file diff --git a/test/unit/mock/profiles/inputs/metadata-legacy/controls/metadata_controls.rb b/test/unit/mock/profiles/inputs/metadata-legacy/controls/metadata_controls.rb new file mode 100644 index 000000000..d2bdcc5b4 --- /dev/null +++ b/test/unit/mock/profiles/inputs/metadata-legacy/controls/metadata_controls.rb @@ -0,0 +1,5 @@ +control 'test_control_01' do + describe attribute('test_01') do + it { should cmp 'test_value_01' } + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/inputs/metadata-legacy/inspec.yml b/test/unit/mock/profiles/inputs/metadata-legacy/inspec.yml new file mode 100644 index 000000000..ab8678007 --- /dev/null +++ b/test/unit/mock/profiles/inputs/metadata-legacy/inspec.yml @@ -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 \ No newline at end of file From 43f7fe52f107d73d7751d7ea563252c03846a9a0 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Wed, 1 May 2019 23:18:05 -0400 Subject: [PATCH 2/6] Implementation and some light refactoring of bind_inputs_from_metadata Signed-off-by: Clinton Wolfe --- etc/deprecations.json | 5 ++++ lib/inspec/input_registry.rb | 53 ++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/etc/deprecations.json b/etc/deprecations.json index 429824a77..5e2b3b831 100644 --- a/etc/deprecations.json +++ b/etc/deprecations.json @@ -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", diff --git a/lib/inspec/input_registry.rb b/lib/inspec/input_registry.rb index 96f39b188..1ced8a4dc 100644 --- a/lib/inspec/input_registry.rb +++ b/lib/inspec/input_registry.rb @@ -205,30 +205,41 @@ 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| - 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") }) - evt = Inspec::Input.infer_event(input_options) - - # Profile metadata may set inputs in other profiles by naming them. - if input_options[:profile] - profile_name = input_options[:profile] || profile_name - # 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, - profile_name, - type: input_options[:type], - required: input_options[:required], - event: evt) - end + if profile_metadata_obj.params.key?(:inputs) + raw_inputs = profile_metadata_obj.params[:inputs] elsif profile_metadata_obj.params.key?(:attributes) - Inspec::Log.warn "Inputs must be defined as an Array. Skipping current definition." + 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 do |input_orig| + input_options = input_orig.dup + input_name = input_options.delete(:name) + 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. + if input_options[:profile] + profile_name = input_options[:profile] || profile_name + # 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, + profile_name, + type: input_options[:type], + required: input_options[:required], + event: evt) end end From 0b8da5bb01a82d12b5a39ddb468be0294baea905 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 6 May 2019 14:49:45 -0400 Subject: [PATCH 3/6] update test expectation with improved log message Signed-off-by: Clinton Wolfe --- test/functional/inputs_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/inputs_test.rb b/test/functional/inputs_test.rb index 8772373c1..52cb4b9cd 100644 --- a/test/functional/inputs_test.rb +++ b/test/functional/inputs_test.rb @@ -99,7 +99,7 @@ describe "inputs" 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 From 4aa805c7ad65599adac46d30325d828f03b5bfed Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 28 May 2019 17:08:16 -0400 Subject: [PATCH 4/6] Linting Signed-off-by: Clinton Wolfe --- lib/inspec/input_registry.rb | 40 ++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/inspec/input_registry.rb b/lib/inspec/input_registry.rb index 1ced8a4dc..6ce144bf4 100644 --- a/lib/inspec/input_registry.rb +++ b/lib/inspec/input_registry.rb @@ -221,26 +221,30 @@ module Inspec return end - raw_inputs.each do |input_orig| - input_options = input_orig.dup - input_name = input_options.delete(:name) - 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) + raw_inputs.each { |i| handle_raw_input_from_metadata(i) } + end - # Profile metadata may set inputs in other profiles by naming them. - if input_options[:profile] - profile_name = input_options[:profile] || profile_name - # 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, - profile_name, - type: input_options[:type], - required: input_options[:required], - event: evt) + def handle_raw_input_from_metadata(input_orig) + input_options = input_orig.dup + input_name = input_options.delete(:name) + 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. + if input_options[:profile] + profile_name = input_options[:profile] || profile_name + # 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, + profile_name, + type: input_options[:type], + required: input_options[:required], + event: evt, + ) end #-------------------------------------------------------------# From 5a4cbe2c174ecc1a71b4d6f53d96e1d72ea77eb4 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 10 Jun 2019 16:54:36 -0400 Subject: [PATCH 5/6] Add missing param Signed-off-by: Clinton Wolfe --- lib/inspec/input_registry.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/inspec/input_registry.rb b/lib/inspec/input_registry.rb index 6ce144bf4..eec60513f 100644 --- a/lib/inspec/input_registry.rb +++ b/lib/inspec/input_registry.rb @@ -221,10 +221,10 @@ module Inspec return end - raw_inputs.each { |i| handle_raw_input_from_metadata(i) } + raw_inputs.each { |i| handle_raw_input_from_metadata(i, profile_name) } end - def handle_raw_input_from_metadata(input_orig) + def handle_raw_input_from_metadata(input_orig, profile_name) input_options = input_orig.dup input_name = input_options.delete(:name) input_options[:provider] = :profile_metadata From 4ce21926780bfcfa37277561e2eaae14520a3e20 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Wed, 12 Jun 2019 19:09:11 -0400 Subject: [PATCH 6/6] Impactful KPI upswing Signed-off-by: Clinton Wolfe --- lib/inspec/input_registry.rb | 4 ++-- test/functional/inputs_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/inspec/input_registry.rb b/lib/inspec/input_registry.rb index eec60513f..fb9e1e8fa 100644 --- a/lib/inspec/input_registry.rb +++ b/lib/inspec/input_registry.rb @@ -228,7 +228,7 @@ module Inspec input_options = input_orig.dup input_name = input_options.delete(:name) input_options[:provider] = :profile_metadata - input_options[:file] = File.join(profile_name, 'inspec.yml') + input_options[:file] = File.join(profile_name, "inspec.yml") input_options[:priority] ||= 30 evt = Inspec::Input.infer_event(input_options) @@ -243,7 +243,7 @@ module Inspec profile_name, type: input_options[:type], required: input_options[:required], - event: evt, + event: evt ) end diff --git a/test/functional/inputs_test.rb b/test/functional/inputs_test.rb index 52cb4b9cd..f92868e21 100644 --- a/test/functional/inputs_test.rb +++ b/test/functional/inputs_test.rb @@ -79,7 +79,7 @@ describe "inputs" do end end - describe 'run profile with metadata inputs' do + describe "run profile with metadata inputs" do it "works when using the new 'inputs' key" do cmd = "exec #{inputs_profiles_path}/metadata-basic" @@ -99,7 +99,7 @@ describe "inputs" 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 in metadata files. Skipping definition from profile-with-empty-attributes.' + 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