-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update bypass rls execution and move alembic files to test
Co-authored-by: Ghaith Kdimati <[email protected]>"
- Loading branch information
Showing
11 changed files
with
205 additions
and
104 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from sqlalchemy import text, TextClause | ||
|
||
|
||
def generate_rls_policy(cmd, definition, policy_name, table_name, expr) -> TextClause: | ||
bypass_rls_expr = ( | ||
"CAST(NULLIF(current_setting('rls.bypass_rls', true), '') AS BOOLEAN) = true" | ||
) | ||
expr = f"(({expr}) OR {bypass_rls_expr})" | ||
|
||
print("%%%%%%%%%%%%%%%%%%%%%%%%%") | ||
print("expr", expr) | ||
print("%%%%%%%%%%%%%%%%%%%%%%%%%") | ||
|
||
if cmd in ["ALL", "SELECT", "UPDATE", "DELETE"]: | ||
return text(f""" | ||
CREATE POLICY {policy_name} ON {table_name} | ||
AS {definition} | ||
FOR {cmd} | ||
USING ({expr}) | ||
""") | ||
|
||
elif cmd in ["INSERT"]: | ||
return text(f""" | ||
CREATE POLICY {policy_name} ON {table_name} | ||
AS {definition} | ||
FOR {cmd} | ||
WITH CHECK ({expr}) | ||
""") | ||
|
||
else: | ||
raise ValueError(f'Unknown policy command"{cmd}"') |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""initial updates | ||
Revision ID: 4e47974b6aaf | ||
Revises: | ||
Create Date: 2024-09-30 12:55:59.618503 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "4e47974b6aaf" | ||
down_revision: Union[str, None] = None | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"users", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("username", sa.String(), nullable=True), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index(op.f("ix_users_id"), "users", ["id"], unique=False) | ||
op.create_index(op.f("ix_users_username"), "users", ["username"], unique=True) | ||
op.create_table( | ||
"items", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("title", sa.String(), nullable=True), | ||
sa.Column("description", sa.String(), nullable=True), | ||
sa.Column("owner_id", sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["owner_id"], | ||
["users.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index(op.f("ix_items_id"), "items", ["id"], unique=False) | ||
op.create_index(op.f("ix_items_title"), "items", ["title"], unique=False) | ||
op.enable_rls("items") | ||
op.create_policy( | ||
table_name="items", | ||
policy_name="items_permissive_all_policy_0", | ||
cmd="ALL", | ||
definition="PERMISSIVE", | ||
expr="owner_id > NULLIF(current_setting('rls.account_id', true),'')::INTEGER", | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_policy(tablename="items", policyname="items_permissive_all_policy_0") | ||
op.disable_rls("items") | ||
op.drop_index(op.f("ix_items_title"), table_name="items") | ||
op.drop_index(op.f("ix_items_id"), table_name="items") | ||
op.drop_table("items") | ||
op.drop_index(op.f("ix_users_username"), table_name="users") | ||
op.drop_index(op.f("ix_users_id"), table_name="users") | ||
op.drop_table("users") | ||
# ### end Alembic commands ### |
Oops, something went wrong.