-
Notifications
You must be signed in to change notification settings - Fork 4
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
bharathkeyvalue
wants to merge
23
commits into
KeyValueSoftwareSystems:development
Choose a base branch
from
bharathkeyvalue:multi-tenant-feature
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
36c2e08
FEAT: Multi-tenancy support
bharathkeyvalue 66d5966
TEST: multi-tenent test fixes
bharathkeyvalue 1e4d149
FEAT: save tenantId in execution context
bharathkeyvalue 2900db2
FEAT: add postgres rls for tenant isolation
sruuuthy 67f6134
FIX: make execution context binder injectable
sruuuthy 65513d8
FEAT: use new database connection per tenant
sruuuthy 7572a60
FEAT: add tenant creation api
bharathkeyvalue 5e0b2a6
REFACTOR: remove tenantId in response
bharathkeyvalue 9d10fea
FEAT: modified usages of db queries to use tenant specific connection
sruuuthy 001b8bb
FIX: add configurable max connection limit per tenant if needed
sruuuthy ea0526f
REFACTOR: rename executionId.middleware.ts to executionContext.middle…
sruuuthy 93da280
FEAT: use sepearate db users for migrations and tenant operations
sruuuthy 29168b2
FIX: use dynamic connection for user permission updation
sruuuthy f9c3033
FIX: use admin user for db connection setup
sruuuthy f0c0d75
FEAT: add env validations for new postgres user variables
sruuuthy c0ec4c5
DOCS: update README with PostgresSQL admin and tenant user setup
sruuuthy d0f032c
FEAT: add tenant module
bharathkeyvalue c985e37
REFACTOR: use NestJS dependency injection in the request scope to obt…
sruuuthy 12e0a8e
REFACTOR: add util function for extracting token
bharathkeyvalue 1e7837b
BLD: Add db init script for creation of tenant user and db
sruuuthy cf1df66
DOC: Added description for multi tenancy support
sruuuthy 6fef4da
FEAT: Handle login for multi-tenancy
bharathkeyvalue 28d64e2
DOC: Add description for env variables used for handling multi-tenanc…
bharathkeyvalue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
POSTGRES_USER=postgres | ||
POSTGRES_PASSWORD=postgres | ||
POSTGRES_DB=authentication-service | ||
POSTGRES_TENANT_USER=tenant | ||
POSTGRES_TENANT_PASSWORD=tenant | ||
PGADMIN_DEFAULT_EMAIL= | ||
PGADMIN_DEFAULT_PASSWORD= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Create the database if it doesn't exist | ||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
DO \$\$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB') THEN | ||
CREATE DATABASE "$POSTGRES_DB" | ||
WITH | ||
OWNER = postgres | ||
ENCODING = 'UTF8'; | ||
END IF; | ||
END | ||
\$\$; | ||
EOSQL | ||
|
||
# Create tenant user if it doesn't exist | ||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
DO \$\$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_user WHERE usename = '$POSTGRES_TENANT_USER') THEN | ||
CREATE USER $POSTGRES_TENANT_USER WITH PASSWORD '$POSTGRES_TENANT_PASSWORD'; | ||
END IF; | ||
END | ||
\$\$; | ||
|
||
GRANT CONNECT ON DATABASE "$POSTGRES_DB" TO "$POSTGRES_TENANT_USER"; | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO "$POSTGRES_TENANT_USER"; | ||
GRANT USAGE ON SCHEMA public TO "$POSTGRES_TENANT_USER"; | ||
EOSQL | ||
|
||
echo "Database setup complete" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
|
||
@Injectable() | ||
export class AuthKeyGuard implements CanActivate { | ||
constructor(private configService: ConfigService) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const ctx = GqlExecutionContext.create(context).getContext(); | ||
if (ctx) { | ||
const authKeyInHeader = ctx.headers['x-api-key']; | ||
if (authKeyInHeader) { | ||
const secretKey = this.configService.get('AUTH_KEY') as string; | ||
return secretKey === authKeyInHeader; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated in commit here