Skip to content

Commit

Permalink
Now checking the status of /_config instead of relying on the vendor …
Browse files Browse the repository at this point in the history
…details
  • Loading branch information
jameswestnz committed Oct 12, 2016
1 parent b62499a commit 35510ba
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 311 deletions.
30 changes: 30 additions & 0 deletions lib/config/db/couchdb-check-vendor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = checkVendor

var findKey = require('lodash').findKey

var toCouchDbUrl = require('../../utils/pouchdb-options-to-couchdb-url')
var removeAuth = require('../../utils/remove-auth-from-url')

function checkVendor (state, couch, callback) {
couch({url: '/'}, function (error, response, data) {
if (error || (response && response.statusCode !== 200)) {
var url = toCouchDbUrl(state.db.options)
return callback(new Error('Could not find CouchDB at ' + removeAuth(url)))
}

var vendor = findKey(data, function (property) {
return /^welcome/i.test(property)
})

if (vendor !== 'couchdb') {
state.server.log(
['database', 'warn'],
'You are not running an official CouchDB distribution, ' +
'but "' + vendor + '". ' +
'This might not be fully supported. Proceed at your own risk.'
)
}

callback(null)
})
}
13 changes: 0 additions & 13 deletions lib/config/db/couchdb-get-admins.js

This file was deleted.

47 changes: 37 additions & 10 deletions lib/config/db/couchdb-get-config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
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, {
admins: undefined,
couch_httpd_auth: {
secret: null,
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)
})
}
49 changes: 0 additions & 49 deletions lib/config/db/couchdb-get-vendor.js

This file was deleted.

15 changes: 0 additions & 15 deletions lib/config/db/couchdb-set-config.js

This file was deleted.

42 changes: 10 additions & 32 deletions lib/config/db/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module.exports = couchDbConfig
var async = require('async')
var request = require('request')

var getVendor = require('./couchdb-get-vendor')
var getAdmins = require('./couchdb-get-admins')
var checkVendor = require('./couchdb-check-vendor')
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 @@ -16,39 +14,19 @@ function couchDbConfig (state, callback) {
json: true
})

getVendor(state, couch, function (error, vendor) {
async.series([
async.apply(checkVendor, state, couch),
async.apply(migrateToStoreDb, state, couch),
async.apply(getConfig, state, couch)
], function (error, results) {
if (error) {
return callback(error)
}

var tasks = [
async.apply(migrateToStoreDb, state, couch)
]
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

if (vendor.type === 'couchdb') {
tasks = tasks.concat([
async.apply(setConfig, couch),
async.apply(getConfig, couch),
async.apply(getAdmins, couch)
])
}

async.series(tasks, function (error, results) {
if (error) {
return callback(error)
}

if (vendor.type === 'couchdb') {
state.db.admins = results[3]
state.db.secret = results[2].secret
state.db.authenticationDb = results[2].authentication_db
}

if (vendor.type === 'cloudant') {
state.db.authenticationDb = '_users'
}

callback(null, state)
})
callback(null, state)
})
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var simple = require('simple-mock')
var test = require('tap').test

var getVendor = require('../../../lib/config/db/couchdb-get-vendor.js')
var checkVendor = require('../../../lib/config/db/couchdb-check-vendor.js')

test('check couch vendor', function (group) {
group.test('request fails', function (t) {
t.plan(2)

getVendor({
checkVendor({
db: {
options: {
prefix: 'http://localhost:5984'
Expand All @@ -19,7 +19,7 @@ test('check couch vendor', function (group) {
t.match(error.message, 'http://localhost:5984')
})

getVendor({
checkVendor({
db: {
options: {
prefix: 'http://localhost:5984'
Expand All @@ -33,24 +33,24 @@ test('check couch vendor', function (group) {
})

group.test('verify vendor', function (t) {
t.plan(2)
t.plan(3)

var logStub = simple.stub()

getVendor({
checkVendor({
server: {
log: logStub
}
}, function (input, callback) {
callback(null, {
statusCode: 200
}, {
'couchdb': 'Welcome'
'<% VENDOR %>': 'Welcome!'
})
}, function (error, vendor) {
t.error(error)
t.is(typeof vendor, 'object')
})

t.is(logStub.callCount, 1)
t.match(logStub.lastCall.args[1], /<% VENDOR %>/)
}, t.error)
})

group.end()
Expand Down
37 changes: 0 additions & 37 deletions test/unit/config/db-couchdb-get-admins-test.js

This file was deleted.

Loading

0 comments on commit 35510ba

Please sign in to comment.