-
Notifications
You must be signed in to change notification settings - Fork 498
encrypt neon connection strings, update connections route #879
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b0563d
encrypted connection strings, updates, tests
BilalG1 993daf0
small fix
BilalG1 b93e6a9
test fix
BilalG1 466db95
Merge remote-tracking branch 'origin/dev' into encrypt-connection-string
BilalG1 f237e8d
fix env vars
BilalG1 5a7a0f5
Merge dev into encrypt-connection-string
N2D4 4cd96a7
remove client side encryption for connection strings
BilalG1 6552a56
Merge branch 'dev' into encrypt-connection-string
BilalG1 094499b
Merge branch 'dev' into encrypt-connection-string
BilalG1 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
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
82 changes: 82 additions & 0 deletions
82
apps/backend/src/app/api/latest/integrations/neon/projects/connection/route.tsx
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,82 @@ | ||
| import { overrideProjectConfigOverride } from "@/lib/config"; | ||
| import { getPrismaClientForSourceOfTruth, globalPrismaClient } from "@/prisma-client"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { stackServerApp } from "@/stack"; | ||
| import { KnownErrors } from "@stackframe/stack-shared"; | ||
| import { neonAuthorizationHeaderSchema, yupArray, yupNumber, yupObject, yupString, yupTuple } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; | ||
| import { decodeBasicAuthorizationHeader } from "@stackframe/stack-shared/dist/utils/http"; | ||
| import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids"; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| query: yupObject({ | ||
| project_id: yupString().defined(), | ||
| }).defined(), | ||
| body: yupObject({ | ||
| connection_strings: yupArray(yupObject({ | ||
| branch_id: yupString().defined(), | ||
| connection_string: yupString().defined(), | ||
| }).defined()).defined(), | ||
| }).defined(), | ||
| headers: yupObject({ | ||
| authorization: yupTuple([neonAuthorizationHeaderSchema.defined()]).defined(), | ||
| }).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| project_id: yupString().defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async (req) => { | ||
| const [clientId] = decodeBasicAuthorizationHeader(req.headers.authorization[0])!; | ||
| const provisionedProject = await globalPrismaClient.provisionedProject.findUnique({ | ||
| where: { | ||
| projectId: req.query.project_id, | ||
| clientId: clientId, | ||
| }, | ||
| }); | ||
| if (!provisionedProject) { | ||
| throw new KnownErrors.ProjectNotFound(req.query.project_id); | ||
| } | ||
|
|
||
| const uuidConnectionStrings: Record<string, string> = {}; | ||
| const store = await stackServerApp.getDataVaultStore('neon-connection-strings'); | ||
| const secret = "no client side encryption"; | ||
| for (const c of req.body.connection_strings) { | ||
| const uuid = generateUuid(); | ||
| await store.setValue(uuid, c.connection_string, { secret }); | ||
| uuidConnectionStrings[c.branch_id] = uuid; | ||
| } | ||
|
|
||
| const sourceOfTruthPersisted = { | ||
| type: 'neon' as const, | ||
| connectionStrings: uuidConnectionStrings, | ||
| }; | ||
| await overrideProjectConfigOverride({ | ||
| projectId: provisionedProject.projectId, | ||
| projectConfigOverrideOverride: { | ||
| sourceOfTruth: sourceOfTruthPersisted, | ||
| }, | ||
| }); | ||
|
|
||
| await Promise.all(req.body.connection_strings.map(({ branch_id, connection_string }) => getPrismaClientForSourceOfTruth({ | ||
| type: 'neon', | ||
| connectionString: undefined, | ||
| connectionStrings: { [branch_id]: connection_string }, | ||
| } as const, branch_id))); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| project_id: provisionedProject.projectId, | ||
| }, | ||
| }; | ||
| }, | ||
BilalG1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
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
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,10 @@ | ||
| import { StackServerApp } from '@stackframe/stack'; | ||
| import { getEnvVariable } from '@stackframe/stack-shared/dist/utils/env'; | ||
|
|
||
| export const stackServerApp = new StackServerApp({ | ||
| projectId: 'internal', | ||
| tokenStore: 'memory', | ||
| baseUrl: getEnvVariable('NEXT_PUBLIC_STACK_API_URL'), | ||
| publishableClientKey: getEnvVariable('STACK_SEED_INTERNAL_PROJECT_PUBLISHABLE_CLIENT_KEY'), | ||
| secretServerKey: getEnvVariable('STACK_SEED_INTERNAL_PROJECT_SECRET_SERVER_KEY'), | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.