Skip to content

Commit

Permalink
Merge pull request #25 from shlomizadok/add_migration
Browse files Browse the repository at this point in the history
Create an Endpoint for migrating old ARF reports
  • Loading branch information
ares committed Mar 30, 2016
2 parents 27226df + 9604130 commit 7be2a37
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/smart_proxy_openscap/http_config.ru
Expand Up @@ -9,7 +9,12 @@
#

require 'smart_proxy_openscap/openscap_api'
require 'smart_proxy_openscap/openscap_import_api'

map '/compliance' do
run Proxy::OpenSCAP::Api
end

map '/compliance-importer' do
run Proxy::OpenSCAP::ImportApi
end
2 changes: 1 addition & 1 deletion lib/smart_proxy_openscap/openscap_api.rb
Expand Up @@ -69,7 +69,7 @@ class Api < ::Sinatra::Base
Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.reportsdir, params[:cname], params[:id], params[:date])
.delete_arf_file
rescue FileNotFound => e
log_halt 500, "Could not find requested file, #{e.message}"
logger.debug "Could not find requested file, #{e.message} - Assuming deleted"
end
end

Expand Down
35 changes: 35 additions & 0 deletions lib/smart_proxy_openscap/openscap_import_api.rb
@@ -0,0 +1,35 @@
module Proxy::OpenSCAP
class ImportApi < ::Sinatra::Base
include ::Proxy::Log
helpers ::Proxy::Helpers
authorize_with_trusted_hosts

require 'smart_proxy_openscap/openscap_lib'

post "/arf/:cname/:policy_id/:date" do
cn = params[:cname]
date = params[:date]
policy = params[:policy_id]
log_halt(500, "Insufficient data") if (cn.nil? || date.nil?)

post_to_foreman = ForemanForwarder.new.post_arf_report(cn, policy, date, request.body.string)
begin
Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.reportsdir, cn, post_to_foreman['id'], date)
.store_archive(request.body.string)
rescue Proxy::OpenSCAP::StoreReportError => e
Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.failed_dir, cn, post_to_foreman['id'], date)
.store_failed(request.body.string)
logger.error "Failed to save Report in reports directory (#{Proxy::OpenSCAP::Plugin.settings.reportsdir}). Failed with: #{e.message}.
Saving file in #{Proxy::OpenSCAP::Plugin.settings.failed_dir}. Please copy manually to #{Proxy::OpenSCAP::Plugin.settings.reportsdir}"
rescue *HTTP_ERRORS => e
### If the upload to foreman fails then store it in the spooldir
logger.error "Failed to upload to Foreman, saving in spool. Failed with: #{e.message}"
Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.spooldir, cn, policy, date)
.store_spool(request.body.string)
rescue Proxy::OpenSCAP::StoreSpoolError => e
log_halt 500, e.message
end
{:success => true, :arf_id => post_to_foreman['id']}.to_json
end
end
end

0 comments on commit 7be2a37

Please sign in to comment.