adamwill / rpms / openscap

Forked from rpms/openscap 3 years ago
Clone

Blame SOURCES/openscap-1.2.17-oscap-docker-cleanup-temp-image.patch

bcc67f
From eea0fd27e7bed6a225bbd6702960bcf394f19536 Mon Sep 17 00:00:00 2001
bcc67f
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
bcc67f
Date: Wed, 24 Jan 2018 17:39:04 +0100
bcc67f
Subject: [PATCH 1/2] Modified the code that temp images are not forgotten.
bcc67f
bcc67f
---
bcc67f
 utils/oscap_docker_python/oscap_docker_util.py | 20 ++++++++++++--------
bcc67f
 1 file changed, 12 insertions(+), 8 deletions(-)
bcc67f
bcc67f
diff --git a/utils/oscap_docker_python/oscap_docker_util.py b/utils/oscap_docker_python/oscap_docker_util.py
bcc67f
index b06b6001a..b9a08a99c 100644
bcc67f
--- a/utils/oscap_docker_python/oscap_docker_util.py
bcc67f
+++ b/utils/oscap_docker_python/oscap_docker_util.py
bcc67f
@@ -155,7 +155,8 @@ def oscap_chroot(self, chroot_path, target, *oscap_args):
bcc67f
             sys.stderr.write(oscap_stderr.decode("utf-8") + "\n")
bcc67f
 
bcc67f
             # Clean up
bcc67f
-            self._cleanup_by_path(chroot_path)
bcc67f
+            DM = DockerMount("/tmp")
bcc67f
+            self._cleanup_by_path(chroot_path, DM)
bcc67f
 
bcc67f
             sys.exit(1)
bcc67f
 
bcc67f
@@ -186,18 +187,17 @@ def resolve_image(self, image):
bcc67f
         # TODO
bcc67f
         pass
bcc67f
 
bcc67f
-    def _cleanup_by_path(self, path):
bcc67f
+    def _cleanup_by_path(self, path, DM):
bcc67f
         '''
bcc67f
         Cleans up the mounted chroot by umounting it and
bcc67f
         removing the temporary directory
bcc67f
         '''
bcc67f
         # Sometimes when this def is called, path will have 'rootfs'
bcc67f
         # appended.  If it does, strip it and proceed
bcc67f
+        _no_rootfs = path
bcc67f
+        if os.path.basename(path) == 'rootfs':
bcc67f
+            _no_rootfs = os.path.dirname(path)
bcc67f
 
bcc67f
-        _no_rootfs = os.path.dirname(path) if os.path.basename(path) == \
bcc67f
-            'rootfs' else path
bcc67f
-
bcc67f
-        DM = DockerMount("/tmp")
bcc67f
         # umount chroot
bcc67f
         DM.unmount_path(_no_rootfs)
bcc67f
 
bcc67f
@@ -206,6 +206,10 @@ def _cleanup_by_path(self, path):
bcc67f
         os.rmdir(_no_rootfs)
bcc67f
 
bcc67f
 
bcc67f
+def mount_image_filesystem():
bcc67f
+            _tmp_mnt_dir = DM.mount(image)
bcc67f
+
bcc67f
+
bcc67f
 class OscapScan(object):
bcc67f
     def __init__(self, tmp_dir=tempfile.gettempdir(), mnt_dir=None,
bcc67f
                  hours_old=2):
bcc67f
@@ -276,7 +280,7 @@ def scan_cve(self, image, scan_args):
bcc67f
 
bcc67f
         finally:
bcc67f
             # Clean up
bcc67f
-            self.helper._cleanup_by_path(_tmp_mnt_dir)
bcc67f
+            self.helper._cleanup_by_path(_tmp_mnt_dir, DM)
bcc67f
             self._remove_mnt_dir(mnt_dir)
bcc67f
 
bcc67f
     def scan(self, image, scan_args):
bcc67f
@@ -301,5 +305,5 @@ def scan(self, image, scan_args):
bcc67f
         sys.stdout.write(self.helper._scan(chroot, image, scan_args))
bcc67f
 
bcc67f
         # Clean up
