Skip to content

Commit 98becf5

Browse files
fix attempt #3: Do not require a encrpytion key at build time
1 parent d17c90a commit 98becf5

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

packages/crypto/src/environment.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import dotenv from 'dotenv';
22

3-
export const getEnv = (env: string | undefined, defaultValue?: string, required?: boolean) => {
4-
if (required && !env && !defaultValue) {
5-
throw new Error(`Missing required environment variable`);
6-
}
7-
3+
export const getEnv = (env: string | undefined, defaultValue?: string) => {
84
return env ?? defaultValue;
95
}
106

@@ -14,4 +10,4 @@ dotenv.config({
1410
});
1511

1612
// @note: You can use https://generate-random.org/encryption-key-generator to create a new 32 byte key
17-
export const SOURCEBOT_ENCRYPTION_KEY = getEnv(process.env.SOURCEBOT_ENCRYPTION_KEY, undefined, true)!;
13+
export const SOURCEBOT_ENCRYPTION_KEY = getEnv(process.env.SOURCEBOT_ENCRYPTION_KEY);

packages/crypto/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const generateIV = (): Buffer => {
99
};
1010

1111
export function encrypt(text: string): { iv: string; encryptedData: string } {
12+
if (!SOURCEBOT_ENCRYPTION_KEY) {
13+
throw new Error('Encryption key is not set');
14+
}
15+
1216
const encryptionKey = Buffer.from(SOURCEBOT_ENCRYPTION_KEY, 'ascii');
1317

1418
const iv = generateIV();
@@ -21,6 +25,10 @@ export function encrypt(text: string): { iv: string; encryptedData: string } {
2125
}
2226

2327
export function decrypt(iv: string, encryptedText: string): string {
28+
if (!SOURCEBOT_ENCRYPTION_KEY) {
29+
throw new Error('Encryption key is not set');
30+
}
31+
2432
const encryptionKey = Buffer.from(SOURCEBOT_ENCRYPTION_KEY, 'ascii');
2533

2634
const ivBuffer = Buffer.from(iv, 'hex');

0 commit comments

Comments
 (0)