-
Notifications
You must be signed in to change notification settings - Fork 25
/
openscap_content_parser.rb
74 lines (64 loc) · 1.78 KB
/
openscap_content_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 { |obj| obj[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