smart_proxy_openscap 0.6.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +41 -0
  3. data/.rubocop_todo.yml +111 -0
  4. data/.travis.yml +14 -0
  5. data/COPYING +674 -0
  6. data/Gemfile +14 -0
  7. data/README.md +107 -0
  8. data/Rakefile +16 -0
  9. data/bin/smart-proxy-arf-html +7 -0
  10. data/bin/smart-proxy-arf-json +7 -0
  11. data/bin/smart-proxy-openscap-send +46 -0
  12. data/bin/smart-proxy-policy-guide +7 -0
  13. data/bin/smart-proxy-scap-profiles +7 -0
  14. data/bin/smart-proxy-scap-validation +7 -0
  15. data/bundler.d/openscap.rb +6 -0
  16. data/extra/rubygem-smart_proxy_openscap.spec +101 -0
  17. data/extra/smart-proxy-openscap-send.cron +2 -0
  18. data/lib/smart_proxy_openscap.rb +14 -0
  19. data/lib/smart_proxy_openscap/arf_html.rb +22 -0
  20. data/lib/smart_proxy_openscap/arf_json.rb +111 -0
  21. data/lib/smart_proxy_openscap/arf_parser.rb +35 -0
  22. data/lib/smart_proxy_openscap/content_parser.rb +30 -0
  23. data/lib/smart_proxy_openscap/fetch_file.rb +58 -0
  24. data/lib/smart_proxy_openscap/fetch_scap_content.rb +17 -0
  25. data/lib/smart_proxy_openscap/fetch_tailoring_file.rb +17 -0
  26. data/lib/smart_proxy_openscap/foreman_forwarder.rb +38 -0
  27. data/lib/smart_proxy_openscap/http_config.ru +20 -0
  28. data/lib/smart_proxy_openscap/openscap_api.rb +171 -0
  29. data/lib/smart_proxy_openscap/openscap_exception.rb +8 -0
  30. data/lib/smart_proxy_openscap/openscap_html_generator.rb +38 -0
  31. data/lib/smart_proxy_openscap/openscap_import_api.rb +32 -0
  32. data/lib/smart_proxy_openscap/openscap_lib.rb +67 -0
  33. data/lib/smart_proxy_openscap/openscap_plugin.rb +27 -0
  34. data/lib/smart_proxy_openscap/policy_guide.rb +24 -0
  35. data/lib/smart_proxy_openscap/policy_parser.rb +33 -0
  36. data/lib/smart_proxy_openscap/profiles_parser.rb +32 -0
  37. data/lib/smart_proxy_openscap/scap_profiles.rb +52 -0
  38. data/lib/smart_proxy_openscap/scap_validation.rb +35 -0
  39. data/lib/smart_proxy_openscap/shell_wrapper.rb +77 -0
  40. data/lib/smart_proxy_openscap/spool_forwarder.rb +77 -0
  41. data/lib/smart_proxy_openscap/storage.rb +47 -0
  42. data/lib/smart_proxy_openscap/storage_fs.rb +92 -0
  43. data/lib/smart_proxy_openscap/version.rb +15 -0
  44. data/settings.d/openscap.yml.example +28 -0
  45. data/smart_proxy_openscap.gemspec +23 -0
  46. data/test/data/arf_report +0 -0
  47. data/test/data/corrupted_arf_report +0 -0
  48. data/test/data/spool/cleanup_spool/arf/2c101b95-033f-4b15-b490-f50bf9090dae/1/1484313035/fa2f68ffb944c917332a284dc63ec7f8fa76990cb815ddcad3318b5d9457f8a1 +0 -0
  49. data/test/data/spool/cleanup_spool/arf/e20b9695-f655-401a-9dda-8cca7a47a8c0/1/1484309984/fa2f68ffb944c917332a284dc63ec7f8fa76990cb815ddcad3318b5d9457f8a1 +0 -0
  50. data/test/data/spool/corrupted_spool/arf/e20b9695-f655-401a-9dda-8cca7a47a8c0/1/1484309984/a4dfba5db27b21795e6fa401b8dce7a70faeb25b7963891f07f6f4baaf052afb +0 -0
  51. data/test/data/spool/corrupted_spool/arf/e20b9695-f655-401a-9dda-8cca7a47a8c0/1/1484313035/fa2f68ffb944c917332a284dc63ec7f8fa76990cb815ddcad3318b5d9457f8a1 +0 -0
  52. data/test/data/spool/valid_spool/arf/e20b9695-f655-401a-9dda-8cca7a47a8c0/1/1484309984/fa2f68ffb944c917332a284dc63ec7f8fa76990cb815ddcad3318b5d9457f8a1 +0 -0
  53. data/test/data/spool/valid_spool/arf/e20b9695-f655-401a-9dda-8cca7a47a8c0/1/1484313035/fa2f68ffb944c917332a284dc63ec7f8fa76990cb815ddcad3318b5d9457f8a1 +0 -0
  54. data/test/data/ssg-rhel7-ds.xml +20271 -0
  55. data/test/data/tailoring.xml +31 -0
  56. data/test/fetch_scap_api_test.rb +73 -0
  57. data/test/fetch_tailoring_api_test.rb +37 -0
  58. data/test/get_report_xml_html_test.rb +58 -0
  59. data/test/post_report_api_test.rb +86 -0
  60. data/test/scap_content_parser_api_test.rb +69 -0
  61. data/test/script_class_test.rb +94 -0
  62. data/test/spool_forwarder_test.rb +82 -0
  63. data/test/test_helper.rb +13 -0
  64. metadata +180 -0
