Skip to content

FEAT: Multi tenant feature #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: development
Choose a base branch
from

Conversation

bharathkeyvalue
Copy link

@bharathkeyvalue bharathkeyvalue commented Dec 12, 2024

Multi-Tenancy Implementation

Description

Key Features

  • Multi-Tenancy Support

    • Introduced foundational support for multi-tenancy across the application.
    • Added a tenant table and a tenant_id column to all relevant tables (excluding permissions).
    • Implemented an ExecutionManager to handle tenant context in asynchronous operations.
  • Row-Level Security (RLS)

    • Implemented Row-Level Security policies on all relevant tables.
    • Created a new database connection per tenant, with the configuration parameter app.tenant_id set to the specific tenant.
    • Enforced tenant-specific data access at the database level.
  • New Tenant Creation API

    • Added an API for tenant creation using an authentication key.

Environment Variable Changes

New Environment Variables

  • AUTH_KEY: Used for authentication key validation in the tenant creation API.
  • POSTGRES_ADMIN_USER: Admin username for migration runner.
  • POSTGRES_ADMIN_PASSWORD: Admin password for migration runner.
  • POSTGRES_TENANT_USER: Restricted tenant user for database connections.
  • POSTGRES_TENANT_PASSWORD: Password for the restricted tenant user.
  • POSTGRES_TENANT_MAX_CONNECTION_LIMIT: Maximum concurrent database connections for tenants (uses the default setting if unspecified).
  • POSTGRES_ADMIN_MAX_CONNECTION_LIMIT: Maximum concurrent database connections for the admin user (uses the default setting if unspecified).
  • MULTI_TENANCY_ENABLED: A boolean that indicates if multi-tenancy is enabled, used for handling user login.
  • DEFAULT_TENANT_ID: Default tenant id to be used when multi-tenancy is disabled.

@bharathkeyvalue bharathkeyvalue changed the title Multi tenant feature FEAT: Multi tenant feature Dec 13, 2024
@sruuuthy
Copy link

Postgres super users can bypass rls. A new user should be created that cannot bypass rls.

Copy link
Collaborator

@doylefermi-kv doylefermi-kv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and in the right direction 👍

* @returns connection
*/

export async function getConnection(): Promise<DataSource> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to use NestJS dependency injection in the request scope to obtain a connection from the connection pool? The connection can be injected to the necessary service as and when it needs it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit here

# Superuser for migrations
POSTGRES_ADMIN_USER=postgres
POSTGRES_ADMIN_PASSWORD=postgres
# Minimal user with restricted access
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a snippet in the readme how multi-tenancy is handled? And also the ways to create a tenant Postgres user.

Let's add a DB Init script in the docker-compose.yml file as well

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in commit here

@doylefermi-kv
Copy link
Collaborator

@bharathkeyvalue Note: Once PR is submitted and ready for review, do not force push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants