Skip to content

Commit

Permalink
feat: change .Config.User from username to userid (#757)
Browse files Browse the repository at this point in the history
* Change .Config.User from username to userid

* Print out the username along the userid

* pass the username as an arg in set_default_user function

* remove unused import
  • Loading branch information
linostar authored Nov 21, 2024
1 parent bc45bed commit 8a4ca17
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
9 changes: 5 additions & 4 deletions rockcraft/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,16 @@ def to_oci_archive(self, tag: str, filename: str) -> None:
src_path = self.path / f"{name}:{tag}"
_copy_image(f"oci:{str(src_path)}", f"oci-archive:{filename}:{tag}")

def set_default_user(self, user: str) -> None:
def set_default_user(self, userid: int, username: str) -> None:
"""Set the default runtime user for the OCI image.
:param user: name of the default user (must already exist)
:param userid: userid of the default user (must already exist)
:param username: username of the default user (must already exist)
"""
image_path = self.path / self.image_name
params = ["--clear=config.entrypoint", "--config.user", user]
params = ["--clear=config.entrypoint", "--config.user", str(userid)]
_config_image(image_path, params)
emit.progress(f"Default user set to {user}")
emit.progress(f"Default user set to {userid} ({username})")

def set_entrypoint(self, entrypoint_service: str | None, build_base: str) -> None:
"""Set the OCI image entrypoint. It is always Pebble."""
Expand Down
5 changes: 3 additions & 2 deletions rockcraft/services/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,17 @@ def _pack(

if project.run_user:
emit.progress(f"Creating new user {project.run_user}")
userid = SUPPORTED_GLOBAL_USERNAMES[project.run_user]["uid"]
new_image.add_user(
prime_dir=prime_dir,
base_layer_dir=base_layer_dir,
tag=version,
username=project.run_user,
uid=SUPPORTED_GLOBAL_USERNAMES[project.run_user]["uid"],
uid=userid,
)

emit.progress(f"Setting the default OCI user to be {project.run_user}")
new_image.set_default_user(project.run_user)
new_image.set_default_user(userid, project.run_user)

emit.progress("Adding Pebble entrypoint")

Expand Down
2 changes: 1 addition & 1 deletion tests/spread/rockcraft/run-user-minimal/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ execute: |
# Ensure container exists
docker images run-user-minimal-test | MATCH "run-user-minimal-test"
docker inspect run-user-minimal-test --format '{{.Config.User}}' | \
MATCH "_daemon_"
MATCH "584792"
# ensure username
docker run --rm --entrypoint /bin/sh run-user-minimal-test \
Expand Down
2 changes: 1 addition & 1 deletion tests/spread/rockcraft/run-user/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ execute: |
sudo rockcraft.skopeo --insecure-policy copy oci-archive:run-user-test_latest_amd64.rock docker-daemon:run-user-test:latest
# Ensure container exists
docker images run-user-test | MATCH "run-user-test"
docker inspect run-user-test --format '{{.Config.User}}' | MATCH "_daemon_"
docker inspect run-user-test --format '{{.Config.User}}' | MATCH "584792"
# ensure container doesn't exist
docker rm -f run-user-test-container
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_digest(self, mocker):
def test_set_default_user(self, mock_run):
image = oci.Image("a:b", Path("/c"))

image.set_default_user("foo")
image.set_default_user(584792, "_daemon_")

assert mock_run.mock_calls == [
call(
Expand All @@ -557,7 +557,7 @@ def test_set_default_user(self, mock_run):
"/c/a:b",
"--clear=config.entrypoint",
"--config.user",
"foo",
"584792",
]
)
]
Expand Down

0 comments on commit 8a4ca17

Please sign in to comment.