blob: 6682ae38b5923135ebcd245e76adaef9ccf3e4dd [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
7def oscap_has_sce():
8 (stdout, _, _) = run('oscap -V')
9 return any([x for x in stdout.splitlines() if x.startswith('SCE Vers')])
10
11def xccdf(benchmark,
12 pillar_data=None,
13 xccdf_version='1.2',
14 profile='default',
15 tailoring_id=None):
16
17 tailoring_file = None
18 profile = normalize_id(profile, xccdf_version=xccdf_version)
19
20 tempdir = tempfile.mkdtemp(prefix='oscap-')
21
22 if pillar_data:
23 if not tailoring_id:
24 raise Exception('Tailoring id must be set!')
25 profile = normalize_id(pillar_data['profile'], typeof='profile')
26 tailoring_file = os.path.join(tempdir, 'tailoring.xml')
27
28 cmd = 'oscap xccdf eval --profile {profile} ' +\
29 '--results results.xml --report report.html'
30 if oscap_has_sce():
31 cmd += ' --sce-results'
32 if tailoring_file:
33 cmd += ' --tailoring-file {tailoring_file}'
34 cmd += ' {benchmark}'
35 cmd = cmd.format(profile=profile,
36 tailoring_file=tailoring_file,
37 benchmark=benchmark)
38
39 if tailoring_file:
40 with open(tailoring_file, 'w') as f:
41 f.write(build_tailoring(pillar_data, tailoring_id))
42
43 stdout, stderr, rc = run(cmd, tempdir)
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +030044 res_file = os.path.join(tempdir, 'results.xml')
45 if os.path.isfile(res_file):
46 xccdf_xml_to_json(res_file)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040047 return stdout, stderr, rc, tempdir
48
49def oval(benchmark):
50 tempdir = tempfile.mkdtemp(prefix='oscap-')
51 cmd = 'oscap oval eval --results results.xml --report report.html {}'
52 cmd = cmd.format(benchmark)
53
54 stdout, stderr, rc = run(cmd, tempdir)
Pavlo Shchelokovskyy4a8f1c12018-09-21 19:17:19 +030055 res_file = os.path.join(tempdir, 'results.xml')
56 if os.path.isfile(res_file):
57 oval_xml_to_json(res_file)
Ivan Suzdal184c4e32018-06-06 13:55:30 +040058 return stdout, stderr, rc, tempdir