fixed test connection for electron + better logging

This commit is contained in:
Jan Prochazka 2021-12-30 09:57:24 +01:00
parent 15de3600c3
commit 88469e7366
5 changed files with 37 additions and 28 deletions

View File

@ -39,7 +39,7 @@ function getDatabaseFileLabel(databaseFile) {
function getPortalCollections() { function getPortalCollections() {
if (process.env.CONNECTIONS) { if (process.env.CONNECTIONS) {
return _.compact(process.env.CONNECTIONS.split(',')).map(id => ({ const connections = _.compact(process.env.CONNECTIONS.split(',')).map(id => ({
_id: id, _id: id,
engine: process.env[`ENGINE_${id}`], engine: process.env[`ENGINE_${id}`],
server: process.env[`SERVER_${id}`], server: process.env[`SERVER_${id}`],
@ -53,6 +53,14 @@ function getPortalCollections() {
singleDatabase: !!process.env[`DATABASE_${id}`], singleDatabase: !!process.env[`DATABASE_${id}`],
displayName: process.env[`LABEL_${id}`], displayName: process.env[`LABEL_${id}`],
})); }));
const noengine = connections.filter(x => !x.engine);
if (noengine.length > 0) {
console.log(
'Warning: Invalid CONNECTIONS configutation, missing ENGINE for connection ID:',
noengine.map(x => x._id)
);
}
return connections;
} }
const args = getNamedArgs(); const args = getNamedArgs();
@ -134,11 +142,8 @@ module.exports = {
return portalConnections || this.datastore.find(); return portalConnections || this.datastore.find();
}, },
test_meta: { test_meta: true,
method: 'post', test(connection) {
raw: true,
},
test(req, res) {
const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [ const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [
'--is-forked-api', '--is-forked-api',
'--start-process', '--start-process',
@ -146,15 +151,17 @@ module.exports = {
...processArgs.getPassArgs(), ...processArgs.getPassArgs(),
// ...process.argv.slice(3), // ...process.argv.slice(3),
]); ]);
subprocess.on('message', resp => { subprocess.send(connection);
if (handleProcessCommunication(resp, subprocess)) return; return new Promise(resolve => {
// @ts-ignore subprocess.on('message', resp => {
const { msgtype } = resp; if (handleProcessCommunication(resp, subprocess)) return;
if (msgtype == 'connected' || msgtype == 'error') { // @ts-ignore
res.json(resp); const { msgtype } = resp;
} if (msgtype == 'connected' || msgtype == 'error') {
resolve(resp);
}
});
}); });
subprocess.send(req.body);
}, },
save_meta: true, save_meta: true,

View File

@ -2,17 +2,9 @@ const childProcessChecker = require('../utility/childProcessChecker');
const requireEngineDriver = require('../utility/requireEngineDriver'); const requireEngineDriver = require('../utility/requireEngineDriver');
const connectUtility = require('../utility/connectUtility'); const connectUtility = require('../utility/connectUtility');
const { handleProcessCommunication } = require('../utility/processComm'); const { handleProcessCommunication } = require('../utility/processComm');
const { pickSafeConnectionInfo } = require('../utility/crypting');
const _ = require('lodash'); const _ = require('lodash');
function pickSafeConnectionInfo(connection) {
return _.mapValues(connection, (v, k) => {
if (k == 'engine' || k == 'port' || k == 'authType' || k == 'sshMode' || k == 'passwordMode') return v;
if (v === null || v === true || v === false) return v;
if (v) return '***';
return undefined;
});
}
const formatErrorDetail = (e, connection) => `${e.stack} const formatErrorDetail = (e, connection) => `${e.stack}
Error JSON: ${JSON.stringify(e, undefined, 2)} Error JSON: ${JSON.stringify(e, undefined, 2)}

View File

@ -2,6 +2,7 @@ const crypto = require('crypto');
const simpleEncryptor = require('simple-encryptor'); const simpleEncryptor = require('simple-encryptor');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const _ = require('lodash');
const { datadir } = require('./directories'); const { datadir } = require('./directories');
@ -81,8 +82,18 @@ function decryptConnection(connection) {
return connection; return connection;
} }
function pickSafeConnectionInfo(connection) {
return _.mapValues(connection, (v, k) => {
if (k == 'engine' || k == 'port' || k == 'authType' || k == 'sshMode' || k == 'passwordMode') return v;
if (v === null || v === true || v === false) return v;
if (v) return '***';
return undefined;
});
}
module.exports = { module.exports = {
loadEncryptionKey, loadEncryptionKey,
encryptConnection, encryptConnection,
decryptConnection, decryptConnection,
pickSafeConnectionInfo,
}; };

View File

@ -1,5 +1,6 @@
const _ = require('lodash'); const _ = require('lodash');
const requirePlugin = require('../shell/requirePlugin'); const requirePlugin = require('../shell/requirePlugin');
const { pickSafeConnectionInfo } = require('./crypting');
/** @returns {import('dbgate-types').EngineDriver} */ /** @returns {import('dbgate-types').EngineDriver} */
function requireEngineDriver(connection) { function requireEngineDriver(connection) {
@ -10,14 +11,14 @@ function requireEngineDriver(connection) {
engine = connection.engine; engine = connection.engine;
} }
if (!engine) { if (!engine) {
throw new Error('Could not get driver from connection'); throw new Error(`Could not get driver from connection ${JSON.stringify(pickSafeConnectionInfo(connection))}`);
} }
if (engine.includes('@')) { if (engine.includes('@')) {
const [shortName, packageName] = engine.split('@'); const [shortName, packageName] = engine.split('@');
const plugin = requirePlugin(packageName); const plugin = requirePlugin(packageName);
return plugin.drivers.find(x => x.engine == engine); return plugin.drivers.find(x => x.engine == engine);
} }
throw new Error(`Could not found engine driver ${engine}`); throw new Error(`Could not find engine driver ${engine}`);
} }
module.exports = requireEngineDriver; module.exports = requireEngineDriver;

View File

@ -20,7 +20,7 @@
import { extensions } from '../stores'; import { extensions } from '../stores';
import _ from 'lodash'; import _ from 'lodash';
import { getDatabaseFileLabel } from '../utility/getConnectionLabel'; import { getDatabaseFileLabel } from '../utility/getConnectionLabel';
import { apiCall } from '../utility/api'; import { apiCall } from '../utility/api';
export let connection; export let connection;
@ -73,7 +73,6 @@ import { apiCall } from '../utility/api';
apiCall('connections/save', connection); apiCall('connections/save', connection);
closeCurrentModal(); closeCurrentModal();
} }
</script> </script>
<FormProviderCore template={FormFieldTemplateLarge} {values}> <FormProviderCore template={FormFieldTemplateLarge} {values}>
@ -156,5 +155,4 @@ import { apiCall } from '../utility/api';
.error-result { .error-result {
white-space: normal; white-space: normal;
} }
</style> </style>