mirror of
https://github.com/darksaid98/ubuntu-server-setup.git
synced 2026-12-03 14:38:57 +00:00
Added functionality to add ssh key
This commit is contained in:
1 parent
003357b848
commit
841312963c
5 files changed
+46
-22
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "tests/lib"]
|
||||||
|
path = tests/lib
|
||||||
|
url = [email protected]:jasonheecs/BashScriptTestingLibrary.git
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
function getCurrentDir() {
|
function getCurrentDir() {
|
||||||
local current_dir="${BASH_SOURCE%/*}"
|
local current_dir="${BASH_SOURCE%/*}"
|
||||||
if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi
|
if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi
|
||||||
@@ -13,6 +15,18 @@ function includeDependencies() {
|
|||||||
current_dir=$(getCurrentDir)
|
current_dir=$(getCurrentDir)
|
||||||
includeDependencies
|
includeDependencies
|
||||||
|
|
||||||
echo -n "Enter the new username of the user account:"
|
read -p "Enter the username of the new user account:" username
|
||||||
read username
|
read -s -p "Enter new UNIX password:" password
|
||||||
addUserAccount ${username}
|
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
@@ -2,12 +2,31 @@
|
|||||||
|
|
||||||
function addUserAccount() {
|
function addUserAccount() {
|
||||||
local username=${1}
|
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}
|
sudo adduser --disabled-password --gecos '' ${username}
|
||||||
else
|
else
|
||||||
sudo adduser ${username}
|
sudo adduser --disabled-password ${username}
|
||||||
|
echo "${username}:${password}" | sudo chpasswd
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sudo usermod -aG sudo ${username}
|
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.
@@ -7,14 +7,14 @@ function getCurrentDir() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
current_dir=$(getCurrentDir)
|
current_dir=$(getCurrentDir)
|
||||||
source "${current_dir}/BashScriptTestingLibrary.shl"
|
source "${current_dir}/lib/BashScriptTestingLibrary.shl"
|
||||||
source "${current_dir}/../setupLibrary.sh"
|
source "${current_dir}/../setupLibrary.sh"
|
||||||
|
|
||||||
test_user_account=testuser3
|
test_user_account=testuser3
|
||||||
|
|
||||||
function testSetup () {
|
function testSetup () {
|
||||||
echo "Test Setup"
|
echo "Test Setup"
|
||||||
addUserAccount ${test_user_account} true
|
addUserAccount ${test_user_account} "" true
|
||||||
}
|
}
|
||||||
|
|
||||||
function testUserAccountCreated() {
|
function testUserAccountCreated() {
|
||||||
@@ -23,21 +23,8 @@ function testUserAccountCreated() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testIfUserIsSudo() {
|
function testIfUserIsSudo() {
|
||||||
# local user_access="$(sudo -l -U ${test_user_account})"
|
local user_access=$(sudo -l -U ${test_user_account})
|
||||||
# case "$user_access" in
|
assertContains "(ALL : ALL) ALL" "${user_access}"
|
||||||
# **)
|
|
||||||
# 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}")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testTeardown () {
|
function testTeardown () {
|
||||||
Reference in new issue
Block a user