Skip to content

Commit a6ed144

Browse files
committed
fix(api): update database routing logic in MainRouter
1 parent ce859dd commit a6ed144

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

api/src/backend/api/db_router.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@ def db_for_write(self, model, **hints): # noqa: F841
4747
def allow_migrate(self, db, app_label, model_name=None, **hints): # noqa: F841
4848
return db == self.admin_db
4949

50+
# def allow_relation(self, obj1, obj2, **hints): # noqa: F841
51+
# # Allow relations when both objects originate from allowed connectors
52+
# allowed_dbs = {self.default_db, self.admin_db}
53+
# if {obj1._state.db, obj2._state.db} <= allowed_dbs:
54+
# return True
55+
# return None
56+
5057
def allow_relation(self, obj1, obj2, **hints): # noqa: F841
51-
# Allow relations if both objects are in either "default" or "admin" db connectors
52-
if {obj1._state.db, obj2._state.db} <= {self.default_db, self.admin_db}:
58+
# Allow relations when both objects originate from allowed connectors
59+
allowed_dbs = {self.default_db, self.admin_db, self.replica_db}
60+
if {obj1._state.db, obj2._state.db} <= allowed_dbs:
5361
return True
5462
return None
5563

api/src/backend/api/v1/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ def create(self, request, *args, **kwargs):
19801980
"scan_id": str(scan.id),
19811981
"provider_id": str(scan.provider_id),
19821982
# Disabled for now
1983-
# checks_to_execute=scan.scanner_args.get("checks_to_execute"),
1983+
"checks_to_execute": ["accessanalyzer_enabled"],
19841984
},
19851985
)
19861986

api/src/backend/tasks/jobs/integrations.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from config.django.base import DJANGO_FINDINGS_BATCH_SIZE
66
from tasks.utils import batched
77

8-
from api.db_router import READ_REPLICA_ALIAS
8+
from api.db_router import READ_REPLICA_ALIAS, MainRouter
99
from api.db_utils import rls_transaction
1010
from api.models import Finding, Integration, Provider
1111
from api.utils import initialize_prowler_integration, initialize_prowler_provider
@@ -208,7 +208,7 @@ def get_security_hub_client_from_integration(
208208
regions_status[region] = region in connection.enabled_regions
209209

210210
# Save regions information in the integration configuration
211-
with rls_transaction(tenant_id):
211+
with rls_transaction(tenant_id, using=MainRouter.default_db):
212212
integration.configuration["regions"] = regions_status
213213
integration.save()
214214

@@ -223,7 +223,7 @@ def get_security_hub_client_from_integration(
223223
return True, security_hub
224224
else:
225225
# Reset regions information if connection fails
226-
with rls_transaction(tenant_id):
226+
with rls_transaction(tenant_id, using=MainRouter.default_db):
227227
integration.configuration["regions"] = {}
228228
integration.save()
229229

@@ -334,8 +334,11 @@ def upload_security_hub_integration(
334334
f"Security Hub connection failed for integration {integration.id}: "
335335
f"{security_hub.error}"
336336
)
337-
integration.connected = False
338-
integration.save()
337+
with rls_transaction(
338+
tenant_id, using=MainRouter.default_db
339+
):
340+
integration.connected = False
341+
integration.save()
339342
break # Skip this integration
340343

341344
security_hub_client = security_hub

0 commit comments

Comments
 (0)