public inbox for gentoo-catalyst@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-catalyst] [PATCH 1/3] catalyst: support 3 new options
@ 2022-03-27 23:37 Matt Turner
  2022-03-27 23:37 ` [gentoo-catalyst] [PATCH 2/3] catalyst: add new options to stage4 step list Matt Turner
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Matt Turner @ 2022-03-27 23:37 UTC (permalink / raw
  To: gentoo-catalyst; +Cc: Patrice Clement

From: Patrice Clement <monsieurp@gentoo.org>

* stage4/groups: create a a list of groups.
* stage4/users: create a list of users. users can also be added to
  groups using the "foo.bar=wheel,audio,baz" format.
* stage4/ssh_public_keys: copy an SSH public key into the stage4 user's home
  (.ssh/authorized_keys) and set the file permission to 0644.

Bug: https://bugs.gentoo.org/236905
Signed-off-by: Patrice Clement <monsieurp@gentoo.org>
---
 catalyst/base/stagebase.py | 70 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index de1e30ef..76feb5f0 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -201,6 +201,9 @@ class StageBase(TargetBase, ClearBase, GenBase):
         self.set_packages()
         self.set_rm()
         self.set_linuxrc()
+        self.set_groups()
+        self.set_users()
+        self.set_ssh_public_keys()
         self.set_busybox_config()
         self.set_overlay()
         self.set_repos()
@@ -583,6 +586,39 @@ class StageBase(TargetBase, ClearBase, GenBase):
                     self.settings[self.settings["spec_prefix"] + "/linuxrc"]
                 del self.settings[self.settings["spec_prefix"] + "/linuxrc"]
 
+    def set_groups(self):
+        groups = self.settings["spec_prefix"] + "/groups"
+        if groups in self.settings:
+            if isinstance(self.settings[groups], str):
+                self.settings["groups"] = self.settings[groups].split(",")
+            self.settings["groups"] = self.settings[groups]
+            del self.settings[groups]
+        else:
+            self.settings["groups"] = []
+        log.info('groups to create: %s' % self.settings["groups"])
+
+	def set_users(self):
+        users = self.settings["spec_prefix"] + "/users"
+        if users in self.settings:
+            if isinstance(self.settings[users], str):
+                self.settings["users"] = self.settings[users].split(",")
+            self.settings["users"] = self.settings[users]
+            del self.settings[users]
+        else:
+            self.settings["users"] = []
+        log.info('users to create: %s' % self.settings["users"])
+
+    def set_ssh_public_keys(self):
+        ssh_public_keys = self.settings["spec_prefix"] + "/ssh_public_keys"
+        if ssh_public_keys in self.settings:
+            if isinstance(self.settings[ssh_public_keys], str):
+                self.settings["ssh_public_keys"] = self.settings[ssh_public_keys].split(",")
+            self.settings["ssh_public_keys"] = self.settings[ssh_public_keys]
+            del self.settings[ssh_public_keys]
+        else:
+            self.settings["ssh_public_keys"] = []
+        log.info('ssh public keys to copy: %s' % self.settings["ssh_public_keys"])
+
     def set_busybox_config(self):
         if self.settings["spec_prefix"] + "/busybox_config" in self.settings:
             if isinstance(self.settings[self.settings['spec_prefix'] + '/busybox_config'], str):
@@ -894,6 +930,40 @@ class StageBase(TargetBase, ClearBase, GenBase):
                     cmd(['rsync', '-a', x + '/', self.settings['stage_path']],
                         env=self.env)
 
+    def groups(self):
+        for x in self.settings["groups"].split():
+            log.notice("Creating group: '%s'", x)
+            cmd(["groupadd", "-R", self.settings['chroot_path'], x], env=self.env)
+
+    def users(self):
+        for x in self.settings["users"]:
+            usr, grp = '', ''
+            try:
+                usr, grp = x.split("=")
+            except ValueError:
+                usr = x
+                log.debug("users: '=' separator not found on line " + x)
+                log.debug("users: missing separator means no groups found")
+            uacmd = ["useradd", "-R", self.settings['chroot_path'], "-m", x]
+            if grp != '':
+                uacmd = ["useradd", "-R", self.settings['chroot_path'], "-m", "-G", grp, usr]
+            log.notice("Creating user: '%s'", f"{usr}={grp}")
+            cmd(uacmd, env=self.env)
+
+    def ssh_public_keys(self):
+        for x in self.settings["ssh_public_keys"]:
+            usr, pub_key_src = '', ''
+            try:
+                usr, pub_key_src = x.split("=")
+            except ValueError:
+                raise CatalystError(f"ssh_public_keys: '=' separator not found on line {x}")
+            log.notice("Copying SSH public key for user: '%s'", usr)
+            pub_key_dest = self.settings['chroot_path'] + f"/home/{usr}/.ssh/authorized_keys"
+            cpcmd = ["cp", "-av", pub_key_src, pub_key_dest]
+            cmd(cpcmd, env=self.env)
+            chcmd = ["chmod", "0644", pub_key_dest]
+            cmd(chcmd, env=self.env)
+
     def bind(self):
         for x in [x for x in self.mount if self.mount[x]['enable']]:
             if str(self.mount[x]['source']) == 'config':
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2022-05-13 17:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-27 23:37 [gentoo-catalyst] [PATCH 1/3] catalyst: support 3 new options Matt Turner
2022-03-27 23:37 ` [gentoo-catalyst] [PATCH 2/3] catalyst: add new options to stage4 step list Matt Turner
2022-03-27 23:37 ` [gentoo-catalyst] [PATCH 3/3] example: document new options Matt Turner
2022-04-19 14:17 ` [gentoo-catalyst] [PATCH 1/3] catalyst: support 3 " Daniel Cordero
2022-04-20 23:25   ` Matt Turner
2022-04-19 15:23 ` Daniel Cordero
2022-04-20 23:26   ` Matt Turner
2022-04-21  7:08     ` [gentoo-catalyst] [PATCH 1/4] stage4: fix handling of groups, users and keys Daniel Cordero
2022-04-21  7:08       ` [gentoo-catalyst] [PATCH 2/4] stage4/groups: don't run split on a list Daniel Cordero
2022-04-21  7:08       ` [gentoo-catalyst] [PATCH 3/4] stage4/users: don't split a single entry Daniel Cordero
2022-04-21  7:08       ` [gentoo-catalyst] [PATCH 4/4] stage4/groups: improve log message Daniel Cordero
2022-04-21 21:01         ` Daniel Cordero
2022-05-09 11:20         ` [gentoo-catalyst] [PATCH v2 4/4] stage4/users: " Daniel Cordero
2022-05-09 17:06       ` [gentoo-catalyst] [PATCH 1/4] stage4: fix handling of groups, users and keys Matt Turner
2022-05-12 10:09         ` Daniel Cordero
2022-05-13 17:45           ` Matt Turner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox