-
Notifications
You must be signed in to change notification settings - Fork 368
/
encryption.manager.ts
429 lines (327 loc) Β· 17.1 KB
/
encryption.manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import crypto, { CipherGCMTypes } from 'crypto';
import logger from '../logger/console.js';
import type { Config as ProviderConfig } from '../models/Provider';
import type { DBConfig } from '../models/Generic.js';
import type { Environment } from '../models/Environment.js';
import type { EnvironmentVariable } from '../models/EnvironmentVariable.js';
import type { Connection, ApiConnection, StoredConnection } from '../models/Connection.js';
import type { RawDataRecordResult, DataRecord, DataRecordWithMetadata, RecordWrapCustomerFacingDataRecord, UnencryptedRawRecord } from '../models/Sync.js';
import db from '../db/database.js';
import util from 'util';
interface DataRecordJson {
encryptedValue: string;
[key: string]: any;
}
class EncryptionManager {
private key: string | undefined;
private algo: CipherGCMTypes = 'aes-256-gcm';
private encoding: BufferEncoding = 'base64';
private encryptionKeyByteLength = 32;
private keySalt = 'X89FHEGqR3yNK0+v7rPWxQ==';
constructor(key: string | undefined) {
this.key = key;
if (key && Buffer.from(key, this.encoding).byteLength != this.encryptionKeyByteLength) {
throw new Error('Encryption key must be base64-encoded and 256-bit long.');
}
}
private shouldEncrypt(): boolean {
return Boolean((this?.key as string) && (this.key as string).length > 0);
}
private encrypt(str: string): [string, string | null, string | null] {
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv(this.algo, Buffer.from(this.key!, this.encoding), iv);
let enc = cipher.update(str, 'utf8', this.encoding);
enc += cipher.final(this.encoding);
return [enc, iv.toString(this.encoding), cipher.getAuthTag().toString(this.encoding)];
}
private decrypt(enc: string, iv: string, authTag: string): string {
const decipher = crypto.createDecipheriv(this.algo, Buffer.from(this.key!, this.encoding), Buffer.from(iv, this.encoding));
decipher.setAuthTag(Buffer.from(authTag, this.encoding));
let str = decipher.update(enc, this.encoding, 'utf8');
str += decipher.final('utf8');
return str;
}
public encryptEnvironment(environment: Environment) {
if (!this.shouldEncrypt()) {
return environment;
}
const encryptedEnvironment: Environment = Object.assign({}, environment);
const [encryptedClientSecret, iv, authTag] = this.encrypt(encryptedEnvironment.secret_key);
encryptedEnvironment.secret_key = encryptedClientSecret;
encryptedEnvironment.secret_key_iv = iv;
encryptedEnvironment.secret_key_tag = authTag;
if (encryptedEnvironment.pending_secret_key) {
const [encryptedPendingClientSecret, pendingIv, pendingAuthTag] = this.encrypt(encryptedEnvironment.pending_secret_key);
encryptedEnvironment.pending_secret_key = encryptedPendingClientSecret;
encryptedEnvironment.pending_secret_key_iv = pendingIv;
encryptedEnvironment.pending_secret_key_tag = pendingAuthTag;
}
return encryptedEnvironment;
}
public decryptEnvironment(environment: Environment | null): Environment | null {
// Check if the individual row is encrypted.
if (environment == null || environment.secret_key_iv == null || environment.secret_key_tag == null) {
return environment;
}
const decryptedEnvironment: Environment = Object.assign({}, environment);
decryptedEnvironment.secret_key = this.decrypt(environment.secret_key, environment.secret_key_iv, environment.secret_key_tag);
if (decryptedEnvironment.pending_secret_key) {
decryptedEnvironment.pending_secret_key = this.decrypt(
environment.pending_secret_key as string,
environment.pending_secret_key_iv as string,
environment.pending_secret_key_tag as string
);
}
return decryptedEnvironment;
}
public encryptApiConnection(connection: ApiConnection): StoredConnection {
if (!this.shouldEncrypt()) {
return connection as StoredConnection;
}
const storedConnection: StoredConnection = Object.assign({}, connection) as StoredConnection;
const [encryptedClientSecret, iv, authTag] = this.encrypt(JSON.stringify(connection.credentials));
const encryptedCreds = { encrypted_credentials: encryptedClientSecret };
storedConnection.credentials = encryptedCreds;
storedConnection.credentials_iv = iv;
storedConnection.credentials_tag = authTag;
return storedConnection;
}
public encryptConnection(connection: Connection): StoredConnection {
if (!this.shouldEncrypt()) {
return connection as StoredConnection;
}
const storedConnection: StoredConnection = Object.assign({}, connection) as StoredConnection;
const [encryptedClientSecret, iv, authTag] = this.encrypt(JSON.stringify(connection.credentials));
const encryptedCreds = { encrypted_credentials: encryptedClientSecret };
storedConnection.credentials = encryptedCreds;
storedConnection.credentials_iv = iv;
storedConnection.credentials_tag = authTag;
return storedConnection;
}
public decryptConnection(connection: StoredConnection | null): Connection | null {
// Check if the individual row is encrypted.
if (connection == null || connection.credentials_iv == null || connection.credentials_tag == null) {
return connection as Connection;
}
const decryptedConnection: StoredConnection = Object.assign({}, connection);
decryptedConnection.credentials = JSON.parse(
this.decrypt(connection.credentials['encrypted_credentials'], connection.credentials_iv, connection.credentials_tag)
);
return decryptedConnection as Connection;
}
public encryptEnvironmentVariables(environmentVariables: EnvironmentVariable[]): EnvironmentVariable[] {
if (!this.shouldEncrypt()) {
return environmentVariables;
}
const encryptedEnvironmentVariables: EnvironmentVariable[] = Object.assign([], environmentVariables);
for (const environmentVariable of encryptedEnvironmentVariables) {
const [encryptedValue, iv, authTag] = this.encrypt(environmentVariable.value);
environmentVariable.value = encryptedValue;
environmentVariable.value_iv = iv;
environmentVariable.value_tag = authTag;
}
return encryptedEnvironmentVariables;
}
public decryptEnvironmentVariables(environmentVariables: EnvironmentVariable[] | null): EnvironmentVariable[] | null {
if (environmentVariables === null) {
return environmentVariables;
}
const decryptedEnvironmentVariables: EnvironmentVariable[] = Object.assign([], environmentVariables);
for (const environmentVariable of decryptedEnvironmentVariables) {
if (environmentVariable.value_iv == null || environmentVariable.value_tag == null) {
continue;
}
environmentVariable.value = this.decrypt(environmentVariable.value, environmentVariable.value_iv, environmentVariable.value_tag);
}
return decryptedEnvironmentVariables;
}
public encryptProviderConfig(config: ProviderConfig): ProviderConfig {
if (!this.shouldEncrypt()) {
return config;
}
const encryptedConfig: ProviderConfig = Object.assign({}, config);
if (!config.oauth_client_secret) {
return config;
}
const [encryptedClientSecret, iv, authTag] = this.encrypt(config.oauth_client_secret);
encryptedConfig.oauth_client_secret = encryptedClientSecret;
encryptedConfig.oauth_client_secret_iv = iv;
encryptedConfig.oauth_client_secret_tag = authTag;
if (config.custom) {
const [encryptedValue, iv, authTag] = this.encrypt(JSON.stringify(config.custom));
encryptedConfig.custom = { encryptedValue, iv: iv as string, authTag: authTag as string };
}
return encryptedConfig;
}
public decryptProviderConfig(config: ProviderConfig | null): ProviderConfig | null {
// Check if the individual row is encrypted.
if (config == null || config.oauth_client_secret_iv == null || config.oauth_client_secret_tag == null) {
return config;
}
const decryptedConfig: ProviderConfig = Object.assign({}, config);
decryptedConfig.oauth_client_secret = this.decrypt(config.oauth_client_secret, config.oauth_client_secret_iv, config.oauth_client_secret_tag);
if (decryptedConfig.custom && config.custom) {
decryptedConfig.custom = JSON.parse(
this.decrypt(config.custom['encryptedValue'] as string, config.custom['iv'] as string, config.custom['authTag'] as string)
);
}
return decryptedConfig;
}
public encryptDataRecords(dataRecords: DataRecord[]): DataRecord[] {
if (!this.shouldEncrypt()) {
return dataRecords;
}
const encryptedDataRecords: DataRecord[] = Object.assign([], dataRecords);
for (const dataRecord of encryptedDataRecords) {
const [encryptedValue, iv, authTag] = this.encrypt(JSON.stringify(dataRecord.json));
dataRecord.json = { encryptedValue, iv, authTag };
}
return encryptedDataRecords;
}
public decryptDataRecords(dataRecords: DataRecord[] | null, field = 'json'): DataRecordWithMetadata[] | RecordWrapCustomerFacingDataRecord | null {
if (dataRecords === null) {
return dataRecords;
}
const decryptedDataRecords: DataRecord[] = [];
for (const dataRecord of dataRecords) {
const record = dataRecord[field] as DataRecordJson;
if (!record.encryptedValue) {
decryptedDataRecords.push(dataRecord);
continue;
}
const { encryptedValue, iv, authTag } = record;
const decryptedString = this.decrypt(encryptedValue, iv, authTag);
let updatedRecord = {
...JSON.parse(decryptedString)
};
if (record['_nango_metadata']) {
updatedRecord['_nango_metadata'] = record['_nango_metadata'];
decryptedDataRecords.push({ [field]: updatedRecord } as DataRecord);
} else {
const { record: _record, ...rest } = dataRecord;
updatedRecord = {
...rest,
record: updatedRecord
};
decryptedDataRecords.push(updatedRecord as DataRecord);
}
}
return decryptedDataRecords as unknown as DataRecordWithMetadata[] | RecordWrapCustomerFacingDataRecord;
}
public decryptDataRecord(dataRecord: RawDataRecordResult): UnencryptedRawRecord {
const record = dataRecord.record;
if (!record['encryptedValue']) {
return record as UnencryptedRawRecord;
}
const { encryptedValue, iv, authTag } = record;
const decryptedString = this.decrypt(encryptedValue, iv, authTag);
return {
...JSON.parse(decryptedString)
} as UnencryptedRawRecord;
}
public async encryptAllDataRecords(): Promise<void> {
const chunkSize = 1000;
const concurrencyLimit = 5;
const encryptAndSave = async (tableName: string, offset: number) => {
const dataRecords: DataRecord[] = await db.knex.select('*').from<DataRecord>(tableName).limit(chunkSize).offset(offset);
if (dataRecords.length === 0) {
return false;
}
const updatePromises = dataRecords.map((dataRecord) =>
db.knex.transaction(async (trx) => {
if ((dataRecord.json as Record<string, string>)['encryptedValue']) {
return;
}
const [encryptedValue, iv, authTag] = this.encrypt(JSON.stringify(dataRecord.json));
dataRecord.json = { encryptedValue, iv, authTag };
await db.knex.from<DataRecord>(tableName).where('id', dataRecord.id).update(dataRecord).transacting(trx);
await trx.commit();
})
);
await Promise.all(updatePromises.slice(0, concurrencyLimit));
return true;
};
let offset = 0;
while (await encryptAndSave('_nango_sync_data_records', offset)) {
offset += chunkSize;
}
offset = 0;
while (await encryptAndSave('_nango_sync_data_records_deletes', offset)) {
offset += chunkSize;
}
}
private async saveDbConfig(dbConfig: DBConfig) {
await db.knex.from<DBConfig>(`_nango_db_config`).del();
await db.knex.from<DBConfig>(`_nango_db_config`).insert(dbConfig);
}
private async hashEncryptionKey(key: string, salt: string): Promise<string> {
const keyBuffer = await util.promisify(crypto.pbkdf2)(key, salt, 310000, 32, 'sha256');
return keyBuffer.toString(this.encoding);
}
public async encryptDatabaseIfNeeded() {
const dbConfig: DBConfig | null = await db.knex.first().from<DBConfig>('_nango_db_config');
const previousEncryptionKeyHash = dbConfig?.encryption_key_hash;
const encryptionKeyHash = this.key ? await this.hashEncryptionKey(this.key, this.keySalt) : null;
const isEncryptionKeyNew = dbConfig == null && this.key;
const isEncryptionIncomplete = dbConfig != null && previousEncryptionKeyHash === encryptionKeyHash && dbConfig.encryption_complete == false;
if (isEncryptionKeyNew || isEncryptionIncomplete) {
if (isEncryptionKeyNew) {
logger.info('π Encryption key has been set. Encrypting database...');
await this.saveDbConfig({ encryption_key_hash: encryptionKeyHash, encryption_complete: false });
} else if (isEncryptionIncomplete) {
logger.info('π Previously started database encryption is incomplete. Continuing encryption of database...');
}
await this.encryptDatabase();
await this.saveDbConfig({ encryption_key_hash: encryptionKeyHash, encryption_complete: true });
return;
}
const isEncryptionKeyChanged = dbConfig?.encryption_key_hash != null && previousEncryptionKeyHash !== encryptionKeyHash;
if (isEncryptionKeyChanged) {
throw new Error('You cannot edit or remove the encryption key once it has been set.');
}
}
private async encryptDatabase() {
logger.info('πβοΈ Starting encryption of database...');
const environments: Environment[] = await db.knex.select('*').from<Environment>(`_nango_environments`);
for (let environment of environments) {
if (environment.secret_key_iv && environment.secret_key_tag) {
continue;
}
environment = this.encryptEnvironment(environment);
await db.knex.from<Environment>(`_nango_environments`).where({ id: environment.id }).update(environment);
}
const connections: Connection[] = await db.knex.select('*').from<Connection>(`_nango_connections`);
for (const connection of connections) {
if (connection.credentials_iv && connection.credentials_tag) {
continue;
}
const storedConnection = this.encryptConnection(connection);
await db.knex.from<StoredConnection>(`_nango_connections`).where({ id: storedConnection.id! }).update(storedConnection);
}
const providerConfigs: ProviderConfig[] = await db.knex.select('*').from<ProviderConfig>(`_nango_configs`);
for (let providerConfig of providerConfigs) {
if (providerConfig.oauth_client_secret_iv && providerConfig.oauth_client_secret_tag) {
continue;
}
providerConfig = this.encryptProviderConfig(providerConfig);
await db.knex.from<ProviderConfig>(`_nango_configs`).where({ id: providerConfig.id! }).update(providerConfig);
}
const environmentVariables: EnvironmentVariable[] = await db.knex.select('*').from<EnvironmentVariable>(`_nango_environment_variables`);
for (const environmentVariable of environmentVariables) {
if (environmentVariable.value_iv && environmentVariable.value_tag) {
continue;
}
const [encryptedValue, iv, authTag] = this.encrypt(environmentVariable.value);
environmentVariable.value = encryptedValue;
environmentVariable.value_iv = iv;
environmentVariable.value_tag = authTag;
await db.knex
.from<EnvironmentVariable>(`_nango_environment_variables`)
.where({ id: environmentVariable.id as number })
.update(environmentVariable);
}
await this.encryptAllDataRecords();
logger.info('πβ
Encryption of database complete!');
}
}
export default new EncryptionManager(process.env['NANGO_ENCRYPTION_KEY']);