From 174df9c28be2bed4ef1e57bd120d89b499670918 Mon Sep 17 00:00:00 2001 From: Thomas Bonnin <233326+TBonnin@users.noreply.github.com> Date: Thu, 7 Mar 2024 09:09:48 +0100 Subject: [PATCH] record encryption cleanup --- .../lib/services/sync/data/records.service.ts | 45 +++++++------------ .../shared/lib/utils/encryption.manager.ts | 12 ++--- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/packages/shared/lib/services/sync/data/records.service.ts b/packages/shared/lib/services/sync/data/records.service.ts index 3201787a0c..0b23e643a4 100644 --- a/packages/shared/lib/services/sync/data/records.service.ts +++ b/packages/shared/lib/services/sync/data/records.service.ts @@ -443,26 +443,19 @@ export async function getAllDataRecords( return { success: true, error: null, response: { records: [], next_cursor: null } }; } - const results = rawResults.flatMap((item) => { + const results = rawResults.map((item) => { const decryptedRecord = encryptionManager.decryptDataRecord(item); - if (!decryptedRecord) { - return []; - } - const lastModifiedAt = item.last_modified_at; - const id = item.id; - const encodedCursor = Buffer.from(`${lastModifiedAt}||${id}`).toString('base64'); - return [ - { - ...decryptedRecord, - _nango_metadata: { - first_seen_at: item.first_seen_at, - last_modified_at: item.last_modified_at, - last_action: item.last_action, - deleted_at: item.deleted_at, - cursor: encodedCursor - } - } as CustomerFacingDataRecord - ]; + const encodedCursor = Buffer.from(`${item.last_modified_at}||${item.id}`).toString('base64'); + return { + ...decryptedRecord, + _nango_metadata: { + first_seen_at: item.first_seen_at, + last_modified_at: item.last_modified_at, + last_action: item.last_action, + deleted_at: item.deleted_at, + cursor: encodedCursor + } + } as CustomerFacingDataRecord; }); if (results.length > Number(limit || 100)) { @@ -470,18 +463,12 @@ export async function getAllDataRecords( rawResults.pop(); const cursorRawElement = rawResults[rawResults.length - 1]; - - if (!cursorRawElement) { - return { success: true, error: null, response: { records: results, next_cursor: null } }; + if (cursorRawElement) { + const encodedCursorValue = Buffer.from(`${cursorRawElement.last_modified_at}||${cursorRawElement.id}`).toString('base64'); + return { success: true, error: null, response: { records: results, next_cursor: encodedCursorValue } }; } - - const lastModifiedAt = cursorRawElement.last_modified_at; - const encodedCursorValue = Buffer.from(`${lastModifiedAt}||${cursorRawElement.id}`).toString('base64'); - - return { success: true, error: null, response: { records: results, next_cursor: encodedCursorValue } }; - } else { - return { success: true, error: null, response: { records: results, next_cursor: null } }; } + return { success: true, error: null, response: { records: results, next_cursor: null } }; } catch (e: any) { const errorMessage = `List records error for model ${model}`; await telemetry.log(LogTypes.SYNC_GET_RECORDS_QUERY_TIMEOUT, errorMessage, LogActionEnum.SYNC, { diff --git a/packages/shared/lib/utils/encryption.manager.ts b/packages/shared/lib/utils/encryption.manager.ts index 9838aeb417..29d45cfe99 100644 --- a/packages/shared/lib/utils/encryption.manager.ts +++ b/packages/shared/lib/utils/encryption.manager.ts @@ -272,11 +272,7 @@ class EncryptionManager { return decryptedDataRecords as unknown as DataRecordWithMetadata[] | RecordWrapCustomerFacingDataRecord; } - public decryptDataRecord(dataRecord: RawDataRecordResult): UnencryptedRawRecord | null { - if (dataRecord === null) { - return dataRecord; - } - + public decryptDataRecord(dataRecord: RawDataRecordResult): UnencryptedRawRecord { const record = dataRecord.record; if (!record['encryptedValue']) { @@ -287,11 +283,9 @@ class EncryptionManager { const decryptedString = this.decrypt(encryptedValue, iv, authTag); - const updatedRecord = { + return { ...JSON.parse(decryptedString) - }; - - return updatedRecord; + } as UnencryptedRawRecord; } public async encryptAllDataRecords(): Promise {