diff options
author | Alberto Contreras <alberto.contreras@canonical.com> | 2022-08-06 00:31:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-05 16:31:42 -0600 |
commit | f4d5f73cfa82a72e3cd967ba4c8c8e7012d3f691 (patch) | |
tree | 16e398dbf29a0d37909d0c3ac1962fe8fbc09e41 /cloudinit/ssh_util.py | |
parent | 3f19ff06ce14c8b826055c62deeec517b583b77e (diff) | |
download | cloud-init-git-f4d5f73cfa82a72e3cd967ba4c8c8e7012d3f691.tar.gz |
ssh_util: Handle sshd_config.d folder
Write sshd config to /etc/ssh/sshd_config.d/50-cloud-init.conf
if the sshd_config sources sshd_config.d
LP: #1968873
Diffstat (limited to 'cloudinit/ssh_util.py')
-rw-r--r-- | cloudinit/ssh_util.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/cloudinit/ssh_util.py b/cloudinit/ssh_util.py index ab4c63aa..5bbbc724 100644 --- a/cloudinit/ssh_util.py +++ b/cloudinit/ssh_util.py @@ -544,11 +544,28 @@ def parse_ssh_config_map(fname): return ret +def _includes_dconf(fname: str) -> bool: + if not os.path.isfile(fname): + return False + with open(fname, "r") as f: + for line in f: + if line.startswith(f"Include {fname}.d/*.conf"): + return True + return False + + def update_ssh_config(updates, fname=DEF_SSHD_CFG): """Read fname, and update if changes are necessary. @param updates: dictionary of desired values {Option: value} @return: boolean indicating if an update was done.""" + if _includes_dconf(fname): + if not os.path.isdir(f"{fname}.d"): + util.ensure_dir(f"{fname}.d", mode=0o755) + fname = os.path.join(f"{fname}.d", "50-cloud-init.conf") + if not os.path.isfile(fname): + # Ensure root read-only: + util.ensure_file(fname, 0o600) lines = parse_ssh_config(fname) changed = update_ssh_config_lines(lines=lines, updates=updates) if changed: |