From 841312963cd9d6de5cae588833e6465eae1cc0bf Mon Sep 17 00:00:00 2001 From: Jason Hee Date: Thu, 24 Nov 2016 01:26:46 +0800 Subject: [PATCH] Added functionality to add ssh key --- .gitmodules | 3 +++ setup.sh | 20 +++++++++++++++++--- setupLibrary.sh | 23 +++++++++++++++++++++-- tests/lib | 1 + tests.sh => tests/tests.sh | 21 ++++----------------- 5 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 .gitmodules create mode 160000 tests/lib rename tests.sh => tests/tests.sh (54%) diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cbaaa92 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tests/lib"] + path = tests/lib + url = git@github.com:jasonheecs/BashScriptTestingLibrary.git diff --git a/setup.sh b/setup.sh index c69a88f..e83a3a7 100644 --- a/setup.sh +++ b/setup.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -e + function getCurrentDir() { local current_dir="${BASH_SOURCE%/*}" if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi @@ -13,6 +15,18 @@ function includeDependencies() { current_dir=$(getCurrentDir) includeDependencies -echo -n "Enter the new username of the user account:" -read username -addUserAccount ${username} \ No newline at end of file +read -p "Enter the username of the new user account:" username +read -s -p "Enter new UNIX password:" password +printf "\n" +read -s -p "Retype new UNIX password:" password_confirmation +printf "\n" + +if [[ "${password}" != "${password_confirmation}" ]]; then + echo "Passwords do not match!" + exit 1 +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 diff --git a/setupLibrary.sh b/setupLibrary.sh index a12eb00..8f6ddce 100644 --- a/setupLibrary.sh +++ b/setupLibrary.sh @@ -2,12 +2,31 @@ function addUserAccount() { local username=${1} + local password=${2} + local silent_mode=${3} - if [ ${2} == "true" ]; then + if [[ ${silent_mode} == "true" ]]; then sudo adduser --disabled-password --gecos '' ${username} else - sudo adduser ${username} + sudo adduser --disabled-password ${username} + echo "${username}:${password}" | sudo chpasswd fi sudo usermod -aG sudo ${username} +} + +function addSSHKey() { + local username=${1} + local sshKey=${2} + + execAsUser "${username}" "mkdir -p ~/.ssh; chmod 700 ~/.ssh; touch ~/.ssh/authorized_keys" + execAsUser "${username}" "echo \"${sshKey}\" | sudo tee -a ~/.ssh/authorized_keys" + execAsUser "${username}" "chmod 600 ~/.ssh/authorized_keys" +} + +function execAsUser() { + local username=${1} + local exec_command=${2} + + sudo -u "${username}" -H sh -c "${exec_command}" } \ No newline at end of file diff --git a/tests/lib b/tests/lib new file mode 160000 index 0000000..e95f7a5 --- /dev/null +++ b/tests/lib @@ -0,0 +1 @@ +Subproject commit e95f7a5c80193ade45831ac8eb018ae8af1c52c0 diff --git a/tests.sh b/tests/tests.sh similarity index 54% rename from tests.sh rename to tests/tests.sh index 78dbb18..2bf9d8e 100644 --- a/tests.sh +++ b/tests/tests.sh @@ -7,14 +7,14 @@ function getCurrentDir() { } current_dir=$(getCurrentDir) -source "${current_dir}/BashScriptTestingLibrary.shl" +source "${current_dir}/lib/BashScriptTestingLibrary.shl" source "${current_dir}/../setupLibrary.sh" test_user_account=testuser3 function testSetup () { echo "Test Setup" - addUserAccount ${test_user_account} true + addUserAccount ${test_user_account} "" true } function testUserAccountCreated() { @@ -23,21 +23,8 @@ function testUserAccountCreated() { } function testIfUserIsSudo() { - # local user_access="$(sudo -l -U ${test_user_account})" - # case "$user_access" in - # **) - # echo 'yes' - # ;; - # esac - # # $user_access=$(echo $user_access) - # # local user_access=$(sudo -l -U ${test_user_account} > /dev/null 2>&1; echo $?); - # # assertEquals 10 10 - # echo $user_access - - local sudoers=$(getent group sudo | cut -d: -f4) - - echo $sudoers - echo $(printf -- '%s' "$sudoers" | egrep -q -- "${test_user_account}") + local user_access=$(sudo -l -U ${test_user_account}) + assertContains "(ALL : ALL) ALL" "${user_access}" } function testTeardown () {