class ForemanScapClient::Client

Public Instance Methods

run(policy_id) click to toggle source
# File lib/foreman_scap_client/client.rb, line 11
def run(policy_id)
  @policy_id = policy_id

  Dir.mktmpdir do |dir|
    @tmp_dir = dir
    scan
    bzip
    upload
  end
end

Private Instance Methods

bzip() click to toggle source
# File lib/foreman_scap_client/client.rb, line 65
def bzip
  puts 'DEBUG: running: ' + bzip_command
  result = %x#{bzip_command}`
  if !$?.success?
    puts 'bzip failed'
    puts results
    exit(2)
  end
end
bzip_command() click to toggle source
# File lib/foreman_scap_client/client.rb, line 61
def bzip_command
  "/usr/bin/bzip2 #{results_path}"
end
config() click to toggle source
# File lib/foreman_scap_client/client.rb, line 24
def config
  @config ||= YAML.load_file(CONFIG_FILE)
rescue => e
  puts 'Config file could not be loaded'
  puts e.message
  exit(1)
end
foreman_proxy_uri() click to toggle source
# File lib/foreman_scap_client/client.rb, line 109
def foreman_proxy_uri
  foreman_proxy_fqdn = config[:server]
  foreman_proxy_port = config[:port]
  "https://#{foreman_proxy_fqdn}:#{foreman_proxy_port}"
end
results_bzip_path() click to toggle source
# File lib/foreman_scap_client/client.rb, line 48
def results_bzip_path
  "#{results_path}.bz2"
end
results_path() click to toggle source
# File lib/foreman_scap_client/client.rb, line 44
def results_path
  "#{@tmp_dir}/results.xml"
end
scan() click to toggle source
# File lib/foreman_scap_client/client.rb, line 32
def scan
  puts "DEBUG: running: " + scan_command
  result = %x#{scan_command}`
  if $?.success? || $?.exitstatus == 2
    @report = results_path
  else
    puts 'Scan failed'
    puts result
    exit(2)
  end
end
scan_command() click to toggle source
# File lib/foreman_scap_client/client.rb, line 52
def scan_command
  if config[@policy_id] && config[@policy_id][:profile] && !config[@policy_id][:profile].empty?
    profile = "--profile #{config[@policy_id][:profile]}"
  else
    profile = ''
  end
  "oscap xccdf eval #{profile} --results-arf #{results_path} #{config[@policy_id][:content_path]}"
end
upload() click to toggle source
# File lib/foreman_scap_client/client.rb, line 75
def upload
  uri = URI.parse(upload_uri)
  puts "Uploading results to #{uri}"
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_PEER
  https.ca_file = config[:ca_file]
  begin
    https.cert = OpenSSL::X509::Certificate.new File.read(config[:host_certificate])
    https.key = OpenSSL::PKey::RSA.new File.read(config[:host_private_key])
  rescue StandardError => e
    puts 'Unable to load certs'
    puts e.message
    exit(3)
  end

  request = Net::HTTP::Put.new uri.path
  request.body = File.read(results_bzip_path)
  request['Content-Type'] = 'text/xml'
  request['Content-Encoding'] = 'x-bzip2'
  begin
    res = https.request(request)
    res.value
  rescue StandardError => e
    puts res.body if res
    puts "Upload failed: #{e.message}"
    exit(4)
  end
end
upload_uri() click to toggle source
# File lib/foreman_scap_client/client.rb, line 105
def upload_uri
  foreman_proxy_uri + "/compliance/arf/#{@policy_id}"
end