From 88469e7366ce438484783897c074ad735297e97b Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 30 Dec 2021 09:57:24 +0100 Subject: [PATCH] fixed test connection for electron + better logging --- packages/api/src/controllers/connections.js | 35 +++++++++++-------- packages/api/src/proc/connectProcess.js | 10 +----- packages/api/src/utility/crypting.js | 11 ++++++ .../api/src/utility/requireEngineDriver.js | 5 +-- .../web/src/modals/ConnectionModal.svelte | 4 +-- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/packages/api/src/controllers/connections.js b/packages/api/src/controllers/connections.js index cedbd783..05368d01 100644 --- a/packages/api/src/controllers/connections.js +++ b/packages/api/src/controllers/connections.js @@ -39,7 +39,7 @@ function getDatabaseFileLabel(databaseFile) { function getPortalCollections() { if (process.env.CONNECTIONS) { - return _.compact(process.env.CONNECTIONS.split(',')).map(id => ({ + const connections = _.compact(process.env.CONNECTIONS.split(',')).map(id => ({ _id: id, engine: process.env[`ENGINE_${id}`], server: process.env[`SERVER_${id}`], @@ -53,6 +53,14 @@ function getPortalCollections() { singleDatabase: !!process.env[`DATABASE_${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(); @@ -134,11 +142,8 @@ module.exports = { return portalConnections || this.datastore.find(); }, - test_meta: { - method: 'post', - raw: true, - }, - test(req, res) { + test_meta: true, + test(connection) { const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [ '--is-forked-api', '--start-process', @@ -146,15 +151,17 @@ module.exports = { ...processArgs.getPassArgs(), // ...process.argv.slice(3), ]); - subprocess.on('message', resp => { - if (handleProcessCommunication(resp, subprocess)) return; - // @ts-ignore - const { msgtype } = resp; - if (msgtype == 'connected' || msgtype == 'error') { - res.json(resp); - } + subprocess.send(connection); + return new Promise(resolve => { + subprocess.on('message', resp => { + if (handleProcessCommunication(resp, subprocess)) return; + // @ts-ignore + const { msgtype } = resp; + if (msgtype == 'connected' || msgtype == 'error') { + resolve(resp); + } + }); }); - subprocess.send(req.body); }, save_meta: true, diff --git a/packages/api/src/proc/connectProcess.js b/packages/api/src/proc/connectProcess.js index 5e604144..be08ab51 100644 --- a/packages/api/src/proc/connectProcess.js +++ b/packages/api/src/proc/connectProcess.js @@ -2,17 +2,9 @@ const childProcessChecker = require('../utility/childProcessChecker'); const requireEngineDriver = require('../utility/requireEngineDriver'); const connectUtility = require('../utility/connectUtility'); const { handleProcessCommunication } = require('../utility/processComm'); +const { pickSafeConnectionInfo } = require('../utility/crypting'); 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} Error JSON: ${JSON.stringify(e, undefined, 2)} diff --git a/packages/api/src/utility/crypting.js b/packages/api/src/utility/crypting.js index d7428df7..64ab3deb 100644 --- a/packages/api/src/utility/crypting.js +++ b/packages/api/src/utility/crypting.js @@ -2,6 +2,7 @@ const crypto = require('crypto'); const simpleEncryptor = require('simple-encryptor'); const fs = require('fs'); const path = require('path'); +const _ = require('lodash'); const { datadir } = require('./directories'); @@ -81,8 +82,18 @@ function decryptConnection(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 = { loadEncryptionKey, encryptConnection, decryptConnection, + pickSafeConnectionInfo, }; diff --git a/packages/api/src/utility/requireEngineDriver.js b/packages/api/src/utility/requireEngineDriver.js index dabdf2be..2b42ed1b 100644 --- a/packages/api/src/utility/requireEngineDriver.js +++ b/packages/api/src/utility/requireEngineDriver.js @@ -1,5 +1,6 @@ const _ = require('lodash'); const requirePlugin = require('../shell/requirePlugin'); +const { pickSafeConnectionInfo } = require('./crypting'); /** @returns {import('dbgate-types').EngineDriver} */ function requireEngineDriver(connection) { @@ -10,14 +11,14 @@ function requireEngineDriver(connection) { engine = connection.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('@')) { const [shortName, packageName] = engine.split('@'); const plugin = requirePlugin(packageName); 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; diff --git a/packages/web/src/modals/ConnectionModal.svelte b/packages/web/src/modals/ConnectionModal.svelte index 773be625..f853e5af 100644 --- a/packages/web/src/modals/ConnectionModal.svelte +++ b/packages/web/src/modals/ConnectionModal.svelte @@ -20,7 +20,7 @@ import { extensions } from '../stores'; import _ from 'lodash'; import { getDatabaseFileLabel } from '../utility/getConnectionLabel'; -import { apiCall } from '../utility/api'; + import { apiCall } from '../utility/api'; export let connection; @@ -73,7 +73,6 @@ import { apiCall } from '../utility/api'; apiCall('connections/save', connection); closeCurrentModal(); } - @@ -156,5 +155,4 @@ import { apiCall } from '../utility/api'; .error-result { white-space: normal; } -