blob: a9b342dddb96997c676fc5ffd6a37a51bbd59e1a [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',
Ivan Udovichenko1ad59ca2018-11-30 17:01:12 +030012 tailoring_id=None,
13 cpe=None):
Ivan Suzdal184c4e32018-06-06 13:55:30 +040014
15 tailoring_file = None
16 profile = normalize_id(profile, xccdf_version=xccdf_version)
17
18 tempdir = tempfile.mkdtemp(prefix='oscap-')
19
20 if pillar_data:
21 if not tailoring_id:
22 raise Exception('Tailoring id must be set!')
23 profile = normalize_id(pillar_data['profile'], typeof='profile')
24 tailoring_file = os.path.join(tempdir, 'tailoring.xml')
25
26 cmd = 'oscap xccdf eval --profile {profile} ' +\
27 '--results results.xml --report report.html'
Ivan Udovichenko1ad59ca2018-11-30 17:01:12 +030028 if cpe:
29 cmd += ' --cpe {cpe}'
Ivan Suzdal184c4e32018-06-06 13:55:30 +040030 if tailoring_file:
31 cmd += ' --tailoring-file {tailoring_file}'
32 cmd += ' {benchmark}'
33 cmd = cmd.format(profile=profile,
34 tailoring_file=tailoring_file,
Ivan Udovichenko1ad59ca2018-11-30 17:01:12 +030035 benchmark=benchmark,
36 cpe=cpe)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040037
38 if tailoring_file:
39 with open(tailoring_file, 'w') as f:
40 f.write(build_tailoring(pillar_data, tailoring_id))
41
42 stdout, stderr, rc = run(cmd, tempdir)
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +030043 res_file = os.path.join(tempdir, 'results.xml')
44 if os.path.isfile(res_file):
45 xccdf_xml_to_json(res_file)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040046 return stdout, stderr, rc, tempdir
47
48def oval(benchmark):
49 tempdir = tempfile.mkdtemp(prefix='oscap-')
50 cmd = 'oscap oval eval --results results.xml --report report.html {}'
51 cmd = cmd.format(benchmark)
52
53 stdout, stderr, rc = run(cmd, tempdir)
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +030054 res_file = os.path.join(tempdir, 'results.xml')
55 if os.path.isfile(res_file):
56 oval_xml_to_json(res_file)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040057 return stdout, stderr, rc, tempdir