Best commit ever

This commit is contained in:
2023-03-21 01:21:56 +02:00
parent d0fa976659
commit f47ac832a6
2 changed files with 78 additions and 76 deletions

148
harden
View File

@@ -78,9 +78,11 @@ function install_packages {
sudo apt install -y "$@" # Install the packages passed as arguments sudo apt install -y "$@" # Install the packages passed as arguments
;; ;;
"dnf") # If the package manager is dnf "dnf") # If the package manager is dnf
sudo dnf upgrade -y # Update the package list
sudo dnf install -y "$@" # Install the packages passed as arguments sudo dnf install -y "$@" # Install the packages passed as arguments
;; ;;
"zypper") # If the package manager is zypper "zypper") # If the package manager is zypper
sudo zypper update -y # Update the package list
sudo zypper install -y "$@" # Install the packages passed as arguments sudo zypper install -y "$@" # Install the packages passed as arguments
;; ;;
*) *)
@@ -96,7 +98,7 @@ function install_packages {
# and store the ones that are indeed absent in another local array. # and store the ones that are indeed absent in another local array.
# Then it will install the packages that are missing by invoking the install_packages function. # Then it will install the packages that are missing by invoking the install_packages function.
function check_dependencies { function check_dependencies {
local dependencies=(curl git sudo vim ssh whiptail) # Declare dependencies as a local array local dependencies=(curl git sudo vim ssh docker docker-compose wget) # Declare dependencies as a local array
#> see what to do with name differences between distros if any <# #> see what to do with name differences between distros if any <#
local missing_dependencies=() # Declare missing_dependencies as a local array local missing_dependencies=() # Declare missing_dependencies as a local array
for dependency in "${dependencies[@]}"; do # Loop through the dependencies array for dependency in "${dependencies[@]}"; do # Loop through the dependencies array
@@ -107,76 +109,6 @@ function check_dependencies {
[[ ${#missing_dependencies[@]} -ne 0 ]] && install_packages "${missing_dependencies[@]}" [[ ${#missing_dependencies[@]} -ne 0 ]] && install_packages "${missing_dependencies[@]}"
} }
# The main function will call the check_dependencies function and exit if it fails.
# It will also output a message to the user to let them know that the script has finished.
function main {
check_dependencies || exit 1 # Check dependencies and exit if it fails
printf "%s" "Script finished" # Output message to the user
}
# The am_i_root function will check if the user is root and exit if they are not.
function am_i_root {
if [[ $EUID -ne 0 ]]; then # Check if the user is root
printf "%s" "Please run as root" # Output message to the user
exit 1 # Exit with error code 1
fi
}
# The getArgs function will get the arguments passed to the script and store them in an array.
# It will also check if the arguments are valid and exit if they are not.
function getArgs {
local args=() # Declare args as a local array
while [[ $# -gt 0 ]]; do # Loop through the arguments
case "$*" in # Use case to check for the arguments
--help | -h) # If the argument is --help or -h
printf "%s" "Usage: $SCRIPT_NAME [OPTION]..."
;;
--create-user | -cu) # If the argument is --create-user or -cu
args+=("$1") # Add the argument to the args array
shift # Shift the arguments
case "$*" in
--username=* | -u=*) # If the argument is --username=* or -u=*
args+=("$1") # Add the argument to the args array
shift # Shift the arguments
;;
--password=* | -p=*) # If the argument is --password=* or -p=*
args+=("$1") # Add the argument to the args array
shift # Shift the arguments
;;
*)
printf "%s" "Invalid argument: $1"
exit 1
;;
esac
create_user "${args[@]}" # Call the create_user function with the args array as arguments
;;
--harden-ssh | -hs) # If the argument is --harden-ssh or -hs
args+=("$1") # Add the argument to the args array
shift # Shift the arguments
harden_ssh "${args[@]}" # Call the harden_ssh function with the args array as arguments
;;
esac
done
printf "%s" "${args[@]}" # Output the args array
}
# The createUser function will create a new user with the username and password passed as arguments.
function createUser {
# Declare username as a local variable and assign it the first argument passed to the function
local username="$1"
# Declare password as a local variable and assign it the second argument passed to the function
local password="$2"
# Check if the user already exists and exit if they do
if id -u "$username" &> /dev/null; then
printf "%s" "User already exists"
exit 1
fi
# Create the user and add them to the sudo group
useradd -m -G sudo "$username"
# Set the user's password using printf to avoid the password being echoed to the terminal
printf "%s" "$username:$password" | chpasswd
}
# Global array of the service names to be restarted # Global array of the service names to be restarted
services=() services=()
@@ -217,11 +149,81 @@ function hardenSSH {
printf "%s" "File sshd_config not found or not readable" printf "%s" "File sshd_config not found or not readable"
exit 1 exit 1
fi fi
} }
# The main function will call the check_dependencies function and exit if it fails.
# It will also output a message to the user to let them know that the script has finished.
function main {
check_dependencies || exit 1 # Check dependencies and exit if it fails
harden_ssh || exit 1 # Harden ssh and exit if it fails
printf "%s" "Script finished" # Output message to the user
}
# # The am_i_root function will check if the user is root and exit if they are not.
# function am_i_root {
# if [[ $EUID -ne 0 ]]; then # Check if the user is root
# printf "%s" "Please run as root" # Output message to the user
# exit 1 # Exit with error code 1
# fi
# }
#
# # The getArgs function will get the arguments passed to the script and store them in an array.
# # It will also check if the arguments are valid and exit if they are not.
# function getArgs {
# local args=() # Declare args as a local array
# while [[ $# -gt 0 ]]; do # Loop through the arguments
# case "$*" in # Use case to check for the arguments
# --help | -h) # If the argument is --help or -h
# printf "%s" "Usage: $SCRIPT_NAME [OPTION]..."
# ;;
# --create-user | -cu) # If the argument is --create-user or -cu
# args+=("$1") # Add the argument to the args array
# shift # Shift the arguments
# case "$*" in
# --username=* | -u=*) # If the argument is --username=* or -u=*
# args+=("$1") # Add the argument to the args array
# shift # Shift the arguments
# ;;
# --password=* | -p=*) # If the argument is --password=* or -p=*
# args+=("$1") # Add the argument to the args array
# shift # Shift the arguments
# ;;
# *)
# printf "%s" "Invalid argument: $1"
# exit 1
# ;;
# esac
# create_user "${args[@]}" # Call the create_user function with the args array as arguments
# ;;
# --harden-ssh | -hs) # If the argument is --harden-ssh or -hs
# args+=("$1") # Add the argument to the args array
# shift # Shift the arguments
# harden_ssh "${args[@]}" # Call the harden_ssh function with the args array as arguments
# ;;
# esac
# done
# printf "%s" "${args[@]}" # Output the args array
# }
#
# # The createUser function will create a new user with the username and password passed as arguments.
# function createUser {
# # Declare username as a local variable and assign it the first argument passed to the function
# local username="$1"
# # Declare password as a local variable and assign it the second argument passed to the function
# local password="$2"
# # Check if the user already exists and exit if they do
# if id -u "$username" &> /dev/null; then
# printf "%s" "User already exists"
# exit 1
# fi
# # Create the user and add them to the sudo group
# useradd -m -G sudo "$username"
# # Set the user's password using printf to avoid the password being echoed to the terminal
# printf "%s" "$username:$password" | chpasswd
# }
# Call the main function # Call the main function
main main
am_i_root # am_i_root
exit 0 # The right and proper way to exit a script exit 0 # The right and proper way to exit a script

