Sha256: 73ddb22eae91be4ec86fe4c7dc0944627fcb3199845d1594802d72a12f125df2

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require 'openscap/ds/sds'
require 'openscap/source'
require 'openscap/xccdf/benchmark'
require 'openscap/xccdf/tailoring'

module Proxy::OpenSCAP
  class ContentParser
    def initialize(scap_file, type = 'scap_content')
      OpenSCAP.oscap_init
      @source = OpenSCAP::Source.new(:content => scap_file)
      @type = type
    end

    def allowed_types
      {
        'tailoring_file' => 'XCCDF Tailoring',
        'scap_content' => 'SCAP Source Datastream'
      }
    end

    def extract_policies
      policies = {}
      bench = benchmark_profiles
      bench.profiles.each do |key, profile|
        policies[key] = profile.title
      end
      bench.destroy
      policies.to_json
    end

    def get_profiles
      tailoring = ::OpenSCAP::Xccdf::Tailoring.new(@source, nil)
      profiles = tailoring.profiles.inject({}) do |memo, (key, profile)|
        memo.tap { |hash| hash[key] = profile.title }
      end
      tailoring.destroy
      profiles.to_json
    end

    def validate
      errors = []

      if @source.type != allowed_types[@type]
        errors << "Uploaded file is #{@source.type}, unexpected file type"
      end

      begin
        @source.validate!
      rescue OpenSCAP::OpenSCAPError
        errors << "Invalid SCAP file type"
      end
      {:errors => errors}.to_json
    end

    def guide(policy)
      sds = OpenSCAP::DS::Sds.new @source
      sds.select_checklist
      profile_id = policy ? nil : policy
      html = sds.html_guide profile_id
      sds.destroy
      {:html => html.force_encoding('UTF-8')}.to_json
    end

    private

    def benchmark_profiles
      sds          = ::OpenSCAP::DS::Sds.new(@source)
      bench_source = sds.select_checklist!
      benchmark = ::OpenSCAP::Xccdf::Benchmark.new(bench_source)
      sds.destroy
      benchmark
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smart_proxy_openscap-0.6.2 lib/smart_proxy_openscap/openscap_content_parser.rb
smart_proxy_openscap-0.6.1 lib/smart_proxy_openscap/openscap_content_parser.rb