Skip to content

Commit

Permalink
Merge pull request #1 from lenchvolodymyr/fix/connection-via-ssl
Browse files Browse the repository at this point in the history
fix connection via ssl
  • Loading branch information
lenchvolodymyr authored Nov 9, 2021
2 parents 887d38f + e1d1b2f commit 3bc2d55
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 236 deletions.
44 changes: 30 additions & 14 deletions reverse_engineering/helpers/connectionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,44 @@ const connectViaSsh = info =>
});
});

const getSslOptions = connectionInfo => {
const getSslOptions = (connectionInfo, logger) => {
const sslType = mapSslType(connectionInfo.sslType);

if (sslType === 'disable') {
return false;
}

if (sslType === 'allow') {
true;
return true;
}

if (['prefer', 'require', 'verify-ca', 'verify-full'].includes(sslType)) {
return {
ca: fs.existsSync(connectionInfo.certAuthority)
? fs.readFileSync(connectionInfo.certAuthority).toString()
: '',
cert: fs.existsSync(connectionInfo.clientCert) ? fs.readFileSync(connectionInfo.clientCert).toString() : '',
key: fs.existsSync(connectionInfo.clientPrivateKey)
? fs.readFileSync(connectionInfo.clientPrivateKey).toString()
: '',
};
let sslOptions = {
checkServerIdentity(hostname, cert) {
logger.info('Certificate', {
hostname,
cert: {
subject: cert.subject,
issuer: cert.issuer,
valid_from: cert.valid_from,
valid_to: cert.valid_to,
},
});
}
};

if (fs.existsSync(connectionInfo.certAuthority)) {
sslOptions.ca = fs.readFileSync(connectionInfo.certAuthority).toString();
}

if (fs.existsSync(connectionInfo.clientCert)) {
sslOptions.cert = fs.readFileSync(connectionInfo.clientCert).toString();
}

if (fs.existsSync(connectionInfo.clientPrivateKey)) {
sslOptions.key = fs.readFileSync(connectionInfo.clientPrivateKey).toString();
}

return sslOptions;
};

const mapSslType = sslType => {
Expand All @@ -77,7 +93,7 @@ const mapSslType = sslType => {
return oldToNewSslType[sslType] || sslType;
};

const createClient = async connectionInfo => {
const createClient = async (connectionInfo, logger) => {
let sshTunnel = null;

if (connectionInfo.ssh) {
Expand All @@ -92,7 +108,7 @@ const createClient = async connectionInfo => {
password: connectionInfo.userPassword,
port: connectionInfo.port,
keepAlive: true,
ssl: getSslOptions(connectionInfo),
ssl: getSslOptions(connectionInfo, logger),
connectionTimeoutMillis: Number(connectionInfo.queryRequestTimeout) || 60000,
query_timeout: Number(connectionInfo.queryRequestTimeout) || 60000,
statement_timeout: Number(connectionInfo.queryRequestTimeout) || 60000,
Expand Down
2 changes: 1 addition & 1 deletion reverse_engineering/helpers/postgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = {
await this.disconnect();
}

const { client, sshTunnel } = await createClient(connectionInfo);
const { client, sshTunnel } = await createClient(connectionInfo, specificLogger);

db.initializeClient(client, specificLogger);
currentSshTunnel = sshTunnel;
Expand Down
221 changes: 0 additions & 221 deletions reverse_engineering/package-lock.json

This file was deleted.

0 comments on commit 3bc2d55

Please sign in to comment.