From e9270e89af6c6e5b84b8c668bf206c8f9ab97a45 Mon Sep 17 00:00:00 2001 From: "Herman J. Radtke III" Date: Fri, 17 May 2019 07:38:25 -0700 Subject: [PATCH] Add support for TLS parameters in URI The connection string now supports the following parameters: - sslcert - sslkey - sslrootcert Fixes #25. --- index.js | 17 +++++++++++++++++ test/example.ca | 1 + test/example.cert | 1 + test/example.key | 1 + test/parse.js | 24 ++++++++++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 test/example.ca create mode 100644 test/example.cert create mode 100644 test/example.key diff --git a/index.js b/index.js index 0042faec..981bdcda 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/test/example.ca b/test/example.ca new file mode 100644 index 00000000..0a6dcf40 --- /dev/null +++ b/test/example.ca @@ -0,0 +1 @@ +example ca diff --git a/test/example.cert b/test/example.cert new file mode 100644 index 00000000..7693b3fe --- /dev/null +++ b/test/example.cert @@ -0,0 +1 @@ +example cert diff --git a/test/example.key b/test/example.key new file mode 100644 index 00000000..1aef9935 --- /dev/null +++ b/test/example.key @@ -0,0 +1 @@ +example key diff --git a/test/parse.js b/test/parse.js index 8ff3ee81..6632cc71 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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');