Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ?sslmode connection string param #2345

Merged
merged 3 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function parse(str) {
config.ssl = false
}

if (config.sslcert || config.sslkey || config.sslrootcert) {
if (config.sslcert || config.sslkey || config.sslrootcert || config.sslmode) {
config.ssl = {}
}

Expand All @@ -81,6 +81,23 @@ function parse(str) {
config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
}

switch (config.sslmode) {
case 'disable': {
config.ssl = false
break
}
case 'prefer':
case 'require':
case 'verify-ca':
case 'verify-full': {
break
}
case 'no-verify': {
config.ssl.rejectUnauthorized = false
break
}
}

return config
}

Expand Down
46 changes: 46 additions & 0 deletions packages/pg-connection-string/test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,52 @@ describe('parse', function () {
})
})

it('configuration parameter sslmode=no-verify', function () {
var connectionString = 'pg:///?sslmode=no-verify'
var subject = parse(connectionString)
subject.ssl.should.eql({
rejectUnauthorized: false,
})
})

it('configuration parameter sslmode=disable', function () {
var connectionString = 'pg:///?sslmode=disable'
var subject = parse(connectionString)
subject.ssl.should.eql(false)
})

it('configuration parameter sslmode=prefer', function () {
var connectionString = 'pg:///?sslmode=prefer'
var subject = parse(connectionString)
subject.ssl.should.eql({})
})

it('configuration parameter sslmode=require', function () {
var connectionString = 'pg:///?sslmode=require'
var subject = parse(connectionString)
subject.ssl.should.eql({})
})

it('configuration parameter sslmode=verify-ca', function () {
var connectionString = 'pg:///?sslmode=verify-ca'
var subject = parse(connectionString)
subject.ssl.should.eql({})
})

it('configuration parameter sslmode=verify-full', function () {
var connectionString = 'pg:///?sslmode=verify-full'
var subject = parse(connectionString)
subject.ssl.should.eql({})
})

it('configuration parameter ssl=true and sslmode=require still work with sslrootcert=/path/to/ca', function () {
var connectionString = 'pg:///?ssl=true&sslrootcert=' + __dirname + '/example.ca&sslmode=require'
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')
Expand Down