Skip to content

Commit

Permalink
fix / encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
psiddharthdesign committed Aug 5, 2024
1 parent 4f5a12e commit 5bbebaa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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 (
<Card className="w-full max-w-5xl ">
<CardHeader className="space-y-1">
<CardTitle className="flex items-center space-x-2">
Secrets Key
</CardTitle>
<CardDescription>
Public key for encrypting sensitive variables
</CardDescription>
</CardHeader>
<CardFooter className='justify-start'>
{children}
</CardFooter>
</Card>
);
}

export async function SetSecretsKey({ organizationId }: { organizationId: string }) {
const publicKey = await getPublicKey(organizationId);
return (
Expand Down
20 changes: 11 additions & 9 deletions src/data/admin/env-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ export async function encryptSecretWithPublicKey(
export async function getOrganizationPublicKey(
orgId: string,
): Promise<string | null> {
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(
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
17 changes: 14 additions & 3 deletions src/data/user/secretKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SAPayload<{ publicKey: string; privateKey: string }>> {
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 5bbebaa

Please sign in to comment.