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