Skip to content

Commit 0f79bde

Browse files
committed
drop alembic_utils
1 parent 097b9d5 commit 0f79bde

File tree

12 files changed

+264
-170
lines changed

12 files changed

+264
-170
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ MANIFEST
5252
.venv*/
5353
.conda*/
5454
.python-version
55+
notebooks

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ pip install fastapi-rowsecurity
3030

3131
## Basic Usage
3232

33-
In your SQLAlchemy model, create a `classmethod` named `__rls_policies__` that returns a list of `Permissive` or `Restrictive` policies:
33+
In your SQLAlchemy model, create an attribute named `__rls_policies__` that is a list of `Permissive` or `Restrictive` policies:
3434

3535
```py
36-
from fastapi_rowsecurity import Permissive, set_rls_policies
36+
from fastapi_rowsecurity import Permissive, register_rls
3737
from fastapi_rowsecurity.principals import Authenticated, UserOwner
3838

3939
Base = declarative_base()
40-
set_rls_policies(Base) # <- create all policies
40+
register_rls(Base) # <- create all policies
4141

4242

4343
class Item(Base):
@@ -48,18 +48,17 @@ class Item(Base):
4848
owner_id = Column(Integer, ForeignKey("users.id"))
4949
owner = relationship("User", back_populates="items")
5050

51-
@classmethod
52-
def __rls_policies__(cls):
53-
return [
54-
Permissive(principal=Authenticated, policy="SELECT"),
55-
Permissive(principal=UserOwner, policy=["INSERT", "UPDATE", "DELETE"]),
51+
52+
__rls_policies__ = [
53+
Permissive(expr=Authenticated, cmd="SELECT"),
54+
Permissive(expr=UserOwner, cmd=["INSERT", "UPDATE", "DELETE"]),
5655
]
5756
```
5857

5958
The above implies that any authenticated user can read all items; but can only insert, update or delete owned items.
6059

61-
- `principal`: any Boolean expression as a string;
62-
- `policy`: any of `ALL`/`SELECT`/`INSERT`/`UPDATE`/`DELETE`.
60+
- `expr`: any Boolean expression as a string;
61+
- `cmd`: any command of `ALL`/`SELECT`/`INSERT`/`UPDATE`/`DELETE`.
6362

6463
Next, attach the `current_user_id` (or other [runtime parameters](https://www.postgresql.org/docs/current/sql-set.html) that you need) to the user session:
6564

@@ -78,9 +77,9 @@ Find a simple example in the ![tests](./tests/simple_model.py).
7877
then ...
7978

8079
- [ ] Support for Alembic
81-
- [ ] How to deal with `BYPASSRLS` such as table owners?
8280
- [ ] When item is tried to delete, no error is raised?
8381
- [ ] Python 3.11
82+
- [ ] Coverage report
8483

8584
## Final note
8685

setup.cfg

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55

66
[metadata]
77
name = fastapi-rowsecurity
8-
description = Add a short description here!
8+
description = Row-Level Security (RLS) in SQLAlchemy.
99
author = jwdobken
1010
author_email = [email protected]
1111
license = MIT
1212
license_files = LICENSE.txt
1313
long_description = file: README.md
1414
long_description_content_type = text/markdown; charset=UTF-8; variant=GFM
15-
url = https://github.com/pyscaffold/pyscaffold/
15+
url = https://github.com/JWDobken/fastapi-rowsecurity
1616
# Add here related links, for example:
1717
project_urls =
18-
Documentation = https://pyscaffold.org/
19-
# Source = https://github.com/pyscaffold/pyscaffold/
18+
Documentation = https://github.com/JWDobken/fastapi-rowsecurity
19+
Source = https://github.com/JWDobken/fastapi-rowsecurity
2020
# Changelog = https://pyscaffold.org/en/latest/changelog.html
2121
# Tracker = https://github.com/pyscaffold/pyscaffold/issues
2222
# Conda-Forge = https://anaconda.org/conda-forge/pyscaffold
@@ -49,7 +49,6 @@ package_dir =
4949
# For more information, check out https://semver.org/.
5050
install_requires =
5151
importlib-metadata; python_version<"3.8"
52-
alembic_utils>=0.8
5352
pydantic>=2.5
5453

5554

src/fastapi_rowsecurity/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
finally:
1616
del version, PackageNotFoundError
1717

18-
from .main import set_rls_policies
18+
from .register_rls import register_rls
1919
from .schemas import Permissive, Policy, Restrictive
2020

21-
__all__ = ["set_rls_policies", "Permissive", "Policy", "Restrictive"]
21+
__all__ = ["register_rls", "Permissive", "Policy", "Restrictive"]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Type
2+
3+
from sqlalchemy import text
4+
from sqlalchemy.engine import Connection
5+
from sqlalchemy.ext.declarative import DeclarativeMeta
6+
7+
from .schemas import Permissive, Policy, Restrictive
8+
9+
10+
def create_policies(Base: Type[DeclarativeMeta], connection: Connection):
11+
"""Create policies for `Base.metadata.create_all()`."""
12+
for table, settings in Base.metadata.info["rls_policies"].items():
13+
# enable
14+
stmt = text(f"ALTER TABLE {table} ENABLE ROW LEVEL SECURITY;")
15+
connection.execute(stmt)
16+
# force by default
17+
stmt = text(f"ALTER TABLE {table} FORCE ROW LEVEL SECURITY;")
18+
connection.execute(stmt)
19+
# policies
20+
print("SETTINGS", settings)
21+
for ix, policy in enumerate(settings):
22+
for pol_stmt in policy.get_sql_policies(
23+
table_name=table, name_suffix=str(ix)
24+
):
25+
connection.execute(pol_stmt)
26+
connection.commit()

src/fastapi_rowsecurity/functions.py

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

src/fastapi_rowsecurity/main.py

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

src/fastapi_rowsecurity/policies.py

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

0 commit comments

Comments
 (0)