anote-server-libs 0.9.6 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -23,7 +23,7 @@ class BaseModelRepository {
|
|
|
23
23
|
this.Migration = new Migration_1.MigrationRepository(db, dbMssql, logger);
|
|
24
24
|
this.ApiCall = new ApiCall_1.ApiCallRepository(db, dbMssql, logger);
|
|
25
25
|
}
|
|
26
|
-
migrate(migrationsPath, callback, onlyAboveOrEquals
|
|
26
|
+
migrate(migrationsPath, callback, onlyAboveOrEquals, onlyBelow) {
|
|
27
27
|
(this.db ? Promise.all([
|
|
28
28
|
this.db.query(`CREATE TABLE IF NOT EXISTS migration (
|
|
29
29
|
id integer PRIMARY KEY,
|
|
@@ -70,22 +70,23 @@ END`)
|
|
|
70
70
|
hash: crypto.createHash('sha256').update(content).digest('hex')
|
|
71
71
|
};
|
|
72
72
|
});
|
|
73
|
-
|
|
73
|
+
const minAvailableId = migrationsAvailable.length > 0 ? migrationsAvailable[0].id : 0;
|
|
74
|
+
const maxAvailableId = migrationsAvailable.length > 0 ? migrationsAvailable[migrationsAvailable.length - 1].id : Infinity;
|
|
75
|
+
migrations = migrations.filter(m => m.id >= minAvailableId && m.id <= maxAvailableId);
|
|
76
|
+
if (onlyAboveOrEquals)
|
|
77
|
+
migrations = migrations.filter(m => m.id >= onlyAboveOrEquals);
|
|
74
78
|
if (onlyBelow)
|
|
75
79
|
migrations = migrations.filter(m => m.id < onlyBelow);
|
|
76
80
|
if (migrationsAvailable.length === 0 && migrations.length === 0)
|
|
77
81
|
process.exit(5);
|
|
78
82
|
if (migrationsAvailable.length && migrationsAvailable.length !== (migrationsAvailable[migrationsAvailable.length - 1].id - migrationsAvailable[0].id + 1))
|
|
79
83
|
process.exit(5);
|
|
80
|
-
let highestCommon =
|
|
81
|
-
while (highestCommon < migrations.length
|
|
82
|
-
&& highestCommon < migrationsAvailable.length
|
|
83
|
-
&& migrations[highestCommon]
|
|
84
|
-
&& migrationsAvailable[highestCommon]
|
|
84
|
+
let highestCommon = onlyAboveOrEquals;
|
|
85
|
+
while (highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
85
86
|
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
86
87
|
highestCommon++;
|
|
87
|
-
this.applyDownUntil(migrations, migrations.length, highestCommon).then(
|
|
88
|
-
this.applyUpUntil(migrationsAvailable, Math.min(
|
|
88
|
+
this.applyDownUntil(migrations, migrations.length, highestCommon).then(highestCommon => {
|
|
89
|
+
this.applyUpUntil(migrationsAvailable, Math.min(highestCommon, migrations.length), migrationsAvailable.length).then(callback, process.exit);
|
|
89
90
|
}, process.exit);
|
|
90
91
|
});
|
|
91
92
|
}, () => process.exit(3));
|
|
@@ -26,7 +26,7 @@ export class BaseModelRepository {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// TODO: alter table migration if exists without ok column
|
|
29
|
-
migrate(migrationsPath: string, callback: (() => void), onlyAboveOrEquals
|
|
29
|
+
migrate(migrationsPath: string, callback: (() => void), onlyAboveOrEquals?: number, onlyBelow?: number) {
|
|
30
30
|
(this.db ? Promise.all([
|
|
31
31
|
this.db.query(`CREATE TABLE IF NOT EXISTS migration (
|
|
32
32
|
id integer PRIMARY KEY,
|
|
@@ -71,19 +71,19 @@ END`)])).then(() => {
|
|
|
71
71
|
hash: crypto.createHash('sha256').update(content).digest('hex')
|
|
72
72
|
};
|
|
73
73
|
});
|
|
74
|
-
|
|
74
|
+
const minAvailableId = migrationsAvailable.length > 0 ? migrationsAvailable[0].id : 0;
|
|
75
|
+
const maxAvailableId = migrationsAvailable.length > 0 ? migrationsAvailable[migrationsAvailable.length - 1].id : Infinity;
|
|
76
|
+
migrations = migrations.filter(m => m.id >= minAvailableId && m.id <= maxAvailableId);
|
|
77
|
+
if(onlyAboveOrEquals) migrations = migrations.filter(m => m.id >= onlyAboveOrEquals);
|
|
75
78
|
if(onlyBelow) migrations = migrations.filter(m => m.id < onlyBelow);
|
|
76
79
|
if(migrationsAvailable.length === 0 && migrations.length === 0) process.exit(5);
|
|
77
80
|
if(migrationsAvailable.length && migrationsAvailable.length !== (migrationsAvailable[migrationsAvailable.length - 1].id - migrationsAvailable[0].id + 1)) process.exit(5);
|
|
78
|
-
let highestCommon =
|
|
79
|
-
while(highestCommon < migrations.length
|
|
80
|
-
|
|
81
|
-
&& migrations[highestCommon]
|
|
82
|
-
&& migrationsAvailable[highestCommon]
|
|
83
|
-
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
81
|
+
let highestCommon = onlyAboveOrEquals;
|
|
82
|
+
while(highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
83
|
+
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
84
84
|
highestCommon++;
|
|
85
|
-
this.applyDownUntil(migrations, migrations.length, highestCommon).then(
|
|
86
|
-
this.applyUpUntil(migrationsAvailable, Math.min(
|
|
85
|
+
this.applyDownUntil(migrations, migrations.length, highestCommon).then(highestCommon => {
|
|
86
|
+
this.applyUpUntil(migrationsAvailable, Math.min(highestCommon, migrations.length), migrationsAvailable.length).then(callback, process.exit);
|
|
87
87
|
}, process.exit);
|
|
88
88
|
});
|
|
89
89
|
}, () => process.exit(3));
|
|
@@ -172,4 +172,4 @@ END`)])).then(() => {
|
|
|
172
172
|
});
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
|
-
}
|
|
175
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CryptModelDao = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const util_1 = require("util");
|
|
6
|
+
const ModelDao_1 = require("./ModelDao");
|
|
7
|
+
const randomFillAsync = (0, util_1.promisify)(crypto_1.randomFill);
|
|
8
|
+
class CryptModelDao extends ModelDao_1.ModelDao {
|
|
9
|
+
constructor(keyBase64, encryptedColumns, ...args) {
|
|
10
|
+
super(...args);
|
|
11
|
+
this.key = Buffer.from(keyBase64, 'base64');
|
|
12
|
+
this.encryptedColumns = encryptedColumns;
|
|
13
|
+
}
|
|
14
|
+
serializeWrapper(instance, request) {
|
|
15
|
+
const props = this.serialize(instance, request);
|
|
16
|
+
const encryptPromises = [];
|
|
17
|
+
this.encryptedColumns.forEach(col => {
|
|
18
|
+
const idx = this.updateDefinition.split(',').findIndex(def => def.trim().startsWith('"' + col + '"') || def.trim().startsWith(col + '='));
|
|
19
|
+
if (idx >= 0) {
|
|
20
|
+
const val = request ? request.parameters[idx] : props[idx];
|
|
21
|
+
if (val !== null && val !== undefined) {
|
|
22
|
+
const encryptPromise = this.encrypt(String(val)).then(encrypted => {
|
|
23
|
+
if (request)
|
|
24
|
+
request.replaceInput(String(idx + 1), encrypted);
|
|
25
|
+
else
|
|
26
|
+
props[idx] = encrypted;
|
|
27
|
+
});
|
|
28
|
+
encryptPromises.push(encryptPromise);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
if (encryptPromises.length > 0) {
|
|
33
|
+
return Promise.all(encryptPromises).then(() => props);
|
|
34
|
+
}
|
|
35
|
+
return props;
|
|
36
|
+
}
|
|
37
|
+
buildObjectWrapper(row) {
|
|
38
|
+
const decryptPromises = [];
|
|
39
|
+
this.encryptedColumns.forEach(col => {
|
|
40
|
+
if (row[col] !== null && row[col] !== undefined) {
|
|
41
|
+
const decryptPromise = this.decrypt(String(row[col])).then(decrypted => {
|
|
42
|
+
row[col] = decrypted;
|
|
43
|
+
});
|
|
44
|
+
decryptPromises.push(decryptPromise);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (decryptPromises.length > 0) {
|
|
48
|
+
return Promise.all(decryptPromises).then(() => this.buildObject(row));
|
|
49
|
+
}
|
|
50
|
+
return this.buildObject(row);
|
|
51
|
+
}
|
|
52
|
+
async encrypt(decrypted) {
|
|
53
|
+
const iv = new Uint8Array(16);
|
|
54
|
+
await randomFillAsync(iv);
|
|
55
|
+
const cipher = (0, crypto_1.createCipheriv)(CryptModelDao.ALGORITHM, this.key, iv);
|
|
56
|
+
let encrypted = '';
|
|
57
|
+
cipher.setEncoding('base64');
|
|
58
|
+
cipher.on('data', (chunk) => encrypted += chunk);
|
|
59
|
+
cipher.write(decrypted);
|
|
60
|
+
cipher.end();
|
|
61
|
+
return `$$enc$$:${Buffer.from(iv).toString('base64')}${encrypted}`;
|
|
62
|
+
}
|
|
63
|
+
async decrypt(encrypted) {
|
|
64
|
+
if (!encrypted.startsWith('$$enc$$:')) {
|
|
65
|
+
return encrypted;
|
|
66
|
+
}
|
|
67
|
+
encrypted = encrypted.slice(8);
|
|
68
|
+
const iv = new Uint8Array(Buffer.from(encrypted.slice(0, 24), 'base64'));
|
|
69
|
+
const decipher = (0, crypto_1.createDecipheriv)(CryptModelDao.ALGORITHM, this.key, iv);
|
|
70
|
+
let decrypted = '';
|
|
71
|
+
decipher.on('readable', () => {
|
|
72
|
+
for (let chunk = decipher.read(); chunk !== null; chunk = decipher.read()) {
|
|
73
|
+
decrypted += chunk.toString('base64');
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
decipher.write(encrypted.slice(24), 'base64');
|
|
77
|
+
decipher.end();
|
|
78
|
+
return Buffer.from(decrypted, 'base64').toString('utf8');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.CryptModelDao = CryptModelDao;
|
|
82
|
+
CryptModelDao.ALGORITHM = 'aes-256-cbc';
|