Add support for TLS parameters in URI

The connection string now supports the following parameters:

- sslcert
- sslkey
- sslrootcert

Fixes #25.
This commit is contained in:
Herman J. Radtke III 2019-05-17 07:38:25 -07:00
parent eafb7acd95
commit e9270e89af
5 changed files with 44 additions and 0 deletions

View File

@ -1,6 +1,7 @@
'use strict';
var url = require('url');
var fs = require('fs');
//Parse method copied from https://github.com/brianc/node-postgres
//Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
@ -48,6 +49,22 @@ function parse(str) {
config.ssl = true;
}
if (config.sslcert || config.sslkey || config.sslrootcert) {
config.ssl = {};
}
if (config.sslcert) {
config.ssl.cert = fs.readFileSync(config.sslcert).toString();
}
if (config.sslkey) {
config.ssl.key = fs.readFileSync(config.sslkey).toString();
}
if (config.sslrootcert) {
config.ssl.ca = fs.readFileSync(config.sslrootcert).toString();
}
return config;
}

1
test/example.ca Normal file
View File

@ -0,0 +1 @@
example ca

1
test/example.cert Normal file
View File

@ -0,0 +1 @@
example cert

1
test/example.key Normal file
View File

@ -0,0 +1 @@
example key

View File

@ -147,6 +147,30 @@ describe('parse', function(){
subject.ssl.should.equal(true);
});
it('configuration parameter sslcert=/path/to/cert', function(){
var connectionString = 'pg:///?sslcert=' + __dirname + '/example.cert';
var subject = parse(connectionString);
subject.ssl.should.eql({
cert: 'example cert\n'
});
});
it('configuration parameter sslkey=/path/to/key', function(){
var connectionString = 'pg:///?sslkey=' + __dirname + '/example.key';
var subject = parse(connectionString);
subject.ssl.should.eql({
key: 'example key\n'
});
});
it('configuration parameter sslrootcert=/path/to/ca', function(){
var connectionString = 'pg:///?sslrootcert=' + __dirname + '/example.ca';
var subject = parse(connectionString);
subject.ssl.should.eql({
ca: 'example ca\n'
});
});
it('allow other params like max, ...', function () {
var subject = parse('pg://myhost/db?max=18&min=4');
subject.max.should.equal('18');