View File

@@ -54,10 +54,10 @@ parser = argparse.ArgumentParser(
prog='secdep.py', prog='secdep.py',
description='Manage cloud instances', description='Manage cloud instances',
) )
parser.add_argument('-l', '--list', help='List all instances', action='store_true') parser.add_argument('-l', '--list', help='List all instances or with -P PROVIDER list a provider\'s instances', action='store_true')
parser.add_argument('-v', '--values', help='Change credential values', action='store_true') parser.add_argument('-v', '--values', help='Change credential values', action='store_true')
parser.add_argument('-P', '--provider', help='Cloud provider', choices=['gce', 'azure', 'aws']) parser.add_argument('-P', '--provider', help='Cloud provider', choices=['gce', 'azure', 'aws'])
parser.add_argument('-a', '--action', help='Action to perform on a single or all instances. Valid options are delete[all] start[all] stop[all] reboot[all]', choices=action_choices, metavar='ACTION') parser.add_argument('-a', '--action', help='Action to perform on a single provider with -P PROVIDER or all instances. Valid options are delete[all] start[all] stop[all] reboot[all]', choices=action_choices, metavar='ACTION')
parser.add_argument('-c', '--create', help='Create an instance', action='store_true') parser.add_argument('-c', '--create', help='Create an instance', action='store_true')
parser.add_argument('-dep', '--deploy', help='Docker images to deploy', type=str, nargs='*', default=None, required=False) parser.add_argument('-dep', '--deploy', help='Docker images to deploy', type=str, nargs='*', default=None, required=False)
parser.add_argument('-I', '--listimages', help='List images', action='store_true') parser.add_argument('-I', '--listimages', help='List images', action='store_true')
@@ -67,7 +67,7 @@ parser.add_argument('-i', '--image', help='Image to use')
parser.add_argument('-s', '--size', help='Size of instance') parser.add_argument('-s', '--size', help='Size of instance')
parser.add_argument('-n', '--name', help='Name of instance') parser.add_argument('-n', '--name', help='Name of instance')
parser.add_argument('-g', '--region', help='Region to use') parser.add_argument('-g', '--region', help='Region to use')
parser.add_argument('-y', '--yes', help='Do not ask for confirmation', action='store_true') parser.add_argument('-y', '--yes', help='Do not ask for confirmation during creation', action='store_true')
parser.add_argument('-p', '--print', help='Also print node, image, location or size', action='store_true') parser.add_argument('-p', '--print', help='Also print node, image, location or size', action='store_true')
parser.add_argument('-ssh', '--ssh', help='Connect to an instance using ssh', action='store_true') parser.add_argument('-ssh', '--ssh', help='Connect to an instance using ssh', action='store_true')
args = parser.parse_args() args = parser.parse_args()