dbgate-api-premium 7.0.6 → 7.1.1
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.
- package/package.json +8 -7
- package/src/auth/storageAuthProvider.js +7 -6
- package/src/controllers/connections.js +5 -2
- package/src/controllers/databaseConnections.js +12 -3
- package/src/controllers/restConnections.js +316 -0
- package/src/controllers/runners.js +10 -10
- package/src/controllers/serverConnections.js +1 -1
- package/src/controllers/storage.js +4 -4
- package/src/controllers/storageDb.js +22 -1
- package/src/currentVersion.js +2 -2
- package/src/main.js +2 -0
- package/src/proc/connectProcess.js +4 -1
- package/src/proc/index.js +2 -0
- package/src/proc/restConnectionProcess.js +771 -0
- package/src/shell/requirePlugin.js +9 -3
- package/src/storageModel.js +78 -78
- package/src/utility/connectUtility.js +29 -1
- package/src/utility/crypting.js +21 -1
- package/src/utility/envtools.js +13 -2
- package/src/utility/hasPermission.js +3 -2
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbgate-api-premium",
|
|
3
3
|
"main": "src/index.js",
|
|
4
|
-
"version": "7.
|
|
5
|
-
"homepage": "https://dbgate.
|
|
4
|
+
"version": "7.1.1",
|
|
5
|
+
"homepage": "https://www.dbgate.io/",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/dbgate/dbgate.git"
|
|
@@ -30,10 +30,11 @@
|
|
|
30
30
|
"compare-versions": "^3.6.0",
|
|
31
31
|
"cors": "^2.8.5",
|
|
32
32
|
"cross-env": "^6.0.3",
|
|
33
|
-
"dbgate-datalib": "^7.
|
|
34
|
-
"dbgate-query-splitter": "^4.
|
|
35
|
-
"dbgate-
|
|
36
|
-
"dbgate-
|
|
33
|
+
"dbgate-datalib": "^7.1.1",
|
|
34
|
+
"dbgate-query-splitter": "^4.12.0",
|
|
35
|
+
"dbgate-rest": "^7.1.1",
|
|
36
|
+
"dbgate-sqltree": "^7.1.1",
|
|
37
|
+
"dbgate-tools": "^7.1.1",
|
|
37
38
|
"debug": "^4.3.4",
|
|
38
39
|
"diff": "^5.0.0",
|
|
39
40
|
"diff2html": "^3.4.13",
|
|
@@ -87,7 +88,7 @@
|
|
|
87
88
|
"devDependencies": {
|
|
88
89
|
"@types/fs-extra": "^9.0.11",
|
|
89
90
|
"@types/lodash": "^4.14.149",
|
|
90
|
-
"dbgate-types": "^7.
|
|
91
|
+
"dbgate-types": "^7.1.1",
|
|
91
92
|
"env-cmd": "^10.1.0",
|
|
92
93
|
"jsdoc-to-markdown": "^9.0.5",
|
|
93
94
|
"node-loader": "^1.0.2",
|
|
@@ -14,6 +14,7 @@ const {
|
|
|
14
14
|
loadSuperadminPermissions,
|
|
15
15
|
readComplexUserRolePermissions,
|
|
16
16
|
readComplexRolePermissions,
|
|
17
|
+
resolvePermissionConnectionIds,
|
|
17
18
|
storageCheckRoleConnectionAccess,
|
|
18
19
|
storageCheckUserRoleConnectionAccess,
|
|
19
20
|
} = require('../controllers/storageDb');
|
|
@@ -61,12 +62,12 @@ class SuperadminAuthProvider extends AuthProviderBase {
|
|
|
61
62
|
|
|
62
63
|
async getCurrentDatabasePermissions(req) {
|
|
63
64
|
const databasePermissions = await readComplexRolePermissions(-3, 'role_databases');
|
|
64
|
-
return databasePermissions;
|
|
65
|
+
return resolvePermissionConnectionIds(databasePermissions);
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
async getCurrentTablePermissions(req) {
|
|
68
69
|
const tablePermissions = await readComplexRolePermissions(-3, 'role_tables');
|
|
69
|
-
return tablePermissions;
|
|
70
|
+
return resolvePermissionConnectionIds(tablePermissions);
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
async getCurrentFilePermissions(req) {
|
|
@@ -104,13 +105,13 @@ class StorageProviderBase extends AuthProviderBase {
|
|
|
104
105
|
async getCurrentDatabasePermissions(req) {
|
|
105
106
|
const userId = this.getUserIdFromRequest(req);
|
|
106
107
|
const databasePermissions = await readComplexUserRolePermissions(userId, 'user_databases', 'role_databases');
|
|
107
|
-
return databasePermissions;
|
|
108
|
+
return resolvePermissionConnectionIds(databasePermissions);
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
async getCurrentTablePermissions(req) {
|
|
111
112
|
const userId = this.getUserIdFromRequest(req);
|
|
112
113
|
const tablePermissions = await readComplexUserRolePermissions(userId, 'user_tables', 'role_tables');
|
|
113
|
-
return tablePermissions;
|
|
114
|
+
return resolvePermissionConnectionIds(tablePermissions);
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
async getCurrentFilePermissions(req) {
|
|
@@ -172,12 +173,12 @@ class AnonymousProvider extends StorageProviderBase {
|
|
|
172
173
|
|
|
173
174
|
async getCurrentDatabasePermissions(req) {
|
|
174
175
|
const databasePermissions = await readComplexRolePermissions(-1, 'role_databases');
|
|
175
|
-
return databasePermissions;
|
|
176
|
+
return resolvePermissionConnectionIds(databasePermissions);
|
|
176
177
|
}
|
|
177
178
|
|
|
178
179
|
async getCurrentTablePermissions(req) {
|
|
179
180
|
const tablePermissions = await readComplexRolePermissions(-1, 'role_tables');
|
|
180
|
-
return tablePermissions;
|
|
181
|
+
return resolvePermissionConnectionIds(tablePermissions);
|
|
181
182
|
}
|
|
182
183
|
|
|
183
184
|
async getCurrentFilePermissions(req) {
|
|
@@ -202,7 +202,7 @@ module.exports = {
|
|
|
202
202
|
|
|
203
203
|
const storageConnections = await storage.connections(req);
|
|
204
204
|
if (storageConnections) {
|
|
205
|
-
return storageConnections;
|
|
205
|
+
return storageConnections.map(maskConnection);
|
|
206
206
|
}
|
|
207
207
|
if (portalConnections) {
|
|
208
208
|
if (platformInfo.allowShellConnection) return portalConnections.map(x => encryptConnection(x));
|
|
@@ -484,7 +484,7 @@ module.exports = {
|
|
|
484
484
|
|
|
485
485
|
const storageConnection = await storage.getConnection({ conid });
|
|
486
486
|
if (storageConnection) {
|
|
487
|
-
return storageConnection;
|
|
487
|
+
return mask ? maskConnection(storageConnection) : storageConnection;
|
|
488
488
|
}
|
|
489
489
|
|
|
490
490
|
if (portalConnections) {
|
|
@@ -502,6 +502,9 @@ module.exports = {
|
|
|
502
502
|
_id: '__model',
|
|
503
503
|
};
|
|
504
504
|
}
|
|
505
|
+
if (!conid) {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
505
508
|
await testConnectionPermission(conid, req);
|
|
506
509
|
return this.getCore({ conid, mask: true });
|
|
507
510
|
},
|
|
@@ -15,6 +15,7 @@ const {
|
|
|
15
15
|
getLogger,
|
|
16
16
|
extractErrorLogData,
|
|
17
17
|
filterStructureBySchema,
|
|
18
|
+
serializeJsTypesForJsonStringify,
|
|
18
19
|
} = require('dbgate-tools');
|
|
19
20
|
const { html, parse } = require('diff2html');
|
|
20
21
|
const { handleProcessCommunication } = require('../utility/processComm');
|
|
@@ -165,6 +166,11 @@ module.exports = {
|
|
|
165
166
|
if (!connection) {
|
|
166
167
|
throw new Error(`databaseConnections: Connection with conid="${conid}" not found`);
|
|
167
168
|
}
|
|
169
|
+
|
|
170
|
+
if (connection.engine?.endsWith('@rest')) {
|
|
171
|
+
return { isApiConnection: true };
|
|
172
|
+
}
|
|
173
|
+
|
|
168
174
|
if (connection.passwordMode == 'askPassword' || connection.passwordMode == 'askUser') {
|
|
169
175
|
throw new MissingCredentialsError({ conid, passwordMode: connection.passwordMode });
|
|
170
176
|
}
|
|
@@ -219,12 +225,13 @@ module.exports = {
|
|
|
219
225
|
this.close(conid, database, false);
|
|
220
226
|
});
|
|
221
227
|
|
|
222
|
-
|
|
228
|
+
const connectMessage = serializeJsTypesForJsonStringify({
|
|
223
229
|
msgtype: 'connect',
|
|
224
230
|
connection: { ...connection, database },
|
|
225
231
|
structure: lastClosed ? lastClosed.structure : null,
|
|
226
232
|
globalSettings: await config.getSettings(),
|
|
227
233
|
});
|
|
234
|
+
subprocess.send(connectMessage);
|
|
228
235
|
return newOpened;
|
|
229
236
|
},
|
|
230
237
|
|
|
@@ -234,7 +241,8 @@ module.exports = {
|
|
|
234
241
|
const promise = new Promise((resolve, reject) => {
|
|
235
242
|
this.requests[msgid] = [resolve, reject, additionalData];
|
|
236
243
|
try {
|
|
237
|
-
|
|
244
|
+
const serializedMessage = serializeJsTypesForJsonStringify({ msgid, ...message });
|
|
245
|
+
conn.subprocess.send(serializedMessage);
|
|
238
246
|
} catch (err) {
|
|
239
247
|
logger.error(extractErrorLogData(err), 'DBGM-00115 Error sending request do process');
|
|
240
248
|
this.close(conn.conid, conn.database);
|
|
@@ -468,6 +476,7 @@ module.exports = {
|
|
|
468
476
|
|
|
469
477
|
const databasePermissions = await loadDatabasePermissionsFromRequest(req);
|
|
470
478
|
const tablePermissions = await loadTablePermissionsFromRequest(req);
|
|
479
|
+
const databasePermissionRole = getDatabasePermissionRole(conid, database, databasePermissions);
|
|
471
480
|
const fieldsAndRoles = [
|
|
472
481
|
[changeSet.inserts, 'create_update_delete'],
|
|
473
482
|
[changeSet.deletes, 'create_update_delete'],
|
|
@@ -482,7 +491,7 @@ module.exports = {
|
|
|
482
491
|
operation.schemaName,
|
|
483
492
|
operation.pureName,
|
|
484
493
|
tablePermissions,
|
|
485
|
-
|
|
494
|
+
databasePermissionRole
|
|
486
495
|
);
|
|
487
496
|
if (getTablePermissionRoleLevelIndex(role) < getTablePermissionRoleLevelIndex(requiredRole)) {
|
|
488
497
|
throw new Error('DBGM-00262 Permission not granted');
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// *** This file is part of DbGate Premium ***
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const connections = require('./connections');
|
|
5
|
+
const socket = require('../utility/socket');
|
|
6
|
+
const { fork } = require('child_process');
|
|
7
|
+
const _ = require('lodash');
|
|
8
|
+
const AsyncLock = require('async-lock');
|
|
9
|
+
const { handleProcessCommunication } = require('../utility/processComm');
|
|
10
|
+
const lock = new AsyncLock();
|
|
11
|
+
const config = require('./config');
|
|
12
|
+
const processArgs = require('../utility/processArgs');
|
|
13
|
+
const { testConnectionPermission, loadPermissionsFromRequest, hasPermission } = require('../utility/hasPermission');
|
|
14
|
+
const { MissingCredentialsError } = require('../utility/exceptions');
|
|
15
|
+
const pipeForkLogs = require('../utility/pipeForkLogs');
|
|
16
|
+
const { getLogger, extractErrorLogData } = require('dbgate-tools');
|
|
17
|
+
const { sendToAuditLog } = require('../utility/auditlog');
|
|
18
|
+
const { decryptPasswordString } = require('../utility/crypting');
|
|
19
|
+
const { getRestAuthFromConnection } = require('../utility/connectUtility');
|
|
20
|
+
|
|
21
|
+
const logger = getLogger('restConnection');
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
opened: [],
|
|
25
|
+
closed: {},
|
|
26
|
+
requests: {},
|
|
27
|
+
|
|
28
|
+
handle_apiInfo(conid, { apiInfo }) {
|
|
29
|
+
const existing = this.opened.find(x => x.conid == conid);
|
|
30
|
+
if (!existing) return;
|
|
31
|
+
existing.apiInfo = apiInfo;
|
|
32
|
+
socket.emitChanged(`rest-api-info-changed`, { conid });
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
handle_status(conid, { status }) {
|
|
36
|
+
const existing = this.opened.find(x => x.conid == conid);
|
|
37
|
+
if (!existing) return;
|
|
38
|
+
if (existing.status && status && existing.status.counter > status.counter) return;
|
|
39
|
+
existing.status = status;
|
|
40
|
+
socket.emitChanged(`rest-status-changed`, { conid });
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
handle_ping() {},
|
|
44
|
+
|
|
45
|
+
handle_response(conid, { msgid, ...response }) {
|
|
46
|
+
const pending = this.requests[msgid];
|
|
47
|
+
if (!pending) {
|
|
48
|
+
logger.warn(
|
|
49
|
+
`DBGM-00275 restConnections: Received response for unknown or already handled msgid="${msgid}" (conid="${conid}")`
|
|
50
|
+
);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const [resolve, reject] = pending;
|
|
54
|
+
resolve(response);
|
|
55
|
+
delete this.requests[msgid];
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
async ensureOpened(conid) {
|
|
59
|
+
const res = await lock.acquire(conid, async () => {
|
|
60
|
+
const existing = this.opened.find(x => x.conid == conid);
|
|
61
|
+
if (existing) return existing;
|
|
62
|
+
|
|
63
|
+
const connection = await connections.getCore({ conid });
|
|
64
|
+
if (!connection) {
|
|
65
|
+
throw new Error(`restConnections: Connection with conid="${conid}" not found`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (connection.passwordMode == 'askPassword' || connection.passwordMode == 'askUser') {
|
|
69
|
+
throw new MissingCredentialsError({ conid, passwordMode: connection.passwordMode });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const subprocess = fork(
|
|
73
|
+
global['API_PACKAGE'] || process.argv[1],
|
|
74
|
+
['--is-forked-api', '--start-process', 'restConnectionProcess', ...processArgs.getPassArgs()],
|
|
75
|
+
{
|
|
76
|
+
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
pipeForkLogs(subprocess);
|
|
81
|
+
|
|
82
|
+
const newOpened = {
|
|
83
|
+
conid,
|
|
84
|
+
subprocess,
|
|
85
|
+
apiInfo: null,
|
|
86
|
+
connection,
|
|
87
|
+
status: {
|
|
88
|
+
name: 'pending',
|
|
89
|
+
},
|
|
90
|
+
disconnected: false,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
this.opened.push(newOpened);
|
|
94
|
+
delete this.closed[conid];
|
|
95
|
+
socket.emitChanged(`rest-status-changed`, { conid });
|
|
96
|
+
|
|
97
|
+
subprocess.on('message', message => {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
const { msgtype } = message;
|
|
100
|
+
if (handleProcessCommunication(message, subprocess)) return;
|
|
101
|
+
if (newOpened.disconnected) return;
|
|
102
|
+
const funcName = `handle_${msgtype}`;
|
|
103
|
+
if (!this[funcName]) {
|
|
104
|
+
logger.error({ msgtype, conid }, 'DBGM-00276 Unknown message type from subprocess restConnectionProcess');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this[funcName](conid, message);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
subprocess.on('exit', () => {
|
|
111
|
+
if (newOpened.disconnected) return;
|
|
112
|
+
this.close(conid, false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
subprocess.on('error', err => {
|
|
116
|
+
logger.error(extractErrorLogData(err), 'DBGM-00277 Error in REST connection subprocess');
|
|
117
|
+
if (newOpened.disconnected) return;
|
|
118
|
+
this.close(conid, false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
subprocess.send({
|
|
122
|
+
msgtype: 'connect',
|
|
123
|
+
connection: { ...connection, restAuth: getRestAuthFromConnection(connection) },
|
|
124
|
+
globalSettings: await config.getSettings(),
|
|
125
|
+
});
|
|
126
|
+
return newOpened;
|
|
127
|
+
});
|
|
128
|
+
return res;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
close(conid, kill = true) {
|
|
132
|
+
const existing = this.opened.find(x => x.conid == conid);
|
|
133
|
+
if (existing) {
|
|
134
|
+
existing.disconnected = true;
|
|
135
|
+
if (kill) {
|
|
136
|
+
try {
|
|
137
|
+
existing.subprocess.kill();
|
|
138
|
+
} catch (err) {
|
|
139
|
+
logger.error(extractErrorLogData(err), 'DBGM-00278 Error killing REST subprocess');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this.opened = this.opened.filter(x => x.conid != conid);
|
|
143
|
+
this.closed[conid] = {
|
|
144
|
+
...existing.status,
|
|
145
|
+
name: 'error',
|
|
146
|
+
};
|
|
147
|
+
socket.emitChanged(`rest-status-changed`, { conid });
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
disconnect_meta: true,
|
|
152
|
+
async disconnect({ conid }, req) {
|
|
153
|
+
await testConnectionPermission(conid, req);
|
|
154
|
+
await this.close(conid, true);
|
|
155
|
+
return { status: 'ok' };
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
getApiInfo_meta: true,
|
|
159
|
+
async getApiInfo({ conid }, req) {
|
|
160
|
+
if (!conid) return null;
|
|
161
|
+
|
|
162
|
+
await testConnectionPermission(conid, req);
|
|
163
|
+
|
|
164
|
+
const opened = await this.ensureOpened(conid);
|
|
165
|
+
sendToAuditLog(req, {
|
|
166
|
+
category: 'restop',
|
|
167
|
+
component: 'RestConnectionsController',
|
|
168
|
+
action: 'getApiInfo',
|
|
169
|
+
event: 'rest.getApiInfo',
|
|
170
|
+
severity: 'info',
|
|
171
|
+
conid,
|
|
172
|
+
sessionParam: `${conid}`,
|
|
173
|
+
sessionGroup: 'getApiInfo',
|
|
174
|
+
message: `Loaded API info for REST connection`,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return opened?.apiInfo ?? null;
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
restStatus_meta: true,
|
|
181
|
+
async restStatus() {
|
|
182
|
+
return {
|
|
183
|
+
...this.closed,
|
|
184
|
+
..._.mapValues(_.keyBy(this.opened, 'conid'), 'status'),
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
ping_meta: true,
|
|
189
|
+
async ping({ conidArray, strmid }) {
|
|
190
|
+
await Promise.all(
|
|
191
|
+
_.uniq(conidArray).map(async conid => {
|
|
192
|
+
try {
|
|
193
|
+
const opened = await this.ensureOpened(conid);
|
|
194
|
+
if (!opened) {
|
|
195
|
+
return Promise.resolve();
|
|
196
|
+
}
|
|
197
|
+
opened.subprocess.send({ msgtype: 'ping' });
|
|
198
|
+
} catch (err) {
|
|
199
|
+
logger.error(extractErrorLogData(err), 'DBGM-00279 Error pinging REST connection');
|
|
200
|
+
this.close(conid);
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
socket.setStreamIdFilter(strmid, { conid: conidArray ?? [] });
|
|
205
|
+
return { status: 'ok' };
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
refresh_meta: true,
|
|
209
|
+
async refresh({ conid, keepOpen }, req) {
|
|
210
|
+
await testConnectionPermission(conid, req);
|
|
211
|
+
if (!keepOpen) this.close(conid);
|
|
212
|
+
|
|
213
|
+
const opened = await this.ensureOpened(conid);
|
|
214
|
+
return this.sendRequest(opened, { msgtype: 'refresh' });
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
testConnection_meta: true,
|
|
218
|
+
async testConnection({ conid }, req) {
|
|
219
|
+
await testConnectionPermission(conid, req);
|
|
220
|
+
const opened = await this.ensureOpened(conid);
|
|
221
|
+
if (!opened) {
|
|
222
|
+
return { errorMessage: 'Could not open REST connection' };
|
|
223
|
+
}
|
|
224
|
+
const res = await this.sendRequest(opened, { msgtype: 'testConnection' });
|
|
225
|
+
if (res.errorMessage) {
|
|
226
|
+
return {
|
|
227
|
+
errorMessage: res.errorMessage,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return { status: 'ok' };
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
async executeCore(msgtype, { conid, method, endpoint, parameters, server }, req) {
|
|
234
|
+
await testConnectionPermission(conid, req);
|
|
235
|
+
const opened = await this.ensureOpened(conid);
|
|
236
|
+
if (!opened) {
|
|
237
|
+
return { errorMessage: 'Could not open REST connection' };
|
|
238
|
+
}
|
|
239
|
+
const auth = getRestAuthFromConnection(opened.connection);
|
|
240
|
+
return this.sendRequest(opened, {
|
|
241
|
+
msgtype,
|
|
242
|
+
method,
|
|
243
|
+
endpoint,
|
|
244
|
+
parameters,
|
|
245
|
+
server,
|
|
246
|
+
auth,
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
|
|
250
|
+
executeOpenapi_meta: true,
|
|
251
|
+
async executeOpenapi({ conid, method, endpoint, parameters, server }, req) {
|
|
252
|
+
return this.executeCore('executeOpenapi', { conid, method, endpoint, parameters, server }, req);
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
executeOdata_meta: true,
|
|
256
|
+
async executeOdata({ conid, method, endpoint, parameters, server }, req) {
|
|
257
|
+
return this.executeCore('executeOdata', { conid, method, endpoint, parameters, server }, req);
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
apiQuery_meta: true,
|
|
261
|
+
async apiQuery({ conid, server, query, variables }, req) {
|
|
262
|
+
await testConnectionPermission(conid, req);
|
|
263
|
+
const opened = await this.ensureOpened(conid);
|
|
264
|
+
if (!opened) {
|
|
265
|
+
return { errorMessage: 'Could not open REST connection' };
|
|
266
|
+
}
|
|
267
|
+
const auth = getRestAuthFromConnection(opened.connection);
|
|
268
|
+
const res = await this.sendRequest(opened, {
|
|
269
|
+
msgtype: 'apiQuery',
|
|
270
|
+
server,
|
|
271
|
+
query,
|
|
272
|
+
variables,
|
|
273
|
+
auth,
|
|
274
|
+
});
|
|
275
|
+
return res;
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
loadGraphqlConnectionData_meta: true,
|
|
279
|
+
async loadGraphqlConnectionData(
|
|
280
|
+
{ conid, server, operationName, projection, queryField, selectedFieldSelection, pageSize, filterParameterName, filterValue },
|
|
281
|
+
req
|
|
282
|
+
) {
|
|
283
|
+
await testConnectionPermission(conid, req);
|
|
284
|
+
const opened = await this.ensureOpened(conid);
|
|
285
|
+
if (!opened) {
|
|
286
|
+
return { errorMessage: 'Could not open REST connection' };
|
|
287
|
+
}
|
|
288
|
+
const auth = getRestAuthFromConnection(opened.connection);
|
|
289
|
+
return this.sendRequest(opened, {
|
|
290
|
+
msgtype: 'loadGraphqlConnectionData',
|
|
291
|
+
server,
|
|
292
|
+
operationName,
|
|
293
|
+
projection,
|
|
294
|
+
queryField,
|
|
295
|
+
selectedFieldSelection,
|
|
296
|
+
pageSize,
|
|
297
|
+
filterParameterName,
|
|
298
|
+
filterValue,
|
|
299
|
+
auth,
|
|
300
|
+
});
|
|
301
|
+
},
|
|
302
|
+
|
|
303
|
+
sendRequest(conn, message) {
|
|
304
|
+
const msgid = crypto.randomUUID();
|
|
305
|
+
const promise = new Promise((resolve, reject) => {
|
|
306
|
+
this.requests[msgid] = [resolve, reject];
|
|
307
|
+
try {
|
|
308
|
+
conn.subprocess.send({ msgid, ...message });
|
|
309
|
+
} catch (err) {
|
|
310
|
+
logger.error(extractErrorLogData(err), 'DBGM-00280 Error sending request to REST connection');
|
|
311
|
+
this.close(conn.conid);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
return promise;
|
|
315
|
+
},
|
|
316
|
+
};
|
|
@@ -172,7 +172,7 @@ module.exports = {
|
|
|
172
172
|
byline(subprocess.stderr).on('data', pipeDispatcher('error'));
|
|
173
173
|
subprocess.on('exit', code => {
|
|
174
174
|
// console.log('... EXITED', code);
|
|
175
|
-
this.rejectRequest(runid, { message: 'No data returned, maybe input data source is too big' });
|
|
175
|
+
this.rejectRequest(runid, { message: 'DBGM-00281 No data returned, maybe input data source is too big' });
|
|
176
176
|
logger.info({ code, pid: subprocess.pid }, 'DBGM-00016 Exited process');
|
|
177
177
|
socket.emit(`runner-done-${runid}`, code);
|
|
178
178
|
this.opened = this.opened.filter(x => x.runid != runid);
|
|
@@ -225,7 +225,7 @@ module.exports = {
|
|
|
225
225
|
subprocess.on('exit', code => {
|
|
226
226
|
console.log('... EXITED', code);
|
|
227
227
|
logger.info({ code, pid: subprocess.pid }, 'DBGM-00017 Exited process');
|
|
228
|
-
this.dispatchMessage(runid, `Finished external process with code ${code}`);
|
|
228
|
+
this.dispatchMessage(runid, `DBGM-00282 Finished external process with code ${code}`);
|
|
229
229
|
socket.emit(`runner-done-${runid}`, code);
|
|
230
230
|
if (onFinished) {
|
|
231
231
|
onFinished();
|
|
@@ -233,7 +233,7 @@ module.exports = {
|
|
|
233
233
|
this.opened = this.opened.filter(x => x.runid != runid);
|
|
234
234
|
});
|
|
235
235
|
subprocess.on('spawn', () => {
|
|
236
|
-
this.dispatchMessage(runid, `Started external process ${command}`);
|
|
236
|
+
this.dispatchMessage(runid, `DBGM-00283 Started external process ${command}`);
|
|
237
237
|
});
|
|
238
238
|
subprocess.on('error', error => {
|
|
239
239
|
console.log('... ERROR subprocess', error);
|
|
@@ -279,7 +279,7 @@ module.exports = {
|
|
|
279
279
|
if (script.type == 'json') {
|
|
280
280
|
if (!platformInfo.isElectron) {
|
|
281
281
|
if (!checkSecureDirectoriesInScript(script)) {
|
|
282
|
-
return { errorMessage: 'Unallowed directories in script' };
|
|
282
|
+
return { errorMessage: 'DBGM-00284 Unallowed directories in script' };
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
285
|
|
|
@@ -299,10 +299,10 @@ module.exports = {
|
|
|
299
299
|
action: 'script',
|
|
300
300
|
severity: 'warn',
|
|
301
301
|
detail: script,
|
|
302
|
-
message: 'Scripts are not allowed',
|
|
302
|
+
message: 'DBGM-00285 Scripts are not allowed',
|
|
303
303
|
});
|
|
304
304
|
|
|
305
|
-
return { errorMessage: 'Shell scripting is not allowed' };
|
|
305
|
+
return { errorMessage: 'DBGM-00286 Shell scripting is not allowed' };
|
|
306
306
|
}
|
|
307
307
|
|
|
308
308
|
sendToAuditLog(req, {
|
|
@@ -312,7 +312,7 @@ module.exports = {
|
|
|
312
312
|
action: 'script',
|
|
313
313
|
severity: 'info',
|
|
314
314
|
detail: script,
|
|
315
|
-
message: 'Running JS script',
|
|
315
|
+
message: 'DBGM-00287 Running JS script',
|
|
316
316
|
});
|
|
317
317
|
|
|
318
318
|
return this.startCore(runid, scriptTemplate(script, false));
|
|
@@ -327,7 +327,7 @@ module.exports = {
|
|
|
327
327
|
async cancel({ runid }) {
|
|
328
328
|
const runner = this.opened.find(x => x.runid == runid);
|
|
329
329
|
if (!runner) {
|
|
330
|
-
throw new Error('Invalid runner');
|
|
330
|
+
throw new Error('DBGM-00288 Invalid runner');
|
|
331
331
|
}
|
|
332
332
|
runner.subprocess.kill();
|
|
333
333
|
return { state: 'ok' };
|
|
@@ -353,7 +353,7 @@ module.exports = {
|
|
|
353
353
|
async loadReader({ functionName, props }) {
|
|
354
354
|
if (!platformInfo.isElectron) {
|
|
355
355
|
if (props?.fileName && !checkSecureDirectories(props.fileName)) {
|
|
356
|
-
return { errorMessage: 'Unallowed file' };
|
|
356
|
+
return { errorMessage: 'DBGM-00289 Unallowed file' };
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
359
|
const prefix = extractShellApiPlugins(functionName)
|
|
@@ -371,7 +371,7 @@ module.exports = {
|
|
|
371
371
|
scriptResult_meta: true,
|
|
372
372
|
async scriptResult({ script }) {
|
|
373
373
|
if (script.type != 'json') {
|
|
374
|
-
return { errorMessage: 'Only JSON scripts are allowed' };
|
|
374
|
+
return { errorMessage: 'DBGM-00290 Only JSON scripts are allowed' };
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
const promise = new Promise(async (resolve, reject) => {
|
|
@@ -171,7 +171,7 @@ module.exports = {
|
|
|
171
171
|
const databasePermissions = await loadDatabasePermissionsFromRequest(req);
|
|
172
172
|
const res = [];
|
|
173
173
|
for (const db of opened?.databases ?? []) {
|
|
174
|
-
const databasePermissionRole = getDatabasePermissionRole(
|
|
174
|
+
const databasePermissionRole = getDatabasePermissionRole(conid, db.name, databasePermissions);
|
|
175
175
|
if (databasePermissionRole != 'deny') {
|
|
176
176
|
res.push({
|
|
177
177
|
...db,
|
|
@@ -1173,7 +1173,7 @@ module.exports = {
|
|
|
1173
1173
|
|
|
1174
1174
|
// Store token in database
|
|
1175
1175
|
await storageSqlCommandFmt(
|
|
1176
|
-
'insert into ~
|
|
1176
|
+
'insert into ~user_password_reset_tokens (~user_id, ~token, ~created_at, ~expires_at) values (%v, %v, %v, %v)',
|
|
1177
1177
|
user.id,
|
|
1178
1178
|
token,
|
|
1179
1179
|
format(now, "yyyy-MM-dd'T'HH:mm:ss"),
|
|
@@ -1219,7 +1219,7 @@ DbGate Team
|
|
|
1219
1219
|
// Cleanup: delete the password reset token that was inserted before sending the email
|
|
1220
1220
|
try {
|
|
1221
1221
|
await storageSqlCommandFmt(
|
|
1222
|
-
'delete from ~
|
|
1222
|
+
'delete from ~user_password_reset_tokens where ~token = %v and ~used_at is null',
|
|
1223
1223
|
token
|
|
1224
1224
|
);
|
|
1225
1225
|
} catch (cleanupErr) {
|
|
@@ -1238,7 +1238,7 @@ DbGate Team
|
|
|
1238
1238
|
async resetPassword({ token, newPassword }, req) {
|
|
1239
1239
|
// Find valid token
|
|
1240
1240
|
const tokens = await storageSelectFmt(
|
|
1241
|
-
'select * from ~
|
|
1241
|
+
'select * from ~user_password_reset_tokens where ~token = %v and ~used_at is null and ~expires_at > %v',
|
|
1242
1242
|
token,
|
|
1243
1243
|
format(new Date(), "yyyy-MM-dd'T'HH:mm:ss")
|
|
1244
1244
|
);
|
|
@@ -1265,7 +1265,7 @@ DbGate Team
|
|
|
1265
1265
|
|
|
1266
1266
|
// Mark token as used
|
|
1267
1267
|
await storageSqlCommandFmt(
|
|
1268
|
-
'update ~
|
|
1268
|
+
'update ~user_password_reset_tokens set ~used_at = %v where ~id = %v',
|
|
1269
1269
|
format(new Date(), "yyyy-MM-dd'T'HH:mm:ss"),
|
|
1270
1270
|
resetToken.id
|
|
1271
1271
|
);
|
|
@@ -247,6 +247,24 @@ async function readComplexRolePermissions(roleId, rolePermissionsTable) {
|
|
|
247
247
|
return rolePermissionsResp;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
+
async function resolvePermissionConnectionIds(permissionRows) {
|
|
251
|
+
if (!permissionRows || permissionRows.length === 0) return permissionRows;
|
|
252
|
+
|
|
253
|
+
const connectionIds = Array.from(new Set(permissionRows.filter(r => r.connection_id).map(r => r.connection_id)));
|
|
254
|
+
if (connectionIds.length === 0) return permissionRows;
|
|
255
|
+
|
|
256
|
+
const conidMap = {};
|
|
257
|
+
const rows = await storageSelectFmt('select ~id, ~conid from ~connections where ~id in (%,v)', connectionIds);
|
|
258
|
+
for (const row of rows ?? []) {
|
|
259
|
+
conidMap[row.id] = row.conid;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return permissionRows.map(r => ({
|
|
263
|
+
...r,
|
|
264
|
+
connection_conid: r.connection_id ? conidMap[r.connection_id] ?? null : null,
|
|
265
|
+
}));
|
|
266
|
+
}
|
|
267
|
+
|
|
250
268
|
async function loadSuperadminPermissions() {
|
|
251
269
|
const rolePermissions = await storageReadRolePermissions(-3);
|
|
252
270
|
return [...getPredefinedPermissions('superadmin'), ...rolePermissions];
|
|
@@ -496,10 +514,12 @@ function getStorageConnectionError() {
|
|
|
496
514
|
async function selectStorageIdentity(tableName) {
|
|
497
515
|
const [conn, driver] = await getStorageConnection();
|
|
498
516
|
|
|
517
|
+
const tableDefinition = storageModel.tables?.find(t => t.pureName === tableName);
|
|
518
|
+
|
|
499
519
|
const resp = await runQueryOnDriver(conn, driver, dmp =>
|
|
500
520
|
dmp.selectScopeIdentity(
|
|
501
521
|
// @ts-ignore
|
|
502
|
-
{ pureName: tableName }
|
|
522
|
+
tableDefinition || { pureName: tableName }
|
|
503
523
|
)
|
|
504
524
|
);
|
|
505
525
|
|
|
@@ -1098,6 +1118,7 @@ module.exports = {
|
|
|
1098
1118
|
storageSaveDetailPermissionsDiff,
|
|
1099
1119
|
readComplexUserRolePermissions,
|
|
1100
1120
|
readComplexRolePermissions,
|
|
1121
|
+
resolvePermissionConnectionIds,
|
|
1101
1122
|
storageCheckRoleConnectionAccess,
|
|
1102
1123
|
storageCheckUserRoleConnectionAccess,
|
|
1103
1124
|
storageCreateTeamFile,
|
package/src/currentVersion.js
CHANGED