Code Update

This commit is contained in:
Christer Warén 2024-06-01 14:47:24 +03:00
parent d6344baf70
commit 9387e15062
12 changed files with 311 additions and 187 deletions

7
build.sh Normal file → Executable file
View File

@ -5,20 +5,21 @@ if [ ! "$BASH_VERSION" ] ; then
exit 1 exit 1
fi fi
wiBold=$(tput bold)
wiNormal=$(tput sgr0)
echo "" echo ""
echo "" echo ""
echo "" echo ""
echo "==============================" echo "=============================="
echo -n "$wiBold" echo -n "$wiBold"
echo " Warén Group " echo " Warén Group "
echo " Init.sh "
echo -n "$wiNormal" echo -n "$wiNormal"
echo "==============================" echo "=============================="
echo " >> Build << " echo " >> Init << "
echo "------------------------------" echo "------------------------------"
podman run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp docker.io/library/php:8-cli php generator.php &> /dev/null podman run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp docker.io/library/php:8-cli php generator.php &> /dev/null
chmod +x init.sh &> /dev/null chmod +x init.sh &> /dev/null
echo "=============================="
echo " " echo " "
echo " " echo " "
echo " " echo " "

248
init.sh
View File

@ -13,15 +13,60 @@ wiPurple=$(tput setaf 165)
wiTurquoise=$(tput setaf 14) wiTurquoise=$(tput setaf 14)
wiPink=$(tput setaf 198) wiPink=$(tput setaf 198)
wiOrange=$(tput setaf 202) wiOrange=$(tput setaf 202)
wiUnderline=$(tput smul)
wiBold=$(tput bold) wiBold=$(tput bold)
wiNormal=$(tput sgr0) wiNormal=$(tput sgr0)
wi-header(){
wx-header $1 $2
}
wi-repeat(){
wx-repeat $1 $2
}
wi-login(){
wx-login $1 $2
}
wx-stop(){
wi-stop
}
wi-delete(){
if [[ -f "init.sh" ]]
then
rm init.sh
fi
}
wx-header(){
if [[ $2 == "h1" ]]
then
echo ""
echo ""
echo ""
echo "=============================="
wx-repeat " " $((30/2-${#1}/2))
echo -n "$wxBold"
echo -n "$1"
echo -n "$wxNormal"
echo ""
echo "=============================="
fi
if [[ $2 == "h2" || -z $2 ]]
then
wx-start
wx-repeat " " $((30/2-6/2-${#1}/2))
echo -n ">> $1 <<"
echo ""
echo "------------------------------"
fi
}
wi-init(){ wi-init(){
wi-login $1 $2 wi-login $1 $2
wi-header "Init"
echo " >> Init << "
echo "------------------------------"
mkdir -p ~/.ssh &> /dev/null mkdir -p ~/.ssh &> /dev/null
@ -71,16 +116,14 @@ wi-init(){
unset HVT unset HVT
} }
wi-login(){ wx-login(){
ORG=$1 ORG=$1
if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]] if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]
then then
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Hostname Required" echo "Status: Hostname Required"
wi-stop wx-stop
fi fi
if [[ ! -z $1 ]] if [[ ! -z $1 ]]
@ -96,11 +139,9 @@ wi-login(){
then then
ORG=cwchristerw ORG=cwchristerw
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Organization Required" echo "Status: Organization Required"
wi-stop wx-stop
fi fi
if [[ $ORG == "warengroup" ]] if [[ $ORG == "warengroup" ]]
@ -113,11 +154,9 @@ wi-login(){
then then
DOMAIN=christerwaren.fi DOMAIN=christerwaren.fi
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Organization Unsupported" echo "Status: Organization Unsupported"
wi-stop wx-stop
fi fi
FOLDER=$ORG FOLDER=$ORG
@ -132,11 +171,9 @@ wi-login(){
then then
HOSTNAME=$(hostname --fqdn) HOSTNAME=$(hostname --fqdn)
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Hostname Required" echo "Status: Hostname Required"
wi-stop wx-stop
fi fi
if [[ -z $USER || $USER == "root" || $USER == "local" ]] if [[ -z $USER || $USER == "root" || $USER == "local" ]]
@ -145,117 +182,144 @@ wi-login(){
then then
if [[ -z LOGNAME ]] if [[ -z LOGNAME ]]
then then
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Username Required" echo "Status: Username Required"
wi-stop wx-stop
else else
USER=$LOGNAME USERNAME=$LOGNAME
fi fi
else else
USER=$SUDO_USER USERNAME=$SUDO_USER
fi fi
else
USERNAME=$USER
fi fi
VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health) VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health)
if [[ $VAULT_STATUS -eq 200 ]] if [[ $VAULT_STATUS -eq 200 ]]
then then
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN) if [[ $USER != "root" && $USER != "local" && -f "$HOME/.config/warengroup/config.json" ]]
if [[ $IDM_STATUS -eq 301 ]]
then then
echo " >> Login << " TOKEN="$(cat $HOME/.config/warengroup/config.json | jq -r .login.$ORG)"
echo "------------------------------" fi
echo $wiBold$ORG$wiNormal VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ ! -z $VAULT_LOGIN && ${#VAULT_LOGIN} == 95 ]]
if [[ -z $USER || $USER == "root" || $USER == "local" ]] then
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then then
echo -n "Username: " jq '.login.'$ORG' = "'$VAULT_LOGIN'"' $HOME/.config/warengroup/config.json 1> $HOME/.config/warengroup/config.json.tmp
read USERNAME mv $HOME/.config/warengroup/config.json.tmp $HOME/.config/warengroup/config.json &> /dev/null
else
echo "Username: $USER"
USERNAME=$USER
fi fi
else
echo -n "Password: " IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN)
read -s PASSWORD if [[ $IDM_STATUS -eq 301 ]]
echo "****************"
if [[ -z $USERNAME || -z $PASSWORD ]]
then then
echo "Status: Username & Password Required" wx-header "Login"
wi-stop echo $wxBold$ORG$wxNormal
if [[ -z $USERNAME || $USERNAME == "root" || $USERNAME == "local" ]]
then
echo -n "Username: "
read USERNAME
else
echo "Username: $USERNAME"
fi
echo -n "Password: "
read -s PASSWORD
echo "****************"
if [[ -z $USERNAME || -z $PASSWORD ]]
then
echo "Status: Username & Password Required"
wx-stop
else
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/ldap/login/$USERNAME -X POST -d '{ "password": "'$PASSWORD'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
echo "Status: Login Failed"
wx-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG' = "'$VAULT_LOGIN'"' $HOME/.config/warengroup/config.json &> $HOME/.config/warengroup/config.json.tmp
mv $HOME/.config/warengroup/config.json.tmp $HOME/.config/warengroup/config.json &> /dev/null
fi
fi
else else
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/ldap/login/$USERNAME -X POST -d '{ "password": "'$PASSWORD'" }' -s | jq -r '.auth.client_token') wx-header "Login"
echo $wxBold$ORG$wxNormal
echo -n "Token: "
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo "Status: Vault Token Required"
wx-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]] if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then then
echo "Status: Login Failed" echo "Status: Login Failed"
wi-stop wx-stop
fi fi
config["login",${ORG}]=$VAULT_LOGIN config["login",${ORG}]=$VAULT_LOGIN
wi-start
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG' = "'$VAULT_LOGIN'"' $HOME/.config/warengroup/config.json &> $HOME/.config/warengroup/config.json.tmp
mv $HOME/.config/warengroup/config.json.tmp $HOME/.config/warengroup/config.json &> /dev/null
fi
fi fi
else
echo " >> Login << "
echo "------------------------------"
echo $wiBold$ORG$wiNormal
echo -n "Token: "
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo "Status: Vault Token Required"
wi-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
echo "Status: Login Failed"
wi-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
wi-start
fi fi
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------" echo $wxBold$ORG$wxNormal
echo $wiBold$ORG$wiNormal
echo "Status: Vault Offline" echo "Status: Vault Offline"
wi-stop wx-stop
fi
}
wx-repeat() {
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
}
wi-restricted(){
if [[ $USER != "root" && $USER != "local" ]]
then
sudo $0 $1 $2
exit 1
fi fi
} }
wi-start(){ wi-start(){
echo "" wi-header "Warén Group" h1
echo ""
echo ""
echo "=============================="
echo -n "$wiBold"
echo " Warén Group "
echo " Init.sh "
echo -n "$wiNormal"
echo "=============================="
} }
wi-stop (){ wi-stop (){
echo "=============================="
echo " " echo " "
echo " " echo " "
echo " " echo " "
wi-delete
exit 1 exit 1
} }
wi-start wi-start
wi-restricted $0 $1 $2
wi-init $1 $2 wi-init $1 $2
wi-stop wi-stop

View File

@ -13,12 +13,12 @@ wiPurple=$(tput setaf 165)
wiTurquoise=$(tput setaf 14) wiTurquoise=$(tput setaf 14)
wiPink=$(tput setaf 198) wiPink=$(tput setaf 198)
wiOrange=$(tput setaf 202) wiOrange=$(tput setaf 202)
wiUnderline=$(tput smul)
wiBold=$(tput bold) wiBold=$(tput bold)
wiNormal=$(tput sgr0) wiNormal=$(tput sgr0)
{{ CODES }} {{ CODES }}
wi-start wi-start
wi-restricted $0 $1 $2
wi-init $1 $2 wi-init $1 $2
wi-stop wi-stop

View File

@ -0,0 +1,15 @@
wi-header(){
wx-header $1 $2
}
wi-repeat(){
wx-repeat $1 $2
}
wi-login(){
wx-login $1 $2
}
wx-stop(){
wi-stop
}

6
src/functions/delete.sh Normal file
View File

@ -0,0 +1,6 @@
wi-delete(){
if [[ -f "init.sh" ]]
then
rm init.sh
fi
}

24
src/functions/header.sh Normal file
View File

@ -0,0 +1,24 @@
wx-header(){
if [[ $2 == "h1" ]]
then
echo ""
echo ""
echo ""
echo "=============================="
wx-repeat " " $((30/2-${#1}/2))
echo -n "$wxBold"
echo -n "$1"
echo -n "$wxNormal"
echo ""
echo "=============================="
fi
if [[ $2 == "h2" || -z $2 ]]
then
wx-start
wx-repeat " " $((30/2-6/2-${#1}/2))
echo -n ">> $1 <<"
echo ""
echo "------------------------------"
fi
}

View File

@ -1,8 +1,6 @@
wi-init(){ wi-init(){
wi-login $1 $2 wi-login $1 $2
wi-header "Init"
echo " >> Init << "
echo "------------------------------"
mkdir -p ~/.ssh &> /dev/null mkdir -p ~/.ssh &> /dev/null

View File

@ -1,13 +1,11 @@
wi-login(){ wx-login(){
ORG=$1 ORG=$1
if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]] if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]
then then
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Hostname Required" echo "Status: Hostname Required"
wi-stop wx-stop
fi fi
if [[ ! -z $1 ]] if [[ ! -z $1 ]]
@ -23,11 +21,9 @@ wi-login(){
then then
ORG=cwchristerw ORG=cwchristerw
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Organization Required" echo "Status: Organization Required"
wi-stop wx-stop
fi fi
if [[ $ORG == "warengroup" ]] if [[ $ORG == "warengroup" ]]
@ -40,11 +36,9 @@ wi-login(){
then then
DOMAIN=christerwaren.fi DOMAIN=christerwaren.fi
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Organization Unsupported" echo "Status: Organization Unsupported"
wi-stop wx-stop
fi fi
FOLDER=$ORG FOLDER=$ORG
@ -59,11 +53,9 @@ wi-login(){
then then
HOSTNAME=$(hostname --fqdn) HOSTNAME=$(hostname --fqdn)
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Hostname Required" echo "Status: Hostname Required"
wi-stop wx-stop
fi fi
if [[ -z $USER || $USER == "root" || $USER == "local" ]] if [[ -z $USER || $USER == "root" || $USER == "local" ]]
@ -72,92 +64,110 @@ wi-login(){
then then
if [[ -z LOGNAME ]] if [[ -z LOGNAME ]]
then then
echo " >> Login << " wx-header "Login"
echo "------------------------------"
echo "Status: Username Required" echo "Status: Username Required"
wi-stop wx-stop
else else
USER=$LOGNAME USERNAME=$LOGNAME
fi fi
else else
USER=$SUDO_USER USERNAME=$SUDO_USER
fi fi
else
USERNAME=$USER
fi fi
VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health) VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health)
if [[ $VAULT_STATUS -eq 200 ]] if [[ $VAULT_STATUS -eq 200 ]]
then then
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN) if [[ $USER != "root" && $USER != "local" && -f "$HOME/.config/warengroup/config.json" ]]
if [[ $IDM_STATUS -eq 301 ]]
then then
echo " >> Login << " TOKEN="$(cat $HOME/.config/warengroup/config.json | jq -r .login.$ORG)"
echo "------------------------------" fi
echo $wiBold$ORG$wiNormal VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ ! -z $VAULT_LOGIN && ${#VAULT_LOGIN} == 95 ]]
if [[ -z $USER || $USER == "root" || $USER == "local" ]] then
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then then
echo -n "Username: " jq '.login.'$ORG' = "'$VAULT_LOGIN'"' $HOME/.config/warengroup/config.json 1> $HOME/.config/warengroup/config.json.tmp
read USERNAME mv $HOME/.config/warengroup/config.json.tmp $HOME/.config/warengroup/config.json &> /dev/null
else
echo "Username: $USER"
USERNAME=$USER
fi fi
else
echo -n "Password: " IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN)
read -s PASSWORD if [[ $IDM_STATUS -eq 301 ]]
echo "****************"
if [[ -z $USERNAME || -z $PASSWORD ]]
then then
echo "Status: Username & Password Required" wx-header "Login"
wi-stop echo $wxBold$ORG$wxNormal
if [[ -z $USERNAME || $USERNAME == "root" || $USERNAME == "local" ]]
then
echo -n "Username: "
read USERNAME
else
echo "Username: $USERNAME"
fi
echo -n "Password: "
read -s PASSWORD
echo "****************"
if [[ -z $USERNAME || -z $PASSWORD ]]
then
echo "Status: Username & Password Required"
wx-stop
else
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/ldap/login/$USERNAME -X POST -d '{ "password": "'$PASSWORD'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
echo "Status: Login Failed"
wx-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG' = "'$VAULT_LOGIN'"' $HOME/.config/warengroup/config.json &> $HOME/.config/warengroup/config.json.tmp
mv $HOME/.config/warengroup/config.json.tmp $HOME/.config/warengroup/config.json &> /dev/null
fi
fi
else else
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/ldap/login/$USERNAME -X POST -d '{ "password": "'$PASSWORD'" }' -s | jq -r '.auth.client_token') wx-header "Login"
echo $wxBold$ORG$wxNormal
echo -n "Token: "
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo "Status: Vault Token Required"
wx-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]] if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then then
echo "Status: Login Failed" echo "Status: Login Failed"
wi-stop wx-stop
fi fi
config["login",${ORG}]=$VAULT_LOGIN config["login",${ORG}]=$VAULT_LOGIN
wi-start
if [[ $USER != "root" && $USER != "local" ]]
then
jq '.login.'$ORG' = "'$VAULT_LOGIN'"' $HOME/.config/warengroup/config.json &> $HOME/.config/warengroup/config.json.tmp
mv $HOME/.config/warengroup/config.json.tmp $HOME/.config/warengroup/config.json &> /dev/null
fi
fi fi
else
echo " >> Login << "
echo "------------------------------"
echo $wiBold$ORG$wiNormal
echo -n "Token: "
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo "Status: Vault Token Required"
wi-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
echo "Status: Login Failed"
wi-stop
fi
config["login",${ORG}]=$VAULT_LOGIN
wi-start
fi fi
else else
echo " >> Login << " wx-header "Login"
echo "------------------------------" echo $wxBold$ORG$wxNormal
echo $wiBold$ORG$wiNormal
echo "Status: Vault Offline" echo "Status: Vault Offline"
wi-stop wx-stop
fi fi
} }

5
src/functions/repeat.sh Normal file
View File

@ -0,0 +1,5 @@
wx-repeat() {
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
}

View File

@ -0,0 +1,7 @@
wi-restricted(){
if [[ $USER != "root" && $USER != "local" ]]
then
sudo $0 $1 $2
exit 1
fi
}

View File

@ -1,11 +1,3 @@
wi-start(){ wi-start(){
echo "" wi-header "Warén Group" h1
echo ""
echo ""
echo "=============================="
echo -n "$wiBold"
echo " Warén Group "
echo " Init.sh "
echo -n "$wiNormal"
echo "=============================="
} }

View File

@ -1,7 +1,9 @@
wi-stop (){ wi-stop (){
echo "=============================="
echo " " echo " "
echo " " echo " "
echo " " echo " "
wi-delete
exit 1 exit 1
} }