Skip to content
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

Tenant provisioning in the dataplane #2694

Merged
merged 7 commits into from
Oct 6, 2024
Merged

Conversation

pablonyx
Copy link
Contributor

@pablonyx pablonyx commented Oct 5, 2024

Description

[Provide a brief description of the changes in this PR]

How Has This Been Tested?

[Describe the tests you ran to verify your changes]

Accepted Risk

[Any know risks or failure modes to point out to reviewers]

Related Issue(s)

[If applicable, link to the issue(s) this PR addresses]

Checklist:

  • All of the automated tests pass
  • All PR comments are addressed and marked resolved
  • If there are migrations, they have been rebased to latest main
  • If there are new dependencies, they are added to the requirements
  • If there are new environment variables, they are added to all of the deployment methods
  • If there are new APIs that don't require auth, they are added to PUBLIC_ENDPOINT_SPECS
  • Docker images build and basic functionalities work
  • Author has done a final read through of the PR right before merge

Copy link

vercel bot commented Oct 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
internal-search ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 6, 2024 3:54am

@pablonyx pablonyx marked this pull request as ready for review October 5, 2024 21:47
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR implements tenant provisioning in the dataplane for the Danswer project, introducing multi-tenancy support across various components.

  • Added new CreateTenantRequest model in backend/ee/danswer/server/tenants/models.py for tenant creation
  • Implemented tenant creation endpoint in backend/ee/danswer/server/tenants/api.py with schema creation and Alembic migrations
  • Modified backend/danswer/db/engine.py to support multi-tenant database operations, including schema switching
  • Added control_plane_dep function in backend/danswer/auth/users.py for JWT-based authentication in the control plane
  • Updated backend/alembic/env.py to support multi-tenant schema migrations

17 file(s) reviewed, 9 comment(s)
Edit PR Review Bot Settings

payload = jwt.decode(token, DATA_PLANE_SECRET, algorithms=["HS256"])
if payload.get("scope") != "tenant:create":
Copy link

Choose a reason for hiding this comment

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

style: Consider using a more specific algorithm like 'HS256' instead of the generic 'HS256'

Comment on lines 298 to 307
# Set the search_path to the tenant's schema
session.execute(text(f'SET search_path = "{tenant_id}"'))
Copy link

Choose a reason for hiding this comment

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

style: Use parameterized query to set search_path for better security

if not is_valid_schema_name(tenant_id):
raise HTTPException(status_code=400, detail="Invalid tenant ID")
# Set the search_path to the tenant's schema
await async_session.execute(text(f'SET search_path = "{tenant_id}"'))
Copy link

Choose a reason for hiding this comment

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

style: Use parameterized query to set search_path for better security

else:
logger.info(f"Schema already exists for tenant {tenant_id}")

run_alembic_migrations(tenant_id)
Copy link

Choose a reason for hiding this comment

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

style: Consider wrapping this in a try-except block to handle migration failures

Comment on lines +42 to +46
except Exception as e:
logger.exception(f"Failed to create tenant {tenant_id}: {str(e)}")
raise HTTPException(
status_code=500, detail=f"Failed to create tenant: {str(e)}"
)
Copy link

Choose a reason for hiding this comment

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

style: Catch specific exceptions and handle them differently

@@ -0,0 +1,6 @@
from pydantic import BaseModel
Copy link

Choose a reason for hiding this comment

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

style: Consider adding type hints for improved code readability and maintainability

Comment on lines +4 to +6
class CreateTenantRequest(BaseModel):
tenant_id: str
initial_admin_email: str
Copy link

Choose a reason for hiding this comment

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

style: Consider adding field descriptions using Pydantic's Field for better documentation

Comment on lines +33 to +34
alembic_cfg.cmd_opts = SimpleNamespace() # type: ignore
alembic_cfg.cmd_opts.x = [f"schema={schema_name}"] # type: ignore
Copy link

Choose a reason for hiding this comment

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

style: Type ignores used here. Consider adding proper type annotations or explaining why they're necessary.

Comment on lines +36 to +40
# Run migrations programmatically
command.upgrade(alembic_cfg, "head")

# Run migrations programmatically
logger.info(
Copy link

Choose a reason for hiding this comment

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

style: Duplicate comment 'Run migrations programmatically'. Remove one instance.

@pablonyx pablonyx force-pushed the tenant_provisioning_dataplane branch from 2543664 to 748d7e1 Compare October 5, 2024 22:31
@pablonyx pablonyx changed the base branch from multi_tenant_alembic to main October 5, 2024 22:40
@pablonyx pablonyx enabled auto-merge October 5, 2024 22:40
Copy link
Contributor

@yuhongsun96 yuhongsun96 left a comment

Choose a reason for hiding this comment

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

didn't read it

@pablonyx pablonyx force-pushed the tenant_provisioning_dataplane branch from 6a7250d to b579648 Compare October 6, 2024 03:52
@pablonyx pablonyx added this pull request to the merge queue Oct 6, 2024
Merged via the queue into main with commit 0da736b Oct 6, 2024
7 checks passed
@pablonyx pablonyx deleted the tenant_provisioning_dataplane branch October 17, 2024 23:14
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.

2 participants