2023-04-03 08:18:01 -07:00
|
|
|
import type { Server } from 'net';
|
2023-08-09 04:30:53 -07:00
|
|
|
import { createServer } from 'net';
|
2023-04-03 08:18:01 -07:00
|
|
|
import { Client } from 'ssh2';
|
|
|
|
import type { ConnectConfig } from 'ssh2';
|
|
|
|
|
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
|
|
|
|
|
|
|
import pgPromise from 'pg-promise';
|
2024-04-09 08:41:51 -07:00
|
|
|
import type {
|
|
|
|
PgpDatabase,
|
|
|
|
PostgresNodeCredentials,
|
|
|
|
PostgresNodeOptions,
|
|
|
|
} from '../helpers/interfaces';
|
2023-08-09 04:30:53 -07:00
|
|
|
import { formatPrivateKey } from '@utils/utilities';
|
2023-04-03 08:18:01 -07:00
|
|
|
|
2024-04-09 08:41:51 -07:00
|
|
|
async function createSshConnectConfig(credentials: PostgresNodeCredentials) {
|
2023-04-03 08:18:01 -07:00
|
|
|
if (credentials.sshAuthenticateWith === 'password') {
|
|
|
|
return {
|
|
|
|
host: credentials.sshHost as string,
|
|
|
|
port: credentials.sshPort as number,
|
|
|
|
username: credentials.sshUser as string,
|
|
|
|
password: credentials.sshPassword as string,
|
|
|
|
} as ConnectConfig;
|
|
|
|
} else {
|
|
|
|
const options: ConnectConfig = {
|
2023-08-09 04:30:53 -07:00
|
|
|
host: credentials.sshHost as string,
|
|
|
|
username: credentials.sshUser as string,
|
|
|
|
port: credentials.sshPort as number,
|
|
|
|
privateKey: formatPrivateKey(credentials.privateKey as string),
|
2023-04-03 08:18:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (credentials.passphrase) {
|
2024-04-09 08:41:51 -07:00
|
|
|
options.passphrase = credentials.passphrase;
|
2023-04-03 08:18:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 06:42:24 -07:00
|
|
|
export async function configurePostgres(
|
2024-04-09 08:41:51 -07:00
|
|
|
credentials: PostgresNodeCredentials,
|
|
|
|
options: PostgresNodeOptions = {},
|
2023-04-03 08:18:01 -07:00
|
|
|
createdSshClient?: Client,
|
|
|
|
) {
|
2023-05-19 06:42:24 -07:00
|
|
|
const pgp = pgPromise({
|
|
|
|
// prevent spam in console "WARNING: Creating a duplicate database object for the same connection."
|
|
|
|
// duplicate connections created when auto loading parameters, they are closed imidiatly after, but several could be open at the same time
|
|
|
|
noWarnings: true,
|
|
|
|
});
|
2023-04-03 08:18:01 -07:00
|
|
|
|
2023-05-04 08:25:54 -07:00
|
|
|
if (typeof options.nodeVersion === 'number' && options.nodeVersion >= 2.1) {
|
|
|
|
// Always return dates as ISO strings
|
|
|
|
[pgp.pg.types.builtins.TIMESTAMP, pgp.pg.types.builtins.TIMESTAMPTZ].forEach((type) => {
|
|
|
|
pgp.pg.types.setTypeParser(type, (value: string) => {
|
|
|
|
return new Date(value).toISOString();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:18:01 -07:00
|
|
|
if (options.largeNumbersOutput === 'numbers') {
|
|
|
|
pgp.pg.types.setTypeParser(20, (value: string) => {
|
|
|
|
return parseInt(value, 10);
|
|
|
|
});
|
|
|
|
pgp.pg.types.setTypeParser(1700, (value: string) => {
|
|
|
|
return parseFloat(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbConfig: IDataObject = {
|
2024-04-09 08:41:51 -07:00
|
|
|
host: credentials.host,
|
|
|
|
port: credentials.port,
|
|
|
|
database: credentials.database,
|
|
|
|
user: credentials.user,
|
|
|
|
password: credentials.password,
|
|
|
|
keepAlive: true,
|
2023-04-03 08:18:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options.connectionTimeout) {
|
2024-04-09 08:41:51 -07:00
|
|
|
dbConfig.connectionTimeoutMillis = options.connectionTimeout * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.delayClosingIdleConnection) {
|
|
|
|
dbConfig.keepAliveInitialDelayMillis = options.delayClosingIdleConnection * 1000;
|
2023-04-03 08:18:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (credentials.allowUnauthorizedCerts === true) {
|
|
|
|
dbConfig.ssl = {
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
dbConfig.ssl = !['disable', undefined].includes(credentials.ssl as string | undefined);
|
2024-04-09 08:41:51 -07:00
|
|
|
dbConfig.sslmode = credentials.ssl || 'disable';
|
2023-04-03 08:18:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!credentials.sshTunnel) {
|
|
|
|
const db = pgp(dbConfig);
|
|
|
|
return { db, pgp };
|
|
|
|
} else {
|
|
|
|
const sshClient = createdSshClient || new Client();
|
|
|
|
|
|
|
|
const tunnelConfig = await createSshConnectConfig(credentials);
|
|
|
|
|
|
|
|
const localHost = '127.0.0.1';
|
|
|
|
const localPort = credentials.sshPostgresPort as number;
|
|
|
|
|
|
|
|
let proxy: Server | undefined;
|
|
|
|
|
|
|
|
const db = await new Promise<PgpDatabase>((resolve, reject) => {
|
|
|
|
let sshClientReady = false;
|
|
|
|
|
|
|
|
proxy = createServer((socket) => {
|
|
|
|
if (!sshClientReady) return socket.destroy();
|
|
|
|
|
|
|
|
sshClient.forwardOut(
|
|
|
|
socket.remoteAddress as string,
|
|
|
|
socket.remotePort as number,
|
2024-04-09 08:41:51 -07:00
|
|
|
credentials.host,
|
|
|
|
credentials.port,
|
2023-04-03 08:18:01 -07:00
|
|
|
(err, stream) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
|
|
|
|
socket.pipe(stream);
|
|
|
|
stream.pipe(socket);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}).listen(localPort, localHost);
|
|
|
|
|
|
|
|
proxy.on('error', (err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
sshClient.connect(tunnelConfig);
|
|
|
|
|
|
|
|
sshClient.on('ready', () => {
|
|
|
|
sshClientReady = true;
|
|
|
|
|
|
|
|
const updatedDbConfig = {
|
|
|
|
...dbConfig,
|
|
|
|
port: localPort,
|
|
|
|
host: localHost,
|
|
|
|
};
|
|
|
|
const dbConnection = pgp(updatedDbConfig);
|
|
|
|
resolve(dbConnection);
|
|
|
|
});
|
|
|
|
|
|
|
|
sshClient.on('error', (err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
sshClient.on('end', async () => {
|
|
|
|
if (proxy) proxy.close();
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
if (proxy) proxy.close();
|
|
|
|
if (sshClient) sshClient.end();
|
|
|
|
|
|
|
|
let message = err.message;
|
|
|
|
let description = err.description;
|
|
|
|
|
|
|
|
if (err.message.includes('ECONNREFUSED')) {
|
|
|
|
message = 'Connection refused';
|
|
|
|
try {
|
|
|
|
description = err.message.split('ECONNREFUSED ')[1].trim();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.message.includes('ENOTFOUND')) {
|
|
|
|
message = 'Host not found';
|
|
|
|
try {
|
|
|
|
description = err.message.split('ENOTFOUND ')[1].trim();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.message.includes('ETIMEDOUT')) {
|
|
|
|
message = 'Connection timed out';
|
|
|
|
try {
|
|
|
|
description = err.message.split('ETIMEDOUT ')[1].trim();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
err.message = message;
|
|
|
|
err.description = description;
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
return { db, pgp, sshClient };
|
|
|
|
}
|
|
|
|
}
|