diff --git a/lib/api/map/anonymous-map-controller.js b/lib/api/map/anonymous-map-controller.js index 85e9860a..7236935b 100644 --- a/lib/api/map/anonymous-map-controller.js +++ b/lib/api/map/anonymous-map-controller.js @@ -152,6 +152,7 @@ function prepareAdapterMapConfig (mapConfigAdapter) { return function prepareAdapterMapConfigMiddleware (req, res, next) { const requestMapConfig = req.body; + const { logger } = res.locals; const { user, api_key: apiKey } = res.locals; const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals; const params = Object.assign({ dbuser, dbname, dbpassword, dbhost, dbport }, req.query); @@ -159,6 +160,7 @@ function prepareAdapterMapConfig (mapConfigAdapter) { const context = { analysisConfiguration: { user, + logger, db: { host: dbhost, port: dbport, diff --git a/lib/api/middlewares/user.js b/lib/api/middlewares/user.js index ce8c73bf..31e7f909 100644 --- a/lib/api/middlewares/user.js +++ b/lib/api/middlewares/user.js @@ -3,9 +3,10 @@ const CdbRequest = require('../../models/cdb-request'); module.exports = function user (metadataBackend) { - const cdbRequest = new CdbRequest(); - return function userMiddleware (req, res, next) { + const { logger } = res.locals; + const cdbRequest = new CdbRequest({ logger }); + res.locals.user = getUserNameFromRequest(req, cdbRequest); metadataBackend.getUserId(res.locals.user, (err, userId) => { diff --git a/lib/backends/analysis.js b/lib/backends/analysis.js index 2844a028..376e12bb 100644 --- a/lib/backends/analysis.js +++ b/lib/backends/analysis.js @@ -30,8 +30,6 @@ AnalysisBackend.prototype.create = function (analysisConfiguration, analysisDefi analysisConfiguration.batch.inlineExecution = this.batchConfig.inlineExecution; analysisConfiguration.batch.hostHeaderTemplate = this.batchConfig.hostHeaderTemplate; - analysisConfiguration.logger = global.logger; - this.getAnalysesLimits(analysisConfiguration.user, function (err, limits) { if (err) {} analysisConfiguration.limits = limits || {}; diff --git a/lib/models/cdb-request.js b/lib/models/cdb-request.js index 54b3db5c..a962b558 100644 --- a/lib/models/cdb-request.js +++ b/lib/models/cdb-request.js @@ -1,29 +1,29 @@ 'use strict'; -function CdbRequest () { - this.RE_USER_FROM_HOST = new RegExp(global.environment.user_from_host || - '^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com" - ); -} - -module.exports = CdbRequest; - -CdbRequest.prototype.userByReq = function (req) { - var host = req.headers.host || ''; - - if (req.params.user) { - return req.params.user; +module.exports = class CdbRequest { + constructor ({ logger }) { + this.logger = logger; + // would extract "strk" from "strk.cartodb.com" + this.RE_USER_FROM_HOST = new RegExp(global.environment.user_from_host || '^([^\\.]+)\\.'); } - var mat = host.match(this.RE_USER_FROM_HOST); + userByReq (req) { + const host = req.headers.host || ''; - if (!mat) { - return global.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' does not match hostname '${host}'`)); + if (req.params.user) { + return req.params.user; + } + + const mat = host.match(this.RE_USER_FROM_HOST); + + if (!mat) { + return this.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' does not match hostname '${host}'`)); + } + + if (mat.length !== 2) { + return this.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' gave unexpected matches against '${host}': ${mat}`)); + } + + return mat[1]; } - - if (mat.length !== 2) { - return global.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' gave unexpected matches against '${host}': ${mat}`)); - } - - return mat[1]; }; diff --git a/test/unit/cdb-request-test.js b/test/unit/cdb-request-test.js index 3e1880f7..9db38488 100644 --- a/test/unit/cdb-request-test.js +++ b/test/unit/cdb-request-test.js @@ -4,6 +4,7 @@ require('../support/test-helper'); var assert = require('assert'); var CdbRequest = require('../../lib/models/cdb-request'); +const { logger } = require('../../lib/server-options'); describe('req2params', function () { function createRequest (host, userParam) { @@ -22,7 +23,7 @@ describe('req2params', function () { } it('extracts name from host header', function () { - var cdbRequest = new CdbRequest(); + var cdbRequest = new CdbRequest({ logger }); var user = cdbRequest.userByReq(createRequest('localhost')); assert.strictEqual(user, 'localhost'); @@ -32,7 +33,7 @@ describe('req2params', function () { var userFromHostConfig = global.environment.user_from_host; global.environment.user_from_host = null; - var cdbRequest = new CdbRequest(); + var cdbRequest = new CdbRequest({ logger }); var user = cdbRequest.userByReq(createRequest('development.localhost.lan')); global.environment.user_from_host = userFromHostConfig; @@ -41,7 +42,7 @@ describe('req2params', function () { }); it('considers user param before headers', function () { - var cdbRequest = new CdbRequest(); + var cdbRequest = new CdbRequest({ logger }); var user = cdbRequest.userByReq(createRequest('localhost', 'development')); assert.strictEqual(user, 'development'); @@ -51,7 +52,7 @@ describe('req2params', function () { var userFromHostConfig = global.environment.user_from_host; global.environment.user_from_host = null; - var cdbRequest = new CdbRequest(); + var cdbRequest = new CdbRequest({ logger }); var user = cdbRequest.userByReq(createRequest('localhost')); global.environment.user_from_host = userFromHostConfig; @@ -63,7 +64,7 @@ describe('req2params', function () { var userFromHostConfig = global.environment.user_from_host; global.environment.user_from_host = null; - var cdbRequest = new CdbRequest(); + var cdbRequest = new CdbRequest({ logger }); var user = cdbRequest.userByReq(createRequest(undefined)); global.environment.user_from_host = userFromHostConfig; @@ -75,7 +76,7 @@ describe('req2params', function () { var userFromHostConfig = global.environment.user_from_host; global.environment.user_from_host = null; - var cdbRequest = new CdbRequest(); + var cdbRequest = new CdbRequest({ logger }); var user = cdbRequest.userByReq(createRequest(null)); global.environment.user_from_host = userFromHostConfig;