Skip to content

Commit

Permalink
Merge 75cdb08 into 6248663
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswestnz committed Oct 12, 2016
2 parents 6248663 + 75cdb08 commit 21e24f2
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 166 deletions.
13 changes: 0 additions & 13 deletions lib/config/db/couchdb-get-admins.js

This file was deleted.

45 changes: 35 additions & 10 deletions lib/config/db/couchdb-get-config.js
@@ -1,23 +1,48 @@
module.exports = getConfig

var pick = require('lodash').pick
var defaults = require('lodash').defaults

function getConfig (couch, callback) {
function getConfig (state, couch, callback) {
getCurrentConfig(couch, function (error, config) {
if (error) state.server.log(['database', 'warn'], 'Could not get CouchDB values from `/_config`. Using defaults.')

config = defaults(config, {
couch_httpd_auth: {
authentication_db: '_users'
}
})

// attempt to setAuthHandlers
setAuthHandlers(couch, function (error) {
if (error) state.server.log(['database', 'warn'], 'Could not set CouchDB values to `/_config`, hoodie may not run correctly.')

callback(null, config)
})
})
}

function getCurrentConfig (couch, callback) {
couch({
url: '/_config/couch_httpd_auth'
url: '/_config'
}, function (error, response, data) {
if (error || (response && response.statusCode !== 200)) {
return callback(new Error('Could not retrieve necessary CouchDB config values'))
return callback(new Error('There was an error loading CouchDB values from `/_config`'))
}

if (!data.secret) {
return callback(new Error('Could not retrieve CouchDB secret'))
}
callback(null, data)
})
}

if (!data.authentication_db) {
return callback(new Error('Could not retrieve CouchDB authentication database'))
function setAuthHandlers (couch, callback) {
couch({
url: '/_config/httpd/authentication_handlers',
method: 'PUT',
body: '{couch_httpd_oauth, oauth_authentication_handler},{couch_httpd_auth, default_authentication_handler},{couch_httpd_auth, cookie_authentication_handler}'
}, function (error, response, data) {
if (error || (response && response.statusCode !== 200)) {
return callback(new Error('Could not set necessary CouchDB config'))
}

callback(null, pick(data, ['secret', 'authentication_db']))
callback(null)
})
}
15 changes: 0 additions & 15 deletions lib/config/db/couchdb-set-config.js

This file was deleted.

12 changes: 4 additions & 8 deletions lib/config/db/couchdb.js
Expand Up @@ -4,10 +4,8 @@ var async = require('async')
var request = require('request')

var checkVendor = require('./couchdb-check-vendor')
var getAdmins = require('./couchdb-get-admins')
var getConfig = require('./couchdb-get-config')
var migrateToStoreDb = require('./couchdb-migrate-to-store-db')
var setConfig = require('./couchdb-set-config')
var toCouchDbUrl = require('../../utils/pouchdb-options-to-couchdb-url')

function couchDbConfig (state, callback) {
Expand All @@ -19,17 +17,15 @@ function couchDbConfig (state, callback) {
async.series([
async.apply(checkVendor, state, couch),
async.apply(migrateToStoreDb, state, couch),
async.apply(setConfig, couch),
async.apply(getConfig, couch),
async.apply(getAdmins, couch)
async.apply(getConfig, state, couch)
], function (error, results) {
if (error) {
return callback(error)
}

state.db.admins = results[4]
state.db.secret = results[3].secret
state.db.authenticationDb = results[3].authentication_db
state.db.admins = results[2].admins
state.db.secret = results[2].couch_httpd_auth.secret
state.db.authenticationDb = results[2].couch_httpd_auth.authentication_db

callback(null, state)
})
Expand Down
37 changes: 0 additions & 37 deletions test/unit/config/db-couchdb-get-admins-test.js

This file was deleted.

81 changes: 43 additions & 38 deletions test/unit/config/db-couchdb-get-config-test.js
@@ -1,54 +1,59 @@
var nock = require('nock')
var test = require('tap').test
var request = require('request')

var getConfig = require('../../../lib/config/db/couchdb-get-config.js')

var state = {
server: {
log: function () {}
}
}

var couch = request.defaults({
baseUrl: 'http://a:b@127.0.0.1:5984/',
json: true
})

test('get couch config', function (group) {
group.test('request fails', function (t) {
t.plan(2)
group.test('couchdb allows `/_config`', function (t) {
t.plan(4)

nock('http://127.0.0.1:5984')
.get('/_config')
.reply(200, {
admins: {
user: 'secret'
},
couch_httpd_auth: {
secret: 'foo',
authentication_db: '_users'
}
})

getConfig(function (input, callback) {
callback(new Error())
}, function (error) {
t.ok(error instanceof Error)
})
.put('/_config/httpd/authentication_handlers')
.reply(204)

getConfig(function (input, callback) {
callback(null, {statusCode: 500})
}, function (error) {
t.ok(error instanceof Error)
getConfig(state, couch, function (error, config) {
t.error(error)
t.is(config.admins.user, 'secret')
t.is(config.couch_httpd_auth.secret, 'foo')
t.is(config.couch_httpd_auth.authentication_db, '_users')
})
})

group.test('request succeds', function (t) {
t.plan(9)
group.test('couchdb disallows `/_config`', function (t) {
t.plan(4)

getConfig(function (input, callback) {
t.is(input.url, '/_config/couch_httpd_auth')
callback(null, null, {})
}, function (error) {
t.ok(error instanceof Error)
})

getConfig(function (input, callback) {
t.is(input.url, '/_config/couch_httpd_auth')
callback(null, null, {secret: 'foo'})
}, function (error) {
t.ok(error instanceof Error)
})
nock('http://127.0.0.1:5984')
.get('/_config')
.reply(403)

getConfig(function (input, callback) {
t.is(input.url, '/_config/couch_httpd_auth')
callback(null, null, {
secret: 'foo',
authentication_db: 'bar',
ignore: 'baz'
})
}, function (error, result) {
getConfig(state, couch, function (error, config) {
t.error(error)

t.is(result.secret, 'foo')
t.is(result.authentication_db, 'bar')
t.notOk(result.ignore)
t.is(config.admins, undefined)
t.is(config.couch_httpd_auth.secret, undefined)
t.is(config.couch_httpd_auth.authentication_db, '_users')
})
})

Expand Down
34 changes: 0 additions & 34 deletions test/unit/config/db-couchdb-set-config-test.js

This file was deleted.

19 changes: 8 additions & 11 deletions test/unit/config/db-couchdb-test.js
Expand Up @@ -6,18 +6,15 @@ test('init couchdb', function (t) {
.get('/')
.reply(200, {couchdb: 'Welcome'})

.put('/_config/httpd/authentication_handlers')
.reply(200)

.get('/_config/couch_httpd_auth')
.get('/_config')
.reply(200, {
secret: 'foo',
authentication_db: '_users'
})

.get('/_config/admins')
.reply(200, {
user: 'secret'
admins: {
user: 'secret'
},
couch_httpd_auth: {
secret: 'foo',
authentication_db: '_users'
}
})

// mocks for migration
Expand Down

0 comments on commit 21e24f2

Please sign in to comment.