Add relabel option to secrets

On selinux enabled system, the secrets cannot be read without proper
relabeling or correct policy being set.

This patch enables user to instruc podman-copose to use :z or :Z podman
volume options to make podman relabel the file under bind-mount.

More info here:
https://unix.stackexchange.com/questions/728801/host-wide-consequences-of-setting-selinux-z-z-option-on-container-bind-mounts?rq=1

Signed-off-by: Jaroslav Henner <1187265+jarovo@users.noreply.github.com>
This commit is contained in:
Jaroslav Henner
2025-05-19 00:16:04 +02:00
parent c26e188991
commit 82d7622c45
4 changed files with 42 additions and 1 deletions

View File

@@ -577,6 +577,7 @@ def get_secret_args(compose, cnt, secret, podman_is_building=False):
declared_secret = compose.declared_secrets[secret_name]
source_file = declared_secret.get("file")
x_podman_relabel = declared_secret.get("x-podman.relabel")
dest_file = ""
secret_opts = ""
@@ -618,7 +619,18 @@ def get_secret_args(compose, cnt, secret, podman_is_building=False):
dest_file = f"/run/secrets/{sec}"
else:
dest_file = secret_target
volume_ref = ["--volume", f"{source_file}:{dest_file}:ro,rprivate,rbind"]
mount_options = 'ro,rprivate,rbind'
selinux_relabel_to_mount_option_map = {None: "", "z": ",z", "Z": ",Z"}
try:
mount_options += selinux_relabel_to_mount_option_map[x_podman_relabel]
except KeyError as exc:
raise ValueError(
f'ERROR: Run secret "{secret_name} has invalid "relabel" option related '
+ f' to SELinux "{x_podman_relabel}". Expected "z" "Z" or nothing.'
) from exc
volume_ref = ["--volume", f"{source_file}:{dest_file}:{mount_options}"]
if secret_uid or secret_gid or secret_mode:
sec = secret_target if secret_target else secret_name