@@ -3,125 +3,125 @@ const ssh = require('tunnel-ssh');
33const pg = require ( 'pg' ) ;
44
55const getSshConfig = info => {
6- const config = {
7- username : info . ssh_user ,
8- host : info . ssh_host ,
9- port : info . ssh_port ,
10- dstHost : info . host ,
11- dstPort : info . port ,
12- localHost : '127.0.0.1' ,
13- localPort : info . port ,
14- keepAlive : true ,
15- } ;
16-
17- if ( info . ssh_method === 'privateKey' ) {
18- return Object . assign ( { } , config , {
19- privateKey : fs . readFileSync ( info . ssh_key_file ) ,
20- passphrase : info . ssh_key_passphrase ,
21- } ) ;
22- } else {
23- return Object . assign ( { } , config , {
24- password : info . ssh_password ,
25- } ) ;
26- }
6+ const config = {
7+ username : info . ssh_user ,
8+ host : info . ssh_host ,
9+ port : info . ssh_port ,
10+ dstHost : info . host ,
11+ dstPort : info . port ,
12+ localHost : '127.0.0.1' ,
13+ localPort : info . port ,
14+ keepAlive : true ,
15+ } ;
16+
17+ if ( info . ssh_method === 'privateKey' ) {
18+ return Object . assign ( { } , config , {
19+ privateKey : fs . readFileSync ( info . ssh_key_file ) ,
20+ passphrase : info . ssh_key_passphrase ,
21+ } ) ;
22+ } else {
23+ return Object . assign ( { } , config , {
24+ password : info . ssh_password ,
25+ } ) ;
26+ }
2727} ;
2828
2929const connectViaSsh = info =>
30- new Promise ( ( resolve , reject ) => {
31- ssh ( getSshConfig ( info ) , ( err , tunnel ) => {
32- if ( err ) {
33- reject ( err ) ;
34- } else {
35- resolve ( {
36- tunnel,
37- info : Object . assign ( { } , info , {
38- host : '127.0.0.1' ,
39- } ) ,
40- } ) ;
41- }
42- } ) ;
43- } ) ;
30+ new Promise ( ( resolve , reject ) => {
31+ ssh ( getSshConfig ( info ) , ( err , tunnel ) => {
32+ if ( err ) {
33+ reject ( err ) ;
34+ } else {
35+ resolve ( {
36+ tunnel,
37+ info : Object . assign ( { } , info , {
38+ host : '127.0.0.1' ,
39+ } ) ,
40+ } ) ;
41+ }
42+ } ) ;
43+ } ) ;
4444
4545const getSslOptions = ( connectionInfo , logger ) => {
46- const sslType = mapSslType ( connectionInfo . sslType ) ;
47-
48- if ( ! sslType || sslType === 'disable' ) {
49- return false ;
50- }
51-
52- if ( sslType === 'allow' ) {
53- return true ;
54- }
55-
56- let sslOptions = {
57- checkServerIdentity ( hostname , cert ) {
58- logger . info ( 'Certificate' , {
59- hostname,
60- cert : {
61- subject : cert . subject ,
62- issuer : cert . issuer ,
63- valid_from : cert . valid_from ,
64- valid_to : cert . valid_to ,
65- } ,
66- } ) ;
67- }
68- } ;
69-
70- if ( fs . existsSync ( connectionInfo . certAuthority ) ) {
71- sslOptions . ca = fs . readFileSync ( connectionInfo . certAuthority ) . toString ( ) ;
72- }
73-
74- if ( fs . existsSync ( connectionInfo . clientCert ) ) {
75- sslOptions . cert = fs . readFileSync ( connectionInfo . clientCert ) . toString ( ) ;
76- }
77-
78- if ( fs . existsSync ( connectionInfo . clientPrivateKey ) ) {
79- sslOptions . key = fs . readFileSync ( connectionInfo . clientPrivateKey ) . toString ( ) ;
80- }
81-
82- return sslOptions ;
46+ const sslType = mapSslType ( connectionInfo . sslType ) ;
47+
48+ if ( ! sslType || sslType === 'disable' ) {
49+ return false ;
50+ }
51+
52+ if ( sslType === 'allow' ) {
53+ return true ;
54+ }
55+
56+ let sslOptions = {
57+ checkServerIdentity ( hostname , cert ) {
58+ logger . info ( 'Certificate' , {
59+ hostname,
60+ cert : {
61+ subject : cert . subject ,
62+ issuer : cert . issuer ,
63+ valid_from : cert . valid_from ,
64+ valid_to : cert . valid_to ,
65+ } ,
66+ } ) ;
67+ } ,
68+ } ;
69+
70+ if ( fs . existsSync ( connectionInfo . certAuthority ) ) {
71+ sslOptions . ca = fs . readFileSync ( connectionInfo . certAuthority ) . toString ( ) ;
72+ }
73+
74+ if ( fs . existsSync ( connectionInfo . clientCert ) ) {
75+ sslOptions . cert = fs . readFileSync ( connectionInfo . clientCert ) . toString ( ) ;
76+ }
77+
78+ if ( fs . existsSync ( connectionInfo . clientPrivateKey ) ) {
79+ sslOptions . key = fs . readFileSync ( connectionInfo . clientPrivateKey ) . toString ( ) ;
80+ }
81+
82+ return sslOptions ;
8383} ;
8484
8585const mapSslType = sslType => {
86- const oldToNewSslType = {
87- Off : 'disable' ,
88- TRUST_ALL_CERTIFICATES : 'allow' ,
89- TRUST_CUSTOM_CA_SIGNED_CERTIFICATES : 'prefer' ,
90- TRUST_SERVER_CLIENT_CERTIFICATES : 'verify-full' ,
91- } ;
92-
93- return oldToNewSslType [ sslType ] || sslType ;
86+ const oldToNewSslType = {
87+ Off : 'disable' ,
88+ TRUST_ALL_CERTIFICATES : 'allow' ,
89+ TRUST_CUSTOM_CA_SIGNED_CERTIFICATES : 'prefer' ,
90+ TRUST_SERVER_CLIENT_CERTIFICATES : 'verify-full' ,
91+ } ;
92+
93+ return oldToNewSslType [ sslType ] || sslType ;
9494} ;
9595
9696const createClient = async ( connectionInfo , logger ) => {
97- let sshTunnel = null ;
98-
99- if ( connectionInfo . ssh ) {
100- const { info, tunnel } = await connectViaSsh ( connectionInfo ) ;
101- sshTunnel = tunnel ;
102- connectionInfo = info ;
103- }
104-
105- const config = {
106- host : connectionInfo . host ,
107- user : connectionInfo . userName ,
108- password : connectionInfo . userPassword ,
109- port : connectionInfo . port ,
110- keepAlive : true ,
111- ssl : getSslOptions ( connectionInfo , logger ) ,
112- connectionTimeoutMillis : Number ( connectionInfo . queryRequestTimeout ) || 60000 ,
113- query_timeout : Number ( connectionInfo . queryRequestTimeout ) || 60000 ,
114- statement_timeout : Number ( connectionInfo . queryRequestTimeout ) || 60000 ,
115- database : connectionInfo . database || connectionInfo . maintenanceDatabase ,
116- application_name : 'Hackolade' ,
117- } ;
118-
119- const client = new pg . Client ( config ) ;
120- await client . connect ( ) ;
121-
122- return { client, sshTunnel } ;
97+ let sshTunnel = null ;
98+
99+ if ( connectionInfo . ssh ) {
100+ const { info, tunnel } = await connectViaSsh ( connectionInfo ) ;
101+ sshTunnel = tunnel ;
102+ connectionInfo = info ;
103+ }
104+
105+ const config = {
106+ host : connectionInfo . host ,
107+ user : connectionInfo . userName ,
108+ password : connectionInfo . userPassword ,
109+ port : connectionInfo . port ,
110+ keepAlive : true ,
111+ ssl : getSslOptions ( connectionInfo , logger ) ,
112+ connectionTimeoutMillis : Number ( connectionInfo . queryRequestTimeout ) || 60000 ,
113+ query_timeout : Number ( connectionInfo . queryRequestTimeout ) || 60000 ,
114+ statement_timeout : Number ( connectionInfo . queryRequestTimeout ) || 60000 ,
115+ database : connectionInfo . database || connectionInfo . maintenanceDatabase ,
116+ application_name : 'Hackolade' ,
117+ } ;
118+
119+ const client = new pg . Client ( config ) ;
120+ await client . connect ( ) ;
121+
122+ return { client, sshTunnel } ;
123123} ;
124124
125125module . exports = {
126- createClient,
126+ createClient,
127127} ;
0 commit comments