1- import {
2- assertNotNullOrUndefined ,
3- EncryptedObject ,
4- isNil ,
5- } from '@openops/shared' ;
1+ import { EncryptedObject , isNil } from '@openops/shared' ;
62import * 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 ;
136const algorithm = 'aes-256-cbc' ;
147const 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
3917function 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
5735function 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
7048function 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
8058function 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
9169function 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
10179function get16ByteKey ( ) : string {
102- assertNotNullOrUndefined ( secret , 'secret is not defined' ) ;
103- return secret ;
80+ return loadEncryptionKey ( ) ;
10481}
10582
10683export const encryptUtils = {
0 commit comments