Skip to content

Commit a517c57

Browse files
revert: RLS transactions handling and DB custom backend (#7995)
Co-authored-by: Víctor Fernández Poyatos <[email protected]>
1 parent 0eec05b commit a517c57

File tree

10 files changed

+29
-68
lines changed

10 files changed

+29
-68
lines changed

.github/workflows/api-pull-request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ jobs:
159159
working-directory: ./api
160160
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
161161
run: |
162-
poetry run safety check --ignore 70612,66963,74429
162+
poetry run safety check --ignore 70612,66963,74429,77119
163163
164164
- name: Vulture
165165
working-directory: ./api

api/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ repos:
8080
- id: safety
8181
name: safety
8282
description: "Safety is a tool that checks your installed dependencies for known security vulnerabilities"
83-
entry: bash -c 'poetry run safety check --ignore 70612,66963,74429'
83+
entry: bash -c 'poetry run safety check --ignore 70612,66963,74429,77119'
8484
language: system
8585

8686
- id: vulture

api/CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to the **Prowler API** are documented in this file.
44

5+
## [v1.8.4] (Prowler v5.7.4)
6+
7+
### Removed
8+
- Reverted RLS transaction handling and DB custom backend [(#7994)](https://github.com/prowler-cloud/prowler/pull/7994).
9+
10+
---
11+
512
## [v1.8.3] (Prowler v5.7.3)
613

714
### Added
@@ -15,7 +22,6 @@ All notable changes to the **Prowler API** are documented in this file.
1522
- Reverted the change `get_with_retry` to use the original `get` method for retrieving tasks [(#7932)](https://github.com/prowler-cloud/prowler/pull/7932).
1623
- Fixed the connection status verification before launching a scan [(#7831)](https://github.com/prowler-cloud/prowler/pull/7831)
1724

18-
1925
---
2026

2127
## [v1.8.2] (Prowler v5.7.2)
@@ -98,7 +104,6 @@ All notable changes to the **Prowler API** are documented in this file.
98104
- Fixed a race condition when deleting export files after the S3 upload [(#7172)](https://github.com/prowler-cloud/prowler/pull/7172).
99105
- Handled exception when a provider has no secret in test connection [(#7283)](https://github.com/prowler-cloud/prowler/pull/7283).
100106

101-
102107
### Added
103108

104109
- Support for developing new integrations [(#7167)](https://github.com/prowler-cloud/prowler/pull/7167).

api/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ name = "prowler-api"
3535
package-mode = false
3636
# Needed for the SDK compatibility
3737
requires-python = ">=3.11,<3.13"
38-
version = "1.8.3"
38+
version = "1.8.4"
3939

4040
[project.scripts]
4141
celery = "src.backend.config.settings.celery"

api/src/backend/api/base_views.py

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ def get_queryset(self):
4747

4848

4949
class BaseRLSViewSet(BaseViewSet):
50-
def initial(self, request, *args, **kwargs):
51-
super().initial(request, *args, **kwargs)
50+
def dispatch(self, request, *args, **kwargs):
51+
with transaction.atomic():
52+
return super().dispatch(request, *args, **kwargs)
5253

54+
def initial(self, request, *args, **kwargs):
5355
# Ideally, this logic would be in the `.setup()` method but DRF view sets don't call it
5456
# https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#django.views.generic.base.View.setup
5557
if request.auth is None:
@@ -59,19 +61,9 @@ def initial(self, request, *args, **kwargs):
5961
if tenant_id is None:
6062
raise NotAuthenticated("Tenant ID is not present in token")
6163

62-
self.request.tenant_id = tenant_id
63-
64-
self._rls_cm = rls_transaction(tenant_id)
65-
self._rls_cm.__enter__()
66-
67-
def finalize_response(self, request, response, *args, **kwargs):
68-
response = super().finalize_response(request, response, *args, **kwargs)
69-
70-
if hasattr(self, "_rls_cm"):
71-
self._rls_cm.__exit__(None, None, None)
72-
del self._rls_cm
73-
74-
return response
64+
with rls_transaction(tenant_id):
65+
self.request.tenant_id = tenant_id
66+
return super().initial(request, *args, **kwargs)
7567

7668
def get_serializer_context(self):
7769
context = super().get_serializer_context()
@@ -117,8 +109,6 @@ def _handle_creation_error(self, error, tenant):
117109
pass # Tenant might not exist, handle gracefully
118110

119111
def initial(self, request, *args, **kwargs):
120-
super().initial(request, *args, **kwargs)
121-
122112
if request.auth is None:
123113
raise NotAuthenticated
124114

@@ -127,44 +117,26 @@ def initial(self, request, *args, **kwargs):
127117
raise NotAuthenticated("Tenant ID is not present in token")
128118

129119
user_id = str(request.user.id)
130-
131-
self._rls_cm = rls_transaction(value=user_id, parameter=POSTGRES_USER_VAR)
132-
self._rls_cm.__enter__()
133-
134-
def finalize_response(self, request, response, *args, **kwargs):
135-
response = super().finalize_response(request, response, *args, **kwargs)
136-
137-
if hasattr(self, "_rls_cm"):
138-
self._rls_cm.__exit__(None, None, None)
139-
del self._rls_cm
140-
141-
return response
120+
with rls_transaction(value=user_id, parameter=POSTGRES_USER_VAR):
121+
return super().initial(request, *args, **kwargs)
142122

143123

144124
class BaseUserViewset(BaseViewSet):
145-
def initial(self, request, *args, **kwargs):
146-
super().initial(request, *args, **kwargs)
125+
def dispatch(self, request, *args, **kwargs):
126+
with transaction.atomic():
127+
return super().dispatch(request, *args, **kwargs)
147128

129+
def initial(self, request, *args, **kwargs):
148130
# TODO refactor after improving RLS on users
149131
if request.stream is not None and request.stream.method == "POST":
150-
return
132+
return super().initial(request, *args, **kwargs)
151133
if request.auth is None:
152134
raise NotAuthenticated
153135

154136
tenant_id = request.auth.get("tenant_id")
155137
if tenant_id is None:
156138
raise NotAuthenticated("Tenant ID is not present in token")
157139

158-
self.request.tenant_id = tenant_id
159-
160-
self._rls_cm = rls_transaction(tenant_id)
161-
self._rls_cm.__enter__()
162-
163-
def finalize_response(self, request, response, *args, **kwargs):
164-
response = super().finalize_response(request, response, *args, **kwargs)
165-
166-
if hasattr(self, "_rls_cm"):
167-
self._rls_cm.__exit__(None, None, None)
168-
del self._rls_cm
169-
170-
return response
140+
with rls_transaction(tenant_id):
141+
self.request.tenant_id = tenant_id
142+
return super().initial(request, *args, **kwargs)

api/src/backend/api/specs/v1.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
openapi: 3.0.3
22
info:
33
title: Prowler API
4-
version: 1.8.3
4+
version: 1.8.4
55
description: |-
66
Prowler API specification.
77

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class SchemaView(SpectacularAPIView):
260260

261261
def get(self, request, *args, **kwargs):
262262
spectacular_settings.TITLE = "Prowler API"
263-
spectacular_settings.VERSION = "1.8.3"
263+
spectacular_settings.VERSION = "1.8.4"
264264
spectacular_settings.DESCRIPTION = (
265265
"Prowler API specification.\n\nThis file is auto-generated."
266266
)

api/src/backend/config/django/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@
127127
}
128128

129129
DATABASE_ROUTERS = ["api.db_router.MainRouter"]
130-
POSTGRES_EXTRA_DB_BACKEND_BASE = "database_backend"
131130

132131

133132
# Password validation

api/src/backend/database_backend/__init__.py

Whitespace-only changes.

api/src/backend/database_backend/base.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)