| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import subprocess |
| 20 |
import platform |
| 21 |
import os |
| 22 |
import collections |
| 23 |
|
| 24 |
|
| 25 |
class OscapError(Exception): |
| 26 |
''' oscap Error''' |
| 27 |
pass |
| 28 |
|
| 29 |
OscapResult = collections.namedtuple("OscapResult", ("returncode", "stdout", "stderr")) |
| 30 |
|
| 31 |
|
| 32 |
def oscap_chroot(chroot_path, oscap_binary, oscap_args, target_name, local_env=[]): |
| 33 |
''' |
| 34 |
Wrapper running oscap_chroot on an OscapDockerScan OscapAtomicScan object |
| 35 |
''' |
| 36 |
os.environ["OSCAP_PROBE_ROOT"] = os.path.join(chroot_path) |
| 37 |
os.environ["OSCAP_EVALUATION_TARGET"] = target_name |
| 38 |
os.environ["OSCAP_CONTAINER_VARS"] = '\n'.join(local_env) |
| 39 |
|
| 40 |
cmd = [oscap_binary] + [x for x in oscap_args] |
| 41 |
|
| 42 |
oscap_process = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 43 |
stderr=subprocess.PIPE) |
| 44 |
oscap_stdout, oscap_stderr = oscap_process.communicate() |
| 45 |
return OscapResult(oscap_process.returncode, |
| 46 |
oscap_stdout.decode("utf-8"), |
| 47 |
oscap_stderr.decode("utf-8")) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
def get_dist(mountpoint, oscap_binary, local_env): |
| 53 |
CPE_RHEL = 'oval:org.open-scap.cpe.rhel:def:' |
| 54 |
DISTS = ["8", "7", "6", "5"] |
| 55 |
|
| 56 |
''' |
| 57 |
Test the chroot and determine what RHEL dist it is; returns |
| 58 |
an integer representing the dist |
| 59 |
''' |
| 60 |
|
| 61 |
cpe_dict = '/usr/share/openscap/cpe/openscap-cpe-oval.xml' |
| 62 |
if not os.path.exists(cpe_dict): |
| 63 |
|
| 64 |
cpe_dict = '/usr/local/share/openscap/cpe/openscap-cpe-oval.xml' |
| 65 |
if not os.path.exists(cpe_dict): |
| 66 |
raise OscapError() |
| 67 |
|
| 68 |
for dist in DISTS: |
| 69 |
result = oscap_chroot( |
| 70 |
mountpoint, oscap_binary, |
| 71 |
("oval", "eval", "--id", CPE_RHEL + dist, cpe_dict, |
| 72 |
mountpoint, "2>&1", ">", "/dev/null"), |
| 73 |
'*', |
| 74 |
local_env |
| 75 |
) |
| 76 |
|
| 77 |
if "{0}{1}: true".format(CPE_RHEL, dist) in result.stdout: |
| 78 |
print("This system seems based on RHEL{0}.".format(dist)) |
| 79 |
return dist |