Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor reporting #214

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ group :test do
gem 'chefspec', '~> 7.0'
gem 'coveralls', '~> 0.8.2', require: false
gem 'rb-readline'
gem 'webmock'
end

group :integration do
Expand Down
65 changes: 50 additions & 15 deletions files/default/handler/audit_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,46 +193,81 @@ def cc_profile_index(profiles)
end

# send InSpec report to the reporter (see libraries/reporters.rb)
def send_report(reporter, server, user, profiles, report)
def send_report(reporter, server, user, profiles, content)
Chef::Log.info "Reporting to #{reporter}"

# Set `insecure` here to avoid passing 6 aruguments to `AuditReport#send_report`
# See `cookstyle` Metrics/ParameterLists
insecure = node['audit']['insecure']
report = JSON.parse(content)

# TODO: harmonize reporter interface
if reporter == 'chef-visibility' || reporter == 'chef-automate'
Reporter::ChefAutomate.new(entity_uuid, run_id, gather_nodeinfo, insecure, report).send_report

elsif reporter == 'chef-compliance'
raise_if_unreachable = node['audit']['raise_if_unreachable']
url = construct_url(server, File.join('/owners', user, 'inspec'))
if server
Reporter::ChefCompliance.new(url, gather_nodeinfo, raise_if_unreachable, cc_profile_index(profiles), report).send_report
else
Chef::Log.warn "'server' and 'token' properties required by inspec report collector #{reporter}. Skipping..."
end
opts = {
entity_uuid: run_status.entity_uuid,
run_id: run_status.run_id,
node_info: gather_nodeinfo,
insecure: insecure,
}
Reporter::ChefAutomate.new(opts).send_report(report)
elsif reporter == 'chef-server-visibility' || reporter == 'chef-server-automate'
chef_url = 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'))
Reporter::ChefServerAutomate.new(entity_uuid, run_id, gather_nodeinfo, insecure, report).send_report(url)
opts = {
entity_uuid: run_status.entity_uuid,
run_id: run_status.run_id,
node_info: gather_nodeinfo,
insecure: insecure,
url: url,
}
Reporter::ChefServerAutomate.new(opts).send_report(report)
else
Chef::Log.warn "unable to determine chef-server url required by inspec report collector '#{reporter}'. Skipping..."
end
elsif reporter == 'chef-server-compliance' || reporter == 'chef-server' # chef-server is legacy reporter
elsif reporter == 'chef-compliance'
if server
raise_if_unreachable = node['audit']['raise_if_unreachable']
url = construct_url(server, File.join('/owners', user, 'inspec'))

# @config = Compliance::Configuration.new
# Chef::Log.info "Report to Chef Compliance: #{@config['server']}/owners/#{@config['user']}/inspec"
# @url = URI("#{@config['server']}/owners/#{@config['user']}/inspec")
token = @config['token']

opts = {
url: url,
node_info: gather_nodeinfo,
raise_if_unreachable: raise_if_unreachable,
profile_index: cc_profile_index(profiles),
token: token,
}
Reporter::ChefCompliance.new(opts).send_report(report)
else
Chef::Log.warn "'server' and 'token' properties required by inspec report collector #{reporter}. Skipping..."
end
elsif reporter == 'chef-server-compliance' || reporter == 'chef-server'
chef_url = server || base_chef_server_url
chef_org = Chef::Config[:chef_server_url].split('/').last
if chef_url
url = construct_url(chef_url + '/compliance/', File.join('organizations', chef_org, 'inspec'))
Reporter::ChefServer.new(url, gather_nodeinfo, raise_if_unreachable, cc_profile_index(profiles), report).send_report
opts = {
url: url,
node_info: gather_nodeinfo,
raise_if_unreachable: raise_if_unreachable,
profile_index: cc_profile_index(profiles),
}
Reporter::ChefServer.new(opts).send_report(report)
else
Chef::Log.warn "unable to determine chef-server url required by inspec report collector '#{reporter}'. Skipping..."
end
elsif reporter == 'json-file'
timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S')
Reporter::JsonFile.new(report, timestamp).send_report
filename = 'inspec' << '-' << timestamp << '.json'
path = File.expand_path("../../../../#{filename}", __FILE__)
Chef::Log.info "Writing report to #{path}"
Reporter::JsonFile.new({ file: path }).send_report(report)
else
Chef::Log.warn "#{reporter} is not a supported InSpec report collector"
end
Expand Down
16 changes: 8 additions & 8 deletions libraries/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def base_chef_server_url
cs.to_s
end

# used for interval timing
def create_timestamp_file
timestamp = Time.now.utc
timestamp_file = File.new(report_timing_file, 'w')
timestamp_file.puts(timestamp)
timestamp_file.close
end

def report_timing_file
# Will create and return the complete folder path for the chef cache location and the passed in value
::File.join(Chef::FileCache.create_cache_path('compliance'), 'report_timing.json')
Expand All @@ -92,14 +100,6 @@ def check_interval_settings(interval, interval_enabled, interval_time)
profile_overdue_to_run?(interval_seconds)
end

# used for interval timing
def create_timestamp_file
timestamp = Time.now.utc
timestamp_file = File.new(report_timing_file, 'w')
timestamp_file.puts(timestamp)
timestamp_file.close
end

# takes value of reporters and returns array to ensure backwards-compatibility
def handle_reporters(reporters)
return reporters if reporters.is_a? Array
Expand Down
Loading