Multitenancy in Postgres with Go using Row Level Security (RLS)

tenancy

A Go library for multitenancy in Postgres using Row Level Security (RLS).

Usage

Tenancy as a connection pool.

By default, tenancy.Open() begins a connection pool which will open a new database connection for each database operation. The purpose of this pool is to support concurrent API layers such as GraphQL where a single request may be served by concurrent database operations.

For uses that do not require concurrent database connections per request, a single connection can be opened with either BeginTx() or Conn() When the request has finished being served, tenancy.Close() should be called which will close any open connections.

Tenancy without a connection pool

To use tenancy with a single connection for all queries rather than a Pool you can use the singleconnection configuration option

    tc, ctx, err := tenancy.Open(ctx, tenanted.DB.DB, "a4fab457-9fb3-4486-a2c3-22e767668506", tenancy.WithSingleConnection())

Tenancy as middleware

auth middleware -> tenancy.Open() -> request handlers -> tenancy.Close()

tenancy for background tasks

for each tenant -> tenancy.Open() -> process requiring tenanted database connection, you will likely use Conn() to get a single connection for the whole process -> tenancy.Close()

Interfaces

The tenancy package provides some interfaces which you can use in your applications where you want to enforce a tenanted database connection. The TContextExecutor interface satisfies the requirements of ORM libraries such as SQLBoiler which was the basis for creating this package.

Acknowledgements

tenancy was created by Joshua Wilkes and is maintained by Common Fate.

Owner
Common Fate
Security tools for innovative teams.
Common Fate
Similar Resources

Low-level Go interface to SQLite 3

zombiezen.com/go/sqlite This package provides a low-level Go interface to SQLite 3. It is a fork of crawshaw.io/sqlite that uses modernc.org/sqlite, a

Dec 21, 2022

write APIs using direct SQL queries with no hassle, let's rethink about SQL

SQLer SQL-er is a tiny portable server enables you to write APIs using SQL query to be executed when anyone hits it, also it enables you to define val

Jan 7, 2023

OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.

OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.

OctoSQL OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases, streaming sources and file formats using

Dec 29, 2022

Use SQL to query host, DNS and exploit information using Shodan. Open source CLI. No DB required.

Shodan Plugin for Steampipe Query Shodan with SQL Use SQL to query host, DNS and exploit information using Shodan. For example: select * from shod

Nov 10, 2022

Query and Provision Cloud Infrastructure using an extensible SQL based grammar

Query and Provision Cloud Infrastructure using an extensible SQL based grammar

Deploy, Manage and Query Cloud Infrastructure using SQL [Documentation] [Developer Guide] Cloud infrastructure coding using SQL InfraQL allows you to

Oct 25, 2022

Worker failover support for PostgreSQL Citus extension using pg_auto_failover.

citus-failover Worker failover support for citus community version using pg_auto_failover. What is this? This is a simple service to monitor changes i

Dec 7, 2022

A river for elasticsearch to automatically index mysql content using the replication feed.

Mysql River Plugin for ElasticSearch The Mysql River plugin allows to hook into Mysql replication feed using the excellent python-mysql-replication an

Jun 1, 2022

Simple instagram backend api built using golang and mongodb

Simple instagram backend api built using golang and mongodb

Insta_api using golang and mongodb Proper hashing algorithms have been implemented so that user password cannot be reverse engineered. Features: User

Apr 9, 2022

GoTSQL : A Better Way to Organize SQL codebase using Templates in Golang

GoTSQL - A Better Way to Organize SQL codebase using Templates in Golang Installation through Go Get command $ go get github.com/migopsrepos/gotsql In

Aug 17, 2022
Comments
  • V1 tenancy

    V1 tenancy

    This PR makes breaking changes to the tenancy package.

    Changes

    • make pool the default connection type when calling Open()
    • pool implements Conn() which behaves like sql.Conn() for checking out a single connection from the pool
    • update readme with some usage guides

    Recommended upgrade strategy, tag this as v1 or similar

    • update imports
    • replace usage of the previous tenancy.FromContextPool with tenancy.FromContext
    • add Pool.Conn() where a single connection was expected rather than a pool connection and you are using the pool
    • you can use a config to specify tenancy to use a single connection when you run tenancy.Open using tenancy.WithSingleConnection tc, ctx, err := tenancy.Open(ctx, tenanted.DB.DB, uuid.NewString(), tenancy.WithSingleConnection())
  • Fix close error wrapping

    Fix close error wrapping

    If the first error in errors.Wrap() is nil, the call to errors.Wrap returns nil, rather than an error!

    In the case where we receive an error while opening the connection, try to close the connection because of this error, and succeed in closing the connection, we get no error in response and our server will think that the connection has been successfully opened.

A fast data generator that's multi-table aware and supports multi-row DML.
A fast data generator that's multi-table aware and supports multi-row DML.

If you need to generate a lot of random data for your database tables but don't want to spend hours configuring a custom tool for the job, then datage

Dec 26, 2022
Go-postgres - go-postgres library provide NoSQL functionality which can execute queries with pool of connections

GO Postgres go-postgres library provide NoSQL functionality which can execute queries with pool of connections. What is this repository for? Establish

Dec 31, 2021
pREST (PostgreSQL REST), simplify and accelerate development, ⚡ instant, realtime, high-performance on any Postgres application, existing or new

pREST pREST (PostgreSQL REST), simplify and accelerate development, instant, realtime, high-performance on any Postgres application, existing or new P

Jan 9, 2023
pggen - generate type safe Go methods from Postgres SQL queries

pggen - generate type safe Go methods from Postgres SQL queries pggen is a tool that generates Go code to provide a typesafe wrapper around Postgres q

Jan 3, 2023
A reverse proxy for postgres which rewrites queries.

pg-rewrite-proxy A reverse proxy for postgres which rewrites queries. Arbitrary rewriting is supported by supplying an LUA script to the proxy applica

Dec 12, 2022
BQB is a lightweight and easy to use query builder that works with sqlite, mysql, mariadb, postgres, and others.

Basic Query Builder Why Simple, lightweight, and fast Supports any and all syntax by the nature of how it works Doesn't require learning special synta

Dec 7, 2022
API Go et db Postgres

API Go et db Postgres Pour faire fonctionner le back il faut suivre ces quelques étapes. -Il faut avoir installer Go et Postgres sur son ordinateur. -

Dec 14, 2021
Golang Redis Postgres to-do Project
Golang Redis Postgres to-do Project

Golang Backend Project Problem Statement Build a to-do application with Golang a

Oct 17, 2022
Go-miprimercrud - Mi Primer Crud - GO - Postgres

Mi Primer Crud - GO - Postgres Instalar driver Postgres go get -u github.com/lib

Jan 24, 2022