Skip to content

Commit e9270e8

Browse files
committed
Add support for TLS parameters in URI
The connection string now supports the following parameters: - sslcert - sslkey - sslrootcert Fixes iceddev#25.
1 parent eafb7ac commit e9270e8

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var url = require('url');
4+
var fs = require('fs');
45

56
//Parse method copied from https://github.com/brianc/node-postgres
67
//Copyright (c) 2010-2014 Brian Carlson ([email protected])
@@ -48,6 +49,22 @@ function parse(str) {
4849
config.ssl = true;
4950
}
5051

52+
if (config.sslcert || config.sslkey || config.sslrootcert) {
53+
config.ssl = {};
54+
}
55+
56+
if (config.sslcert) {
57+
config.ssl.cert = fs.readFileSync(config.sslcert).toString();
58+
}
59+
60+
if (config.sslkey) {
61+
config.ssl.key = fs.readFileSync(config.sslkey).toString();
62+
}
63+
64+
if (config.sslrootcert) {
65+
config.ssl.ca = fs.readFileSync(config.sslrootcert).toString();
66+
}
67+
5168
return config;
5269
}
5370

test/example.ca

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example ca

test/example.cert

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example cert

test/example.key

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example key

test/parse.js

+24
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,30 @@ describe('parse', function(){
147147
subject.ssl.should.equal(true);
148148
});
149149

150+
it('configuration parameter sslcert=/path/to/cert', function(){
151+
var connectionString = 'pg:///?sslcert=' + __dirname + '/example.cert';
152+
var subject = parse(connectionString);
153+
subject.ssl.should.eql({
154+
cert: 'example cert\n'
155+
});
156+
});
157+
158+
it('configuration parameter sslkey=/path/to/key', function(){
159+
var connectionString = 'pg:///?sslkey=' + __dirname + '/example.key';
160+
var subject = parse(connectionString);
161+
subject.ssl.should.eql({
162+
key: 'example key\n'
163+
});
164+
});
165+
166+
it('configuration parameter sslrootcert=/path/to/ca', function(){
167+
var connectionString = 'pg:///?sslrootcert=' + __dirname + '/example.ca';
168+
var subject = parse(connectionString);
169+
subject.ssl.should.eql({
170+
ca: 'example ca\n'
171+
});
172+
});
173+
150174
it('allow other params like max, ...', function () {
151175
var subject = parse('pg://myhost/db?max=18&min=4');
152176
subject.max.should.equal('18');

0 commit comments

Comments
 (0)