Skip to content

Commit 2790997

Browse files
authored
feat: delete sharedkey if invalid and attempt other auth method (#253)
* feat: delete sharedkey if invalid and attempt other auth method * feat/delete shared key if not valid * refactor/optimize imports * feat: remove verification of instance in the decorator and use delete_token response instead
1 parent 8c4d4a4 commit 2790997

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/sumo/wrapper/_auth_provider.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def has_case_token(self, case_uuid):
9999
get_token_path(self._resource_id, ".sharedkey", case_uuid)
100100
)
101101

102+
def delete_token(self):
103+
return False
104+
102105
pass
103106

104107

@@ -400,9 +403,10 @@ class AuthProviderSumoToken(AuthProvider):
400403
def __init__(self, resource_id, case_uuid=None):
401404
super().__init__(resource_id)
402405
protect_token_cache(resource_id, ".sharedkey", case_uuid)
403-
token_path = get_token_path(resource_id, ".sharedkey", case_uuid)
404-
with open(token_path, "r") as f:
406+
self.token_path = get_token_path(resource_id, ".sharedkey", case_uuid)
407+
with open(self.token_path, "r") as f:
405408
self._token = f.readline().strip()
409+
406410
return
407411

408412
def get_token(self):
@@ -411,6 +415,11 @@ def get_token(self):
411415
def get_authorization(self):
412416
return {"X-SUMO-Token": self._token}
413417

418+
def delete_token(self):
419+
if os.path.exists(self.token_path):
420+
os.unlink(self.token_path)
421+
return True
422+
414423

415424
@tn.retry(
416425
retry=tn.retry_if_exception(_maybe_nfs_exception),

src/sumo/wrapper/_decorators.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
def raise_for_status(func):
66
@wraps(func)
7-
def wrapper(*args, **kwargs):
7+
def wrapper(self, *args, **kwargs):
88
# FIXME: in newer versions of httpx, raise_for_status() is chainable,
99
# so we could simply write
1010
# return func(*args, **kwargs).raise_for_status()
11-
response = func(*args, **kwargs)
11+
response = func(self, *args, **kwargs)
12+
if response.status_code == 401:
13+
self._handle_invalid_shared_key()
1214
response.raise_for_status()
1315
return response
1416

@@ -17,11 +19,13 @@ def wrapper(*args, **kwargs):
1719

1820
def raise_for_status_async(func):
1921
@wraps(func)
20-
async def wrapper(*args, **kwargs):
22+
async def wrapper(self, *args, **kwargs):
2123
# FIXME: in newer versions of httpx, raise_for_status() is chainable,
2224
# so we could simply write
2325
# return func(*args, **kwargs).raise_for_status()
24-
response = await func(*args, **kwargs)
26+
response = await func(self, *args, **kwargs)
27+
if response.status_code == 401:
28+
self._handle_invalid_shared_key()
2529
response.raise_for_status()
2630
return response
2731

src/sumo/wrapper/sumo_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ def blob_client(self) -> BlobClient:
208208
self._retry_strategy,
209209
)
210210

211+
def _handle_invalid_shared_key(self):
212+
"""Handle the invalid shared key by deleting it."""
213+
if self.auth.delete_token():
214+
print(
215+
"Invalid shared key detected and deleted, run again to reset automatically"
216+
)
217+
211218
@raise_for_status
212219
def get(self, path: str, params: Optional[Dict] = None) -> httpx.Response:
213220
"""Performs a GET-request to the Sumo API.

0 commit comments

Comments
 (0)