@@ -53,12 +53,16 @@ def get_session_cookies(self) -> str:
5353 :return: a string of session cookies in the form "key1=value1; key2=value2"
5454 """
5555 max_retries = 3
56- retry_delay = 2
57-
58- # Use a persistent session for cookies
59- session = requests .Session ()
56+ base_retry_delay = 2
6057
6158 for attempt in range (max_retries ):
59+ # Create a fresh session for each attempt to avoid stale state
60+ session = requests .Session ()
61+
62+ if attempt > 0 :
63+ delay = base_retry_delay * (2 ** (attempt - 1 ))
64+ time .sleep (delay )
65+
6266 try :
6367 # GET the endpoint_url, which should redirect to Dex
6468 response = session .get (
@@ -68,8 +72,8 @@ def get_session_cookies(self) -> str:
6872 )
6973 if response .status_code == 200 :
7074 pass
71- elif response .status_code == 403 :
72- # if we get 403, we might be at the oauth2-proxy sign-in page
75+ elif response .status_code in [ 401 , 403 ] :
76+ # if we get 401/ 403, we might be at the oauth2-proxy sign-in page
7377 # the default path to start the sign-in flow is `/oauth2/start?rd=<url>`
7478 url_object = urlsplit (response .url )
7579 url_object = url_object ._replace (
@@ -81,6 +85,10 @@ def get_session_cookies(self) -> str:
8185 allow_redirects = True ,
8286 verify = not self ._skip_tls_verify
8387 )
88+ if response .status_code not in [200 , 302 ]:
89+ raise RuntimeError (
90+ f"HTTP status code '{ response .status_code } ' for GET against oauth2/start"
91+ )
8492 else :
8593 raise RuntimeError (
8694 f"HTTP status code '{ response .status_code } ' for GET against: { self ._endpoint_url } "
@@ -169,10 +177,8 @@ def get_session_cookies(self) -> str:
169177 if attempt == max_retries - 1 : # Last attempt
170178 print (f"All { max_retries } attempts failed. Last error: { str (e )} " )
171179 raise
180+ next_delay = base_retry_delay * (2 ** attempt )
172181 print (f"Attempt { attempt + 1 } failed: { str (e )} " )
173- print (f"Retrying in { retry_delay } seconds..." )
174- time .sleep (retry_delay )
175- retry_delay *= 2 # Exponential backoff
176182
177183
178184KUBEFLOW_ENDPOINT = "http://localhost:8080"
0 commit comments