Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCK-6142: Fix ssh tunneling #46

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions forward_engineering/applyToInstanceHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const removeDelimiter = (statement) => {
const applyToInstance = async (connectionInfo, logger, app) => {
const _ = app.require('lodash');
const async = app.require('async');
const sshService = app.require('@hackolade/ssh-service');
const connection = connectionHelper.createInstance(
await connectionHelper.connect(connectionInfo),
await connectionHelper.connect(connectionInfo, sshService),
logger,
);

Expand All @@ -34,7 +35,7 @@ const applyToInstance = async (connectionInfo, logger, app) => {
});

} catch (e) {
connectionHelper.close();
connectionHelper.close(sshService);
throw e;
}

Expand Down
16 changes: 10 additions & 6 deletions reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ BigInt.prototype.toJSON = function () {
const ACCESS_DENIED_ERROR = 1045;

module.exports = {
async connect(connectionInfo) {
const connection = await connectionHelper.connect(connectionInfo);
async connect(connectionInfo, app) {
const sshService = app.require('@hackolade/ssh-service');

const connection = await connectionHelper.connect(connectionInfo, sshService);

return connection;
},

disconnect(connectionInfo, logger, callback, app) {
connectionHelper.close();
const sshService = app.require('@hackolade/ssh-service');

connectionHelper.close(sshService);

callback();
},
Expand All @@ -33,7 +37,7 @@ module.exports = {
logger.clear();
logger.log('info', connectionInfo, 'connectionInfo', connectionInfo.hiddenKeys);

const connection = await this.connect(connectionInfo);
const connection = await this.connect(connectionInfo, app);
const instance = connectionHelper.createInstance(connection, logger);

await instance.ping();
Expand Down Expand Up @@ -70,7 +74,7 @@ module.exports = {
logger.log('info', connectionInfo, 'connectionInfo', connectionInfo.hiddenKeys);
const systemDatabases = connectionInfo.includeSystemCollection ? [] : ['information_schema', 'mysql', 'performance_schema'];

const connection = await this.connect(connectionInfo);
const connection = await this.connect(connectionInfo, app);
const instance = connectionHelper.createInstance(connection, logger);
const databases = connectionInfo.databaseName ? [connectionInfo.databaseName] : await instance.getDatabases(systemDatabases);

Expand Down Expand Up @@ -125,7 +129,7 @@ module.exports = {

const collections = data.collectionData.collections;
const dataBaseNames = data.collectionData.dataBaseNames;
const connection = await this.connect(data);
const connection = await this.connect(data, app);
const instance = await connectionHelper.createInstance(connection, logger);

log.info('MariaDB version: ' + await instance.serverVersion());
Expand Down
78 changes: 26 additions & 52 deletions reverse_engineering/helpers/connectionHelper.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
const mysql = require('mysql');
const fs = require('fs');
const ssh = require('tunnel-ssh');

let connection;
let sshTunnel;

const getSshConfig = (info) => {
const config = {
username: info.ssh_user,
host: info.ssh_host,
port: info.ssh_port,
dstHost: info.host,
dstPort: info.port,
localHost: '127.0.0.1',
localPort: info.port,
keepAlive: true
};

if (info.ssh_method === 'privateKey') {
return Object.assign({}, config, {
privateKey: fs.readFileSync(info.ssh_key_file),
passphrase: info.ssh_key_passphrase
});
} else {
return Object.assign({}, config, {
password: info.ssh_password
});
}
};

const connectViaSsh = (info) => new Promise((resolve, reject) => {
ssh(getSshConfig(info), (err, tunnel) => {
if (err) {
reject(err);
} else {
resolve({
tunnel,
info: Object.assign({}, info, {
host: '127.0.0.1',
})
});
}
});
});
let isSshTunnel = false;

const getSslOptions = (connectionInfo) => {
if (connectionInfo.sslType === 'Off') {
Expand Down Expand Up @@ -70,14 +30,28 @@ const getSslOptions = (connectionInfo) => {
}
};

const createConnection = async (connectionInfo) => {
const createConnection = async (connectionInfo, sshService) => {
if (connectionInfo.ssh) {
const { info, tunnel } = await connectViaSsh(connectionInfo);
sshTunnel = tunnel;
connectionInfo = info;
const { options } = await sshService.openTunnel({
sshAuthMethod: connectionInfo.ssh_method === 'privateKey' ? 'IDENTITY_FILE' : 'USER_PASSWORD',
sshTunnelHostname: connectionInfo.ssh_host,
sshTunnelPort: connectionInfo.ssh_port,
sshTunnelUsername: connectionInfo.ssh_user,
sshTunnelPassword: connectionInfo.ssh_password,
sshTunnelIdentityFile: connectionInfo.ssh_key_file,
sshTunnelPassphrase: connectionInfo.ssh_key_passphrase,
host: connectionInfo.host,
port: connectionInfo.port,
});

isSshTunnel = true;
connectionInfo = {
...connectionInfo,
...options,
};
}

return await mysql.createConnection({
return mysql.createConnection({
host: connectionInfo.host,
user: connectionInfo.userName,
password: connectionInfo.userPassword,
Expand All @@ -104,12 +78,12 @@ const promisify = (f, context) => (...args) => {
});
};

const connect = async (connectionInfo) => {
const connect = async (connectionInfo, sshService) => {
if (connection) {
return connection;
}

connection = await createConnection(connectionInfo);
connection = await createConnection(connectionInfo, sshService);

return connection;
};
Expand Down Expand Up @@ -251,15 +225,15 @@ const createInstance = (connection, logger) => {
};
};

const close = () => {
const close = async (sshService) => {
if (connection) {
connection.end();
connection = null;
}

if (sshTunnel) {
sshTunnel.close();
sshTunnel = null;
if (isSshTunnel) {
await sshService.closeConsumer();
isSshTunnel = false;
}
};

Expand Down
100 changes: 13 additions & 87 deletions reverse_engineering/node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions reverse_engineering/node_modules/asn1/LICENSE

This file was deleted.

Loading