Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions modules/base/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ run_base() {
timedatectl set-timezone Europe/Lisbon || true
fi

if [[ -f /etc/ssh/sshd_config ]]; then
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config || true
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config || true
local sshd_config="${SSHD_CONFIG:-/etc/ssh/sshd_config}"

if [[ -f "$sshd_config" ]]; then
backup_file "$sshd_config"
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' "$sshd_config" || true
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin prohibit-password/' "$sshd_config" || true
systemctl reload ssh || systemctl reload sshd || true
fi

Expand Down
29 changes: 29 additions & 0 deletions tests/modules_base.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bats

setup() { cd "$BATS_TEST_DIRNAME/.."; }

@test "run_base does not overwrite sshd_config backup" {
sshd_dir="$BATS_TEST_TMPDIR/etc/ssh"
mkdir -p "$sshd_dir"
sshd_config="$sshd_dir/sshd_config"
echo 'PasswordAuthentication yes' >"$sshd_config"
rm -f "$sshd_dir"/sshd_config.bak.mona.*

for cmd in timedatectl systemctl ufw; do
printf '#!/usr/bin/env bash\nexit 0\n' >"$BATS_TEST_TMPDIR/$cmd"
chmod +x "$BATS_TEST_TMPDIR/$cmd"
done

run bash -lc "export PATH='$BATS_TEST_TMPDIR':\$PATH; export MONA_DIR=$(pwd); export SSHD_CONFIG=$sshd_config; . modules/base/base.sh; run_base"
[ "$status" -eq 0 ]
[[ "$output" == *"[mona] backup:"* ]]
first_backup=$(ls "$sshd_dir"/sshd_config.bak.mona.*)

sleep 1

run bash -lc "export PATH='$BATS_TEST_TMPDIR':\$PATH; export MONA_DIR=$(pwd); export SSHD_CONFIG=$sshd_config; . modules/base/base.sh; run_base"
[ "$status" -eq 0 ]
backups=("$sshd_dir"/sshd_config.bak.mona.*)
[ -f "$first_backup" ]
[ "${#backups[@]}" -eq 2 ]
}