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', |
Ivan Udovichenko | 1ad59ca | 2018-11-30 17:01:12 +0300 | [diff] [blame^] | 12 | tailoring_id=None, |
| 13 | cpe=None): |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 14 | |
| 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 Udovichenko | 1ad59ca | 2018-11-30 17:01:12 +0300 | [diff] [blame^] | 28 | if cpe: |
| 29 | cmd += ' --cpe {cpe}' |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 30 | if tailoring_file: |
| 31 | cmd += ' --tailoring-file {tailoring_file}' |
| 32 | cmd += ' {benchmark}' |
| 33 | cmd = cmd.format(profile=profile, |
| 34 | tailoring_file=tailoring_file, |
Ivan Udovichenko | 1ad59ca | 2018-11-30 17:01:12 +0300 | [diff] [blame^] | 35 | benchmark=benchmark, |
| 36 | cpe=cpe) |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 37 | |
| 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 Shchelokovskyy | 4a8f1c1 | 2018-09-21 19:17:19 +0300 | [diff] [blame] | 43 | res_file = os.path.join(tempdir, 'results.xml') |
| 44 | if os.path.isfile(res_file): |
| 45 | xccdf_xml_to_json(res_file) |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 46 | return stdout, stderr, rc, tempdir |
| 47 | |
| 48 | def 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 Shchelokovskyy | 4a8f1c1 | 2018-09-21 19:17:19 +0300 | [diff] [blame] | 54 | res_file = os.path.join(tempdir, 'results.xml') |
| 55 | if os.path.isfile(res_file): |
| 56 | oval_xml_to_json(res_file) |
Ivan Suzdal | 184c4e3 | 2018-06-06 13:55:30 +0400 | [diff] [blame] | 57 | return stdout, stderr, rc, tempdir |