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

Tenancies #418

Merged
merged 29 commits into from
Feb 8, 2025
Merged

Tenancies #418

merged 29 commits into from
Feb 8, 2025

Conversation

N2D4
Copy link
Contributor

@N2D4 N2D4 commented Feb 6, 2025

Adds Tenancies to the database schema. This is the name of the concept that's Project x Branch x Environment x Organization.

For now, this adds a tenancy property to SmartRequestAuth. Later, in the "Make all endpoints support organizations" step, we want to get rid of this, as only projectId and branchId are known by SmartRequest (each endpoint may use a different strategy to pick the organization — usually based on the user ID, but can be anything). However, for now, it makes the transition much easier.


Important

Introduces 'Tenancies' to support multi-tenancy, affecting database schema, authentication, and API handling.

  • Database Schema:
    • Introduces Tenancy table in migration.sql to represent Project x Branch x Environment x Organization.
    • Updates foreign keys and indices in migration.sql to use tenancyId instead of projectId.
    • Removes ProjectConfigOverride table.
  • Authentication:
    • Adds tenancy property to SmartRequestAuth in smart-request.tsx.
    • Modifies createAuthTokens() and generateAccessToken() in tokens.tsx to use tenancy.
    • Updates OAuthModel in model.tsx to handle tenancy for token operations.
  • API Changes:
    • Refactors CRUD handlers in crud-handler.tsx to support tenancy.
    • Updates verification code handling in verification-code-handler.tsx to use tenancy.
    • Modifies parseAuth() in smart-request.tsx to include tenancy in authentication context.
  • Miscellaneous:
    • Adjusts error handling in errors.tsx to accommodate new tenancy logic.
    • Updates e2e tests in backend-helpers.ts to validate tenancy-related changes.
    • Fixes minor UI issues in page-client.tsx and utils.tsx for webhooks.

This description was created by Ellipsis for 60f6231. It will automatically update as commits are pushed.

Copy link

vercel bot commented Feb 6, 2025

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

Name Status Preview Comments Updated (UTC)
stack-backend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 8, 2025 6:46am
stack-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 8, 2025 6:46am
stack-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 8, 2025 6:46am

@N2D4 N2D4 marked this pull request as draft February 6, 2025 05:53
apps/backend/src/route-handlers/smart-request.tsx Outdated Show resolved Hide resolved
apps/backend/src/lib/tenancies.tsx Outdated Show resolved Hide resolved
teamId String @default(uuid()) @db.Uuid
// Team IDs must be unique across all organizations (but not necessarily across all branches).
// To model this in the DB, we add two columns that are always equal to tenancy.projectId and tenancy.branchId.
mirroredProjectId String
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we call these mirroredProjectId instead of just projectId

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because they're not the source of truth, they are just a mirror. Like a cache

}

export default function PageClient() {
const stackAdminApp = useAdminApp();
const svixToken = stackAdminApp.useSvixToken();
const [updateCounter, setUpdateCounter] = useState(0);

// This is a hack to make sure svix hooks update when content changes
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure that after removing this code, the manual Svix API requests are refreshed?

Copy link
Contributor Author

@N2D4 N2D4 Feb 8, 2025

Choose a reason for hiding this comment

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

I haven't tested it but I'd be surprised if it didn't, I added key={updateCounter}

const { payload } = await jose.jwtVerify(accessToken, jwks);
expect(payload).toEqual({
"exp": expect.any(Number),
"iat": expect.any(Number),
"iss": "https://access-token.jwt-signature.stack-auth.com",
"aud": expect.any(String),
"sub": expect.any(String),
"branchId": "main",
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we going to have a different secret key per branch in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would the purpose of that be?

* eventually, we'll nicely pass around tenancies and won't need this function anymore, so the cache is a good temp
* solution
*/
const soleTenancyIdsCache = new Map<string, string>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it problematic if we delete a tenancy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that would only happen if we delete the associated Project

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