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

Chore/365 django rls #2558

Draft
wants to merge 13 commits into
base: develop
Choose a base branch
from
Draft

Chore/365 django rls #2558

wants to merge 13 commits into from

Conversation

dleard
Copy link
Contributor

@dleard dleard commented Dec 6, 2024

Not to be merged, for example only
Django - RLS proof of concept work.
Instructions in docs/rls_demo_instructions.md file

with connection.cursor() as cursor:
cursor.execute("select current_setting('my.guid', true)")
x = cursor.fetchone()
print("GUID: ", x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this print and the one after it for debugging purposes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keeping this middleware in the RLS app would be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this.

Comment on lines 1 to 15
class RlsRoles:
INDUSTRY_USER = "industry_user"
CAS_DIRECTOR = "cas_director"
CAS_ADMIN = "cas_admin"
CAS_ANALYST = "cas_analyst"
CAS_PENDING = "cas_pending"
CAS_VIEW_ONLY = "cas_view_only"
ALL_ROLES = 'industry_user, cas_director, cas_admin, cas_analyst, cas_pending, cas_view_only'


class RlsOperations:
SELECT = "select"
UPDATE = "update"
INSERT = "insert"
DELETE = "delete"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: suggest moving these to an enums.py and make them Enum

if auth_header:
user_guid = UUID(json.loads(auth_header).get('user_guid'), version=4)
with connection.cursor() as cursor:
cursor.execute(f'SET my.guid = "{user_guid}" ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this statement it would be better to use placeholders to prevent SQL injection.
cursor.execute('SET my.guid = %s', [str(user_guid)])

def revoke_all_privileges(self):
with connection.cursor() as cursor:
cursor.execute(f'drop owned by {RlsRoles.ALL_ROLES}')
cursor.execute(f'grant usage on schema erc to {RlsRoles.ALL_ROLES}')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add usage on schema common as well

self.schema = schema

def apply_policy(self, cursor): # type: ignore
execute_string = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use parameterized queries for all "execute string" calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants