Skip to content

Commit

Permalink
HCK-6139: Fix ssh tunneling (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlikRakhmonov authored Jun 10, 2024
1 parent 02caffd commit eb162c6
Show file tree
Hide file tree
Showing 220 changed files with 45 additions and 39,158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ const applyToInstance = (cassandraHelper) => (connectionInfo, logger, app) => {
message: 'Cassandra script has been applied successfully!'
}, 'Cassandra script');

cassandra.close();

return;
cassandra.close(app);
}, (commonError) => {
if (!commonError.query) {
return Promise.reject(commonError);
Expand All @@ -54,7 +52,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 @@ -60,7 +60,7 @@ module.exports = {
},

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

Expand Down
121 changes: 37 additions & 84 deletions reverse_engineering/cassandraHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ 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 xmlParser = require('fast-xml-parser');
const filterComplexUdt = require('./helpers/filterComplexUdt');

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

const COLUMNS_TO_FILTER_OUT = ['solr_query'];
Expand Down Expand Up @@ -282,92 +281,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
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,
});
} else {
return Object.assign({}, config, {
password: info.ssh_password
});
}
};

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 Expand Up @@ -587,17 +550,7 @@ module.exports = (_) => {
tableOptions: getTableOptions(table)
};
};

const changeQuotes = (str) => {
return String(str).replace(/\"/g, '\'');
};

const getCompaction = (data) => {
return Object.assign({}, data.compactionOptions, {
class: data.compactionClass
});
};


const getOptionsFromTab = config => {
const optionsBlock = config.structure.find(prop => prop.propertyName === 'Options');
return optionsBlock.structure;
Expand Down
57 changes: 1 addition & 56 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.

66 changes: 0 additions & 66 deletions reverse_engineering/node_modules/bcrypt-pbkdf/LICENSE

This file was deleted.

Loading

0 comments on commit eb162c6

Please sign in to comment.