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-6146: Fix ssh tunneling #59

Merged
merged 1 commit into from
Jun 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const applyToInstance = (cassandraHelper) => (connectionInfo, logger, app) => {
message: 'Cassandra script has been applied successfully!'
}, 'Cassandra script');

cassandra.close();
cassandra.close(app);

return;
}, (commonError) => {
Expand All @@ -50,7 +50,7 @@ const applyToInstance = (cassandraHelper) => (connectionInfo, logger, app) => {
return Promise.reject(preparedError);
})
.catch(err => {
cassandra.close();
cassandra.close(app);

return Promise.reject(cassandra.prepareError(err));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module.exports = (cassandraHelper) => (connectionInfo, app) => {

return cassandra.connect(app)(connectionInfo)
.then(() => {
cassandra.close();
cassandra.close(app);
}, (err) => {
cassandra.close();
cassandra.close(app);
return Promise.reject(cassandra.prepareError(err));
});
};
2 changes: 1 addition & 1 deletion reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
},

disconnect: function(connectionInfo, cb, app){
cassandraHelper(app.require('lodash')).close();
cassandraHelper(app.require('lodash')).close(app);
cb();
},

Expand Down
109 changes: 36 additions & 73 deletions reverse_engineering/cassandraHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ const cassandra = require('cassandra-driver');
const typesHelper = require('./typesHelper');
let _;
const fs = require('fs');
const ssh = require('tunnel-ssh');
const { createTableOptionsFromMeta } = require('./helpers/createTableOptionsFromMeta');
const { getEntityLevelConfig } = require('../forward_engineering/helpers/generalHelper');
const CassandraRetryPolicy = require('./cassandraRetryPolicy');
const filterComplexUdt = require('./helpers/filterComplexUdt');

var state = {
const state = {
client: null,
sshTunnel: null,
isSshTunnel: false,
};

module.exports = (_) => {
Expand Down Expand Up @@ -279,92 +278,56 @@ module.exports = (_) => {
}
};

const getSshConfig = (info) => {
if (!Array.isArray(info.hosts)) {
throw new Error('Hosts were not defined');
const connect = (app, logger) => async (info) => {
if (state.client) {
return state.client.connect();
}

const host = info.hosts[0];
const config = {
username: info.ssh_user,
host: info.ssh_host,
port: info.ssh_port,
dstHost: host.host,
dstPort: host.port,
localHost: '127.0.0.1',
localPort: host.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
if (info.ssh) {
const sshService = app.require('@hackolade/ssh-service');
const host = info.hosts[0];

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

const connectViaSsh = (info) => new Promise((resolve, reject) => {
if (!info.ssh) {
return resolve({
tunnel: null,
info
});
state.isSshTunnel = true;
info = {
...info,
hosts: [options],
};
}

ssh(getSshConfig(info), (err, tunnel) => {
if (err) {
reject(err);
} else {
resolve({
tunnel,
info: Object.assign({}, info, {
hosts: info.hosts.map(host => ({
...host,
host: '127.0.0.1'
})),
})
});
}
});
});

const connect = (app, logger) => (info) => {
if (!state.client) {
return connectViaSsh(info).then(({ info, tunnel }) => {
state.sshTunnel = tunnel;
state.client = await getClient(app, info, logger);

return getClient(app, info, logger);
})
.then((client) => {
state.client = client;
state.client.on('log', (type, name, info, furtherInfo) => {
if (logger) {
const message = '[' + type + '] ' + name + ': ' + info + '. ' + furtherInfo;
logger.log('info', { message }, 'Cassandra Info');
}
});

client.on('log', (type, name, info, furtherInfo) => {
if (logger) {
logger.log('info', { message: '[' + type + '] ' + name + ': ' + info + '. ' + furtherInfo }, 'Cassandra Info');
}
});

return state.client.connect();
});
}

return state.client.connect();
};

const close = () => {
const close = async (app) => {
if (state.client) {
state.client.shutdown();
state.client = null;
}

if (state.sshTunnel) {
state.sshTunnel.close();
state.sshTunnel = null;
if (state.isSshTunnel) {
const sshService = app.require('@hackolade/ssh-service');
await sshService.closeConsumer();
state.isSshTunnel = false;
}
};

Expand Down
96 changes: 13 additions & 83 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.

13 changes: 0 additions & 13 deletions reverse_engineering/node_modules/bcrypt-pbkdf/CONTRIBUTING.md

This file was deleted.

Loading