Skip to content

Commit 532a6c7

Browse files
committed
utils/oscap-podman: Detect ambiguous scan target
In case that a container image and a running container have the same name, `oscap-podman` scans container image and a running container is skipped. This might be unexpected and might cause a confusion for user. Therefore, this commit adds a code which detects such situation and rather informs user about ambiguous scan target and terminates. In such cases the unique container image/container ID should be used for specifying the target of the scan.
1 parent 66447a2 commit 532a6c7

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

utils/oscap-podman

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,30 @@ if grep -q "\-\-remediate" <<< "$@"; then
6565
die
6666
fi
6767

68+
IMAGE_NAME=$(podman image exists "$1" \
69+
&& podman image inspect --format "{{.Id}} {{.RepoTags}}" "$1")
70+
CONTAINER_NAME=$(podman container exists "$1" \
71+
&& podman container inspect --format "{{.Id}} {{.Name}}" "$1")
72+
73+
if [ -n "$IMAGE_NAME" ] && [ -n "$CONTAINER_NAME" ]; then
74+
echo "Ambiguous target, container image and container with the same name detected: '$1'." >&2
75+
echo "Please rather use an unique ID to specify the target of the scan." >&2
76+
die
77+
fi
78+
6879
# Check if the target of scan is image or container.
6980
CLEANUP=0
70-
if podman images | grep -q $1; then
81+
if [ -n "$IMAGE_NAME" ]; then
7182
ID=$(podman create $1) || die
72-
IMG_NAME=$(podman images --format "{{.ID}} ({{.Repository}}:{{.Tag}})" | grep -m1 $1)
73-
TARGET="podman-image://$IMG_NAME"
83+
TARGET="podman-image://$IMAGE_NAME"
7484
CLEANUP=1
75-
else
85+
elif [ -n "$CONTAINER_NAME" ]; then
7686
# If the target was not found in images we suppose it is a container.
7787
ID=$1
78-
TARGET="podman-container://$1"
88+
TARGET="podman-container://$CONTAINER_NAME"
89+
else
90+
echo "Target of the scan not found: '$1'." >&2
91+
die
7992
fi
8093

8194
# podman init creates required files such as: /run/.containerenv - we don't care about output and exit code

0 commit comments

Comments
 (0)