dbgate-api-premium 5.5.7-alpha.45
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/.env +19 -0
- package/.yarnrc +2 -0
- package/README.md +1 -0
- package/env/dblogin/.env +14 -0
- package/env/portal/.env +70 -0
- package/env/singledb/.env +17 -0
- package/env/storage/.env +43 -0
- package/package.json +89 -0
- package/src/auth/authCommon.js +16 -0
- package/src/auth/authProvider.js +343 -0
- package/src/auth/storageAuthProvider.js +393 -0
- package/src/controllers/apps.js +280 -0
- package/src/controllers/archive.js +217 -0
- package/src/controllers/auth.js +136 -0
- package/src/controllers/config.js +271 -0
- package/src/controllers/connections.js +486 -0
- package/src/controllers/databaseConnections.js +561 -0
- package/src/controllers/files.js +222 -0
- package/src/controllers/jsldata.js +296 -0
- package/src/controllers/metadata.js +47 -0
- package/src/controllers/plugins.js +216 -0
- package/src/controllers/queryHistory.js +54 -0
- package/src/controllers/runners.js +234 -0
- package/src/controllers/scheduler.js +46 -0
- package/src/controllers/serverConnections.js +271 -0
- package/src/controllers/sessions.js +243 -0
- package/src/controllers/storage.js +380 -0
- package/src/controllers/storageDb.js +215 -0
- package/src/controllers/uploads.js +133 -0
- package/src/currentVersion.js +5 -0
- package/src/gistSecret.js +2 -0
- package/src/index.js +139 -0
- package/src/main.js +202 -0
- package/src/packagedPluginsContent.js +1 -0
- package/src/proc/connectProcess.js +38 -0
- package/src/proc/databaseConnectionProcess.js +431 -0
- package/src/proc/index.js +15 -0
- package/src/proc/jslDatastoreProcess.js +60 -0
- package/src/proc/serverConnectionProcess.js +188 -0
- package/src/proc/sessionProcess.js +390 -0
- package/src/proc/sshForwardProcess.js +75 -0
- package/src/shell/archiveReader.js +11 -0
- package/src/shell/archiveWriter.js +22 -0
- package/src/shell/autoIndexForeignKeysTransform.js +19 -0
- package/src/shell/collectorWriter.js +33 -0
- package/src/shell/consoleObjectWriter.js +16 -0
- package/src/shell/copyStream.js +48 -0
- package/src/shell/dataDuplicator.js +63 -0
- package/src/shell/dataTypeMapperTransform.js +21 -0
- package/src/shell/dbModelToJson.js +16 -0
- package/src/shell/deployDb.js +56 -0
- package/src/shell/download.js +15 -0
- package/src/shell/dropAllDbObjects.js +42 -0
- package/src/shell/dumpDatabase.js +49 -0
- package/src/shell/executeQuery.js +39 -0
- package/src/shell/fakeObjectReader.js +35 -0
- package/src/shell/finalizer.js +12 -0
- package/src/shell/generateDeploySql.js +95 -0
- package/src/shell/generateModelSql.js +30 -0
- package/src/shell/importDatabase.js +85 -0
- package/src/shell/index.js +80 -0
- package/src/shell/initializeApiEnvironment.js +9 -0
- package/src/shell/jslDataReader.js +9 -0
- package/src/shell/jsonLinesReader.js +52 -0
- package/src/shell/jsonLinesWriter.js +36 -0
- package/src/shell/jsonReader.js +84 -0
- package/src/shell/jsonToDbModel.js +9 -0
- package/src/shell/jsonWriter.js +97 -0
- package/src/shell/loadDatabase.js +27 -0
- package/src/shell/loadFile.js +10 -0
- package/src/shell/modifyJsonLinesReader.js +148 -0
- package/src/shell/queryReader.js +30 -0
- package/src/shell/registerPlugins.js +9 -0
- package/src/shell/requirePlugin.js +43 -0
- package/src/shell/runScript.js +19 -0
- package/src/shell/sqlDataWriter.js +52 -0
- package/src/shell/sqlTextReplacementTransform.js +32 -0
- package/src/shell/tableReader.js +39 -0
- package/src/shell/tableWriter.js +18 -0
- package/src/storageModel.js +819 -0
- package/src/utility/ColumnMapTransformStream.js +21 -0
- package/src/utility/DatastoreProxy.js +106 -0
- package/src/utility/EnsureStreamHeaderStream.js +31 -0
- package/src/utility/JsonLinesDatabase.js +148 -0
- package/src/utility/JsonLinesDatastore.js +232 -0
- package/src/utility/LineReader.js +88 -0
- package/src/utility/SSHConnection.js +251 -0
- package/src/utility/authProxy.js +133 -0
- package/src/utility/checkLicense.js +186 -0
- package/src/utility/childProcessChecker.js +21 -0
- package/src/utility/cleanDirectory.js +24 -0
- package/src/utility/cloudUpgrade.js +61 -0
- package/src/utility/connectUtility.js +111 -0
- package/src/utility/crypting.js +105 -0
- package/src/utility/diff2htmlPage.js +8 -0
- package/src/utility/directories.js +179 -0
- package/src/utility/downloadPackage.js +51 -0
- package/src/utility/downloader.js +25 -0
- package/src/utility/exceptions.js +9 -0
- package/src/utility/exportDbModel.js +31 -0
- package/src/utility/exportDbModelSql.js +80 -0
- package/src/utility/getChartExport.js +55 -0
- package/src/utility/getDiagramExport.js +25 -0
- package/src/utility/getExpressPath.js +10 -0
- package/src/utility/getJslFileName.js +16 -0
- package/src/utility/getMapExport.js +77 -0
- package/src/utility/hardwareFingerprint.js +89 -0
- package/src/utility/hasPermission.js +101 -0
- package/src/utility/importDbModel.js +9 -0
- package/src/utility/loadFilesRecursive.js +20 -0
- package/src/utility/loadModelFolder.js +29 -0
- package/src/utility/loadModelTransform.js +36 -0
- package/src/utility/pipeForkLogs.js +19 -0
- package/src/utility/platformInfo.js +62 -0
- package/src/utility/processArgs.js +39 -0
- package/src/utility/processComm.js +18 -0
- package/src/utility/requireEngineDriver.js +26 -0
- package/src/utility/requirePluginFunction.js +16 -0
- package/src/utility/socket.js +68 -0
- package/src/utility/sshTunnel.js +106 -0
- package/src/utility/sshTunnelProxy.js +36 -0
- package/src/utility/timingSafeCheckToken.js +9 -0
- package/src/utility/useController.js +99 -0
- package/tsconfig.json +13 -0
- package/webpack.config.js +55 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2018 Stocard GmbH.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const { Client } = require('ssh2');
|
|
18
|
+
const net = require('net');
|
|
19
|
+
const fs = require('fs');
|
|
20
|
+
const os = require('os');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
const debug = require('debug');
|
|
23
|
+
|
|
24
|
+
// interface Options {
|
|
25
|
+
// username?: string;
|
|
26
|
+
// password?: string;
|
|
27
|
+
// privateKey?: string | Buffer;
|
|
28
|
+
// agentForward?: boolean;
|
|
29
|
+
// bastionHost?: string;
|
|
30
|
+
// passphrase?: string;
|
|
31
|
+
// endPort?: number;
|
|
32
|
+
// endHost: string;
|
|
33
|
+
// agentSocket?: string;
|
|
34
|
+
// skipAutoPrivateKey?: boolean;
|
|
35
|
+
// noReadline?: boolean;
|
|
36
|
+
// }
|
|
37
|
+
|
|
38
|
+
// interface ForwardingOptions {
|
|
39
|
+
// fromPort: number;
|
|
40
|
+
// toPort: number;
|
|
41
|
+
// toHost?: string;
|
|
42
|
+
// }
|
|
43
|
+
|
|
44
|
+
class SSHConnection {
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this.options = options;
|
|
47
|
+
this.debug = debug('ssh');
|
|
48
|
+
this.connections = [];
|
|
49
|
+
this.isWindows = process.platform === 'win32';
|
|
50
|
+
if (!options.username) {
|
|
51
|
+
this.options.username = process.env['SSH_USERNAME'] || process.env['USER'];
|
|
52
|
+
}
|
|
53
|
+
if (!options.endPort) {
|
|
54
|
+
this.options.endPort = 22;
|
|
55
|
+
}
|
|
56
|
+
if (!options.privateKey && !options.agentForward && !options.skipAutoPrivateKey) {
|
|
57
|
+
const defaultFilePath = path.join(os.homedir(), '.ssh', 'id_rsa');
|
|
58
|
+
if (fs.existsSync(defaultFilePath)) {
|
|
59
|
+
this.options.privateKey = fs.readFileSync(defaultFilePath);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async shutdown() {
|
|
65
|
+
this.debug('Shutdown connections');
|
|
66
|
+
for (const connection of this.connections) {
|
|
67
|
+
connection.removeAllListeners();
|
|
68
|
+
connection.end();
|
|
69
|
+
}
|
|
70
|
+
return new Promise(resolve => {
|
|
71
|
+
if (this.server) {
|
|
72
|
+
this.server.close(resolve);
|
|
73
|
+
}
|
|
74
|
+
return resolve();
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async tty() {
|
|
79
|
+
const connection = await this.establish();
|
|
80
|
+
this.debug('Opening tty');
|
|
81
|
+
await this.shell(connection);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async executeCommand(command) {
|
|
85
|
+
const connection = await this.establish();
|
|
86
|
+
this.debug('Executing command "%s"', command);
|
|
87
|
+
await this.shell(connection, command);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async shell(connection, command) {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
connection.shell((err, stream) => {
|
|
93
|
+
if (err) {
|
|
94
|
+
return reject(err);
|
|
95
|
+
}
|
|
96
|
+
stream
|
|
97
|
+
.on('close', async () => {
|
|
98
|
+
stream.end();
|
|
99
|
+
process.stdin.unpipe(stream);
|
|
100
|
+
process.stdin.destroy();
|
|
101
|
+
connection.end();
|
|
102
|
+
await this.shutdown();
|
|
103
|
+
return resolve();
|
|
104
|
+
})
|
|
105
|
+
.stderr.on('data', data => {
|
|
106
|
+
return reject(data);
|
|
107
|
+
});
|
|
108
|
+
stream.pipe(process.stdout);
|
|
109
|
+
|
|
110
|
+
if (command) {
|
|
111
|
+
stream.end(`${command}\nexit\n`);
|
|
112
|
+
} else {
|
|
113
|
+
process.stdin.pipe(stream);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async establish() {
|
|
120
|
+
let connection;
|
|
121
|
+
if (this.options.bastionHost) {
|
|
122
|
+
connection = await this.connectViaBastion(this.options.bastionHost);
|
|
123
|
+
} else {
|
|
124
|
+
connection = await this.connect(this.options.endHost);
|
|
125
|
+
}
|
|
126
|
+
return connection;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async connectViaBastion(bastionHost) {
|
|
130
|
+
this.debug('Connecting to bastion host "%s"', bastionHost);
|
|
131
|
+
const connectionToBastion = await this.connect(bastionHost);
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
connectionToBastion.forwardOut(
|
|
134
|
+
'127.0.0.1',
|
|
135
|
+
22,
|
|
136
|
+
this.options.endHost,
|
|
137
|
+
this.options.endPort || 22,
|
|
138
|
+
async (err, stream) => {
|
|
139
|
+
if (err) {
|
|
140
|
+
return reject(err);
|
|
141
|
+
}
|
|
142
|
+
const connection = await this.connect(this.options.endHost, stream);
|
|
143
|
+
return resolve(connection);
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async connect(host, stream) {
|
|
150
|
+
this.debug('Connecting to "%s"', host);
|
|
151
|
+
const connection = new Client();
|
|
152
|
+
return new Promise(async (resolve, reject) => {
|
|
153
|
+
const options = {
|
|
154
|
+
host,
|
|
155
|
+
port: this.options.endPort,
|
|
156
|
+
username: this.options.username,
|
|
157
|
+
password: this.options.password,
|
|
158
|
+
privateKey: this.options.privateKey,
|
|
159
|
+
};
|
|
160
|
+
if (this.options.agentForward) {
|
|
161
|
+
options['agentForward'] = true;
|
|
162
|
+
|
|
163
|
+
// see https://github.com/mscdex/ssh2#client for agents on Windows
|
|
164
|
+
// guaranteed to give the ssh agent sock if the agent is running (posix)
|
|
165
|
+
let agentDefault = process.env['SSH_AUTH_SOCK'];
|
|
166
|
+
if (this.isWindows) {
|
|
167
|
+
// null or undefined
|
|
168
|
+
if (agentDefault == null) {
|
|
169
|
+
agentDefault = 'pageant';
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const agentSock = this.options.agentSocket ? this.options.agentSocket : agentDefault;
|
|
174
|
+
if (agentSock == null) {
|
|
175
|
+
throw new Error('SSH Agent Socket is not provided, or is not set in the SSH_AUTH_SOCK env variable');
|
|
176
|
+
}
|
|
177
|
+
options['agent'] = agentSock;
|
|
178
|
+
}
|
|
179
|
+
if (stream) {
|
|
180
|
+
options['sock'] = stream;
|
|
181
|
+
}
|
|
182
|
+
// PPK private keys can be encrypted, but won't contain the word 'encrypted'
|
|
183
|
+
// in fact they always contain a `encryption` header, so we can't do a simple check
|
|
184
|
+
options['passphrase'] = this.options.passphrase;
|
|
185
|
+
const looksEncrypted = this.options.privateKey
|
|
186
|
+
? this.options.privateKey.toString().toLowerCase().includes('encrypted')
|
|
187
|
+
: false;
|
|
188
|
+
if (looksEncrypted && !options['passphrase'] && !this.options.noReadline) {
|
|
189
|
+
// options['passphrase'] = await this.getPassphrase();
|
|
190
|
+
}
|
|
191
|
+
connection.on('ready', () => {
|
|
192
|
+
this.connections.push(connection);
|
|
193
|
+
return resolve(connection);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
connection.on('error', error => {
|
|
197
|
+
reject(error);
|
|
198
|
+
});
|
|
199
|
+
try {
|
|
200
|
+
connection.connect(options);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
reject(error);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// private async getPassphrase() {
|
|
208
|
+
// return new Promise(resolve => {
|
|
209
|
+
// const rl = readline.createInterface({
|
|
210
|
+
// input: process.stdin,
|
|
211
|
+
// output: process.stdout,
|
|
212
|
+
// });
|
|
213
|
+
// rl.question('Please type in the passphrase for your private key: ', answer => {
|
|
214
|
+
// return resolve(answer);
|
|
215
|
+
// });
|
|
216
|
+
// });
|
|
217
|
+
// }
|
|
218
|
+
|
|
219
|
+
async forward(options) {
|
|
220
|
+
const connection = await this.establish();
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
this.server = net
|
|
223
|
+
.createServer(socket => {
|
|
224
|
+
this.debug(
|
|
225
|
+
'Forwarding connection from "localhost:%d" to "%s:%d"',
|
|
226
|
+
options.fromPort,
|
|
227
|
+
options.toHost,
|
|
228
|
+
options.toPort
|
|
229
|
+
);
|
|
230
|
+
connection.forwardOut(
|
|
231
|
+
'localhost',
|
|
232
|
+
options.fromPort,
|
|
233
|
+
options.toHost || 'localhost',
|
|
234
|
+
options.toPort,
|
|
235
|
+
(error, stream) => {
|
|
236
|
+
if (error) {
|
|
237
|
+
return reject(error);
|
|
238
|
+
}
|
|
239
|
+
socket.pipe(stream);
|
|
240
|
+
stream.pipe(socket);
|
|
241
|
+
}
|
|
242
|
+
);
|
|
243
|
+
})
|
|
244
|
+
.listen(options.fromPort, 'localhost', () => {
|
|
245
|
+
return resolve();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
module.exports = { SSHConnection };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const { Signer } = require('@aws-sdk/rds-signer');
|
|
3
|
+
|
|
4
|
+
const AUTH_PROXY_URL = process.env.DEVWEB ? 'https://dbgate-auth-proxy.stages.udolni.net' : 'https://auth.dbgate.eu';
|
|
5
|
+
// const AUTH_PROXY_URL = 'https://dbgate-auth-proxy.stages.udolni.net';
|
|
6
|
+
|
|
7
|
+
let licenseKey = null;
|
|
8
|
+
|
|
9
|
+
function setAuthProxyLicense(value) {
|
|
10
|
+
licenseKey = value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isAuthProxySupported() {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function authProxyGetRedirectUrl({ client, type, state, redirectUri }) {
|
|
18
|
+
const respSession = await axios.default.post(
|
|
19
|
+
`${AUTH_PROXY_URL}/create-session`,
|
|
20
|
+
{
|
|
21
|
+
client,
|
|
22
|
+
type,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
Authorization: `Bearer ${licenseKey ?? process.env.DBGATE_LICENSE}`,
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const { sid } = respSession.data;
|
|
33
|
+
|
|
34
|
+
const url = redirectUri
|
|
35
|
+
? `${AUTH_PROXY_URL}/login?sid=${sid}&redirectUri=${encodeURIComponent(redirectUri)}&state=${encodeURIComponent(
|
|
36
|
+
state
|
|
37
|
+
)}`
|
|
38
|
+
: `${AUTH_PROXY_URL}/login?sid=${sid}&state=${encodeURIComponent(state)}`;
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
url,
|
|
42
|
+
sid,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function authProxyGetTokenFromCode({ sid, code }) {
|
|
47
|
+
try {
|
|
48
|
+
const respToken = await axios.default.post(
|
|
49
|
+
`${AUTH_PROXY_URL}/acquire-token`,
|
|
50
|
+
{
|
|
51
|
+
sid,
|
|
52
|
+
code,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
headers: {
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
Authorization: `Bearer ${licenseKey ?? process.env.DBGATE_LICENSE}`,
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
return respToken.data.token;
|
|
62
|
+
} catch (err) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function startTokenChecking(sid, callback) {
|
|
68
|
+
const started = Date.now();
|
|
69
|
+
const interval = setInterval(async () => {
|
|
70
|
+
if (Date.now() - started > 60 * 1000) {
|
|
71
|
+
clearInterval(interval);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const resp = await axios.default.post(
|
|
77
|
+
`${AUTH_PROXY_URL}/get-token`,
|
|
78
|
+
{
|
|
79
|
+
sid,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
headers: {
|
|
83
|
+
'Content-Type': 'application/json',
|
|
84
|
+
Authorization: `Bearer ${licenseKey ?? process.env.DBGATE_LICENSE}`,
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
if (resp.data.status == 'ok') {
|
|
90
|
+
clearInterval(interval);
|
|
91
|
+
callback(resp.data.token);
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
console.error('Error checking token', err);
|
|
95
|
+
}
|
|
96
|
+
}, 500);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function getAuthProxyUrl() {
|
|
100
|
+
return AUTH_PROXY_URL;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function supportsAwsIam() {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function getAwsIamToken(props) {
|
|
108
|
+
const { accessKeyId, secretAccessKey, awsRegion, port, user, server } = props;
|
|
109
|
+
const signer = new Signer({
|
|
110
|
+
credentials: {
|
|
111
|
+
accessKeyId,
|
|
112
|
+
secretAccessKey,
|
|
113
|
+
},
|
|
114
|
+
region: awsRegion,
|
|
115
|
+
port,
|
|
116
|
+
username: user,
|
|
117
|
+
hostname: server,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const token = await signer.getAuthToken();
|
|
121
|
+
return token;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = {
|
|
125
|
+
isAuthProxySupported,
|
|
126
|
+
authProxyGetRedirectUrl,
|
|
127
|
+
authProxyGetTokenFromCode,
|
|
128
|
+
setAuthProxyLicense,
|
|
129
|
+
startTokenChecking,
|
|
130
|
+
getAuthProxyUrl,
|
|
131
|
+
supportsAwsIam,
|
|
132
|
+
getAwsIamToken,
|
|
133
|
+
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
const { getLogger, extractErrorLogData } = require('dbgate-tools');
|
|
2
|
+
const jwt = require('jsonwebtoken');
|
|
3
|
+
const isElectron = require('is-electron');
|
|
4
|
+
const { storageReadConfig } = require('../controllers/storageDb');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const { setAuthProxyLicense } = require('./authProxy');
|
|
9
|
+
const axios = require('axios');
|
|
10
|
+
const crypto = require('crypto');
|
|
11
|
+
const platformInfo = require('./platformInfo');
|
|
12
|
+
|
|
13
|
+
const logger = getLogger('checkLicense');
|
|
14
|
+
|
|
15
|
+
const publicKey = `-----BEGIN PUBLIC KEY-----
|
|
16
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnkFvSGF5FuAJQEZWYTZw
|
|
17
|
+
ILZD0nxnc1oOm71m0tzC+VGNm7wmSmP2Rt+kwsrqp/TrbIjH62B4FPumbFPVOsCj
|
|
18
|
+
WQ0s2QnV3uclvC4ZeIhCtqSrxnFYfLLr1rA8Zg6WYutUK+I4djMHzqkcMfzKJTup
|
|
19
|
+
J28ZL1Tn0W3ZnJ2GW5cSmmuHxOu+7PXFx0UjyaYMWGUvaXo6A5wLFR5BynCJMeOX
|
|
20
|
+
HSYeVUN88w4uOzQUA+agUVitXMHtKs0LckadGptxJ31Ll1M2TCWggajuaqW8tHCg
|
|
21
|
+
OaqL08uYAfHQAWER0F9wumsvD/zVWnc/Wi1fH7wt19XoBEt1TWpu+VcgPIDb3naQ
|
|
22
|
+
mQIDAQAB
|
|
23
|
+
-----END PUBLIC KEY-----
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
let awsMetadataLoaded = false;
|
|
27
|
+
let awsMetadata = null;
|
|
28
|
+
async function getAwsMetadata() {
|
|
29
|
+
if (awsMetadataLoaded) {
|
|
30
|
+
return awsMetadata;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const tokenResponse = await axios.default.put('http://169.254.169.254/latest/api/token', null, {
|
|
34
|
+
headers: {
|
|
35
|
+
'X-aws-ec2-metadata-token-ttl-seconds': '21600',
|
|
36
|
+
},
|
|
37
|
+
timeout: 1000,
|
|
38
|
+
});
|
|
39
|
+
const token = tokenResponse.data;
|
|
40
|
+
|
|
41
|
+
const amiIdResponse = await axios.default.get('http://169.254.169.254/latest/meta-data/ami-id', {
|
|
42
|
+
headers: {
|
|
43
|
+
'X-aws-ec2-metadata-token': token,
|
|
44
|
+
},
|
|
45
|
+
timeout: 1000,
|
|
46
|
+
});
|
|
47
|
+
const amiId = amiIdResponse.data;
|
|
48
|
+
|
|
49
|
+
const regionResponse = await axios.default.get('http://169.254.169.254/latest/meta-data/placement/region', {
|
|
50
|
+
headers: {
|
|
51
|
+
'X-aws-ec2-metadata-token': token,
|
|
52
|
+
},
|
|
53
|
+
timeout: 1000,
|
|
54
|
+
});
|
|
55
|
+
const region = regionResponse.data;
|
|
56
|
+
|
|
57
|
+
awsMetadata = { amiId, region };
|
|
58
|
+
awsMetadataLoaded = true;
|
|
59
|
+
|
|
60
|
+
logger.info(`Loaded AWS metadata, AMIID=${amiId}, region=${region}`);
|
|
61
|
+
return { amiId, region };
|
|
62
|
+
} catch (error) {
|
|
63
|
+
logger.error(extractErrorLogData(error), 'Error getting AWS metadata');
|
|
64
|
+
awsMetadataLoaded = true;
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function checkLicenseKey(licenseKey) {
|
|
70
|
+
try {
|
|
71
|
+
const decoded = jwt.verify(licenseKey, publicKey, {
|
|
72
|
+
algorithms: ['RS256'],
|
|
73
|
+
});
|
|
74
|
+
if (decoded.licenseType != 'premium') {
|
|
75
|
+
logger.error(`Incorrect license type, expected premium, found ${decoded.licenseType}`);
|
|
76
|
+
return {
|
|
77
|
+
status: 'error',
|
|
78
|
+
error: `Incorrect license type, expected premium, found ${decoded.licenseType}`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
status: 'ok',
|
|
83
|
+
type: 'premium',
|
|
84
|
+
validTo: decoded.validTo,
|
|
85
|
+
expiration: new Date(decoded.exp * 1000).toISOString(),
|
|
86
|
+
daysLeft: Math.round((decoded.exp * 1000 - Date.now()) / (24 * 60 * 60 * 1000)),
|
|
87
|
+
isGeneratedTrial: decoded.isGeneratedTrial,
|
|
88
|
+
};
|
|
89
|
+
} catch (err) {
|
|
90
|
+
try {
|
|
91
|
+
// detect expired license
|
|
92
|
+
const decoded = jwt.decode(licenseKey);
|
|
93
|
+
if (decoded) {
|
|
94
|
+
const { exp } = decoded;
|
|
95
|
+
if (exp * 1000 < Date.now()) {
|
|
96
|
+
return {
|
|
97
|
+
status: 'error',
|
|
98
|
+
isExpired: true,
|
|
99
|
+
errorMessage: 'License key is expired',
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
} catch (err) {}
|
|
104
|
+
|
|
105
|
+
logger.error(extractErrorLogData(err), 'License token is invalid');
|
|
106
|
+
return {
|
|
107
|
+
status: 'error',
|
|
108
|
+
error: err.message ?? 'License token is invalid',
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let awsTokenLoaded = false;
|
|
114
|
+
let awsTokenHash = null;
|
|
115
|
+
function getAwsToken() {
|
|
116
|
+
if (awsTokenLoaded) {
|
|
117
|
+
return awsTokenHash;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const token = fs
|
|
121
|
+
.readFileSync('/etc/drunid', {
|
|
122
|
+
encoding: 'utf-8',
|
|
123
|
+
})
|
|
124
|
+
.trim();
|
|
125
|
+
awsTokenHash = crypto.createHash('md5').update(token).digest('hex');
|
|
126
|
+
} catch (err) {}
|
|
127
|
+
awsTokenLoaded = true;
|
|
128
|
+
return awsTokenHash;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function checkLicense() {
|
|
132
|
+
if (process.env.DBGATE_LICENSE) {
|
|
133
|
+
return checkLicenseKey(process.env.DBGATE_LICENSE);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (platformInfo.isAwsUbuntuLayout && getAwsToken() == 'b93c7491890460063003a02de06ec84a') {
|
|
137
|
+
const metadata = await getAwsMetadata();
|
|
138
|
+
if (metadata?.amiId) {
|
|
139
|
+
return {
|
|
140
|
+
status: 'ok',
|
|
141
|
+
type: 'premium',
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (process.env.STORAGE_DATABASE) {
|
|
147
|
+
const licenseConfig = await storageReadConfig('license');
|
|
148
|
+
const key = licenseConfig?.licenseKey;
|
|
149
|
+
if (key) {
|
|
150
|
+
setAuthProxyLicense(key);
|
|
151
|
+
return checkLicenseKey(key);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const datadir = path.join(os.homedir(), '.dbgate');
|
|
156
|
+
if (isElectron()) {
|
|
157
|
+
try {
|
|
158
|
+
const licenseKey = fs.readFileSync(path.join(datadir, 'license.key'), {
|
|
159
|
+
encoding: 'utf-8',
|
|
160
|
+
});
|
|
161
|
+
setAuthProxyLicense(licenseKey);
|
|
162
|
+
if (licenseKey) {
|
|
163
|
+
return checkLicenseKey(licenseKey);
|
|
164
|
+
}
|
|
165
|
+
} catch (err) {
|
|
166
|
+
logger.warn(extractErrorLogData(err), 'Error loading license key');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (isElectron()) {
|
|
171
|
+
return {
|
|
172
|
+
status: 'error',
|
|
173
|
+
error: 'License key not set',
|
|
174
|
+
};
|
|
175
|
+
} else {
|
|
176
|
+
return {
|
|
177
|
+
status: 'error',
|
|
178
|
+
error: 'Variable DBGATE_LICENSE is not set',
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
module.exports = {
|
|
184
|
+
checkLicense,
|
|
185
|
+
checkLicenseKey,
|
|
186
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const { getLogger, extractErrorLogData } = require('dbgate-tools');
|
|
2
|
+
|
|
3
|
+
const logger = getLogger('childProcessChecked');
|
|
4
|
+
|
|
5
|
+
let counter = 0;
|
|
6
|
+
|
|
7
|
+
function childProcessChecker() {
|
|
8
|
+
setInterval(() => {
|
|
9
|
+
try {
|
|
10
|
+
process.send({ msgtype: 'ping', counter: counter++ });
|
|
11
|
+
} catch (err) {
|
|
12
|
+
// This will come once parent dies.
|
|
13
|
+
// One way can be to check for error code ERR_IPC_CHANNEL_CLOSED
|
|
14
|
+
// and call process.exit()
|
|
15
|
+
logger.error(extractErrorLogData(err), 'parent died');
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
}, 1000);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = childProcessChecker;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const ageSeconds = 3600;
|
|
4
|
+
|
|
5
|
+
async function cleanDirectory(directory, age = undefined) {
|
|
6
|
+
const files = await fs.readdir(directory);
|
|
7
|
+
const now = new Date().getTime();
|
|
8
|
+
|
|
9
|
+
for (const file of files) {
|
|
10
|
+
const full = path.join(directory, file);
|
|
11
|
+
const stat = await fs.stat(full);
|
|
12
|
+
const mtime = stat.mtime.getTime();
|
|
13
|
+
const expirationTime = mtime + (age || ageSeconds) * 1000;
|
|
14
|
+
if (now > expirationTime) {
|
|
15
|
+
if (stat.isDirectory()) {
|
|
16
|
+
await fs.rmdir(full, { recursive: true });
|
|
17
|
+
} else {
|
|
18
|
+
await fs.unlink(full);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = cleanDirectory;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const fsp = require('fs/promises');
|
|
4
|
+
const semver = require('semver');
|
|
5
|
+
const currentVersion = require('../currentVersion');
|
|
6
|
+
const { getLogger, extractErrorLogData } = require('dbgate-tools');
|
|
7
|
+
|
|
8
|
+
const logger = getLogger('cloudUpgrade');
|
|
9
|
+
|
|
10
|
+
async function checkCloudUpgrade() {
|
|
11
|
+
try {
|
|
12
|
+
const resp = await axios.default.get('https://api.github.com/repos/dbgate/dbgate/releases/latest');
|
|
13
|
+
const json = resp.data;
|
|
14
|
+
const version = json.name.substring(1);
|
|
15
|
+
let cloudDownloadedVersion = null;
|
|
16
|
+
try {
|
|
17
|
+
cloudDownloadedVersion = await fsp.readFile(process.env.CLOUD_UPGRADE_FILE + '.version', 'utf-8');
|
|
18
|
+
} catch (err) {
|
|
19
|
+
cloudDownloadedVersion = null;
|
|
20
|
+
}
|
|
21
|
+
if (
|
|
22
|
+
semver.gt(version, currentVersion.version) &&
|
|
23
|
+
(!cloudDownloadedVersion || semver.gt(version, cloudDownloadedVersion))
|
|
24
|
+
) {
|
|
25
|
+
logger.info(`New version available: ${version}`);
|
|
26
|
+
const zipUrl = json.assets.find(x => x.name == 'cloud-build.zip').browser_download_url;
|
|
27
|
+
|
|
28
|
+
const writer = fs.createWriteStream(process.env.CLOUD_UPGRADE_FILE);
|
|
29
|
+
|
|
30
|
+
const response = await axios.default({
|
|
31
|
+
url: zipUrl,
|
|
32
|
+
method: 'GET',
|
|
33
|
+
responseType: 'stream',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
response.data.pipe(writer);
|
|
37
|
+
|
|
38
|
+
await new Promise((resolve, reject) => {
|
|
39
|
+
writer.on('finish', resolve);
|
|
40
|
+
writer.on('error', reject);
|
|
41
|
+
});
|
|
42
|
+
await fsp.writeFile(process.env.CLOUD_UPGRADE_FILE + '.version', version);
|
|
43
|
+
|
|
44
|
+
logger.info(`Downloaded new version from ${zipUrl}`);
|
|
45
|
+
} else {
|
|
46
|
+
logger.info(`Checked version ${version} is not newer than ${cloudDownloadedVersion ?? currentVersion.version}, upgrade skippped`);
|
|
47
|
+
}
|
|
48
|
+
} catch (err) {
|
|
49
|
+
logger.error(extractErrorLogData(err), 'Error checking cloud upgrade');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function startCloudUpgradeTimer() {
|
|
54
|
+
// at first in 5 seconds
|
|
55
|
+
setTimeout(checkCloudUpgrade, 5000);
|
|
56
|
+
|
|
57
|
+
// hourly
|
|
58
|
+
setInterval(checkCloudUpgrade, 60 * 60 * 1000);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = startCloudUpgradeTimer;
|