Debian Codegraph @

utils/oscap_docker_python/oscap_docker_common.py

raw symbols SCIP
1 # Copyright (C) 2015 Brent Baude <[email protected]>
2 # Copyright (C) 2019 Dominique Blaze <[email protected]>
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the
16 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 # Boston, MA 02110-1301 USA
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 # TODO replace by _get_cpe (in order to indentify any containerized system)
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 # sometime it's installed into /usr/local/share instead of /usr/local
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