c36ff1
From e6412be62079bbec5d67d178711ea42f21cafab8 Mon Sep 17 00:00:00 2001
c36ff1
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
Date: Tue, 12 Oct 2021 16:35:00 +0200
c36ff1
Subject: [PATCH 1/2] Inhibit sshd-keygen@.service if cloud-init is active
c36ff1
 (#1028)
c36ff1
c36ff1
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
RH-MergeRequest: 11: Add drop-in to prevent race with sshd-keygen service
c36ff1
RH-Commit: [1/2] 77ba3f167e71c43847aa5b38e1833d84568ed5a7 (eesposit/cloud-init-centos-)
c36ff1
RH-Bugzilla: 2002492
c36ff1
RH-Acked-by: Eduardo Otubo <otubo@redhat.com>
c36ff1
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
c36ff1
c36ff1
TESTED: by me and QA
c36ff1
BREW: 40286693
c36ff1
c36ff1
commit 02c71f097bca455a0f87d3e0a2af4d04b1cbd727
c36ff1
Author: Ryan Harper <ryan.harper@canonical.com>
c36ff1
Date:   Tue Oct 12 09:31:36 2021 -0500
c36ff1
c36ff1
    Inhibit sshd-keygen@.service if cloud-init is active (#1028)
c36ff1
c36ff1
    In some cloud-init enabled images the sshd-keygen@.service
c36ff1
    may race with cloud-init and prevent ssh host keys from being
c36ff1
    generated or generating host keys twice slowing boot and  consuming
c36ff1
    additional entropy during boot.  This drop-in unit adds a condition to
c36ff1
    the sshd-keygen@.service which prevents running if cloud-init is active.
c36ff1
c36ff1
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
c36ff1
Conflicts: minor conflict in setup.py (line 253), where we still use
c36ff1
"/usr/lib/" instead of LIB
c36ff1
---
c36ff1
 packages/redhat/cloud-init.spec.in                    | 1 +
c36ff1
 packages/suse/cloud-init.spec.in                      | 1 +
c36ff1
 setup.py                                              | 5 ++++-
c36ff1
 systemd/disable-sshd-keygen-if-cloud-init-active.conf | 8 ++++++++
c36ff1
 4 files changed, 14 insertions(+), 1 deletion(-)
c36ff1
 create mode 100644 systemd/disable-sshd-keygen-if-cloud-init-active.conf
c36ff1
c36ff1
diff --git a/packages/redhat/cloud-init.spec.in b/packages/redhat/cloud-init.spec.in
c36ff1
index 16138012..1491822b 100644
c36ff1
--- a/packages/redhat/cloud-init.spec.in
c36ff1
+++ b/packages/redhat/cloud-init.spec.in
c36ff1
@@ -175,6 +175,7 @@ fi
c36ff1
 
c36ff1
 %if "%{init_system}" == "systemd"
c36ff1
 /usr/lib/systemd/system-generators/cloud-init-generator
c36ff1
+%{_sysconfdir}/systemd/system/sshd-keygen@.service.d/disable-sshd-keygen-if-cloud-init-active.conf
c36ff1
 %{_unitdir}/cloud-*
c36ff1
 %else
c36ff1
 %attr(0755, root, root) %{_initddir}/cloud-config
c36ff1
diff --git a/packages/suse/cloud-init.spec.in b/packages/suse/cloud-init.spec.in
c36ff1
index 004b875f..da8107b4 100644
c36ff1
--- a/packages/suse/cloud-init.spec.in
c36ff1
+++ b/packages/suse/cloud-init.spec.in
c36ff1
@@ -126,6 +126,7 @@ version_pys=$(cd "%{buildroot}" && find . -name version.py -type f)
c36ff1
 
c36ff1
 %{_sysconfdir}/dhcp/dhclient-exit-hooks.d/hook-dhclient
c36ff1
 %{_sysconfdir}/NetworkManager/dispatcher.d/hook-network-manager
c36ff1
+%{_sysconfdir}/systemd/system/sshd-keygen@.service.d/disable-sshd-keygen-if-cloud-init-active.conf
c36ff1
 
c36ff1
 # Python code is here...
c36ff1
 %{python_sitelib}/*
c36ff1
diff --git a/setup.py b/setup.py
c36ff1
index d5cd01a4..ec03fa27 100755
c36ff1
--- a/setup.py
c36ff1
+++ b/setup.py
c36ff1
@@ -38,6 +38,7 @@ def is_generator(p):
c36ff1
 def pkg_config_read(library, var):
c36ff1
     fallbacks = {
c36ff1
         'systemd': {
c36ff1
+            'systemdsystemconfdir': '/etc/systemd/system',
c36ff1
             'systemdsystemunitdir': '/lib/systemd/system',
c36ff1
             'systemdsystemgeneratordir': '/lib/systemd/system-generators',
c36ff1
         }
c36ff1
@@ -249,7 +250,9 @@ if not platform.system().endswith('BSD'):
c36ff1
     data_files.extend([
c36ff1
         (ETC + '/NetworkManager/dispatcher.d/',
c36ff1
          ['tools/hook-network-manager']),
c36ff1
-        ('/usr/lib/udev/rules.d', [f for f in glob('udev/*.rules')])
c36ff1
+        ('/usr/lib/udev/rules.d', [f for f in glob('udev/*.rules')]),
c36ff1
+        (ETC + '/systemd/system/sshd-keygen@.service.d/',
c36ff1
+         ['systemd/disable-sshd-keygen-if-cloud-init-active.conf']),
c36ff1
     ])
c36ff1
 # Use a subclass for install that handles
c36ff1
 # adding on the right init system configuration files
c36ff1
diff --git a/systemd/disable-sshd-keygen-if-cloud-init-active.conf b/systemd/disable-sshd-keygen-if-cloud-init-active.conf
c36ff1
new file mode 100644
c36ff1
index 00000000..71e35876
c36ff1
--- /dev/null
c36ff1
+++ b/systemd/disable-sshd-keygen-if-cloud-init-active.conf
c36ff1
@@ -0,0 +1,8 @@
c36ff1
+# In some cloud-init enabled images the sshd-keygen template service may race
c36ff1
+# with cloud-init during boot causing issues with host key generation.  This
c36ff1
+# drop-in config adds a condition to sshd-keygen@.service if it exists and
c36ff1
+# prevents the sshd-keygen units from running *if* cloud-init is going to run.
c36ff1
+#
c36ff1
+[Unit]
c36ff1
+ConditionPathExists=!/run/systemd/generator.early/multi-user.target.wants/cloud-init.target
c36ff1
+EOF
c36ff1
-- 
c36ff1
2.27.0
c36ff1