From 293860028ffe518f5a764d8b808774bee7315668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=8C=AF=E5=AE=87?= <> Date: Wed, 15 Jan 2025 03:24:48 +0800 Subject: [PATCH] fix(k8s): validate Mathmast username input and enhance error handling in kubectl config clearing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 孙振宇 <> --- bin/freeleaps-cluster-authenticator | 31 +++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/bin/freeleaps-cluster-authenticator b/bin/freeleaps-cluster-authenticator index 1476238d..e9d9bc08 100755 --- a/bin/freeleaps-cluster-authenticator +++ b/bin/freeleaps-cluster-authenticator @@ -186,7 +186,15 @@ setup_kubelogin() { prompt_username() { echo "[PROMPT] Please enter your Mathmast account name (ending with @mathmast.com, eg. jack@mathmast.com):" + read -r username + + # While loop to check if username is valid + while ! echo "${username}" | grep -qE '^[a-zA-Z0-9._%+-]+@mathmast.com$'; do + echo "[ERROR] Username invalid, please enter a valid Mathmast account name (ending with @mathmast.com, eg. jack@mathmast.com):" + read -r username + done + echo "[PROMPT] Username: ${username}" } @@ -260,10 +268,25 @@ clear_auth() { echo "[CLEAR] Clearing kubectl authentication..." - kubectl config delete-user "${username}" - kubectl config delete-context "${username}@freeleaps-cluster" - kubectl config delete-cluster freeleaps-cluster - kubectl config unset current-context + if ! kubectl config delete-user "${username}" > /dev/null 2>&1; then + echo "[ERROR] User ${username} not found in kubectl config." + exit 1 + fi + + if ! kubectl config delete-context "${username}@freeleaps-cluster" > /dev/null 2>&1; then + echo "[ERROR] Context ${username}@freeleaps-cluster not found in kubectl config." + exit 1 + fi + + if ! kubectl config delete-cluster freeleaps-cluster > /dev/null 2>&1; then + echo "[ERROR] Cluster freeleaps-cluster not found in kubectl config." + exit 1 + fi + + if ! kubectl config unset current-context > /dev/null 2>&1; then + echo "[ERROR] Unable to unset current context in kubectl config." + exit 1 + fi echo "[CLEAR] kubectl authentication cleared successfully." }