diff --git a/setup.sh b/setup.sh index e83a3a7..7e668c2 100644 --- a/setup.sh +++ b/setup.sh @@ -29,4 +29,8 @@ fi addUserAccount "${username}" "${password}" read -rp $'Paste in the public SSH key for the new user:\n' sshKey -addSSHKey "${username}" "${sshKey}" \ No newline at end of file +addSSHKey "${username}" "${sshKey}" +changeSSHConfig +setupUfw + +sudo service ssh restart \ No newline at end of file diff --git a/setupLibrary.sh b/setupLibrary.sh index 9c47b63..710afb6 100644 --- a/setupLibrary.sh +++ b/setupLibrary.sh @@ -29,4 +29,14 @@ function execAsUser() { local exec_command=${2} sudo -u "${username}" -H sh -c "${exec_command}" +} + +function changeSSHConfig() { + sudo sed -re 's/^(\#?)(PasswordAuthentication)([[:space:]]+)yes/\2\3no/' -i.$(echo 'old') /etc/ssh/sshd_config + sudo sed -re 's/^(\#?)(PermitRootLogin)([[:space:]]+)(.*)/PermitRootLogin no/' -i /etc/ssh/sshd_config +} + +function setupUfw() { + sudo ufw allow OpenSSH + yes y | sudo ufw enable } \ No newline at end of file diff --git a/tests/tests.sh b/tests/tests.sh index 7290a05..21e9b56 100644 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -40,10 +40,31 @@ function testAddingOfSSHKey() { assertEquals "${ssh_file}" "${dummy_key}" } +function testChangeSSHConfig() { + changeSSHConfig + + local ssh_config="$(sudo cat /etc/ssh/sshd_config)" + assertContains "PasswordAuthentication no" "${ssh_config}" + assertContains "PermitRootLogin no" "${ssh_config}" +} + +function testUfw() { + setupUfw + + local ufw_status=$(sudo ufw status) + assertContains "Status: active" "${ufw_status}" + assertContains "OpenSSH" "${ufw_status}" +} + function testTeardown () { echo "Test Teardown" + deleteTestUser revertSudoers + revertSSHConfig + + sudo ufw delete allow OpenSSH + sudo ufw disable } ### Helper Functions ### @@ -55,7 +76,7 @@ function deleteTestUser() { function revertSudoers() { sudo cp /etc/sudoers.bak /etc/sudoers - sudo rm -rf sudoers.bak + sudo rm -rf /etc/sudoers.bak } function disableSudoPassword() { @@ -63,4 +84,9 @@ function disableSudoPassword() { sudo bash -c "echo '${test_user_account} ALL=(ALL) NOPASSWD: ALL' | (EDITOR='tee -a' visudo)" } +function revertSSHConfig() { + sudo cp /etc/ssh/sshd_config.old /etc/ssh/sshd_config + sudo rm -rf /etc/ssh/sshd_config.old +} + runUnitTests \ No newline at end of file