Class: Proxy::OpenSCAP::ContentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_openscap/openscap_content_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(scap_file, type = 'scap_content') ⇒ ContentParser

Returns a new instance of ContentParser.



8
9
10
11
12
# File 'lib/smart_proxy_openscap/openscap_content_parser.rb', line 8

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

Instance Method Details

#allowed_typesObject



14
15
16
17
18
19
# File 'lib/smart_proxy_openscap/openscap_content_parser.rb', line 14

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

#extract_policiesObject



21
22
23
24
25
26
27
28
29
# File 'lib/smart_proxy_openscap/openscap_content_parser.rb', line 21

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

#get_profilesObject



31
32
33
34
35
36
37
38
# File 'lib/smart_proxy_openscap/openscap_content_parser.rb', line 31

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

#guide(policy) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/smart_proxy_openscap/openscap_content_parser.rb', line 55

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

#validateObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smart_proxy_openscap/openscap_content_parser.rb', line 40

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