Path: blob/main/libexec/nuageinit/tests/update_sshd_config.lua
288926 views
#!/usr/libexec/flua1---2-- SPDX-License-Identifier: BSD-2-Clause3--4-- Copyright (c) 2026 Baptiste Daroussin <[email protected]>56local n = require("nuage")78local root = os.getenv("NUAGE_FAKE_ROOTDIR")9if not root then10root = ""11end1213local sshd_config = root .. "/etc/ssh/sshd_config"1415local function setup(content)16local dir = root .. "/etc/ssh"17n.mkdir_p(dir)18local f = assert(io.open(sshd_config, "w"))19f:write(content)20f:close()21end2223local function read_config()24local f = assert(io.open(sshd_config, "r"))25local content = f:read("*a")26f:close()27return content28end2930-- Key not found: appended31setup("SomeOtherKey yes\n")32n.update_sshd_config("PasswordAuthentication", "yes")33if read_config() ~= "SomeOtherKey yes\nPasswordAuthentication yes\n" then34n.err("Key not found: should be appended")35end3637-- Key with same value: no change38setup("PasswordAuthentication yes\n")39n.update_sshd_config("PasswordAuthentication", "yes")40if read_config() ~= "PasswordAuthentication yes\n" then41n.err("Same value: should not change")42end4344-- Key with different value: changed45setup("PasswordAuthentication no\n")46n.update_sshd_config("PasswordAuthentication", "yes")47if read_config() ~= "PasswordAuthentication yes\n" then48n.err("Different value: should change")49end5051-- Key with comment52setup("PasswordAuthentication no # keep this\n")53n.update_sshd_config("PasswordAuthentication", "yes")54if read_config() ~= "PasswordAuthentication yes\n" then55n.err("Comment stripped: '" .. read_config() .. "'")56end5758-- Case insensitive key matching59setup("passwordauthentication no\n")60n.update_sshd_config("PasswordAuthentication", "yes")61if read_config() ~= "PasswordAuthentication yes\n" then62n.err("Case insensitive matching failed")63end6465-- Extra spaces66setup(" PasswordAuthentication no \n")67n.update_sshd_config("PasswordAuthentication", "yes")68if read_config() ~= "PasswordAuthentication yes\n" then69n.err("Extra spaces handling failed: '" .. read_config() .. "'")70end7172-- File does not exist: should be created with key/value73os.remove(sshd_config)74n.update_sshd_config("PasswordAuthentication", "yes")75if read_config() ~= "PasswordAuthentication yes\n" then76n.err("Missing file: should create: '" .. read_config() .. "'")77end7879os.exit(0)808182