From 5bbebaa28d6b4b64c431d6954d52978c2f23319e Mon Sep 17 00:00:00 2001 From: psiddharthdesign <107192927+psiddharthdesign@users.noreply.github.com> Date: Tue, 6 Aug 2024 00:27:14 +0530 Subject: [PATCH] fix / encryption --- .../settings/SetSecretsKey.tsx | 22 ------------------- src/data/admin/env-vars.ts | 20 +++++++++-------- src/data/user/secretKey.ts | 17 +++++++++++--- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/settings/SetSecretsKey.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/settings/SetSecretsKey.tsx index e68d5cbd..78a5eb51 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/settings/SetSecretsKey.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/settings/SetSecretsKey.tsx @@ -1,30 +1,8 @@ 'use server'; -import { Card, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { createKeyPair, deletePublicKey, getPublicKey } from '@/data/user/secretKey'; import { SecretsKeyManager } from './SecretKeyManager'; -const publicKey: string = 'asdfasdf'; //TODO state, fetch -const privateKey: string = 'asdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaaasdfaa'; //TODO state - -function Wrapper({ children }: { children: React.ReactNode }) { - return ( - - - - Secrets Key - - - Public key for encrypting sensitive variables - - - - {children} - - - ); -} - export async function SetSecretsKey({ organizationId }: { organizationId: string }) { const publicKey = await getPublicKey(organizationId); return ( diff --git a/src/data/admin/env-vars.ts b/src/data/admin/env-vars.ts index 9720787a..2df08b15 100644 --- a/src/data/admin/env-vars.ts +++ b/src/data/admin/env-vars.ts @@ -27,15 +27,18 @@ export async function encryptSecretWithPublicKey( export async function getOrganizationPublicKey( orgId: string, ): Promise { - const { data: publicKeyData } = await supabaseAdminClient + const { data: publicKeyData, error } = await supabaseAdminClient .from('organizations') .select('public_key') .eq('id', orgId) .single(); - if (publicKeyData?.public_key) { - return publicKeyData.public_key; + + if (error) { + console.error('Error fetching public key:', error); + throw error; } - return null; + + return publicKeyData?.public_key || null; } export async function storeEnvVar( @@ -45,14 +48,13 @@ export async function storeEnvVar( value: string, isSecret: boolean, ) { - const publicKey = await getOrganizationPublicKey(orgId); - let storedValue; if (isSecret) { + const publicKey = await getOrganizationPublicKey(orgId); if (!publicKey) { throw new Error('Cannot encrypt secret - no public key'); } - storedValue = encryptSecretWithPublicKey(value, publicKey); + storedValue = await encryptSecretWithPublicKey(value, publicKey); } else { storedValue = value; } @@ -71,10 +73,10 @@ export async function storeEnvVar( ); if (error) { - console.error('Encryption: Error storing variable:', error); + console.error('Error storing variable:', error); throw error; } - console.log('Encryption: Variable stored successfully'); + console.log('Variable stored successfully:', { name, isSecret }); return data; } export async function getEnvVar(projectId: string, name: string) { diff --git a/src/data/user/secretKey.ts b/src/data/user/secretKey.ts index 405b6397..33acd3d8 100644 --- a/src/data/user/secretKey.ts +++ b/src/data/user/secretKey.ts @@ -25,6 +25,13 @@ export async function getPublicKey( return data?.public_key || null; } +function stripKeyHeaders(key: string): string { + return key + .replace(/-----BEGIN (PUBLIC|PRIVATE) KEY-----/, '') + .replace(/-----END (PUBLIC|PRIVATE) KEY-----/, '') + .replace(/\n/g, ''); +} + export async function createKeyPair( organizationId: string, ): Promise> { @@ -44,10 +51,14 @@ export async function createKeyPair( }, }); - // Save public key to the database + // Strip headers and footers + const strippedPublicKey = stripKeyHeaders(publicKey); + const strippedPrivateKey = stripKeyHeaders(privateKey); + + // Save stripped public key to the database const { error } = await supabase .from('organizations') - .update({ public_key: publicKey }) + .update({ public_key: strippedPublicKey }) .eq('id', organizationId); if (error) throw error; @@ -56,7 +67,7 @@ export async function createKeyPair( return { status: 'success', - data: { publicKey, privateKey }, + data: { publicKey: strippedPublicKey, privateKey: strippedPrivateKey }, }; } catch (error) { console.error('Error creating key pair:', error);