summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-02-03 15:42:26 -0800
committerGitHub <noreply@github.com>2021-02-03 15:42:26 -0800
commit8482b2f5df0333b2551dcc844ccac198838012d6 (patch)
treec805f41fec834fc0bb41485475bb2e0322cca966
parent9a1662ea9f37d6a5dea82e0cbec20d5759e0612c (diff)
parente4090938e8cea73e425352ae27c8d06b448b0ec4 (diff)
downloadchef-8482b2f5df0333b2551dcc844ccac198838012d6.tar.gz
Merge pull request #10939 from aknarts/compliance_cli_report
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/compliance/default_attributes.rb2
-rw-r--r--lib/chef/compliance/reporter/cli.rb73
-rw-r--r--lib/chef/compliance/runner.rb3
3 files changed, 77 insertions, 1 deletions
diff --git a/lib/chef/compliance/default_attributes.rb b/lib/chef/compliance/default_attributes.rb
index 9b368d4f64..ee57be7b89 100644
--- a/lib/chef/compliance/default_attributes.rb
+++ b/lib/chef/compliance/default_attributes.rb
@@ -27,7 +27,7 @@ class Chef
# Controls what is done with the resulting report after the Chef InSpec run.
# Accepts a single string value or an array of multiple values.
- # Accepted values: 'chef-server-automate', 'chef-automate', 'json-file', 'audit-enforcer'
+ # Accepted values: 'chef-server-automate', 'chef-automate', 'json-file', 'audit-enforcer', 'cli'
"reporter" => "json-file",
# Controls if Chef InSpec profiles should be fetched from Chef Automate or Chef Infra Server
diff --git a/lib/chef/compliance/reporter/cli.rb b/lib/chef/compliance/reporter/cli.rb
new file mode 100644
index 0000000000..e8bcee46c4
--- /dev/null
+++ b/lib/chef/compliance/reporter/cli.rb
@@ -0,0 +1,73 @@
+class Chef
+ module Compliance
+ module Reporter
+ class Cli
+ def send_report(report)
+ # iterate over each profile and control
+ output = ["\nCompliance report:"]
+ report[:profiles].each do |profile|
+ next if profile[:controls].nil?
+
+ output << " * #{profile[:title]}"
+ profile[:controls].each do |control|
+ next if control[:results].nil?
+
+ output << "#{' ' * 6}#{control[:title]}"
+ control[:results].each do |result|
+ output << format_result(result)
+ end
+ end
+ end
+ output << "\n"
+ puts output.join("\n")
+ end
+
+ private
+
+ # pastel.decorate is a lightweight replacement for highline.color
+ def pastel
+ @pastel ||= begin
+ require "pastel" unless defined?(Pastel)
+ Pastel.new
+ end
+ end
+
+ def format_result(result)
+ output = []
+ found = false
+ if result[:status] == "failed"
+ if result[:code_desc]
+ found = true
+ output << pastel.red("#{' ' * 9}- #{result[:code_desc]}")
+ end
+ if result[:message]
+ if found
+ result[:message].split(/\n/).reject(&:empty?).each do |m|
+ output << pastel.red("#{' ' * 12}#{m}")
+ end
+ else
+ result[:message].split(/\n/).reject(&:empty?).each do |m|
+ output << pastel.red("#{' ' * 9}#{m}")
+ end
+ end
+ found = true
+ end
+ unless found
+ output << pastel.red("#{' ' * 9}- #{result[:status]}")
+ end
+ else
+ found = false
+ if result[:code_desc]
+ found = true
+ output << pastel.green("#{' ' * 9}+ #{result[:code_desc]}")
+ end
+ unless found
+ output << pastel.green("#{' ' * 9}+ #{result[:status]}")
+ end
+ end
+ output
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/compliance/runner.rb b/lib/chef/compliance/runner.rb
index 871d86ea23..4d8ffc9e5c 100644
--- a/lib/chef/compliance/runner.rb
+++ b/lib/chef/compliance/runner.rb
@@ -4,6 +4,7 @@ require_relative "default_attributes"
require_relative "reporter/automate"
require_relative "reporter/chef_server_automate"
require_relative "reporter/compliance_enforcer"
+require_relative "reporter/cli"
require_relative "reporter/json_file"
class Chef
@@ -241,6 +242,8 @@ class Chef
Chef::Compliance::Reporter::JsonFile.new(file: path)
when "audit-enforcer"
Chef::Compliance::Reporter::ComplianceEnforcer.new
+ when "cli"
+ Chef::Compliance::Reporter::Cli.new
else
raise "'#{reporter_type}' is not a supported reporter for Compliance Phase."
end