class Proxy::OpenSCAP::ForemanForwarder

Public Instance Methods

do(arf_dir) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 75
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

Private Instance Methods

forward_arf_file(foreman_api_path, arf_file_path) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 121
def forward_arf_file(foreman_api_path, arf_file_path)
  begin
    data = File.read(arf_file_path)
    response = send_request(foreman_api_path, data)
    response.value
    raise StandardError, "Received #{response.code}: #{response.message}" unless response.code.to_i == 200
    res = JSON.parse(response.body)
    raise StandardError, "Received result: #{res['result']}" unless res['result'] == 'OK'
    raise StandardError, "Sent bytes: #{data.length}, but foreman received: #{res['received']}" unless data.length == res['received']
    File.delete arf_file_path
  rescue StandardError => e
    logger.debug response.body if response
    raise e
  end
end
forward_cname_dir(cname, cname_dir) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 85
def forward_cname_dir(cname, cname_dir)
  Dir.foreach(cname_dir) { |policy_name|
    policy_dir = File.join(cname_dir, policy_name)
    if File.directory? policy_dir and !(policy_name == '.' || policy_name == '..')
      forward_policy_dir(cname, policy_name, policy_dir)
    end
  }
  remove cname_dir
end
forward_date_dir(cname, policy_name, date, date_dir) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 105
def forward_date_dir(cname, policy_name, date, date_dir)
  path = upload_path(cname, policy_name, 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
forward_policy_dir(cname, policy_name, policy_dir) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 95
def forward_policy_dir(cname, policy_name, 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_name, date, date_dir)
    end
  }
  remove policy_dir
end
remove(dir) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 137
def remove(dir)
  begin
    Dir.delete dir
  rescue StandardError => e
    logger.error "Could not remove directory: #{e.message}"
  end
end
send_request(path, body) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 145
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
upload_path(cname, policy_name, date) click to toggle source
# File lib/foreman-proxy_openscap/openscap_lib.rb, line 117
def upload_path(cname, policy_name, date)
  return "/api/v2/openscap/arf_reports/#{cname}/#{policy_name}/#{date}"
end