blob: 69e7fbc1c1730d5b239751d42434b6c6811c3f6a [file] [log] [blame]
Ivan Suzdal184c4e32018-06-06 13:55:30 +04001from __future__ import absolute_import
2import tempfile
3import os
4from oscap.utils import build_tailoring, normalize_id, run
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +03005from oscap.utils import xccdf_xml_to_json, oval_xml_to_json
Ivan Suzdal184c4e32018-06-06 13:55:30 +04006
Ivan Suzdal184c4e32018-06-06 13:55:30 +04007
8def xccdf(benchmark,
9 pillar_data=None,
10 xccdf_version='1.2',
11 profile='default',
12 tailoring_id=None):
13
14 tailoring_file = None
15 profile = normalize_id(profile, xccdf_version=xccdf_version)
16
17 tempdir = tempfile.mkdtemp(prefix='oscap-')
18
19 if pillar_data:
20 if not tailoring_id:
21 raise Exception('Tailoring id must be set!')
22 profile = normalize_id(pillar_data['profile'], typeof='profile')
23 tailoring_file = os.path.join(tempdir, 'tailoring.xml')
24
25 cmd = 'oscap xccdf eval --profile {profile} ' +\
26 '--results results.xml --report report.html'
Ivan Suzdal184c4e32018-06-06 13:55:30 +040027 if tailoring_file:
28 cmd += ' --tailoring-file {tailoring_file}'
29 cmd += ' {benchmark}'
30 cmd = cmd.format(profile=profile,
31 tailoring_file=tailoring_file,
32 benchmark=benchmark)
33
34 if tailoring_file:
35 with open(tailoring_file, 'w') as f:
36 f.write(build_tailoring(pillar_data, tailoring_id))
37
38 stdout, stderr, rc = run(cmd, tempdir)
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +030039 res_file = os.path.join(tempdir, 'results.xml')
40 if os.path.isfile(res_file):
41 xccdf_xml_to_json(res_file)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040042 return stdout, stderr, rc, tempdir
43
44def oval(benchmark):
45 tempdir = tempfile.mkdtemp(prefix='oscap-')
46 cmd = 'oscap oval eval --results results.xml --report report.html {}'
47 cmd = cmd.format(benchmark)
48
49 stdout, stderr, rc = run(cmd, tempdir)
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +030050 res_file = os.path.join(tempdir, 'results.xml')
51 if os.path.isfile(res_file):
52 oval_xml_to_json(res_file)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040053 return stdout, stderr, rc, tempdir