]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Move extra groups list calculation into INVOKING_USER
authorRichard Maw <richard.maw@codethink.co.uk>
Tue, 19 Mar 2024 17:55:51 +0000 (17:55 +0000)
committerRichard Maw <richard.maw@codethink.co.uk>
Wed, 20 Mar 2024 11:06:25 +0000 (11:06 +0000)
mkosi/__init__.py
mkosi/user.py

index c681d712ff485ae6db4a0db62968b8b623ab409e..98fc92947f9bbc7bff3a687f099a00a9c3ce1fa8 100644 (file)
@@ -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)
 
index 095722801f06809209b9e8a97a0db504d8c3b584..408dc0aae1096f1d62718300a8e0eede600d2338 100644 (file)
@@ -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