Skip to content

Commit

Permalink
record encryption cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TBonnin committed Mar 7, 2024
1 parent 214409f commit 174df9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
45 changes: 16 additions & 29 deletions packages/shared/lib/services/sync/data/records.service.ts
Expand Up @@ -443,45 +443,32 @@ 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)) {
results.pop();
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, {
Expand Down
12 changes: 3 additions & 9 deletions packages/shared/lib/utils/encryption.manager.ts
Expand Up @@ -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']) {
Expand All @@ -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<void> {
Expand Down

0 comments on commit 174df9c

Please sign in to comment.