From cb79eddbd7bc54b7e743519426e3a2af6c2e1f12 Mon Sep 17 00:00:00 2001 From: norakoiralamsft <88748701+norakoiralamsft@users.noreply.github.com> Date: Mon, 5 Dec 2022 12:30:15 -0800 Subject: [PATCH] Support other.conf files in Ubuntu 2210 (#1635) * bug fix * Update distroutils.py --- Utils/distroutils.py | 13 +++++++++++++ VMAccess/vmaccess.py | 11 ++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Utils/distroutils.py b/Utils/distroutils.py index b6b611eb6..d9f241443 100644 --- a/Utils/distroutils.py +++ b/Utils/distroutils.py @@ -39,6 +39,8 @@ def get_my_distro(config): if re.search("sles", os_name, re.IGNORECASE): # SuSE return SuSEDistro(config) + if re.search("ubuntu", os_name, re.IGNORECASE): + return UbuntuDistro(config) return GenericDistro(config) @@ -247,6 +249,17 @@ def delete_account(self, user): pass return +class UbuntuDistro(GenericDistro): + def __init__(self, config): + """ + Generic Attributes go here. These are based on 'majority rules'. + This __init__() may be called or overriden by the child. + """ + super(UbuntuDistro, self).__init__(config) + self.selinux = False + self.ssh_service_name = 'sshd' + self.sudoers_dir_base = '/usr/local/etc' + self.distro_name = 'Ubuntu' class FreeBSDDistro(GenericDistro): """ diff --git a/VMAccess/vmaccess.py b/VMAccess/vmaccess.py index 583d4e1b2..4f0d1bf1d 100644 --- a/VMAccess/vmaccess.py +++ b/VMAccess/vmaccess.py @@ -348,6 +348,13 @@ def _allow_password_auth(): _set_sshd_config(config, "PasswordAuthentication", "yes") ext_utils.replace_file_with_contents_atomic(SshdConfigPath, "\n".join(config)) + if isinstance(MyDistro, dist_utils.UbuntuDistro): #handle ubuntu 22.04 (sshd_config.d directory) + cloudInitConfigPath = "/etc/ssh/sshd_config.d/50-cloud-init.conf" + config = ext_utils.get_file_contents(cloudInitConfigPath) + if config is not None: #other versions of ubuntu don't contain this file + config = config.split("\n") + _set_sshd_config(config, "PasswordAuthentication", "yes") + ext_utils.replace_file_with_contents_atomic(cloudInitConfigPath, "\n".join(config)) def _set_sshd_config(config, name, val): notfound = True @@ -360,9 +367,7 @@ def _set_sshd_config(config, name, val): # Match block must be put in the end of sshd config break if notfound: - if i is None: - i = 0 - config.insert(i, "{0} {1}".format(name, val)) + config = "{0} {1}".format(name, val) + config return config