From: Richard Maw Date: Tue, 19 Mar 2024 17:55:51 +0000 (+0000) Subject: Move extra groups list calculation into INVOKING_USER X-Git-Tag: v23~72^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bff67d03028e49e7f41ccbd59104c622caa4c81c;p=thirdparty%2Fmkosi.git Move extra groups list calculation into INVOKING_USER --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index c681d712f..98fc92947 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -4058,7 +4058,7 @@ def sync_repository_metadata(context: Context) -> None: def run_sync(args: Args, config: Config, *, resources: Path) -> None: if os.getuid() == 0: - os.setgroups(os.getgrouplist(INVOKING_USER.name(), INVOKING_USER.gid)) + os.setgroups(INVOKING_USER.extra_groups()) os.setgid(INVOKING_USER.gid) os.setuid(INVOKING_USER.uid) diff --git a/mkosi/user.py b/mkosi/user.py index 095722801..408dc0aae 100644 --- a/mkosi/user.py +++ b/mkosi/user.py @@ -7,6 +7,7 @@ import logging import os import pwd import tempfile +from collections.abc import Sequence from pathlib import Path from mkosi.log import die @@ -25,7 +26,11 @@ class INVOKING_USER: def init(cls) -> None: name = cls.name() home = cls.home() - logging.debug(f"Running as user '{name}' ({cls.uid}:{cls.gid}) with home {home}.") + extra_groups = cls.extra_groups() + logging.debug( + f"Running as user '{name}' ({cls.uid}:{cls.gid}) with home {home} " + f"and extra groups {extra_groups}." + ) @classmethod def is_running_user(cls) -> bool: @@ -41,6 +46,11 @@ class INVOKING_USER: def home(cls) -> Path: return Path(f"~{cls.name()}").expanduser() + @classmethod + @functools.lru_cache(maxsize=1) + def extra_groups(cls) -> Sequence[int]: + return os.getgrouplist(cls.name(), cls.gid) + @classmethod def is_regular_user(cls) -> bool: return cls.uid >= 1000