Skip to main content

No project description provided

Project description

pg-rls-sqlalchemy

Work in progress.

SQLAlchemy and Alembic support for Postgres features like:

  • Row Level Security (RLS)
  • Policies

Built on top of alembic_utils but provides a more usable interface and a few missing features

Installation

pip install pg-rls-sqlalchemy

OR

poetry add pgalchemy

Policy and Row Level Security

Using RLS BaseModel

Recommended most projects. This is for projects with majority of tables using RLS which will also be almost all new projects using this library.

from sqlalchemy.orm import declarative_base
from pgalchemy import Policy, PolicyType, PolicyCommands, rls_base

BaseModel = rls_base(declarative_base())

class MyModel(BaseModel):
    ...
Policy("pol_my_models_select_primary", on=MyModel, as_=PolicyType.PERMISSIVE, for_=PolicyCommands.SELECT, using="user_id == auth.uid()")
Policy("pol_my_models_delete_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.DELETE, using="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.UPDATE, using="user_id == auth.uid()", with_check="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.INSERT, with_check="user_id == auth.uid()")

Using RLS Decorator

Only intended for projects with majority of tables without RLS enabled. Usually only for existing projects with most tables not protected using RLS that are only using RLS for a niche use case

This is not recommended for other use cases as it makes it easy for a developer to forget to enable RLS and expose a security vulnerability.

from sqlalchemy.orm import declarative_base
from pgalchemy import , rls_base, Policy, PolicyType, PolicyCommands

BaseModel = declarative_base()

@rls()
# Equivalent to:
# @policy(Policy("pol_my_models_primary", as_=PolicyType.PERMISSIVE, for_=PolicyCommands.ALL, using="user_id == auth.uid()", with_check="user_id == auth.uid()"))
class MyModel(BaseModel):
    ...
Policy("pol_my_models_select_primary", on=MyModel, as_=PolicyType.PERMISSIVE, for_=PolicyCommands.SELECT, using="user_id == auth.uid()")
Policy("pol_my_models_delete_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.DELETE, using="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.UPDATE, using="user_id == auth.uid()", with_check="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.INSERT, with_check="user_id == auth.uid()")

Functions

Using Python Functions

from pgalchemy.functions import sql_function
from pgalchemy.types import ReturnTypedExpression
@sql_function(schema='test')
def get_thing(id: int):
    return ReturnTypedExpression[MyModel](
        select(MyModel).where(MyModel.id == id)
    )

Using SQL File with empty python function

from pgalchemy.functions import sql_function
@sql_function(schema='test', path='../functions/get_thing.sql')
def get_thing(id: int) -> MyModel:
    pass

Using SQL File with metadata

import inspect
from pgalchemy.functions import Function

Function(
    schema='test', 
    path='../functions/get_thing.sql', 
    returns=MyModel,
    parameters=[inspect.Parameter(name='id', annotation=int)]
)

Views

Using Python Functions

from pgalchemy.views import sql_view
from pgalchemy.types import ReturnTypedExpression
@sql_view(schema='test')
def my_view():
    return select(MyModel).where(MyModel.id == id)

Using SQL File

import inspect
from pgalchemy.views import View

View(
    schema='test', 
    path='../views/my_view.sql', 
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgalchemy-0.1.3.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgalchemy-0.1.3-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file pgalchemy-0.1.3.tar.gz.

File metadata

  • Download URL: pgalchemy-0.1.3.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1025-azure

File hashes

Hashes for pgalchemy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7b64b98615ca849abb2e392328f771bee7743301ae4af3b8e74f23383d312452
MD5 c8898dca5ebe1cd317f4e2c8e740ff7d
BLAKE2b-256 707b8614721fb95e7fd0906ca1299e4039626e77e9713b97415d51c7af6e66e3

See more details on using hashes here.

File details

Details for the file pgalchemy-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pgalchemy-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1025-azure

File hashes

Hashes for pgalchemy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0cf6d55870b5737ee76b1aad95007f2c8c5e838e078cef1e4da158dfc893bc11
MD5 b21e461efaa170dee440d545942e3c52
BLAKE2b-256 92169d04e3d8f16906f306ab1130be8c4886dd7a531488c53bd9c146672da27f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page