From 1bef9e0aa26e15921d48205335a32565b255a6da Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 23 Jul 2015 12:46:14 -0700 Subject: [PATCH] fix(events): require events to be configured in production While abrupt, forgetting to delete user information is a big deal, so this will prevent the server from running as long as the event queue is not setup. BREAKING CHANGE: Server will fail to start up if `config.events` is not set with values when in production. --- lib/config.js | 1 - lib/env.js | 10 ++++++++++ lib/events.js | 11 ++++++++++- lib/server/index.js | 3 ++- lib/server/internal.js | 3 ++- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lib/env.js diff --git a/lib/config.js b/lib/config.js index e2f91d2..e75b46d 100644 --- a/lib/config.js +++ b/lib/config.js @@ -208,5 +208,4 @@ var options = { conf.validate(options); - module.exports = conf; diff --git a/lib/env.js b/lib/env.js new file mode 100644 index 0000000..f46c879 --- /dev/null +++ b/lib/env.js @@ -0,0 +1,10 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const config = require('./config'); + +exports.isProdLike = function isProdLike() { + var env = config.get('env'); + return env === 'prod' || env === 'stage'; +}; diff --git a/lib/events.js b/lib/events.js index 1abe656..a6a15db 100644 --- a/lib/events.js +++ b/lib/events.js @@ -4,6 +4,7 @@ const config = require('./config').root(); const db = require('./db'); +const env = require('./env'); const logger = require('./logging')('events'); const Sink = require('fxa-notifier-aws').Sink; const HEX_STRING = require('./validators').HEX_STRING; @@ -12,12 +13,19 @@ var fxaEvents; if (!config.events.region || !config.events.queueUrl) { fxaEvents = { - start: function () { logger.warn('accountEvent.unconfigured'); } + start: function start() { + if (env.isProdLike()) { + throw new Error('config.events must be included in prod'); + } else { + logger.warn('accountEvent.unconfigured'); + } + } }; } else { fxaEvents = new Sink(config.events.region, config.events.queueUrl); fxaEvents.on('data', function (message) { + logger.verbose('data', message); if (message.event === 'delete') { var userId = message.uid.split('@')[0]; if (!HEX_STRING.test(userId)) { @@ -26,6 +34,7 @@ if (!config.events.region || !config.events.queueUrl) { } db.removeUser(userId) .done(function () { + logger.info('delete', { uid: userId }); message.del(); }, function (err) { diff --git a/lib/server/index.js b/lib/server/index.js index 3184e65..4f8a304 100644 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -6,6 +6,7 @@ const Hapi = require('hapi'); const AppError = require('../error'); const config = require('../config').root(); +const env = require('../env'); const logger = require('../logging')('server'); const hapiLogger = require('../logging')('server.hapi'); const summary = require('../logging/summary'); @@ -18,7 +19,7 @@ exports.create = function createServer() { logger.warn('localRedirect', '*** localRedirects is set to TRUE. Should only be used for developers.'); } - var isProd = config.env === 'prod'; + var isProd = env.isProdLike(); var server = Hapi.createServer( config.server.host, config.server.port, diff --git a/lib/server/internal.js b/lib/server/internal.js index c4dbe7f..0d7f183 100644 --- a/lib/server/internal.js +++ b/lib/server/internal.js @@ -7,12 +7,13 @@ const Hapi = require('hapi'); const AppError = require('../error'); const auth = require('../auth'); const config = require('../config').root(); +const env = require('../env'); const logger = require('../logging')('server.clients'); const hapiLogger = require('../logging')('server.hapi'); const summary = require('../logging/summary'); exports.create = function createServer() { - var isProd = config.env === 'prod'; + var isProd = env.isProdLike(); var server = Hapi.createServer( config.serverInternal.host, config.serverInternal.port,