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-6139: Fix ssh tunneling #132

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
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