Skip to content

Commit 4203caa

Browse files
committed
Ensure secret is loaded
1 parent aaf4b1e commit 4203caa

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ let secret: string | null;
1414
const algorithm = 'aes-256-cbc';
1515
const ivLength = 16;
1616

17+
function ensureSecretLoaded(): void {
18+
if (isNil(secret)) {
19+
const loadedSecret = system.get(AppSystemProp.ENCRYPTION_KEY);
20+
if (loadedSecret) {
21+
secret = loadedSecret;
22+
logger.debug('Encryption key loaded via ensureSecretLoaded()');
23+
} else {
24+
logger.warn(
25+
'Encryption key not found in system properties when ensureSecretLoaded() was called. ' +
26+
'This may indicate an issue with the Lambda environment or OPS_ENCRYPTION_KEY not being set.',
27+
);
28+
}
29+
}
30+
}
31+
1732
const loadEncryptionKey = async (
1833
queueMode: QueueMode,
1934
): Promise<string | null> => {
@@ -45,6 +60,7 @@ const generateAndStoreSecret = async (): Promise<string> => {
4560
};
4661

4762
function encryptString(inputString: string): EncryptedObject {
63+
ensureSecretLoaded();
4864
const iv = crypto.randomBytes(ivLength); // Generate a random initialization vector
4965
assertNotNullOrUndefined(secret, 'secret');
5066
const key = Buffer.from(secret, 'binary');
@@ -63,6 +79,7 @@ function encryptObject(object: unknown): EncryptedObject {
6379
}
6480

6581
function encryptBuffer(inputBuffer: Buffer): EncryptedObject {
82+
ensureSecretLoaded();
6683
const iv = crypto.randomBytes(ivLength);
6784
assertNotNullOrUndefined(secret, 'secret');
6885
const key = Buffer.from(secret, 'binary');
@@ -76,6 +93,7 @@ function encryptBuffer(inputBuffer: Buffer): EncryptedObject {
7693
}
7794

7895
function decryptObject<T>(encryptedObject: EncryptedObject): T {
96+
ensureSecretLoaded();
7997
const iv = Buffer.from(encryptedObject.iv, 'hex');
8098
assertNotNullOrUndefined(secret, 'secret');
8199
const key = Buffer.from(secret, 'binary');
@@ -86,6 +104,7 @@ function decryptObject<T>(encryptedObject: EncryptedObject): T {
86104
}
87105

88106
function decryptBuffer(encryptedObject: EncryptedObject): Buffer {
107+
ensureSecretLoaded();
89108
const iv = Buffer.from(encryptedObject.iv, 'hex');
90109
assertNotNullOrUndefined(secret, 'secret');
91110
const key = Buffer.from(secret, 'binary');
@@ -97,6 +116,7 @@ function decryptBuffer(encryptedObject: EncryptedObject): Buffer {
97116
}
98117

99118
function decryptString(encryptedObject: EncryptedObject): string {
119+
ensureSecretLoaded();
100120
const iv = Buffer.from(encryptedObject.iv, 'hex');
101121
assertNotNullOrUndefined(secret, 'secret');
102122
const key = Buffer.from(secret, 'binary');
@@ -107,6 +127,7 @@ function decryptString(encryptedObject: EncryptedObject): string {
107127
}
108128

109129
function get16ByteKey(): string {
130+
ensureSecretLoaded();
110131
assertNotNullOrUndefined(secret, 'secret is not defined');
111132
return secret;
112133
}

0 commit comments

Comments
 (0)