@@ -0,0 +1,35 @@
1
+ require 'smart_proxy_openscap/shell_wrapper'
2
+
3
+ module Proxy
4
+ module OpenSCAP
5
+ class ArfParser < ShellWrapper
6
+
7
+ def initialize(cname, policy_id, date)
8
+ @cname = cname
9
+ @policy_id = policy_id
10
+ @date = date
11
+ @script_name = 'smart-proxy-arf-json'
12
+ end
13
+
14
+ def as_json(arf_data)
15
+ execute_shell_command arf_data
16
+ end
17
+
18
+ def in_filename
19
+ "#{super}-#{@cname}-#{@policy_id}-#{@date}-"
20
+ end
21
+
22
+ def out_filename
23
+ "#{in_filename}json-"
24
+ end
25
+
26
+ def failure_message
27
+ "Failure when running script which parses reports"
28
+ end
29
+
30
+ def command(in_file, out_file)
31
+ "#{script_location} #{in_file.path} #{out_file.path}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ require 'smart_proxy_openscap/shell_wrapper'
2
+
3
+ module Proxy::OpenSCAP
4
+ class ContentParser < ShellWrapper
5
+ def initialize(type)
6
+ @type = type
7
+ @script_name = 'smart-proxy-scap-validation'
8
+ end
9
+
10
+ def validate(scap_file)
11
+ execute_shell_command scap_file
12
+ end
13
+
14
+ def out_filename
15
+ "#{in_filename}json-"
16
+ end
17
+
18
+ def in_filename
19
+ "#{super}-#{@type}-validate-"
20
+ end
21
+
22
+ def failure_message
23
+ "Failure when running script which validates scap files"
24
+ end
25
+
26
+ def command(in_file, out_file)
27
+ "#{script_location} #{in_file.path} #{out_file.path} #{@type}"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ module Proxy::OpenSCAP
2
+ class FetchFile
3
+ include ::Proxy::Log
4
+
5
+ private
6
+
7
+ def create_store_dir(store_dir)
8
+ logger.info "Creating directory to store SCAP file: #{store_dir}"
9
+ FileUtils.mkdir_p(store_dir) # will fail silently if exists
10
+ rescue Errno::EACCES => e
11
+ logger.error "No permission to create directory #{store_dir}"
12
+ raise e
13
+ rescue StandardError => e
14
+ logger.error "Could not create '#{store_dir}' directory: #{e.message}"
15
+ raise e
16
+ end
17
+
18
+ def policy_content_file(policy_scap_file)
19
+ return nil if !File.file?(policy_scap_file) || File.zero?(policy_scap_file)
20
+ File.open(policy_scap_file, 'rb').read
21
+ end
22
+
23
+ def clean_store_folder(policy_store_dir)
24
+ FileUtils.rm_f Dir["#{policy_store_dir}/*.xml"]
25
+ end
26
+
27
+ def save_or_serve_scap_file(policy_scap_file, file_download_path)
28
+ lock = Proxy::FileLock::try_locking(policy_scap_file)
29
+ response = fetch_scap_content_xml(file_download_path)
30
+ if lock.nil?
31
+ return response
32
+ else
33
+ begin
34
+ File.open(policy_scap_file, 'wb') do |file|
35
+ file << response
36
+ end
37
+ ensure
38
+ Proxy::FileLock::unlock(lock)
39
+ end
40
+ scap_file = policy_content_file(policy_scap_file)
41
+ raise FileNotFound if scap_file.nil?
42
+ return scap_file
43
+ end
44
+ end
45
+
46
+ def fetch_scap_content_xml(file_download_path)
47
+ foreman_request = Proxy::HttpRequest::ForemanRequest.new
48
+ req = foreman_request.request_factory.create_get(file_download_path)
49
+ response = foreman_request.send_request(req)
50
+ response.value
51
+ response.body
52
+ end
53
+
54
+ def clean_store_folder(policy_store_dir)
55
+ FileUtils.rm_f Dir["#{policy_store_dir}/*.xml"]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ require 'smart_proxy_openscap/fetch_file'
2
+
3
+ module Proxy::OpenSCAP
4
+ class FetchScapContent < FetchFile
5
+ def get_policy_content(policy_id, digest)
6
+ policy_store_dir = File.join(Proxy::OpenSCAP.fullpath(Proxy::OpenSCAP::Plugin.settings.contentdir), policy_id.to_s)
7
+ policy_scap_file = File.join(policy_store_dir, "#{policy_id}_#{digest}.xml")
8
+ file_download_path = "api/v2/compliance/policies/#{policy_id}/content"
9
+
10
+ create_store_dir policy_store_dir
11
+
12
+ scap_file = policy_content_file(policy_scap_file)
13
+ clean_store_folder(policy_store_dir) unless scap_file
14
+ scap_file ||= save_or_serve_scap_file(policy_scap_file, file_download_path)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'smart_proxy_openscap/fetch_file'
2
+
3
+ module Proxy::OpenSCAP
4
+ class FetchTailoringFile < FetchFile
5
+ def get_tailoring_file(policy_id, digest)
6
+ store_dir = File.join(Proxy::OpenSCAP.fullpath(Proxy::OpenSCAP::Plugin.settings.tailoring_dir), policy_id.to_s)
7
+ policy_tailoring_file = File.join(store_dir, "#{policy_id}_#{digest}.xml")
8
+ file_download_path = "api/v2/compliance/policies/#{policy_id}/tailoring"
9
+
10
+ create_store_dir store_dir
11
+
12
+ scap_file = policy_content_file(policy_tailoring_file)
13
+ clean_store_folder(store_dir) unless scap_file
14
+ scap_file ||= save_or_serve_scap_file(policy_tailoring_file, file_download_path)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ module Proxy::OpenSCAP
2
+ class ForemanForwarder < Proxy::HttpRequest::ForemanRequest
3
+ include ::Proxy::Log
4
+
5
+ def post_arf_report(cname, policy_id, date, data)
6
+ begin
7
+ foreman_api_path = upload_path(cname, policy_id, date)
8
+ json = Proxy::OpenSCAP::ArfParser.new(cname, policy_id, date).as_json(data)
9
+ response = send_request(foreman_api_path, json)
10
+ # Raise an HTTP error if the response is not 2xx (success).
11
+ response.value
12
+ res = JSON.parse(response.body)
13
+ raise StandardError, "Received response: #{response.code} #{response.msg}" unless res['result'] == 'OK'
14
+ rescue StandardError => e
15
+ logger.debug response.body if response
16
+ logger.debug e.backtrace.join("\n\t")
17
+ raise e
18
+ end
19
+ res
20
+ end
21
+
22
+ private
23
+
24
+ def upload_path(cname, policy_id, date)
25
+ "/api/v2/compliance/arf_reports/#{cname}/#{policy_id}/#{date}"
26
+ end
27
+
28
+ def send_request(path, body)
29
+ # Override the parent method to set the right headers
30
+ path = [uri.path, path].join('/') unless uri.path.empty?
31
+ req = Net::HTTP::Post.new(URI.join(uri.to_s, path).path)
32
+ req.add_field('Accept', 'application/json,version=2')
33
+ req.content_type = 'application/json'
34
+ req.body = body
35
+ http.request(req)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,20 @@
1
+ #
2
+ # Copyright (c) 2014--2015 Red Hat Inc.
3
+ #
4
+ # This software is licensed to you under the GNU General Public License,
5
+ # version 3 (GPLv3). There is NO WARRANTY for this software, express or
6
+ # implied, including the implied warranties of MERCHANTABILITY or FITNESS
7
+ # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3
8
+ # along with this software; if not, see http://www.gnu.org/licenses/gpl.txt
9
+ #
10
+
11
+ require 'smart_proxy_openscap/openscap_api'
12
+ require 'smart_proxy_openscap/openscap_import_api'
13
+
14
+ map '/compliance' do
15
+ run Proxy::OpenSCAP::Api
16
+ end
17
+
18
+ map '/compliance-importer' do
19
+ run Proxy::OpenSCAP::ImportApi
20
+ end
@@ -0,0 +1,171 @@
1
+ #
2
+ # Copyright (c) 2014--2015 Red Hat Inc.
3
+ #
4
+ # This software is licensed to you under the GNU General Public License,
5
+ # version 3 (GPLv3). There is NO WARRANTY for this software, express or
6
+ # implied, including the implied warranties of MERCHANTABILITY or FITNESS
7
+ # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3
8
+ # along with this software; if not, see http://www.gnu.org/licenses/gpl.txt
9
+ #
10
+ require 'smart_proxy_openscap/openscap_lib'
11
+
12
+ module Proxy::OpenSCAP
13
+ HTTP_ERRORS = [
14
+ EOFError,
15
+ Errno::ECONNRESET,
16
+ Errno::EINVAL,
17
+ Errno::ECONNREFUSED,
18
+ Net::HTTPBadResponse,
19
+ Net::HTTPHeaderSyntaxError,
20
+ Net::ProtocolError,
21
+ Timeout::Error
22
+ ]
23
+
24
+ class Api < ::Sinatra::Base
25
+ include ::Proxy::Log
26
+ helpers ::Proxy::Helpers
27
+ authorize_with_ssl_client
28
+
29
+ post "/arf/:policy" do
30
+ # first let's verify client's certificate
31
+ begin
32
+ cn = Proxy::OpenSCAP::common_name request
33
+ rescue Proxy::Error::Unauthorized => e
34
+ log_halt 403, "Client authentication failed: #{e.message}"
35
+ end
36
+ date = Time.now.to_i
37
+ policy = params[:policy]
38
+
39
+ begin
40
+ post_to_foreman = ForemanForwarder.new.post_arf_report(cn, policy, date, request.body.string)
41
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.reportsdir, cn, post_to_foreman['id'], date).store_archive(request.body.string)
42
+ rescue Proxy::OpenSCAP::StoreReportError => e
43
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.failed_dir, cn, post_to_foreman['id'], date).store_failed(request.body.string)
44
+ logger.error "Failed to save Report in reports directory (#{Proxy::OpenSCAP::Plugin.settings.reportsdir}). Failed with: #{e.message}.
45
+ Saving file in #{Proxy::OpenSCAP::Plugin.settings.failed_dir}. Please copy manually to #{Proxy::OpenSCAP::Plugin.settings.reportsdir}"
46
+ rescue Proxy::OpenSCAP::OpenSCAPException => e
47
+ logger.error "Failed to parse Arf Report, moving to #{Proxy::OpenSCAP::Plugin.settings.corrupted_dir}"
48
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.corrupted_dir, cn, policy, date).store_corrupted(request.body.string)
49
+ rescue *HTTP_ERRORS => e
50
+ ### If the upload to foreman fails then store it in the spooldir
51
+ logger.error "Failed to upload to Foreman, saving in spool. Failed with: #{e.message}"
52
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.spooldir, cn, policy, date).store_spool(request.body.string)
53
+ rescue Proxy::OpenSCAP::StoreSpoolError => e
54
+ log_halt 500, e.message
55
+ end
56
+ end
57
+
58
+ get "/arf/:id/:cname/:date/:digest/xml" do
59
+ content_type 'application/x-bzip2'
60
+ begin
61
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.reportsdir, params[:cname], params[:id], params[:date]).get_arf_xml(params[:digest])
62
+ rescue FileNotFound => e
63
+ log_halt 500, "Could not find requested file, #{e.message}"
64
+ end
65
+ end
66
+
67
+ delete "/arf/:id/:cname/:date/:digest" do
68
+ begin
69
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.reportsdir, params[:cname], params[:id], params[:date]).delete_arf_file
70
+ rescue FileNotFound => e
71
+ logger.debug "Could not find requested file, #{e.message} - Assuming deleted"
72
+ end
73
+ end
74
+
75
+ get "/arf/:id/:cname/:date/:digest/html" do
76
+ begin
77
+ Proxy::OpenSCAP::OpenscapHtmlGenerator.new(params[:cname], params[:id], params[:date], params[:digest]).get_html
78
+ rescue FileNotFound => e
79
+ log_halt 500, "Could not find requested file, #{e.message}"
80
+ rescue OpenSCAPException => e
81
+ log_halt 500, "Could not generate report in HTML"
82
+ end
83
+ end
84
+
85
+ get "/policies/:policy_id/content/:digest" do
86
+ content_type 'application/xml'
87
+ begin
88
+ Proxy::OpenSCAP::FetchScapContent.new.get_policy_content(params[:policy_id], params[:digest])
89
+ rescue *HTTP_ERRORS => e
90
+ log_halt e.response.code.to_i, "File not found on Foreman. Wrong policy id?"
91
+ rescue StandardError => e
92
+ log_halt 500, "Error occurred: #{e.message}"
93
+ end
94
+ end
95
+
96
+ get "/policies/:policy_id/content" do
97
+ content_type 'application/xml'
98
+ logger.warn 'DEPRECATION WARNING: /policies/:policy_id/content/:digest should be used, please update foreman_openscap'
99
+ begin
100
+ Proxy::OpenSCAP::FetchScapContent.new.get_policy_content(params[:policy_id], 'scap_content')
101
+ rescue *HTTP_ERRORS => e
102
+ log_halt e.response.code.to_i, "File not found on Foreman. Wrong policy id?"
103
+ rescue StandardError => e
104
+ log_halt 500, "Error occurred: #{e.message}"
105
+ end
106
+ end
107
+
108
+ get "/policies/:policy_id/tailoring/:digest" do
109
+ content_type 'application/xml'
110
+ begin
111
+ Proxy::OpenSCAP::FetchTailoringFile.new.get_tailoring_file(params[:policy_id], params[:digest])
112
+ rescue *HTTP_ERRORS => e
113
+ log_halt e.response.code.to_i, "File not found on Foreman. Wrong policy id?"
114
+ rescue StandardError => e
115
+ log_halt 500, "Error occurred: #{e.message}"
116
+ end
117
+ end
118
+
119
+ post "/scap_content/policies" do
120
+ begin
121
+ Proxy::OpenSCAP::ProfilesParser.new('scap_content').profiles(request.body.string)
122
+ rescue *HTTP_ERRORS => e
123
+ log_halt 500, e.message
124
+ rescue StandardError => e
125
+ log_halt 500, "Error occurred: #{e.message}"
126
+ end
127
+ end
128
+
129
+ post "/tailoring_file/profiles" do
130
+ begin
131
+ Proxy::OpenSCAP::ProfilesParser.new('tailoring_file').profiles(request.body.string)
132
+ rescue *HTTP_ERRORS => e
133
+ log_halt 500, e.message
134
+ rescue StandardError => e
135
+ log_halt 500, "Error occurred: #{e.message}"
136
+ end
137
+ end
138
+
139
+ post "/scap_file/validator/:type" do
140
+ validate_scap_file params
141
+ end
142
+
143
+ post "/scap_content/validator" do
144
+ logger.warn "DEPRECATION WARNING: '/scap_content/validator' will be removed in the future. Use '/scap_file/validator/scap_content' instead"
145
+ params[:type] = 'scap_content'
146
+ validate_scap_file params
147
+ end
148
+
149
+ post "/scap_content/guide/?:policy?" do
150
+ begin
151
+ Proxy::OpenSCAP::PolicyParser.new(params[:policy]).guide(request.body.string)
152
+ rescue *HTTP_ERRORS => e
153
+ log_halt 500, e.message
154
+ rescue StandardError => e
155
+ log_halt 500, "Error occurred: #{e.message}"
156
+ end
157
+ end
158
+
159
+ private
160
+
161
+ def validate_scap_file(params)
162
+ begin
163
+ Proxy::OpenSCAP::ContentParser.new(params[:type]).validate(request.body.string)
164
+ rescue *HTTP_ERRORS => e
165
+ log_halt 500, e.message
166
+ rescue StandardError => e
167
+ log_halt 500, "Error occurred: #{e.message}"
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,8 @@
1
+ module Proxy::OpenSCAP
2
+ class OpenSCAPException < StandardError; end
3
+ class StoreReportError < StandardError; end
4
+ class StoreSpoolError < StandardError; end
5
+ class StoreFailedError < StandardError; end
6
+ class FileNotFound < StandardError; end
7
+ class StoreCorruptedError < StandardError; end
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'smart_proxy_openscap/storage_fs'
2
+ require 'smart_proxy_openscap/shell_wrapper'
3
+
4
+ module Proxy
5
+ module OpenSCAP
6
+ class OpenscapHtmlGenerator < ShellWrapper
7
+ def initialize(cname, id, date, digest)
8
+ @cname = cname
9
+ @id = id
10
+ @date = date
11
+ @digest = digest
12
+ @script_name = 'smart-proxy-arf-html'
13
+ end
14
+
15
+ def get_html
16
+ execute_shell_command
17
+ end
18
+
19
+ def out_filename
20
+ "#{super}-#{@cname}-#{@id}-#{@date}-#{@digest}-"
21
+ end
22
+
23
+ def command(in_file, out_file)
24
+ "#{script_location} #{file_path_in_storage} #{out_file.path}"
25
+ end
26
+
27
+ def failure_message
28
+ "Failure when running script which generates html reports"
29
+ end
30
+
31
+ def file_path_in_storage
32
+ path_to_dir = Proxy::OpenSCAP::Plugin.settings.reportsdir
33
+ storage = Proxy::OpenSCAP::StorageFS.new(path_to_dir, @cname, @id, @date)
34
+ storage.get_path(@digest)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ module Proxy::OpenSCAP
2
+ class ImportApi < ::Sinatra::Base
3
+ include ::Proxy::Log
4
+ helpers ::Proxy::Helpers
5
+ authorize_with_trusted_hosts
6
+
7
+ require 'smart_proxy_openscap/openscap_lib'
8
+
9
+ post "/arf/:cname/:policy_id/:date" do
10
+ cn = params[:cname]
11
+ date = params[:date]
12
+ policy = params[:policy_id]
13
+ log_halt(500, "Insufficient data") if (cn.nil? || date.nil?)
14
+
15
+ post_to_foreman = ForemanForwarder.new.post_arf_report(cn, policy, date, request.body.string)
16
+ begin
17
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.reportsdir, cn, post_to_foreman['id'], date).store_archive(request.body.string)
18
+ rescue Proxy::OpenSCAP::StoreReportError => e
19
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.failed_dir, cn, post_to_foreman['id'], date).store_failed(request.body.string)
20
+ logger.error "Failed to save Report in reports directory (#{Proxy::OpenSCAP::Plugin.settings.reportsdir}). Failed with: #{e.message}.
21
+ Saving file in #{Proxy::OpenSCAP::Plugin.settings.failed_dir}. Please copy manually to #{Proxy::OpenSCAP::Plugin.settings.reportsdir}"
22
+ rescue *HTTP_ERRORS => e
23
+ ### If the upload to foreman fails then store it in the spooldir
24
+ logger.error "Failed to upload to Foreman, saving in spool. Failed with: #{e.message}"
25
+ Proxy::OpenSCAP::StorageFS.new(Proxy::OpenSCAP::Plugin.settings.spooldir, cn, policy, date).store_spool(request.body.string)
26
+ rescue Proxy::OpenSCAP::StoreSpoolError => e
27
+ log_halt 500, e.message
28
+ end
29
+ {:success => true, :arf_id => post_to_foreman['id']}.to_json
30
+ end
31
+ end
32
+ end