Debian Codegraph @

utils/oscap-podman

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/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 diescip-shell shell shell-project 0.0.0 die().()
20 {
21 echoscip-shell system . . echo. "$*" >&2
22 exit 1
23 }
24
25 function invalidscip-shell shell shell-project 0.0.0 invalid().()
26 {
27 echoscip-shell system . . echo. -e "$*\n" >&2
28 usagescip-shell shell shell-project 0.0.0 usage().
29 exit 1
30 }
31
32 function usagescip-shell shell shell-project 0.0.0 usage().()
33 {
34 echoscip-shell system . . echo. "oscap-podman -- Tool for SCAP evaluation of Podman images and containers."
35 echoscip-shell system . . echo.
36 echoscip-shell system . . echo. "Compliance scan of Podman image:"
37 echoscip-shell system . . echo. "$ sudo oscap-podman [--oscap=<OSCAP_BINARY>] IMAGE_NAME OSCAP_ARGUMENT [OSCAP_ARGUMENT...]"
38 echoscip-shell system . . echo.
39 echoscip-shell system . . echo. "Compliance scan of Podman container:"
40 echoscip-shell system . . echo. "$ sudo oscap-podman [--oscap=<OSCAP_BINARY>] CONTAINER_NAME OSCAP_ARGUMENT [OSCAP_ARGUMENT...]"
41 echoscip-shell system . . echo.
42 echoscip-shell system . . echo. "See \`man oscap\` to learn more about semantics of OSCAP_ARGUMENT options."
43 }
44
45 OSCAP_BINARY=oscap
46
47 if [scip-shell system . . `[`. $# -lt 1 ]; then
48 invalidscip-shell shell shell-project 0.0.0 invalid(). "No arguments provided."
49 elif [scip-shell system . . `[`. "$1" == "-h" ] || [scip-shell system . . `[`. "$1" == "--help" ]; then
50 usagescip-shell shell shell-project 0.0.0 usage().
51 exit 0
52 elif [[ "$1" == --oscap=* ]] && [scip-shell system . . `[`. $# -gt 2 ]; then
53 OSCAP_BINARY=${1#"--oscap="}
54 shift
55 elif [scip-shell system . . `[`. "$#" -gt 1 ]; then
56 truescip-shell system . . true.
57 else
58 invalidscip-shell shell shell-project 0.0.0 invalid(). "Invalid arguments provided."
59 fi
60
61 if [scip-shell system . . `[`. "$(idscip-shell system . . id. -u)" -ne 0 ]; then
62 diescip-shell shell shell-project 0.0.0 die(). "This script cannot run in rootless mode."
63 fi
64 if grepscip-shell system . . grep. -q -- "--remediate" <<< "$@"; then
65 diescip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -n "$IMAGE_NAME" ] && [scip-shell system . . `[`. -n "$CONTAINER_NAME" ]; then
74 echoscip-shell system . . echo. "Ambiguous target, container image and container with the same name detected: '$1'." >&2
75 diescip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -n "$IMAGE_NAME" ]; then
81 ID=$(podman create $1) || diescip-shell shell shell-project 0.0.0 die(). "Unable to create a container."
82 TARGET="podman-image://$IMAGE_NAME"
83 CLEANUP=1
84 elif [scip-shell system . . `[`. -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 diescip-shell shell shell-project 0.0.0 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/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.
94
95 DIR=$(podman mount $ID) || diescip-shell shell shell-project 0.0.0 die(). "Failed to mount."
96
97 if [scip-shell system . . `[`. ! -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 touchscip-shell system . . 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" && pwdscip-shell system . . pwd.)" || diescip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. $CLEANUP -eq 1 ]; then
114 # podman-rm should handle also unmounting of the container filesystem.
115 podman rm -f $ID > /dev/nullscip-shell filesystem . . `/dev/null`. || diescip-shell shell shell-project 0.0.0 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/nullscip-shell filesystem . . `/dev/null`. || diescip-shell shell shell-project 0.0.0 die(). "Failed to unmount."
119 fi
120 exit $EXIT_CODE