17
17
18
18
''' Utilities for oscap-docker '''
19
19
20
+ from __future__ import print_function
21
+
20
22
import os
21
23
import tempfile
22
24
import subprocess
25
27
from oscap_docker_python .get_cve_input import getInputCVE
26
28
import sys
27
29
import docker
30
+ import collections
28
31
29
32
try :
30
33
from Atomic .mount import DockerMount
@@ -68,6 +71,9 @@ class OscapError(Exception):
68
71
pass
69
72
70
73
74
+ OscapResult = collections .namedtuple ("OscapResult" , ("returncode" , "stdout" , "stderr" ))
75
+
76
+
71
77
class OscapHelpers (object ):
72
78
''' oscap class full of helpers for scanning '''
73
79
CPE = 'oval:org.open-scap.cpe.rhel:def:'
@@ -102,10 +108,10 @@ def _get_dist(self, chroot, target):
102
108
if not os .path .exists (cpe_dict ):
103
109
raise OscapError ()
104
110
for dist in self .DISTS :
105
- output = self .oscap_chroot (chroot , target , 'oval' , 'eval' ,
111
+ result = self .oscap_chroot (chroot , target , 'oval' , 'eval' ,
106
112
'--id' , self .CPE + dist , cpe_dict ,
107
113
'2>&1' , '>' , '/dev/null' )
108
- if "{0}{1}: true" .format (self .CPE , dist ) in output :
114
+ if "{0}{1}: true" .format (self .CPE , dist ) in result . stdout :
109
115
return dist
110
116
111
117
def _get_target_name (self , target ):
@@ -149,15 +155,8 @@ def oscap_chroot(self, chroot_path, target, *oscap_args):
149
155
cmd = ['oscap' ] + [x for x in oscap_args ]
150
156
oscap_process = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
151
157
oscap_stdout , oscap_stderr = oscap_process .communicate ()
152
- if oscap_process .returncode not in [0 , 2 ]:
153
- sys .stderr .write ("\n Command: {0} failed!\n " .format (" " .join (cmd )))
154
- sys .stderr .write ("Command returned exit code {0}.\n " .format (oscap_process .returncode ))
155
- sys .stderr .write (oscap_stderr .decode ("utf-8" ) + "\n " )
156
-
157
- sys .exit (1 )
158
-
159
- sys .stderr .write (oscap_stderr .decode ("utf-8" ) + "\n " )
160
- return oscap_stdout .decode ("utf-8" )
158
+ return OscapResult (oscap_process .returncode ,
159
+ oscap_stdout .decode ("utf-8" ), oscap_stderr .decode ("utf-8" ))
161
160
162
161
def _scan_cve (self , chroot , target , dist , scan_args ):
163
162
'''
@@ -272,13 +271,17 @@ def scan_cve(self, image, scan_args):
272
271
fetch ._fetch_single (dist )
273
272
274
273
# Scan the chroot
275
- sys .stdout .write (self .helper ._scan_cve (chroot , image , dist , scan_args ))
274
+ scan_result = self .helper ._scan_cve (chroot , image , dist , scan_args )
275
+ print (scan_result .stdout )
276
+ print (scan_result .stderr , file = sys .stderr )
276
277
277
278
finally :
278
279
# Clean up
279
280
self .helper ._cleanup_by_path (_tmp_mnt_dir , DM )
280
281
self ._remove_mnt_dir (mnt_dir )
281
282
283
+ return scan_result .returncode
284
+
282
285
def scan (self , image , scan_args ):
283
286
'''
284
287
Wrapper function for basic security scans using
@@ -299,9 +302,13 @@ def scan(self, image, scan_args):
299
302
chroot = self ._find_chroot_path (_tmp_mnt_dir )
300
303
301
304
# Scan the chroot
302
- sys .stdout .write (self .helper ._scan (chroot , image , scan_args ))
305
+ scan_result = self .helper ._scan (chroot , image , scan_args )
306
+ print (scan_result .stdout )
307
+ print (scan_result .stderr , file = sys .stderr )
303
308
304
309
finally :
305
310
# Clean up
306
311
self .helper ._cleanup_by_path (_tmp_mnt_dir , DM )
307
312
self ._remove_mnt_dir (mnt_dir )
313
+
314
+ return scan_result .returncode
0 commit comments