Skip to content

Commit a70a5dc

Browse files
authored
Load the encryption key on demand, if it is not already loaded (#1660)
Part of OPS-3021.
1 parent 4bf77ff commit a70a5dc

File tree

7 files changed

+22
-47
lines changed

7 files changed

+22
-47
lines changed

packages/engine/src/engine-executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function executeEngine(
1515
): Promise<string> {
1616
const startTime = performance.now();
1717

18-
await encryptionKeyInitializer();
18+
encryptionKeyInitializer();
1919

2020
// TODO: Remove this from the server side
2121
engineInput.publicUrl = await networkUtls.getPublicUrl();

packages/server/api/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function validateEnvPropsOnStartup(): Promise<void> {
6464
);
6565
}
6666

67-
await encryptionKeyInitializer();
67+
void encryptionKeyInitializer();
6868

6969
const jwtSecret = await jwtUtils.getJwtSecret();
7070
if (isNil(jwtSecret)) {

packages/server/api/test/integration/ce/flows/flow-step-test-output.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encryptUtils, QueueMode } from '@openops/server-shared';
1+
import { encryptUtils } from '@openops/server-shared';
22
import { FlowVersionState, openOpsId } from '@openops/shared';
33
import { FastifyInstance } from 'fastify';
44
import { databaseConnection } from '../../../../src/app/database/database-connection';
@@ -13,7 +13,7 @@ import {
1313
let app: FastifyInstance | null = null;
1414

1515
beforeAll(async () => {
16-
await encryptUtils.loadEncryptionKey(QueueMode.MEMORY);
16+
encryptUtils.loadEncryptionKey();
1717
await databaseConnection().initialize();
1818
app = await setupServer();
1919
});

packages/server/api/test/integration/ce/flows/flow-version.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let app: FastifyInstance | null = null;
2121

2222
beforeAll(async () => {
2323
await databaseConnection().initialize();
24-
await encryptionKeyInitializer();
24+
void encryptionKeyInitializer();
2525
app = await setupServer();
2626
});
2727

packages/server/api/test/integration/cloud/project/project.service.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encryptUtils, QueueMode } from '@openops/server-shared';
1+
import { encryptUtils } from '@openops/server-shared';
22
import { openOpsId } from '@openops/shared';
33
import { databaseConnection } from '../../../../src/app/database/database-connection';
44
import { projectService } from '../../../../src/app/project/project-service';
@@ -8,7 +8,7 @@ import {
88
} from '../../../helpers/mocks';
99

1010
beforeAll(async () => {
11-
await encryptUtils.loadEncryptionKey(QueueMode.MEMORY);
11+
encryptUtils.loadEncryptionKey();
1212
await databaseConnection().initialize();
1313
});
1414

packages/server/shared/src/lib/security/encryption-key-initializer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { AppSystemProp, QueueMode, system } from '../system';
21
import { encryptUtils } from './encryption';
32

4-
export async function encryptionKeyInitializer(): Promise<void> {
5-
const queueMode = system.getOrThrow<QueueMode>(AppSystemProp.QUEUE_MODE);
6-
const encryptionKey = await encryptUtils.loadEncryptionKey(queueMode);
3+
export function encryptionKeyInitializer(): void {
4+
const encryptionKey = encryptUtils.loadEncryptionKey();
75
const isValidHexKey =
86
encryptionKey && /^[A-Fa-z0-9]{32}$/.test(encryptionKey);
97

packages/server/shared/src/lib/security/encryption.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
1-
import {
2-
assertNotNullOrUndefined,
3-
EncryptedObject,
4-
isNil,
5-
} from '@openops/shared';
1+
import { EncryptedObject, isNil } from '@openops/shared';
62
import * as crypto from 'crypto';
7-
import { randomBytes } from 'node:crypto';
8-
import { promisify } from 'util';
9-
import { AppSystemProp, QueueMode, system } from '../system';
10-
import { localFileStore } from './local-store';
3+
import { AppSystemProp, system } from '../system';
114

12-
let secret: string | null;
5+
let encryptionKey: string | null;
136
const algorithm = 'aes-256-cbc';
147
const ivLength = 16;
158

16-
const loadEncryptionKey = async (
17-
queueMode: QueueMode,
18-
): Promise<string | null> => {
19-
secret = system.get(AppSystemProp.ENCRYPTION_KEY) ?? null;
20-
if (queueMode === QueueMode.MEMORY) {
21-
if (isNil(secret)) {
22-
secret = await localFileStore.load(AppSystemProp.ENCRYPTION_KEY);
23-
}
24-
if (isNil(secret)) {
25-
secret = await generateAndStoreSecret();
26-
}
9+
const loadEncryptionKey = (): string => {
10+
if (isNil(encryptionKey)) {
11+
encryptionKey = system.getOrThrow(AppSystemProp.ENCRYPTION_KEY);
2712
}
28-
return secret;
29-
};
3013

31-
const generateAndStoreSecret = async (): Promise<string> => {
32-
const secretLengthInBytes = 16;
33-
const secretBuffer = await promisify(randomBytes)(secretLengthInBytes);
34-
const secret = secretBuffer.toString('hex'); // Convert to hexadecimal
35-
await localFileStore.save(AppSystemProp.ENCRYPTION_KEY, secret);
36-
return secret;
14+
return encryptionKey;
3715
};
3816

3917
function encryptString(inputString: string): EncryptedObject {
18+
const secret = loadEncryptionKey();
4019
const iv = crypto.randomBytes(ivLength); // Generate a random initialization vector
41-
assertNotNullOrUndefined(secret, 'secret');
4220
const key = Buffer.from(secret, 'binary');
4321
const cipher = crypto.createCipheriv(algorithm, key, iv); // Create a cipher with the key and initialization vector
4422
let encrypted = cipher.update(inputString, 'utf8', 'hex');
@@ -55,8 +33,8 @@ function encryptObject(object: unknown): EncryptedObject {
5533
}
5634

5735
function encryptBuffer(inputBuffer: Buffer): EncryptedObject {
36+
const secret = loadEncryptionKey();
5837
const iv = crypto.randomBytes(ivLength);
59-
assertNotNullOrUndefined(secret, 'secret');
6038
const key = Buffer.from(secret, 'binary');
6139
const cipher = crypto.createCipheriv(algorithm, key, iv);
6240
let encrypted = cipher.update(inputBuffer).toString('hex');
@@ -68,8 +46,8 @@ function encryptBuffer(inputBuffer: Buffer): EncryptedObject {
6846
}
6947

7048
function decryptObject<T>(encryptedObject: EncryptedObject): T {
49+
const secret = loadEncryptionKey();
7150
const iv = Buffer.from(encryptedObject.iv, 'hex');
72-
assertNotNullOrUndefined(secret, 'secret');
7351
const key = Buffer.from(secret, 'binary');
7452
const decipher = crypto.createDecipheriv(algorithm, key, iv);
7553
let decrypted = decipher.update(encryptedObject.data, 'hex', 'utf8');
@@ -78,8 +56,8 @@ function decryptObject<T>(encryptedObject: EncryptedObject): T {
7856
}
7957

8058
function decryptBuffer(encryptedObject: EncryptedObject): Buffer {
59+
const secret = loadEncryptionKey();
8160
const iv = Buffer.from(encryptedObject.iv, 'hex');
82-
assertNotNullOrUndefined(secret, 'secret');
8361
const key = Buffer.from(secret, 'binary');
8462
const decipher = crypto.createDecipheriv(algorithm, key, iv);
8563
return Buffer.concat([
@@ -89,8 +67,8 @@ function decryptBuffer(encryptedObject: EncryptedObject): Buffer {
8967
}
9068

9169
function decryptString(encryptedObject: EncryptedObject): string {
70+
const secret = loadEncryptionKey();
9271
const iv = Buffer.from(encryptedObject.iv, 'hex');
93-
assertNotNullOrUndefined(secret, 'secret');
9472
const key = Buffer.from(secret, 'binary');
9573
const decipher = crypto.createDecipheriv(algorithm, key, iv);
9674
let decrypted = decipher.update(encryptedObject.data, 'hex', 'utf8');
@@ -99,8 +77,7 @@ function decryptString(encryptedObject: EncryptedObject): string {
9977
}
10078

10179
function get16ByteKey(): string {
102-
assertNotNullOrUndefined(secret, 'secret is not defined');
103-
return secret;
80+
return loadEncryptionKey();
10481
}
10582

10683
export const encryptUtils = {

0 commit comments

Comments
 (0)