Use req/res logger instead of the one bound to global object

This commit is contained in:
Daniel García Aubert 2020-06-02 12:31:18 +02:00
parent ffe19827fd
commit b60116410a
5 changed files with 34 additions and 32 deletions

View File

@ -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,

View File

@ -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) => {

View File

@ -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 || {};

View File

@ -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];
};

View File

@ -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;