Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix - if refresh token was revoked, runner did not recover #1445

Merged
merged 1 commit into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/robusta/core/sinks/robusta/dal/supabase_dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from typing import Any, Dict, List, Optional

import requests
from postgrest._sync.request_builder import SyncQueryRequestBuilder
from postgrest.base_request_builder import BaseFilterRequestBuilder
from postgrest.exceptions import APIError as PostgrestAPIError
from postgrest.types import ReturnMethod
from postgrest.utils import sanitize_param
from supabase import create_client
Expand Down Expand Up @@ -70,6 +72,7 @@ def __init__(
self.cluster = cluster_name
options = ClientOptions(postgrest_client_timeout=SUPABASE_TIMEOUT_SECONDS, auto_refresh_token=True)
self.client = create_client(url, key, options)
self.patch_postgrest_execute()
self.email = email
self.password = password
self.sign_in()
Expand All @@ -78,6 +81,24 @@ def __init__(
self.persist_events = persist_events
self.signing_key = signing_key

def patch_postgrest_execute(self):
# This is somewhat hacky.
def execute_with_retry(_self):
try:
return self._original_execute(_self)
except PostgrestAPIError as exc:
message = exc.message or ""
if exc.code == "PGRST301" or "expired" in message.lower():
# JWT expired. Sign in again and retry the query
logging.error("JWT token expired/invalid, signing in to Supabase again")
self.sign_in()
return self._original_execute(_self)
else:
raise

self._original_execute = SyncQueryRequestBuilder.execute
SyncQueryRequestBuilder.execute = execute_with_retry

def __to_db_scanResult(self, scanResult: ScanReportRow) -> Dict[Any, Any]:
db_sr = scanResult.dict()
db_sr["account_id"] = self.account_id
Expand Down
Loading