# File lib/smart_proxy_openscap/openscap_lib.rb, line 124 def do(arf_dir) Dir.foreach(arf_dir) { |cname| cname_dir = File.join(arf_dir, cname) if File.directory? cname_dir and !(cname == '.' || cname == '..') forward_cname_dir(cname, cname_dir) end } end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 170 def forward_arf_file(foreman_api_path, arf_file_path) begin data = File.read(arf_file_path) response = send_request(foreman_api_path, data) # Raise an HTTP error if the response is not 2xx (success). response.value res = JSON.parse(response.body) raise StandardError, "Received result: #{res['result']}" unless res['result'] == 'OK' File.delete arf_file_path rescue StandardError => e logger.debug response.body if response raise e end end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 134 def forward_cname_dir(cname, cname_dir) Dir.foreach(cname_dir) { |policy_id| policy_dir = File.join(cname_dir, policy_id) if File.directory? policy_dir and !(policy_id == '.' || policy_id == '..') forward_policy_dir(cname, policy_id, policy_dir) end } remove(cname_dir) end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 154 def forward_date_dir(cname, policy_id, date, date_dir) path = upload_path(cname, policy_id, date) Dir.foreach(date_dir) { |arf| arf_path = File.join(date_dir, arf) if File.file? arf_path and !(arf == '.' || arf == '..') logger.debug("Uploading #{arf} to #{path}") forward_arf_file(path, arf_path) end } remove(date_dir) end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 144 def forward_policy_dir(cname, policy_id, policy_dir) Dir.foreach(policy_dir) { |date| date_dir = File.join(policy_dir, date) if File.directory? date_dir and !(date == '.' || date == '..') forward_date_dir(cname, policy_id, date, date_dir) end } remove(policy_dir) end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 185 def remove(dir) begin Dir.delete dir rescue StandardError => e logger.error "Could not remove directory: #{e.message}" end end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 193 def send_request(path, body) # Override the parent method to set the right headers path = [uri.path, path].join('/') unless uri.path.empty? req = Net::HTTP::Post.new(URI.join(uri.to_s, path).path) # Well, this is unfortunate. We want to have content-type text/xml. We # also need the content-encoding to equal with x-bzip2. However, when # the Foreman's framework sees text/xml, it will rewrite it to application/xml. # What's worse, a framework will try to parse body as an utf8 string, # no matter what content-encoding says. Oh my. # Let's pass content-type arf-bzip2 and move forward. req.content_type = 'application/arf-bzip2' req['Content-Encoding'] = 'x-bzip2' req.body = body http.request(req) end
# File lib/smart_proxy_openscap/openscap_lib.rb, line 166 def upload_path(cname, policy_id, date) return "/api/v2/compliance/arf_reports/#{cname}/#{policy_id}/#{date}" end