5.17. Configuring pre-execution hooks in Podman
You can create plugin scripts to define a fine-control over container operations, especially blocking unauthorized actions, for example pulling, running, or listing container images.
The file /etc/containers/podman_preexec_hooks.txt must be created by an administrator and can be empty. If the /etc/containers/podman_preexec_hooks.txt does not exist, the plugin scripts will not be executed.
The following rules apply to the plugin scripts:
- Have to be root-owned and not writable.
-
Have to be located in the
/usr/libexec/podman/pre-exec-hooksand/etc/containers/pre-exec-hooksdirectories. - Execute in sequentially and alphanumeric order.
-
If all plugin scripts return zero value, then the
podmancommand is executed. -
If any of the plugin scripts return a non-zero value, it indicates a failure. The
podmancommand exits and returns the non-zero value of the first-failed script. Red Hat recommends to use the following naming convention to execute the scripts in the correct order:
DDD_name.lang, where:-
The
DDDis the decimal number indicating the order of script execution. Use one or two leading zeros if necessary. -
The
nameis the name of the plugin script. -
The
lang(optional) is the file extension for the given programming language. For example, the name of the plugin script can be:001-check-groups.sh.
-
The
The plugin scripts are valid at the time of creation. Containers created before plugin scripts are not affected.
Prerequisites
-
The
container-toolsmeta-package is installed.
Procedure
Create the script plugin named
001-check-groups.sh. For example:#!/bin/bash if id -nG "$USER" 2> /dev/null | grep -qw "$GROUP" 2> /dev/null ; then exit 0 else exit 1 fi- The script checks if a user is in a specified group.
-
The
USERandGROUPare environment variables set by Podman. -
Exit code provided by the
001-check-groups.shscript would be provided to thepodmanbinary. -
The
podmancommand exits and returns the non-zero value of the first-failed script.
Verification
Check if the
001-check-groups.shscript works correctly:$ podman run image ...If the user is not in the correct group, the following error appears:
external preexec hook /etc/containers/pre-exec-hooks/001-check-groups.sh failed