From 2fc41ca52d229f24f3872c68211c13a0a7033c8d Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Fri, 11 Dec 2020 11:14:50 -0800 Subject: Fix broken code in compliance runner's send_report. Signed-off-by: Pete Higgins --- lib/chef/compliance/runner.rb | 24 +++++++++++++++++++----- spec/unit/compliance/runner_spec.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/chef/compliance/runner.rb b/lib/chef/compliance/runner.rb index 677349df3e..828cce9094 100644 --- a/lib/chef/compliance/runner.rb +++ b/lib/chef/compliance/runner.rb @@ -178,6 +178,8 @@ class Chef # extracts relevant node data def node_info + chef_server_uri = URI(Chef::Config[:chef_server_url]) + runlist_roles = node.run_list.select { |item| item.type == :role }.map(&:name) runlist_recipes = node.run_list.select { |item| item.type == :recipe }.map(&:name) { @@ -218,10 +220,8 @@ class Chef } Chef::Compliance::Reporter::Automate.new(opts).send_report(report) when "chef-server-automate" - chef_url = node["audit"]["server"] || base_chef_server_url - chef_org = Chef::Config[:chef_server_url].split("/").last - if chef_url - url = construct_url(chef_url, File.join("organizations", chef_org, "data-collector")) + url = chef_server_automate_url + if url opts = { entity_uuid: node["chef_guid"], run_id: run_id, @@ -231,7 +231,7 @@ class Chef run_time_limit: run_time_limit, control_results_limit: control_results_limit, } - Chef::Compliance::Reporter::ChefServer.new(opts).send_report(report) + Chef::Compliance::Reporter::ChefServerAutomate.new(opts).send_report(report) else logger.warn "Unable to determine #{ChefUtils::Dist::Server::PRODUCT} url required by #{Inspec::Dist::PRODUCT_NAME} report collector '#{reporter}'. Skipping..." end @@ -245,6 +245,20 @@ class Chef logger.warn "#{reporter} is not a supported #{Inspec::Dist::PRODUCT_NAME} report collector" end end + + def chef_server_automate_url + url = if node["audit"]["server"] + URI(node["audit"]["server"]) + else + URI(Chef::Config[:chef_server_url]).tap do |u| + u.path = "" + end + end + + org = Chef::Config[:chef_server_url].split("/").last + url.path = File.join(url.path, "organizations/#{org}/data-collector") + url + end end end end diff --git a/spec/unit/compliance/runner_spec.rb b/spec/unit/compliance/runner_spec.rb index 68c8a9553b..ddc9bc3c4e 100644 --- a/spec/unit/compliance/runner_spec.rb +++ b/spec/unit/compliance/runner_spec.rb @@ -110,4 +110,37 @@ describe Chef::Compliance::Runner do runner.warn_for_deprecated_config_values! end end + + describe "#send_report" do + before do + Chef::Config[:chef_server_url] = "https://chef_config_url.example.com/my_org" + end + + it "uses the correct URL when 'server' attribute is set for chef-server-automate reporter" do + node.normal["audit"]["server"] = "https://server_attribute_url.example.com/application/sub_application" + report = { fake_report: true } + + reporter = double(:chef_server_automate_reporter) + expect(reporter).to receive(:send_report).with(report) + + expected_opts = hash_including(url: URI("https://server_attribute_url.example.com/application/sub_application/organizations/my_org/data-collector")) + + expect(Chef::Compliance::Reporter::ChefServerAutomate).to receive(:new).with(expected_opts).and_return(reporter) + + runner.send_report("chef-server-automate", report) + end + + it "falls back to chef_server_url for URL when 'server' attribute is not set for chef-server-automate reporter" do + report = { fake_report: true } + + reporter = double(:chef_server_automate_reporter) + expect(reporter).to receive(:send_report).with(report) + + expected_opts = hash_including(url: URI("https://chef_config_url.example.com/organizations/my_org/data-collector")) + + expect(Chef::Compliance::Reporter::ChefServerAutomate).to receive(:new).with(expected_opts).and_return(reporter) + + runner.send_report("chef-server-automate", report) + end + end end -- cgit v1.2.1