bcc67f
-        self.helper._cleanup_by_path(_tmp_mnt_dir)
bcc67f
+        self.helper._cleanup_by_path(_tmp_mnt_dir, DM)
bcc67f
         self._remove_mnt_dir(mnt_dir)
bcc67f
bcc67f
From 432ee1841003b57408e7a1040c6f317cc56a9071 Mon Sep 17 00:00:00 2001
bcc67f
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
bcc67f
Date: Thu, 25 Jan 2018 14:03:48 +0100
bcc67f
Subject: [PATCH 2/2] Refactored error handling during scan.
bcc67f
bcc67f
---
bcc67f
 utils/oscap_docker_python/oscap_docker_util.py | 24 +++++++++++-------------
bcc67f
 1 file changed, 11 insertions(+), 13 deletions(-)
bcc67f
bcc67f
diff --git a/utils/oscap_docker_python/oscap_docker_util.py b/utils/oscap_docker_python/oscap_docker_util.py
bcc67f
index b9a08a99c..ca48d5846 100644
bcc67f
--- a/utils/oscap_docker_python/oscap_docker_util.py
bcc67f
+++ b/utils/oscap_docker_python/oscap_docker_util.py
bcc67f
@@ -154,10 +154,6 @@ def oscap_chroot(self, chroot_path, target, *oscap_args):
bcc67f
             sys.stderr.write("Command returned exit code {0}.\n".format(oscap_process.returncode))
bcc67f
             sys.stderr.write(oscap_stderr.decode("utf-8") + "\n")
bcc67f
 
bcc67f
-            # Clean up
bcc67f
-            DM = DockerMount("/tmp")
bcc67f
-            self._cleanup_by_path(chroot_path, DM)
bcc67f
-
bcc67f
             sys.exit(1)
bcc67f
 
bcc67f
         sys.stderr.write(oscap_stderr.decode("utf-8") + "\n")
bcc67f
@@ -207,7 +203,7 @@ def _cleanup_by_path(self, path, DM):
bcc67f
 
bcc67f
 
bcc67f
 def mount_image_filesystem():
bcc67f
-            _tmp_mnt_dir = DM.mount(image)
bcc67f
+    _tmp_mnt_dir = DM.mount(image)
bcc67f
 
bcc67f
 
bcc67f
 class OscapScan(object):
bcc67f
@@ -261,9 +257,9 @@ def scan_cve(self, image, scan_args):
bcc67f
             sys.stderr.write(str(e) + "\n")
bcc67f
             return None
bcc67f
 
bcc67f
-        chroot = self._find_chroot_path(_tmp_mnt_dir)
bcc67f
-
bcc67f
         try:
bcc67f
+            chroot = self._find_chroot_path(_tmp_mnt_dir)
bcc67f
+
bcc67f
             # Figure out which RHEL dist is in the chroot
bcc67f
             dist = self.helper._get_dist(chroot, image)
bcc67f
 
bcc67f
@@ -299,11 +295,13 @@ def scan(self, image, scan_args):
bcc67f
             sys.stderr.write(str(e) + "\n")
bcc67f
             return None
bcc67f
 
bcc67f
-        chroot = self._find_chroot_path(_tmp_mnt_dir)
bcc67f
+        try:
bcc67f
+            chroot = self._find_chroot_path(_tmp_mnt_dir)
bcc67f
 
bcc67f
-        # Scan the chroot
bcc67f
-        sys.stdout.write(self.helper._scan(chroot, image, scan_args))
bcc67f
+            # Scan the chroot
bcc67f
+            sys.stdout.write(self.helper._scan(chroot, image, scan_args))
bcc67f
 
bcc67f
-        # Clean up
bcc67f
-        self.helper._cleanup_by_path(_tmp_mnt_dir, DM)
bcc67f
-        self._remove_mnt_dir(mnt_dir)
bcc67f
+        finally:
bcc67f
+            # Clean up
bcc67f
+            self.helper._cleanup_by_path(_tmp_mnt_dir, DM)
bcc67f
+            self._remove_mnt_dir(mnt_dir)