Skip to content

Commit f43afc5

Browse files
authored
Adding ssl config params to Postgres URI (#6580)
* use pg-promise native pg-connection-string to parse uri instead of ParseConfigParser.js. The allows for a more felxible uri for ssl and other params * added ssl config params and others to PostgresConfigParser * forgot to add back the original client file * need to read in file at path for pfx, ca, key, and key * convert file buffer to string to be consistant with node-postgres examples
1 parent e6e5a8c commit f43afc5

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

spec/PostgresConfigParser.spec.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const parser = require('../lib/Adapters/Storage/Postgres/PostgresConfigParser');
2+
const fs = require('fs');
23

34
const queryParamTests = {
45
'a=1&b=2': { a: '1', b: '2' },
@@ -23,7 +24,7 @@ describe('PostgresConfigParser.parseQueryParams', () => {
2324
});
2425

2526
const baseURI = 'postgres://username:password@localhost:5432/db-name';
26-
27+
const testfile = fs.readFileSync('./Dockerfile').toString();
2728
const dbOptionsTest = {};
2829
dbOptionsTest[
2930
`${baseURI}?ssl=true&binary=true&application_name=app_name&fallback_application_name=f_app_name&poolSize=10`
@@ -35,9 +36,38 @@ dbOptionsTest[
3536
poolSize: 10,
3637
};
3738
dbOptionsTest[`${baseURI}?ssl=&binary=aa`] = {
38-
ssl: false,
3939
binary: false,
4040
};
41+
dbOptionsTest[
42+
`${baseURI}?ssl=true&ca=./Dockerfile&pfx=./Dockerfile&cert=./Dockerfile&key=./Dockerfile&binary=aa&passphrase=word&secureOptions=20`
43+
] = {
44+
ssl: {
45+
ca: testfile,
46+
pfx: testfile,
47+
cert: testfile,
48+
key: testfile,
49+
passphrase: 'word',
50+
secureOptions: 20,
51+
},
52+
binary: false,
53+
};
54+
dbOptionsTest[
55+
`${baseURI}?ssl=false&ca=./Dockerfile&pfx=./Dockerfile&cert=./Dockerfile&key=./Dockerfile&binary=aa`
56+
] = {
57+
ssl: { ca: testfile, pfx: testfile, cert: testfile, key: testfile },
58+
binary: false,
59+
};
60+
dbOptionsTest[`${baseURI}?rejectUnauthorized=true`] = {
61+
ssl: { rejectUnauthorized: true },
62+
};
63+
dbOptionsTest[
64+
`${baseURI}?max=5&query_timeout=100&idleTimeoutMillis=1000&keepAlive=true`
65+
] = {
66+
max: 5,
67+
query_timeout: 100,
68+
idleTimeoutMillis: 1000,
69+
keepAlive: true,
70+
};
4171

4272
describe('PostgresConfigParser.getDatabaseOptionsFromURI', () => {
4373
it('creates a db options map from a query string', () => {

src/Adapters/Storage/Postgres/PostgresConfigParser.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const url = require('url');
2-
2+
const fs = require('fs');
33
function getDatabaseOptionsFromURI(uri) {
44
const databaseOptions = {};
55

@@ -16,8 +16,44 @@ function getDatabaseOptionsFromURI(uri) {
1616
databaseOptions.user = authParts.length > 0 ? authParts[0] : '';
1717
databaseOptions.password = authParts.length > 1 ? authParts[1] : '';
1818

19-
databaseOptions.ssl =
20-
queryParams.ssl && queryParams.ssl.toLowerCase() === 'true' ? true : false;
19+
if (queryParams.ssl && queryParams.ssl.toLowerCase() === 'true') {
20+
databaseOptions.ssl = true;
21+
}
22+
23+
if (
24+
queryParams.ca ||
25+
queryParams.pfx ||
26+
queryParams.cert ||
27+
queryParams.key ||
28+
queryParams.passphrase ||
29+
queryParams.rejectUnauthorized ||
30+
queryParams.secureOptions
31+
) {
32+
databaseOptions.ssl = {};
33+
if (queryParams.ca) {
34+
databaseOptions.ssl.ca = fs.readFileSync(queryParams.ca).toString();
35+
}
36+
if (queryParams.pfx) {
37+
databaseOptions.ssl.pfx = fs.readFileSync(queryParams.pfx).toString();
38+
}
39+
if (queryParams.cert) {
40+
databaseOptions.ssl.cert = fs.readFileSync(queryParams.cert).toString();
41+
}
42+
if (queryParams.key) {
43+
databaseOptions.ssl.key = fs.readFileSync(queryParams.key).toString();
44+
}
45+
if (queryParams.passphrase) {
46+
databaseOptions.ssl.passphrase = queryParams.passphrase;
47+
}
48+
if (queryParams.rejectUnauthorized) {
49+
databaseOptions.ssl.rejectUnauthorized =
50+
queryParams.rejectUnauthorized.toLowerCase() === 'true' ? true : false;
51+
}
52+
if (queryParams.secureOptions) {
53+
databaseOptions.ssl.secureOptions = parseInt(queryParams.secureOptions);
54+
}
55+
}
56+
2157
databaseOptions.binary =
2258
queryParams.binary && queryParams.binary.toLowerCase() === 'true'
2359
? true
@@ -31,6 +67,19 @@ function getDatabaseOptionsFromURI(uri) {
3167
if (queryParams.poolSize) {
3268
databaseOptions.poolSize = parseInt(queryParams.poolSize) || 10;
3369
}
70+
if (queryParams.max) {
71+
databaseOptions.max = parseInt(queryParams.max) || 10;
72+
}
73+
if (queryParams.query_timeout) {
74+
databaseOptions.query_timeout = parseInt(queryParams.query_timeout);
75+
}
76+
if (queryParams.idleTimeoutMillis) {
77+
databaseOptions.idleTimeoutMillis = parseInt(queryParams.idleTimeoutMillis);
78+
}
79+
if (queryParams.keepAlive) {
80+
databaseOptions.keepAlive =
81+
queryParams.keepAlive.toLowerCase() === 'true' ? true : false;
82+
}
3483

3584
return databaseOptions;
3685
}

0 commit comments

Comments
 (0)