88import jwt
99import base64
1010import uuid
11+ import requests
1112from kubernetes import client , config
1213
1314
15+ def revoke_all_tokens_via_api (cluster_url , bearer_token ):
16+ url = f"https://{ cluster_url } /rest-server/api/v1/token"
17+ headers = {
18+ "Authorization" : f"Bearer { bearer_token } " ,
19+ "Content-Type" : "application/json"
20+ }
21+ response = requests .delete (url , headers = headers )
22+ if response .status_code == 200 :
23+ print ("✓ All tokens revoked via REST API (cache cleared)." )
24+ return True
25+ else :
26+ print (f"✗ Failed to revoke tokens. Status: { response .status_code } , Response: { response .text } " )
27+ return False
28+
29+
1430def get_application_token (namespace = "default" , deployment_name = "alertmanager" , container_name = "job-status-change-notification" ):
1531 """
1632 Retrieve PAI_BEARER_TOKEN from application deployment.
@@ -50,38 +66,6 @@ def get_application_token(namespace="default", deployment_name="alertmanager", c
5066 return None
5167
5268
53- def delete_all_token_secrets (namespace = "pai-user-token" ):
54- """
55- Delete all secrets in the token namespace.
56-
57- Args:
58- namespace: Kubernetes namespace (default: pai-user-token)
59-
60- Returns:
61- Number of secrets deleted, or -1 on error
62- """
63- try :
64- config .load_kube_config ()
65- v1 = client .CoreV1Api ()
66-
67- # List all secrets in the namespace
68- secrets = v1 .list_namespaced_secret (namespace = namespace )
69-
70- deleted_count = 0
71- for secret in secrets .items :
72- secret_name = secret .metadata .name
73- try :
74- v1 .delete_namespaced_secret (name = secret_name , namespace = namespace )
75- print (f" Deleted secret: { secret_name } " )
76- deleted_count += 1
77- except Exception as e :
78- print (f" Failed to delete secret { secret_name } : { e } " )
79-
80- return deleted_count
81-
82- except Exception as e :
83- print (f"Error deleting secrets: { e } " )
84- return - 1
8569
8670
8771def add_token_to_k8s_secret (token_string , namespace = "pai-user-token" ):
@@ -144,10 +128,21 @@ def add_token_to_k8s_secret(token_string, namespace="pai-user-token"):
144128 print ("=" * 70 )
145129 print ("Token Revocation and Restoration Tool" )
146130 print ("=" * 70 )
147- print ("\n This tool will delete all the user tokens but keep the application token." )
131+ print ("\n This tool will revoke all user tokens via REST API (clearing cache)" )
132+ print ("and restore the application token." )
148133 print ("\n ⚠️ WARNING: This will cause service disruptions during execution!" )
149134 print ("=" * 70 )
150135
136+ cluster_url = input ("\n Enter the cluster URL (e.g. example.ltp.hpc-lucia.com): " )
137+ if not cluster_url :
138+ print ("Cluster URL cannot be empty." )
139+ sys .exit (1 )
140+
141+ admin_token = input ("Enter an admin bearer token: " )
142+ if not admin_token :
143+ print ("Bearer token cannot be empty." )
144+ sys .exit (1 )
145+
151146 confirm = input ("\n Type 'yes' to proceed: " )
152147 if confirm .lower () != 'yes' :
153148 print ("Operation cancelled." )
@@ -158,11 +153,7 @@ def add_token_to_k8s_secret(token_string, namespace="pai-user-token"):
158153 print ("Step 1: Retrieving PAI_BEARER_TOKEN from alert-manager" )
159154 print ("=" * 70 )
160155
161- alert_namespace = "default"
162- alert_deployment = "alertmanager"
163- alert_container = "job-status-change-notification"
164-
165- alert_token = get_application_token (alert_namespace , alert_deployment , alert_container )
156+ alert_token = get_application_token ()
166157
167158 if not alert_token :
168159 print ("\n ✗ Failed to retrieve alert-manager token." )
@@ -175,18 +166,12 @@ def add_token_to_k8s_secret(token_string, namespace="pai-user-token"):
175166 print (f"✓ Successfully retrieved alert-manager token" )
176167 print (f"Token (truncated): { alert_token [:20 ]} ...{ alert_token [- 20 :]} " )
177168
178- # Step 2: Delete all token secrets
169+ # Step 2: Revoke all tokens via REST API (clears cache)
179170 print ("\n " + "=" * 70 )
180- print ("Step 2: Deleting all token secrets " )
171+ print ("Step 2: Revoking all tokens via REST API " )
181172 print ("=" * 70 )
182173
183- token_namespace = "pai-user-token"
184-
185- deleted_count = delete_all_token_secrets (token_namespace )
186- if deleted_count >= 0 :
187- print (f"✓ Successfully deleted { deleted_count } secrets." )
188- else :
189- print (f"✗ Failed to delete secrets." )
174+ if not revoke_all_tokens_via_api (cluster_url , admin_token ):
190175 sys .exit (1 )
191176
192177 # Step 3: Add alert-manager token back
@@ -195,7 +180,7 @@ def add_token_to_k8s_secret(token_string, namespace="pai-user-token"):
195180 print ("Step 3: Adding alert-manager token back" )
196181 print ("=" * 70 )
197182
198- if add_token_to_k8s_secret (alert_token , token_namespace ):
183+ if add_token_to_k8s_secret (alert_token ):
199184 print ("✓ Alert-manager token has been restored successfully." )
200185 else :
201186 print ("✗ Failed to restore alert-manager token." )
0 commit comments