c36ff1
From 2a6b3b5afb20a7856ad81b3ec3da621571c3bec3 Mon Sep 17 00:00:00 2001
c36ff1
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
Date: Wed, 20 Oct 2021 10:41:36 +0200
c36ff1
Subject: [PATCH] cc_ssh.py: fix private key group owner and permissions
c36ff1
 (#1070)
c36ff1
c36ff1
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
RH-MergeRequest: 12: cc_ssh.py: fix private key group owner and permissions (#1070)
c36ff1
RH-Commit: [1/1] b2dc9cfd18ac0a8e1e22a37b1585d22dbde11536 (eesposit/cloud-init-centos-)
c36ff1
RH-Bugzilla: 2015974
c36ff1
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
c36ff1
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
c36ff1
c36ff1
commit ee296ced9c0a61b1484d850b807c601bcd670ec1
c36ff1
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
Date:   Tue Oct 19 21:32:10 2021 +0200
c36ff1
c36ff1
    cc_ssh.py: fix private key group owner and permissions (#1070)
c36ff1
c36ff1
    When default host keys are created by sshd-keygen (/etc/ssh/ssh_host_*_key)
c36ff1
    in RHEL/CentOS/Fedora, openssh it performs the following:
c36ff1
c36ff1
    // create new keys
c36ff1
    if ! $KEYGEN -q -t $KEYTYPE -f $KEY -C '' -N '' >&/dev/null; then
c36ff1
            exit 1
c36ff1
    fi
c36ff1
c36ff1
    // sanitize permissions
c36ff1
    /usr/bin/chgrp ssh_keys $KEY
c36ff1
    /usr/bin/chmod 640 $KEY
c36ff1
    /usr/bin/chmod 644 $KEY.pub
c36ff1
    Note that the group ssh_keys exists only in RHEL/CentOS/Fedora.
c36ff1
c36ff1
    Now that we disable sshd-keygen to allow only cloud-init to create
c36ff1
    them, we miss the "sanitize permissions" part, where we set the group
c36ff1
    owner as ssh_keys and the private key mode to 640.
c36ff1
c36ff1
    According to https://bugzilla.redhat.com/show_bug.cgi?id=2013644#c8, failing
c36ff1
    to set group ownership and permissions like openssh does makes the RHEL openscap
c36ff1
    tool generate an error.
c36ff1
c36ff1
    Signed-off-by: Emanuele Giuseppe Esposito eesposit@redhat.com
c36ff1
c36ff1
    RHBZ: 2013644
c36ff1
c36ff1
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
---
c36ff1
 cloudinit/config/cc_ssh.py |  7 +++++++
c36ff1
 cloudinit/util.py          | 14 ++++++++++++++
c36ff1
 2 files changed, 21 insertions(+)
c36ff1
c36ff1
diff --git a/cloudinit/config/cc_ssh.py b/cloudinit/config/cc_ssh.py
c36ff1
index 05a16dbc..4e986c55 100755
c36ff1
--- a/cloudinit/config/cc_ssh.py
c36ff1
+++ b/cloudinit/config/cc_ssh.py
c36ff1
@@ -240,6 +240,13 @@ def handle(_name, cfg, cloud, log, _args):
c36ff1
                 try:
c36ff1
                     out, err = subp.subp(cmd, capture=True, env=lang_c)
c36ff1
                     sys.stdout.write(util.decode_binary(out))
c36ff1
+
c36ff1
+                    gid = util.get_group_id("ssh_keys")
c36ff1
+                    if gid != -1:
c36ff1
+                        # perform same "sanitize permissions" as sshd-keygen
c36ff1
+                        os.chown(keyfile, -1, gid)
c36ff1
+                        os.chmod(keyfile, 0o640)
c36ff1
+                        os.chmod(keyfile + ".pub", 0o644)
c36ff1
                 except subp.ProcessExecutionError as e:
c36ff1
                     err = util.decode_binary(e.stderr).lower()
c36ff1
                     if (e.exit_code == 1 and
c36ff1
diff --git a/cloudinit/util.py b/cloudinit/util.py
c36ff1
index 343976ad..fe37ae89 100644
c36ff1
--- a/cloudinit/util.py
c36ff1
+++ b/cloudinit/util.py
c36ff1
@@ -1831,6 +1831,20 @@ def chmod(path, mode):
c36ff1
             os.chmod(path, real_mode)
c36ff1
 
c36ff1
 
c36ff1
+def get_group_id(grp_name: str) -> int:
c36ff1
+    """
c36ff1
+    Returns the group id of a group name, or -1 if no group exists
c36ff1
+
c36ff1
+    @param grp_name: the name of the group
c36ff1
+    """
c36ff1
+    gid = -1
c36ff1
+    try:
c36ff1
+        gid = grp.getgrnam(grp_name).gr_gid
c36ff1
+    except KeyError:
c36ff1
+        LOG.debug("Group %s is not a valid group name", grp_name)
c36ff1
+    return gid
c36ff1
+
c36ff1
+
c36ff1
 def get_permissions(path: str) -> int:
c36ff1
     """
c36ff1
     Returns the octal permissions of the file/folder pointed by the path,
c36ff1
-- 
c36ff1
2.27.0
c36ff1