Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 1 | from __future__ import absolute_import |
| 2 | import tempfile |
| 3 | import os |
| 4 | from oscap.utils import build_tailoring, normalize_id, run |
Pavlo Shchelokovskyy | 4a8f1c1 | 2018-09-21 19:17:19 +0300 | [diff] [blame] | 5 | from oscap.utils import xccdf_xml_to_json, oval_xml_to_json |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 6 | |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 7 | |
| 8 | def 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 Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 27 | 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 Shchelokovskyy | 4a8f1c1 | 2018-09-21 19:17:19 +0300 | [diff] [blame] | 39 | res_file = os.path.join(tempdir, 'results.xml') |
| 40 | if os.path.isfile(res_file): |
| 41 | xccdf_xml_to_json(res_file) |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 42 | return stdout, stderr, rc, tempdir |
| 43 | |
| 44 | def 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 Shchelokovskyy | 4a8f1c1 | 2018-09-21 19:17:19 +0300 | [diff] [blame] | 50 | res_file = os.path.join(tempdir, 'results.xml') |
| 51 | if os.path.isfile(res_file): |
| 52 | oval_xml_to_json(res_file) |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 53 | return stdout, stderr, rc, tempdir |