From a1ff2b40d73e21b28ad70e6ed1abc69ee198b196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Sat, 27 Apr 2024 18:25:14 +0300 Subject: [PATCH] Source Update --- config.yml | 10 --- src/base.sh | 146 ++++++++++++++++++++++++++++++++- src/commands/auth/login.sh | 3 - src/commands/auth/logout.sh | 3 - src/commands/help.sh | 4 - wx | 158 ++++++++++++++++++++++++++++++++---- 6 files changed, 288 insertions(+), 36 deletions(-) delete mode 100644 config.yml delete mode 100644 src/commands/auth/login.sh delete mode 100644 src/commands/auth/logout.sh diff --git a/config.yml b/config.yml deleted file mode 100644 index 180b9bb..0000000 --- a/config.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -warengroup: - domain: waren.io - folder: warengroup -cwinfo: - domain: cwinfo.net - folder: cwinfo -cwchristerw: - domain: christerwaren.fi - folder: cwchristerw diff --git a/src/base.sh b/src/base.sh index 98662f1..8bd5ff3 100644 --- a/src/base.sh +++ b/src/base.sh @@ -23,6 +23,150 @@ start() { syntax-line-douple } +login() { + echo ">>> Login <<<" + ORG=$2 + + if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then + stop + fi + + if [[ $(hostname -d) == "devices.waren.io" ]] + then + ORG=warengroup + elif [[ $(hostname -d) == "devices.cwinfo.net" ]] + then + ORG=cwinfo + elif [[ $(hostname -d) == "devices.christerwaren.fi" ]] + then + ORG=cwchristerw + elif [[ -z $ORG ]] + then + echo "Organization: Required" + exit 1 + fi + + if [[ $ORG == "warengroup" ]] + then + DOMAIN=waren.io + elif [[ $ORG == "cwinfo" ]] + then + DOMAIN=cwinfo.net + elif [[ $ORG == "cwchristerw" ]] + then + DOMAIN=christerwaren.fi + else + echo "Organization: Unsupported" + exit 1 + fi + + FOLDER=$ORG + DEVICE_DOMAIN="devices.$DOMAIN" + IDM_DOMAIN="idm.cwinfo.net" + VAULT_DOMAIN="vault.cwinfo.net" + GIT_DOMAIN="git.cwinfo.net" + + if [[ $(hostname -d) ]] + then + HOSTNAME=$(hostname --fqdn) + elif [[ -z $(hostname -d) ]] && [[ ! -z $2 ]] + then + HOSTNAME="$2.$DEVICE_DOMAIN" + else + echo "Hostname: Required" + exit 1 + fi + + if [[ -z $USER ]] + then + if [[ -z $SUDO_USER ]] + then + if [[ -z LOGNAME ]] + then + echo "Username: Required" + exit 1 + else + USER=$LOGNAME + fi + else + USER=$SUDO_USER + fi + fi + + VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health) + if [[ $VAULT_STATUS -eq 200 ]] + then + IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN) + if [[ $IDM_STATUS -eq 301 ]] + then + echo "Organization: $ORG" + if [[ -z $USER || $USER == "root" || $USER == "local" ]] + then + echo -n "Username: " + read USERNAME + else + echo "Username: $USER" + USERNAME=$USER + fi + + echo -n "Password: " + read -s PASSWORD + echo -n "****************" + 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" + exit 1 + fi + else + echo "Organization: $ORG" + echo "Status: IDM Offline" + exit 1 + fi + else + echo "Organization: $ORG" + echo "Status: Vault Offline" + exit 1 + fi + + + + # VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://vault.$DOMAIN/v1/sys/health) + # if [[ $VAULT_STATUS -eq 200 ]]; then + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault token lookup -address=https://vault.$DOMAIN 2> /dev/null) + # if [[ -z $VAULT_LOGIN ]]; then + # IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://idm.$DOMAIN) + # if [[ $IDM_STATUS -eq 301 ]]; then + # echo "vault.$DOMAIN - Login" + # echo "Username: $USER" + # echo -n "Password: " + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=ldap username=$USER 2> /dev/null) + # echo "" + # if [[ -z $VAULT_LOGIN ]]; then + # exit 1 + # fi + # else + # echo "vault.$DOMAIN - Login" + # echo -n "Token: " + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=token 2> /dev/null) + # echo "" + # if [[ -z $VAULT_LOGIN || ${#VAULT_TOKEN} -lt 95 || ${#VAULT_TOKEN} -gt 95 ]]; then + # exit 1 + # fi + # fi + # fi + # fi + + + # if [[ $USER == "root" || $USER == "local" ]] + # then + # echo "Status: Login Failed" + # fi + start +} + stop () { if [[ -d "./src" ]] then @@ -44,8 +188,6 @@ stop () { {{ COMMANDS }} -COMMAND=$1 - start if [[ ! -z $1 ]] && [[ $(type -t warencli-$1) == function ]] then diff --git a/src/commands/auth/login.sh b/src/commands/auth/login.sh deleted file mode 100644 index 12e8195..0000000 --- a/src/commands/auth/login.sh +++ /dev/null @@ -1,3 +0,0 @@ -warencli-login() { - echo ">>> Login <<<"; -} diff --git a/src/commands/auth/logout.sh b/src/commands/auth/logout.sh deleted file mode 100644 index 1fe7fe5..0000000 --- a/src/commands/auth/logout.sh +++ /dev/null @@ -1,3 +0,0 @@ -warencli-logout() { - echo ">>> Logout <<<"; -} diff --git a/src/commands/help.sh b/src/commands/help.sh index 45231ac..bbf1a3e 100644 --- a/src/commands/help.sh +++ b/src/commands/help.sh @@ -7,10 +7,6 @@ Common Commands: config Config sign Certificates -Authentication Commands: - login Login - logout Logout - Management Commands: settings Settings "; diff --git a/wx b/wx index 4830b4b..5dcce9b 100755 --- a/wx +++ b/wx @@ -23,6 +23,150 @@ start() { syntax-line-douple } +login() { + echo ">>> Login <<<" + ORG=$2 + + if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then + stop + fi + + if [[ $(hostname -d) == "devices.waren.io" ]] + then + ORG=warengroup + elif [[ $(hostname -d) == "devices.cwinfo.net" ]] + then + ORG=cwinfo + elif [[ $(hostname -d) == "devices.christerwaren.fi" ]] + then + ORG=cwchristerw + elif [[ -z $ORG ]] + then + echo "Organization: Required" + exit 1 + fi + + if [[ $ORG == "warengroup" ]] + then + DOMAIN=waren.io + elif [[ $ORG == "cwinfo" ]] + then + DOMAIN=cwinfo.net + elif [[ $ORG == "cwchristerw" ]] + then + DOMAIN=christerwaren.fi + else + echo "Organization: Unsupported" + exit 1 + fi + + FOLDER=$ORG + DEVICE_DOMAIN="devices.$DOMAIN" + IDM_DOMAIN="idm.cwinfo.net" + VAULT_DOMAIN="vault.cwinfo.net" + GIT_DOMAIN="git.cwinfo.net" + + if [[ $(hostname -d) ]] + then + HOSTNAME=$(hostname --fqdn) + elif [[ -z $(hostname -d) ]] && [[ ! -z $2 ]] + then + HOSTNAME="$2.$DEVICE_DOMAIN" + else + echo "Hostname: Required" + exit 1 + fi + + if [[ -z $USER ]] + then + if [[ -z $SUDO_USER ]] + then + if [[ -z LOGNAME ]] + then + echo "Username: Required" + exit 1 + else + USER=$LOGNAME + fi + else + USER=$SUDO_USER + fi + fi + + VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health) + if [[ $VAULT_STATUS -eq 200 ]] + then + IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN) + if [[ $IDM_STATUS -eq 301 ]] + then + echo "Organization: $ORG" + if [[ -z $USER || $USER == "root" || $USER == "local" ]] + then + echo -n "Username: " + read USERNAME + else + echo "Username: $USER" + USERNAME=$USER + fi + + echo -n "Password: " + read -s PASSWORD + echo -n "****************" + 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" + exit 1 + fi + else + echo "Organization: $ORG" + echo "Status: IDM Offline" + exit 1 + fi + else + echo "Organization: $ORG" + echo "Status: Vault Offline" + exit 1 + fi + + + + # VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://vault.$DOMAIN/v1/sys/health) + # if [[ $VAULT_STATUS -eq 200 ]]; then + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault token lookup -address=https://vault.$DOMAIN 2> /dev/null) + # if [[ -z $VAULT_LOGIN ]]; then + # IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://idm.$DOMAIN) + # if [[ $IDM_STATUS -eq 301 ]]; then + # echo "vault.$DOMAIN - Login" + # echo "Username: $USER" + # echo -n "Password: " + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=ldap username=$USER 2> /dev/null) + # echo "" + # if [[ -z $VAULT_LOGIN ]]; then + # exit 1 + # fi + # else + # echo "vault.$DOMAIN - Login" + # echo -n "Token: " + # export VAULT_ADDR="https://vault.$DOMAIN" + # VAULT_LOGIN=$(vault login -address=https://vault.$DOMAIN -method=token 2> /dev/null) + # echo "" + # if [[ -z $VAULT_LOGIN || ${#VAULT_TOKEN} -lt 95 || ${#VAULT_TOKEN} -gt 95 ]]; then + # exit 1 + # fi + # fi + # fi + # fi + + + # if [[ $USER == "root" || $USER == "local" ]] + # then + # echo "Status: Login Failed" + # fi + start +} + stop () { if [[ -d "./src" ]] then @@ -51,10 +195,6 @@ Common Commands: config Config sign Certificates -Authentication Commands: - login Login - logout Logout - Management Commands: settings Settings "; @@ -68,14 +208,6 @@ warencli-ssh() { echo ">>> SSH <<<"; } -warencli-login() { - echo ">>> Login <<<"; -} - -warencli-logout() { - echo ">>> Logout <<<"; -} - warencli-ssh-config(){ echo ">>> SSH: Config <<<" } @@ -85,8 +217,6 @@ warencli-ssh-sign(){ } -COMMAND=$1 - start if [[ ! -z $1 ]] && [[ $(type -t warencli-$1) == function ]] then