-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
podman wrapper: uses userns option when possible (#211)
* podman wrapper: uses userns option when possible On podman >= 4.3, the --userns keep-id:uid=X,gid=Y option allows to map the current user to a specific container user. Previous code: we created an entire UID/GID mapping to make this happen In this change: if podman >= 4.3, we simply use the userns option, otherwise we fallback to the previous workaround. Also: - in the workaround: only do the mapping when subUID/subGID are configured. - Comments improved (hopefully) - Updated README troubleshooting * added pytest * Exception if subuid is not correctly enabled
- Loading branch information
Showing
3 changed files
with
143 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,70 @@ | ||
import shutil | ||
|
||
import pytest | ||
import subprocess | ||
|
||
from unittest.mock import patch | ||
|
||
from scanners.podman_wrapper import PodmanWrapper | ||
|
||
|
||
@pytest.mark.skipif( | ||
shutil.which("podman") == False, reason="podman is required for this test" | ||
) | ||
def test_podman_mappings(): | ||
@patch("scanners.podman_wrapper.subprocess.run") | ||
def test_change_user_id(mock_subprocess): | ||
wrap = PodmanWrapper(app_name="pytest", scan_name="pytest", image="nothing") | ||
|
||
version = '{"Client":{"APIVersion":"5.2.2","Version":"5.2.2","GoVersion":"go1.22.6","GitCommit":"","BuiltTime":"Wed Aug 21 02:00:00 2024","Built":1724198400,"OsArch":"linux/amd64","Os":"linux"}}' | ||
run = subprocess.CompletedProcess(args=None, returncode=0, stdout=version.encode('utf-8')) | ||
|
||
mock_subprocess.return_value = run | ||
|
||
wrap.change_user_id(1000, 1000) | ||
|
||
i = wrap.opts.index("--userns") | ||
assert wrap.opts[i + 1] == "keep-id:uid=1000,gid=1000" | ||
|
||
@patch("scanners.podman_wrapper.subprocess.run") | ||
def test_change_user_id_workaround(mock_subprocess): | ||
wrap = PodmanWrapper(app_name="pytest", scan_name="pytest", image="nothing") | ||
|
||
info = """ | ||
{ | ||
"host": { | ||
"idMappings": { | ||
"gidmap": [ | ||
{ | ||
"container_id": 0, | ||
"host_id": 1000, | ||
"size": 1 | ||
}, | ||
{ | ||
"container_id": 1, | ||
"host_id": 524288, | ||
"size": 65536 | ||
} | ||
], | ||
"uidmap": [ | ||
{ | ||
"container_id": 0, | ||
"host_id": 1000, | ||
"size": 1 | ||
}, | ||
{ | ||
"container_id": 1, | ||
"host_id": 524288, | ||
"size": 65536 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
run = subprocess.CompletedProcess(args=None, returncode=0, stdout=info.encode('utf-8')) | ||
|
||
mock_subprocess.return_value = run | ||
|
||
wrap.change_user_id_workaround(1000, 1000) | ||
|
||
assert "--uidmap" in wrap.opts | ||
assert "0:1:1000" in wrap.opts | ||
assert "--gidmap" in wrap.opts |