From c17af23a4068e86ba7086aa681b6b1de268bfbd9 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 4 Feb 2015 19:31:20 +0100 Subject: [PATCH] A non empty datasource from MapConfigNamedLayersAdapter.getLayers means the affected tables can have private tables involved. That implies QueryTablesApi will need the proper user to use CDB_QueryTables. So we store it in a request context to use it in the afterLayergroupCreate call. Tiles for these layergroups will fail to add a X-Cache-Channel header because it won't be possible to use the proper user within those tiles. Ok, they will fail if they are not requested through the same tiler instance because if they are they most likely will reuse the in memory cache. See https://github.com/CartoDB/Windshaft-cartodb/issues/253 --- lib/cartodb/server_options.js | 43 +++++++++++++++++++++++++++++++-- test/acceptance/named_layers.js | 9 ++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index 948349ba..43f33bba 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -312,6 +312,12 @@ module.exports = function(redisPool) { return callback(err); } + if (!datasource.isEmpty()) { + setContext(req, 'queryTablesApiDatasource', _.find(datasource.layersDbParams, function(layerDbParams) { + return !!layerDbParams; + })); + } + requestMapConfig.layers = layers; return callback(null, requestMapConfig, datasource) }); @@ -366,14 +372,16 @@ module.exports = function(redisPool) { Step( function getAffectedTablesAndLastUpdatedTime() { - queryTablesApi.getAffectedTablesAndLastUpdatedTime(usr, { + var queryTablesOpts = { user: req.params.dbuser, pass: req.params.dbpass, host: req.params.dbhost, port: req.params.dbport, dbname: req.params.dbname, api_key: key - }, sql, this); + }; + _.extend(queryTablesOpts, getContext(req, 'queryTablesApiDatasource')); + queryTablesApi.getAffectedTablesAndLastUpdatedTime(usr, queryTablesOpts, sql, this); }, function handleAffectedTablesAndLastUpdatedTime(err, result) { if (req.profiler) req.profiler.done('queryTablesAndLastUpdated'); @@ -873,5 +881,36 @@ module.exports = function(redisPool) { ); }; + /******************************************************************************************************************* + * Private methods + ******************************************************************************************************************/ + + /** + * Handles context for a given Request object + * @param {Object|IncomingMessage} req + * @param {String} key + * @returns {*} + */ + function getContext(req, key) { + return req.context && req.context[key]; + } + + /** + * Handles context for a given Request object + * @param {Object|IncomingMessage} req + * @param {String} key + * @param {*} value + * @returns {*} The previous value + */ + function setContext(req, key, value) { + var previousValue; + if (value) { + req.context = req.context || {}; + previousValue = req.context[key]; + req.context[key] = value; + } + return previousValue; + } + return me; }; diff --git a/test/acceptance/named_layers.js b/test/acceptance/named_layers.js index 2d4a918c..f1a67528 100644 --- a/test/acceptance/named_layers.js +++ b/test/acceptance/named_layers.js @@ -422,10 +422,17 @@ suite('named_layers', function() { } }, function(res, err) { - next(err); + next(err, res); } ); }, + function handleTileResponse(err, res) { + if (err) { + throw err; + } + //assert.ok(res.headers['X-Cache-Channel']); -> https://github.com/CartoDB/Windshaft-cartodb/issues/253 + return true; + }, function deleteTemplate(err) { var next = this; templateMaps.delTemplate(username, privateTableTemplate, function(/*delErr*/) {