Skip to content

Commit d828dd7

Browse files
committed
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 mount 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>
1 parent c26e188 commit d828dd7

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

docs/Extensions.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ services:
2727
2828
For explanations of these extensions, please refer to the [Podman Documentation](https://docs.podman.io/).
2929
30+
## Secrets
31+
The following extension keys are available under `secret` configuration:
32+
33+
x-podman.relabel - Configure SELinux relabeling
34+
35+
For example, the following configures custom-secret to use mount with private and unshared content.
36+
Only the current container can use a private volume.
37+
38+
```yml
39+
secrets:
40+
custom-secret:
41+
x-podman.relabel: Z
42+
```
43+
44+
For explanations of these extensions, please refer to the [podman-run --volume documentation](https://docs.podman.io/en/latest/markdown/podman-run.1.html#volume-v-source-volume-host-dir-container-dir-options)).
45+
3046
## Network management
3147

3248
The following extension keys are available under network configuration:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add relabel option to secret to make possible to read the secret file by the contained process.

podman_compose.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ def get_secret_args(compose, cnt, secret, podman_is_building=False):
577577
declared_secret = compose.declared_secrets[secret_name]
578578

579579
source_file = declared_secret.get("file")
580+
x_podman_relabel = declared_secret.get("x-podman.relabel")
580581
dest_file = ""
581582
secret_opts = ""
582583

@@ -618,7 +619,18 @@ def get_secret_args(compose, cnt, secret, podman_is_building=False):
618619
dest_file = f"/run/secrets/{sec}"
619620
else:
620621
dest_file = secret_target
621-
volume_ref = ["--volume", f"{source_file}:{dest_file}:ro,rprivate,rbind"]
622+
623+
mount_options = 'ro,rprivate,rbind'
624+
625+
selinux_relabel_to_mount_option_map = {None: "", "z": ",z", "Z": ",Z"}
626+
try:
627+
mount_options += selinux_relabel_to_mount_option_map[x_podman_relabel]
628+
except KeyError as exc:
629+
raise ValueError(
630+
f'ERROR: Run secret "{secret_name} has invalid "relabel" option related '
631+
+ f' to SELinux "{x_podman_relabel}". Expected "z" "Z" or nothing.'
632+
) from exc
633+
volume_ref = ["--volume", f"{source_file}:{dest_file}:{mount_options}"]
622634

623635
if secret_uid or secret_gid or secret_mode:
624636
sec = secret_target if secret_target else secret_name

tests/unit/test_container_to_args_secrets.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ async def test_secret_target_matches_secret_name_secret_type_not_env(self):
302302
"file_secret",
303303
repo_root() + "/test_dirname/my_secret:/run/secrets/file_secret:ro,rprivate,rbind",
304304
),
305+
(
306+
"relabel",
307+
{"file_secret": {"file": "./my_secret", "x-podman.relabel": "Z"}},
308+
"file_secret",
309+
repo_root() + "/test_dirname/my_secret:/run/secrets/file_secret:ro,rprivate,rbind,Z",
310+
),
311+
(
312+
"relabel",
313+
{"file_secret": {"file": "./my_secret", "x-podman.relabel": "z"}},
314+
"file_secret",
315+
repo_root() + "/test_dirname/my_secret:/run/secrets/file_secret:ro,rprivate,rbind,z",
316+
),
305317
(
306318
"custom_target_name",
307319
{

0 commit comments

Comments
 (0)