Debian Codegraph @

utils/oscap-podman

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # Copyright 2019 Red Hat Inc., Durham, North Carolina.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 function die()
20 {
21 echo "$*" >&2
22 exit 1
23 }
24
25 function invalid()
26 {
27 echo -e "$*\n" >&2
28 usage
29 exit 1
30 }
31
32 function usage()
33 {
34 echo "oscap-podman -- Tool for SCAP evaluation of Podman images and containers."
35 echo
36 echo "Compliance scan of Podman image:"
37 echo "$ sudo oscap-podman [--oscap=<OSCAP_BINARY>] IMAGE_NAME OSCAP_ARGUMENT [OSCAP_ARGUMENT...]"
38 echo
39 echo "Compliance scan of Podman container:"
40 echo "$ sudo oscap-podman [--oscap=<OSCAP_BINARY>] CONTAINER_NAME OSCAP_ARGUMENT [OSCAP_ARGUMENT...]"
41 echo
42 echo "See \`man oscap\` to learn more about semantics of OSCAP_ARGUMENT options."
43 }
44
45 OSCAP_BINARY=oscap
46
47 if [ $# -lt 1 ]; then
48 invalid "No arguments provided."
49 elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
50 usage
51 exit 0
52 elif [[ "$1" == --oscap=* ]] && [ $# -gt 2 ]; then
53 OSCAP_BINARY=${1#"--oscap="}
54 shift
55 elif [ "$#" -gt 1 ]; then
56 true
57 else
58 invalid "Invalid arguments provided."
59 fi
60
61 if [ "$(id -u)" -ne 0 ]; then
62 die "This script cannot run in rootless mode."
63 fi
64 if grep -q -- "--remediate" <<< "$@"; then
65 die "This script does not support '--remediate' option."
66 fi
67
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 die "Please rather use an unique ID to specify the target of the scan."
76 fi
77
78 # Check if the target of scan is image or container.
79 CLEANUP=0
80 if [ -n "$IMAGE_NAME" ]; then
81 ID=$(podman create $1) || die "Unable to create a container."
82 TARGET="podman-image://$IMAGE_NAME"
83 CLEANUP=1
84 elif [ -n "$CONTAINER_NAME" ]; then
85 # If the target was not found in images we suppose it is a container.
86 ID=$1
87 TARGET="podman-container://$CONTAINER_NAME"
88 else
89 die "Target of the scan not found: '$1'."
90 fi
91
92 # podman init creates required files such as: /run/.containerenv - we don't care about output and exit code
93 podman init $ID &> /dev/null || true
94
95 DIR=$(podman mount $ID) || die "Failed to mount."
96
97 if [ ! -f "$DIR/run/.containerenv" ]; then
98 # ubi8-init image does not create .containerenv when running podman init, but we need to make sure that the file is there
99 touch "$DIR/run/.containerenv"
100 fi
101
102 export OSCAP_CONTAINER_VARS
103 OSCAP_CONTAINER_VARS=`podman inspect $ID --format '{{join .Config.Env "\n"}}'`
104
105 export OSCAP_PROBE_ROOT
106 OSCAP_PROBE_ROOT="$(cd "$DIR" && pwd)" || die "Unable to change current directory to OSCAP_PROBE_ROOT (DIR)."
107 export OSCAP_EVALUATION_TARGET="$TARGET"
108 shift 1
109
110 $OSCAP_BINARY "$@"
111 EXIT_CODE=$?
112
113 if [ $CLEANUP -eq 1 ]; then
114 # podman-rm should handle also unmounting of the container filesystem.
115 podman rm -f $ID > /dev/null || die "Failed to clean up."
116 else
117 # If a running container is target of the scan just unmount its filesystem.
118 podman umount $ID > /dev/null || die "Failed to unmount."
119 fi
120 exit $EXIT_CODE