GithubHelp home page GithubHelp logo

logan-connolly / python-sqlalchemy-cerbos Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cerbos/python-sqlalchemy-cerbos

0.0 0.0 0.0 107 KB

An example application of integrating Cerbos with a FastAPI server using SQLAlchemy as the ORM.

License: Apache License 2.0

Shell 0.93% Python 99.07%

python-sqlalchemy-cerbos's Introduction

python-sqlalchemy-cerbos

An example application of integrating Cerbos with a FastAPI server using SQLAlchemy as the ORM. Cerbos helps you super-charge your authorization implementation by writing context-aware access control policies for your application resources. Author access rules using an intuitive YAML configuration language, use your Git-ops infrastructure to test and deploy them and, make simple API requests to the Cerbos PDP to evaluate the policies and make dynamic access decisions.

Dependencies

Getting Started

  1. Start up the Cerbos PDP instance docker container. This will be called by the FastAPI app to check authorization.
cd cerbos
./start.sh
  1. Install python dependencies
# from project root
pdm install
  1. Start the FastAPI dev server
pdm run demo

Sample data

On startup, the app will create an in-memory SQLite instance, and populate it with some sample data - see here.

The following users will be created. Authentication is done via Basic authentication using the provided Usernames (password is omitted). The Role and Department is loaded from the database after successful authentication.

ID Username Role Department
1 alice Admin IT
2 john User Sales
3 sarah User Sales
4 geri User Marketing

Policies

This example has a simple CRUD policy in place for a resource kind of contact - like a CRM system would have. The policy files can be found in the cerbos/policies folder here.

Should you wish to experiment with this policy, you can try it in the Cerbos Playground.

The policy expects one of two roles to be set on the principal - admin and user and an attribute which defines their department as either Sales or Marketing.

These roles are authorized as follows:

Action Role: Admin Derived Role: Owner (User) Role: User, department: Sales Role: User, department: Marketing
read Y Y if Contact is active if Contact is active and opted in to marketing
update Y Y
delete Y Y
create Y Y

Example Requests

Get all contacts

curl -i http://john@localhost:8000/contacts

Get a contact

As a Sales user => 200 OK

curl -i http://john@localhost:8000/contacts/2

As a Marketing user => 403 Unauthorized

curl -i http://geri@localhost:8000/contacts/2

python-sqlalchemy-cerbos's People

Contributors

sambigeara avatar logan-connolly avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.