adamwill / rpms / openscap

Forked from rpms/openscap 3 years ago
Clone

Blame SOURCES/04-oscap-podman-detect-ambiguous-targets.patch

2f7d90
From 532a6c77f388d2e06ec12338df9ea97d955f5edc Mon Sep 17 00:00:00 2001
2f7d90
From: Matus Marhefka <mmarhefk@redhat.com>
2f7d90
Date: Thu, 16 Jan 2020 15:39:37 +0100
2f7d90
Subject: [PATCH] utils/oscap-podman: Detect ambiguous scan target
2f7d90
2f7d90
In case that a container image and a running container have the same
2f7d90
name, `oscap-podman` scans container image and a running container is
2f7d90
skipped. This might be unexpected and might cause a confusion for user.
2f7d90
Therefore, this commit adds a code which detects such situation and
2f7d90
rather informs user about ambiguous scan target and terminates.
2f7d90
In such cases the unique container image/container ID should be used
2f7d90
for specifying the target of the scan.
2f7d90
---
2f7d90
 utils/oscap-podman | 23 ++++++++++++++++++-----
2f7d90
 1 file changed, 18 insertions(+), 5 deletions(-)
2f7d90
2f7d90
diff --git a/utils/oscap-podman b/utils/oscap-podman
2f7d90
index 272afd988..32ec0cfcb 100755
2f7d90
--- a/utils/oscap-podman
2f7d90
+++ b/utils/oscap-podman
2f7d90
@@ -65,17 +65,30 @@ if grep -q "\-\-remediate" <<< "$@"; then
2f7d90
     die
2f7d90
 fi
2f7d90
 
2f7d90
+IMAGE_NAME=$(podman image exists "$1" \
2f7d90
+    && podman image inspect --format "{{.Id}} {{.RepoTags}}" "$1")
2f7d90
+CONTAINER_NAME=$(podman container exists "$1" \
2f7d90
+    && podman container inspect --format "{{.Id}} {{.Name}}" "$1")
2f7d90
+
2f7d90
+if [ -n "$IMAGE_NAME" ] && [ -n "$CONTAINER_NAME" ]; then
2f7d90
+    echo "Ambiguous target, container image and container with the same name detected: '$1'." >&2
2f7d90
+    echo "Please rather use an unique ID to specify the target of the scan." >&2
2f7d90
+    die
2f7d90
+fi
2f7d90
+
2f7d90
 # Check if the target of scan is image or container.
2f7d90
 CLEANUP=0
2f7d90
-if podman images | grep -q $1; then
2f7d90
+if [ -n "$IMAGE_NAME" ]; then
2f7d90
     ID=$(podman create $1) || die
2f7d90
-    IMG_NAME=$(podman images --format "{{.ID}} ({{.Repository}}:{{.Tag}})" | grep -m1 $1)
2f7d90
-    TARGET="podman-image://$IMG_NAME"
2f7d90
+    TARGET="podman-image://$IMAGE_NAME"
2f7d90
     CLEANUP=1
2f7d90
-else
2f7d90
+elif [ -n "$CONTAINER_NAME" ]; then
2f7d90
     # If the target was not found in images we suppose it is a container.
2f7d90
     ID=$1
2f7d90
-    TARGET="podman-container://$1"
2f7d90
+    TARGET="podman-container://$CONTAINER_NAME"
2f7d90
+else
2f7d90
+    echo "Target of the scan not found: '$1'." >&2
2f7d90
+    die
2f7d90
 fi
2f7d90
 
2f7d90
 # podman init creates required files such as: /run/.containerenv - we don't care about output and exit code