Added functionality to add ssh key

This commit is contained in:
Jason Hee committed 2016-11-24 01:26:46 +08:00
1 parent 003357b848
commit 841312963c
5 files changed
+46 -22

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
[submodule "tests/lib"]
path = tests/lib
url = [email protected]:jasonheecs/BashScriptTestingLibrary.git
+17 -3
View File
@@ -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}
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}"
+21 -2
View File
@@ -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}"
}
Submodule
+1
Submodule tests/lib added at e95f7a5c80.
+4 -17
View File
@@ -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 () {