summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorMilan Stastny <milan@stastnej.ch>2021-01-26 21:22:50 +0100
committerMilan Stastny <milan@stastnej.ch>2021-01-26 21:43:55 +0100
commit03270d203dae0ea3ee5309f12e4d5079f7e543d2 (patch)
treec873ef573c3bf2b66f3926b8a844c87d2377dccf /lib/chef
parent83f861c0f0e44b240ab44e5860830e0a96741193 (diff)
downloadchef-03270d203dae0ea3ee5309f12e4d5079f7e543d2.tar.gz
Add Cli output to the Chef Compliance Phase
Signed-off-by: Milan Stastny <mistastn@cisco.com>
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/compliance/default_attributes.rb2
-rw-r--r--lib/chef/compliance/reporter/cli.rb68
-rw-r--r--lib/chef/compliance/runner.rb3
3 files changed, 72 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..c0908ebc34
--- /dev/null
+++ b/lib/chef/compliance/reporter/cli.rb
@@ -0,0 +1,68 @@
+class Chef
+ module Compliance
+ module Reporter
+ class Cli
+ def send_report(report)
+ # iterate over each profile and control
+ output = []
+ report[:profiles].each do |profile|
+ next if profile[:controls].nil?
+
+ output << "\n"
+ output << profile[:title]
+ profile[:controls].each do |control|
+ next if control[:results].nil?
+
+ output << "\t#{control[:title]}"
+ control[:results].each do |result|
+ output << format_result(result)
+ end
+ end
+ end
+ output << "\n"
+ puts output.join("\n")
+ end
+
+ private
+
+ def format_result(result)
+ output = []
+ found = false
+ if result[:status] == 'failed'
+ if result[:code_desc]
+ found = true
+ output << "\t\t\033[31m\xE2\x9D\x8C #{result[:code_desc]}\033[0m"
+ end
+ if result[:message]
+ if found
+ result[:message].split(/\n/).reject(&:empty?).each do |m|
+ output << "\t\t\t\033[31m#{m}\033[0m"
+ end
+ else
+ prefix = "\xE2\x9D\x8C"
+ result[:message].split(/\n/).reject(&:empty?).each do |m|
+ output << "\t\t\033[31m#{prefix}#{m}\033[0m"
+ prefix = ''
+ end
+ end
+ found = true
+ end
+ unless found
+ output << "\t\t\033[31m\xE2\x9D\x8C #{result[:status]}\033[0m"
+ end
+ else
+ found = false
+ if result[:code_desc]
+ found = true
+ output << "\t\t\033[32m\xE2\x9C\x94 #{result[:code_desc]}\033[0m"
+ end
+ unless found
+ output << "\t\t\033[32m\xE2\x9C\x94 #{result[:status]}\033[0m"
+ end
+ end
+ output
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/compliance/runner.rb b/lib/chef/compliance/runner.rb
index 8ab80e11ff..f37d93ff1e 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