diff --git a/lib/cartodb/api/api-router.js b/lib/cartodb/api/api-router.js index 5a08eb6d..7108bbbf 100644 --- a/lib/cartodb/api/api-router.js +++ b/lib/cartodb/api/api-router.js @@ -14,7 +14,7 @@ const PgQueryRunner = require('../backends/pg_query_runner'); const StatsBackend = require('../backends/stats'); const AuthBackend = require('../backends/auth'); -const UserLimitsApi = require('../backends/api/user_limits_api'); +const UserLimitsBackend = require('../backends/user-limits'); const OverviewsMetadataBackend = require('../backends/overviews-metadata'); const FilterStatsApi = require('../backends/filter-stats'); const TablesExtentBackend = require('../backends/tables-extent'); @@ -106,7 +106,7 @@ module.exports = class ApiRouter { const dataviewBackend = new DataviewBackend(analysisBackend); const statsBackend = new StatsBackend(); - const userLimitsApi = new UserLimitsApi(metadataBackend, { + const userLimitsBackend = new UserLimitsBackend(metadataBackend, { limits: { cacheOnTimeout: serverOptions.renderer.mapnik.limits.cacheOnTimeout || false, render: serverOptions.renderer.mapnik.limits.render || 0, @@ -145,7 +145,7 @@ module.exports = class ApiRouter { templateMaps, pgConnection, metadataBackend, - userLimitsApi, + userLimitsBackend, mapConfigAdapter, layergroupAffectedTablesCache ); @@ -162,7 +162,7 @@ module.exports = class ApiRouter { tileBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache, diff --git a/lib/cartodb/api/map/analyses-catalog-controller.js b/lib/cartodb/api/map/analyses-catalog-controller.js index 855666dd..91defc21 100644 --- a/lib/cartodb/api/map/analyses-catalog-controller.js +++ b/lib/cartodb/api/map/analyses-catalog-controller.js @@ -8,10 +8,10 @@ const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; const cacheControlHeader = require('../middlewares/cache-control-header'); const dbParamsFromResLocals = require('../../utils/database-params'); -function AnalysesController(pgConnection, authBackend, userLimitsApi) { +function AnalysesController(pgConnection, authBackend, userLimitsBackend) { this.pgConnection = pgConnection; this.authBackend = authBackend; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; } module.exports = AnalysesController; @@ -22,7 +22,7 @@ AnalysesController.prototype.register = function (mapRouter) { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.ANALYSIS_CATALOG), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.ANALYSIS_CATALOG), cleanUpQueryParams(), createPGClient(), getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }), diff --git a/lib/cartodb/api/map/analysis-layergroup-controller.js b/lib/cartodb/api/map/analysis-layergroup-controller.js index e0cf3222..5d89803b 100644 --- a/lib/cartodb/api/map/analysis-layergroup-controller.js +++ b/lib/cartodb/api/map/analysis-layergroup-controller.js @@ -12,7 +12,7 @@ module.exports = class AnalysisLayergroupController { analysisStatusBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -20,7 +20,7 @@ module.exports = class AnalysisLayergroupController { this.analysisStatusBackend = analysisStatusBackend; this.pgConnection = pgConnection; this.mapStore = mapStore; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTablesCache = layergroupAffectedTablesCache; this.authBackend = authBackend; this.surrogateKeysCache = surrogateKeysCache; @@ -33,7 +33,7 @@ module.exports = class AnalysisLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.ANALYSIS), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.ANALYSIS), cleanUpQueryParams(), analysisNodeStatus(this.analysisStatusBackend) ); diff --git a/lib/cartodb/api/map/anonymous-map-controller.js b/lib/cartodb/api/map/anonymous-map-controller.js index a20b1407..964ffacd 100644 --- a/lib/cartodb/api/map/anonymous-map-controller.js +++ b/lib/cartodb/api/map/anonymous-map-controller.js @@ -29,7 +29,7 @@ const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; * @param {MapBackend} mapBackend * @param metadataBackend * @param {SurrogateKeysCache} surrogateKeysCache - * @param {UserLimitsApi} userLimitsApi + * @param {UserLimitsBackend} userLimitsBackend * @param {LayergroupAffectedTables} layergroupAffectedTables * @param {MapConfigAdapter} mapConfigAdapter * @param {StatsBackend} statsBackend @@ -41,7 +41,7 @@ function AnonymousMapController ( mapBackend, metadataBackend, surrogateKeysCache, - userLimitsApi, + userLimitsBackend, layergroupAffectedTables, mapConfigAdapter, statsBackend, @@ -53,7 +53,7 @@ function AnonymousMapController ( this.mapBackend = mapBackend; this.metadataBackend = metadataBackend; this.surrogateKeysCache = surrogateKeysCache; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTables = layergroupAffectedTables; this.mapConfigAdapter = mapConfigAdapter; this.statsBackend = statsBackend; @@ -80,7 +80,7 @@ AnonymousMapController.prototype.composeCreateMapMiddleware = function () { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS), cleanUpQueryParams(['aggregation']), initProfiler(isTemplateInstantiation), checkJsonContentType(), @@ -88,7 +88,7 @@ AnonymousMapController.prototype.composeCreateMapMiddleware = function () { prepareAdapterMapConfig(this.mapConfigAdapter), createLayergroup ( this.mapBackend, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTables ), @@ -166,7 +166,7 @@ function prepareAdapterMapConfig (mapConfigAdapter) { }; } -function createLayergroup (mapBackend, userLimitsApi, pgConnection, affectedTablesCache) { +function createLayergroup (mapBackend, userLimitsBackend, pgConnection, affectedTablesCache) { return function createLayergroupMiddleware (req, res, next) { const requestMapConfig = req.body; @@ -185,7 +185,7 @@ function createLayergroup (mapBackend, userLimitsApi, pgConnection, affectedTabl const mapConfigProvider = new CreateLayergroupMapConfigProvider( mapConfig, user, - userLimitsApi, + userLimitsBackend, pgConnection, affectedTablesCache, params diff --git a/lib/cartodb/api/map/attributes-layergroup-controller.js b/lib/cartodb/api/map/attributes-layergroup-controller.js index 17af3df7..ef9614fc 100644 --- a/lib/cartodb/api/map/attributes-layergroup-controller.js +++ b/lib/cartodb/api/map/attributes-layergroup-controller.js @@ -16,7 +16,7 @@ module.exports = class AttributesLayergroupController { attributesBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -24,7 +24,7 @@ module.exports = class AttributesLayergroupController { this.attributesBackend = attributesBackend; this.pgConnection = pgConnection; this.mapStore = mapStore; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTablesCache = layergroupAffectedTablesCache; this.authBackend = authBackend; this.surrogateKeysCache = surrogateKeysCache; @@ -37,11 +37,11 @@ module.exports = class AttributesLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.ATTRIBUTES), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.ATTRIBUTES), cleanUpQueryParams(), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), diff --git a/lib/cartodb/api/map/dataview-layergroup-controller.js b/lib/cartodb/api/map/dataview-layergroup-controller.js index 84b4c905..0b6f8cc9 100644 --- a/lib/cartodb/api/map/dataview-layergroup-controller.js +++ b/lib/cartodb/api/map/dataview-layergroup-controller.js @@ -31,7 +31,7 @@ module.exports = class DataviewLayergroupController { dataviewBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -39,7 +39,7 @@ module.exports = class DataviewLayergroupController { this.dataviewBackend = dataviewBackend; this.pgConnection = pgConnection; this.mapStore = mapStore; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTablesCache = layergroupAffectedTablesCache; this.authBackend = authBackend; this.surrogateKeysCache = surrogateKeysCache; @@ -55,11 +55,11 @@ module.exports = class DataviewLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW), cleanUpQueryParams(ALLOWED_DATAVIEW_QUERY_PARAMS), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), @@ -76,11 +76,11 @@ module.exports = class DataviewLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW), cleanUpQueryParams(ALLOWED_DATAVIEW_QUERY_PARAMS), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), @@ -97,11 +97,11 @@ module.exports = class DataviewLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW_SEARCH), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW_SEARCH), cleanUpQueryParams(ALLOWED_DATAVIEW_QUERY_PARAMS), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), @@ -118,11 +118,11 @@ module.exports = class DataviewLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW_SEARCH), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.DATAVIEW_SEARCH), cleanUpQueryParams(ALLOWED_DATAVIEW_QUERY_PARAMS), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), diff --git a/lib/cartodb/api/map/map-router.js b/lib/cartodb/api/map/map-router.js index 78b87d1e..a6e91c29 100644 --- a/lib/cartodb/api/map/map-router.js +++ b/lib/cartodb/api/map/map-router.js @@ -19,7 +19,7 @@ module.exports = class MapRouter { tileBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache, @@ -37,7 +37,7 @@ module.exports = class MapRouter { analysisStatusBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -47,7 +47,7 @@ module.exports = class MapRouter { attributesBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -57,7 +57,7 @@ module.exports = class MapRouter { dataviewBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -67,7 +67,7 @@ module.exports = class MapRouter { previewBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -77,7 +77,7 @@ module.exports = class MapRouter { tileBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -89,7 +89,7 @@ module.exports = class MapRouter { mapBackend, metadataBackend, surrogateKeysCache, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, mapConfigAdapter, statsBackend, @@ -105,13 +105,13 @@ module.exports = class MapRouter { metadataBackend, pgConnection, authBackend, - userLimitsApi + userLimitsBackend ); this.analysesController = new AnalysesCatalogController( pgConnection, authBackend, - userLimitsApi + userLimitsBackend ); } diff --git a/lib/cartodb/api/map/preview-layergroup-controller.js b/lib/cartodb/api/map/preview-layergroup-controller.js index 484b831c..dcacc4f5 100644 --- a/lib/cartodb/api/map/preview-layergroup-controller.js +++ b/lib/cartodb/api/map/preview-layergroup-controller.js @@ -16,7 +16,7 @@ module.exports = class PreviewLayergroupController { previewBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -24,7 +24,7 @@ module.exports = class PreviewLayergroupController { this.previewBackend = previewBackend; this.pgConnection = pgConnection; this.mapStore = mapStore; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTablesCache = layergroupAffectedTablesCache; this.authBackend = authBackend; this.surrogateKeysCache = surrogateKeysCache; @@ -39,11 +39,11 @@ module.exports = class PreviewLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC), cleanUpQueryParams(['layer']), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache, forcedFormat @@ -61,11 +61,11 @@ module.exports = class PreviewLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC), cleanUpQueryParams(['layer']), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache, forcedFormat diff --git a/lib/cartodb/api/map/preview-template-controller.js b/lib/cartodb/api/map/preview-template-controller.js index acf58a91..91a3f5b6 100644 --- a/lib/cartodb/api/map/preview-template-controller.js +++ b/lib/cartodb/api/map/preview-template-controller.js @@ -30,7 +30,7 @@ function PreviewTemplateController ( metadataBackend, pgConnection, authBackend, - userLimitsApi + userLimitsBackend ) { this.namedMapProviderCache = namedMapProviderCache; this.previewBackend = previewBackend; @@ -39,7 +39,7 @@ function PreviewTemplateController ( this.metadataBackend = metadataBackend; this.pgConnection = pgConnection; this.authBackend = authBackend; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; } module.exports = PreviewTemplateController; @@ -50,7 +50,7 @@ PreviewTemplateController.prototype.register = function (mapRouter) { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC_NAMED), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC_NAMED), cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']), namedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, diff --git a/lib/cartodb/api/map/tile-layergroup-controller.js b/lib/cartodb/api/map/tile-layergroup-controller.js index d747d040..40d85b7c 100644 --- a/lib/cartodb/api/map/tile-layergroup-controller.js +++ b/lib/cartodb/api/map/tile-layergroup-controller.js @@ -26,7 +26,7 @@ module.exports = class TileLayergroupController { tileBackend, pgConnection, mapStore, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, authBackend, surrogateKeysCache @@ -34,7 +34,7 @@ module.exports = class TileLayergroupController { this.tileBackend = tileBackend; this.pgConnection = pgConnection; this.mapStore = mapStore; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTablesCache = layergroupAffectedTablesCache; this.authBackend = authBackend; this.surrogateKeysCache = surrogateKeysCache; @@ -52,11 +52,11 @@ module.exports = class TileLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.TILE), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.TILE), cleanUpQueryParams(), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), @@ -77,11 +77,11 @@ module.exports = class TileLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.TILE), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.TILE), cleanUpQueryParams(), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), @@ -102,11 +102,11 @@ module.exports = class TileLayergroupController { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.TILE), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.TILE), cleanUpQueryParams(), createMapStoreMapConfigProvider( this.mapStore, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTablesCache ), diff --git a/lib/cartodb/api/middlewares/map-store-map-config-provider.js b/lib/cartodb/api/middlewares/map-store-map-config-provider.js index f84dd3fc..58cdb79e 100644 --- a/lib/cartodb/api/middlewares/map-store-map-config-provider.js +++ b/lib/cartodb/api/middlewares/map-store-map-config-provider.js @@ -2,7 +2,7 @@ const MapStoreMapConfigProvider = require('../../models/mapconfig/provider/map-s module.exports = function createMapStoreMapConfigProvider ( mapStore, - userLimitsApi, + userLimitsBackend, pgConnection, affectedTablesCache, forcedFormat = null @@ -26,7 +26,7 @@ module.exports = function createMapStoreMapConfigProvider ( res.locals.mapConfigProvider = new MapStoreMapConfigProvider( mapStore, user, - userLimitsApi, + userLimitsBackend, pgConnection, affectedTablesCache, params diff --git a/lib/cartodb/api/middlewares/rate-limit.js b/lib/cartodb/api/middlewares/rate-limit.js index b3c6d7fe..55062d76 100644 --- a/lib/cartodb/api/middlewares/rate-limit.js +++ b/lib/cartodb/api/middlewares/rate-limit.js @@ -19,13 +19,13 @@ const RATE_LIMIT_ENDPOINTS_GROUPS = { NAMED_TILES: 'named_tiles' }; -function rateLimit(userLimitsApi, endpointGroup = null) { +function rateLimit(userLimitsBackend, endpointGroup = null) { if (!isRateLimitEnabled(endpointGroup)) { return function rateLimitDisabledMiddleware(req, res, next) { next(); }; } return function rateLimitMiddleware(req, res, next) { - userLimitsApi.getRateLimit(res.locals.user, endpointGroup, function (err, userRateLimit) { + userLimitsBackend.getRateLimit(res.locals.user, endpointGroup, function (err, userRateLimit) { if (err) { return next(err); } diff --git a/lib/cartodb/api/template/admin-template-controller.js b/lib/cartodb/api/template/admin-template-controller.js index e79f6bd9..b4d1b65f 100644 --- a/lib/cartodb/api/template/admin-template-controller.js +++ b/lib/cartodb/api/template/admin-template-controller.js @@ -9,10 +9,10 @@ const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; * @param {TemplateMaps} templateMaps * @constructor */ -function AdminTemplateController(authBackend, templateMaps, userLimitsApi) { +function AdminTemplateController(authBackend, templateMaps, userLimitsBackend) { this.authBackend = authBackend; this.templateMaps = templateMaps; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; } module.exports = AdminTemplateController; @@ -24,7 +24,7 @@ AdminTemplateController.prototype.register = function (templateRouter) { `/`, credentials(), authorizedByAPIKey({ authBackend: this.authBackend, action: 'create', label: 'POST TEMPLATE' }), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_CREATE), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_CREATE), checkContentType({ action: 'POST', label: 'POST TEMPLATE' }), createTemplate({ templateMaps: this.templateMaps }) ); @@ -33,7 +33,7 @@ AdminTemplateController.prototype.register = function (templateRouter) { `/:template_id`, credentials(), authorizedByAPIKey({ authBackend: this.authBackend, action: 'update', label: 'PUT TEMPLATE' }), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_UPDATE), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_UPDATE), checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }), updateTemplate({ templateMaps: this.templateMaps }) ); @@ -42,7 +42,7 @@ AdminTemplateController.prototype.register = function (templateRouter) { `/:template_id`, credentials(), authorizedByAPIKey({ authBackend: this.authBackend, action: 'get', label: 'GET TEMPLATE' }), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET), retrieveTemplate({ templateMaps: this.templateMaps }) ); @@ -50,7 +50,7 @@ AdminTemplateController.prototype.register = function (templateRouter) { `/:template_id`, credentials(), authorizedByAPIKey({ authBackend: this.authBackend, action: 'delete', label: 'DELETE TEMPLATE' }), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE), destroyTemplate({ templateMaps: this.templateMaps }) ); @@ -58,7 +58,7 @@ AdminTemplateController.prototype.register = function (templateRouter) { `/`, credentials(), authorizedByAPIKey({ authBackend: this.authBackend, action: 'list', label: 'GET TEMPLATE LIST' }), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST), listTemplates({ templateMaps: this.templateMaps }) ); }; diff --git a/lib/cartodb/api/template/named-template-controller.js b/lib/cartodb/api/template/named-template-controller.js index f61d5dc7..eed39b34 100644 --- a/lib/cartodb/api/template/named-template-controller.js +++ b/lib/cartodb/api/template/named-template-controller.js @@ -27,7 +27,7 @@ const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; * @param {MapBackend} mapBackend * @param metadataBackend * @param {SurrogateKeysCache} surrogateKeysCache - * @param {UserLimitsApi} userLimitsApi + * @param {UserLimitsBackend} userLimitsBackend * @param {LayergroupAffectedTables} layergroupAffectedTables * @param {MapConfigAdapter} mapConfigAdapter * @param {StatsBackend} statsBackend @@ -39,7 +39,7 @@ function NamedMapController ( mapBackend, metadataBackend, surrogateKeysCache, - userLimitsApi, + userLimitsBackend, layergroupAffectedTables, mapConfigAdapter, statsBackend, @@ -51,7 +51,7 @@ function NamedMapController ( this.mapBackend = mapBackend; this.metadataBackend = metadataBackend; this.surrogateKeysCache = surrogateKeysCache; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.layergroupAffectedTables = layergroupAffectedTables; this.mapConfigAdapter = mapConfigAdapter; this.statsBackend = statsBackend; @@ -84,7 +84,7 @@ NamedMapController.prototype.composeInstantiateTemplateMiddleware = function () credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED), cleanUpQueryParams(['aggregation']), initProfiler(isTemplateInstantiation), checkJsonContentType(), @@ -93,13 +93,13 @@ NamedMapController.prototype.composeInstantiateTemplateMiddleware = function () this.templateMaps, this.pgConnection, this.metadataBackend, - this.userLimitsApi, + this.userLimitsBackend, this.mapConfigAdapter, this.layergroupAffectedTables ), instantiateLayergroup( this.mapBackend, - this.userLimitsApi, + this.userLimitsBackend, this.pgConnection, this.layergroupAffectedTables ), @@ -145,7 +145,7 @@ function getTemplate ( templateMaps, pgConnection, metadataBackend, - userLimitsApi, + userLimitsBackend, mapConfigAdapter, affectedTablesCache ) { @@ -161,7 +161,7 @@ function getTemplate ( templateMaps, pgConnection, metadataBackend, - userLimitsApi, + userLimitsBackend, mapConfigAdapter, affectedTablesCache, user, @@ -186,13 +186,13 @@ function getTemplate ( }; } -function instantiateLayergroup (mapBackend, userLimitsApi, pgConnection, affectedTablesCache) { +function instantiateLayergroup (mapBackend, userLimitsBackend, pgConnection, affectedTablesCache) { return function instantiateLayergroupMiddleware (req, res, next) { const { user, mapConfig, rendererParams } = res.locals; const mapConfigProvider = new CreateLayergroupMapConfigProvider( mapConfig, user, - userLimitsApi, + userLimitsBackend, pgConnection, affectedTablesCache, rendererParams diff --git a/lib/cartodb/api/template/template-router.js b/lib/cartodb/api/template/template-router.js index 87f381d1..643e83c3 100644 --- a/lib/cartodb/api/template/template-router.js +++ b/lib/cartodb/api/template/template-router.js @@ -12,7 +12,7 @@ module.exports = class TemplateRouter { mapBackend, metadataBackend, surrogateKeysCache, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, mapConfigAdapter, statsBackend, @@ -28,7 +28,7 @@ module.exports = class TemplateRouter { mapBackend, metadataBackend, surrogateKeysCache, - userLimitsApi, + userLimitsBackend, layergroupAffectedTablesCache, mapConfigAdapter, statsBackend, @@ -42,13 +42,13 @@ module.exports = class TemplateRouter { surrogateKeysCache, pgConnection, authBackend, - userLimitsApi + userLimitsBackend ); this.adminTemplateController = new AdminTemplateController( authBackend, templateMaps, - userLimitsApi + userLimitsBackend ); } diff --git a/lib/cartodb/api/template/tile-template-controller.js b/lib/cartodb/api/template/tile-template-controller.js index 5e2cb72b..c99a0aea 100644 --- a/lib/cartodb/api/template/tile-template-controller.js +++ b/lib/cartodb/api/template/tile-template-controller.js @@ -17,14 +17,14 @@ function TileTemplateController ( surrogateKeysCache, pgConnection, authBackend, - userLimitsApi + userLimitsBackend ) { this.namedMapProviderCache = namedMapProviderCache; this.tileBackend = tileBackend; this.surrogateKeysCache = surrogateKeysCache; this.pgConnection = pgConnection; this.authBackend = authBackend; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; } module.exports = TileTemplateController; @@ -35,7 +35,7 @@ TileTemplateController.prototype.register = function (templateRouter) { credentials(), authorize(this.authBackend), dbConnSetup(this.pgConnection), - rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_TILES), + rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_TILES), cleanUpQueryParams(), namedMapProvider({ namedMapProviderCache: this.namedMapProviderCache, diff --git a/lib/cartodb/backends/api/user_limits_api.js b/lib/cartodb/backends/user-limits.js similarity index 81% rename from lib/cartodb/backends/api/user_limits_api.js rename to lib/cartodb/backends/user-limits.js index caf9f151..519f1cd1 100644 --- a/lib/cartodb/backends/api/user_limits_api.js +++ b/lib/cartodb/backends/user-limits.js @@ -5,9 +5,9 @@ var step = require('step'); * @param metadataBackend * @param options * @constructor - * @type {UserLimitsApi} + * @type {UserLimitsBackend} */ -function UserLimitsApi(metadataBackend, options) { +function UserLimitsBackend(metadataBackend, options) { this.metadataBackend = metadataBackend; this.options = options || {}; this.options.limits = this.options.limits || {}; @@ -15,9 +15,9 @@ function UserLimitsApi(metadataBackend, options) { this.preprareRateLimit(); } -module.exports = UserLimitsApi; +module.exports = UserLimitsBackend; -UserLimitsApi.prototype.getRenderLimits = function (username, apiKey, callback) { +UserLimitsBackend.prototype.getRenderLimits = function (username, apiKey, callback) { var self = this; var limits = { @@ -40,7 +40,7 @@ UserLimitsApi.prototype.getRenderLimits = function (username, apiKey, callback) }); }; -UserLimitsApi.prototype.getTimeoutRenderLimit = function (username, apiKey, callback) { +UserLimitsBackend.prototype.getTimeoutRenderLimit = function (username, apiKey, callback) { var self = this; step( @@ -80,12 +80,12 @@ UserLimitsApi.prototype.getTimeoutRenderLimit = function (username, apiKey, call ); }; -UserLimitsApi.prototype.preprareRateLimit = function () { +UserLimitsBackend.prototype.preprareRateLimit = function () { if (this.options.limits.rateLimitsEnabled) { this.metadataBackend.loadRateLimitsScript(); } }; -UserLimitsApi.prototype.getRateLimit = function (user, endpointGroup, callback) { +UserLimitsBackend.prototype.getRateLimit = function (user, endpointGroup, callback) { this.metadataBackend.getRateLimit(user, 'maps', endpointGroup, callback); }; diff --git a/lib/cartodb/cache/named_map_provider_cache.js b/lib/cartodb/cache/named_map_provider_cache.js index e0850822..b64decb1 100644 --- a/lib/cartodb/cache/named_map_provider_cache.js +++ b/lib/cartodb/cache/named_map_provider_cache.js @@ -10,14 +10,14 @@ function NamedMapProviderCache( templateMaps, pgConnection, metadataBackend, - userLimitsApi, + userLimitsBackend, mapConfigAdapter, affectedTablesCache ) { this.templateMaps = templateMaps; this.pgConnection = pgConnection; this.metadataBackend = metadataBackend; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.mapConfigAdapter = mapConfigAdapter; this.affectedTablesCache = affectedTablesCache; @@ -36,7 +36,7 @@ NamedMapProviderCache.prototype.get = function(user, templateId, config, authTok this.templateMaps, this.pgConnection, this.metadataBackend, - this.userLimitsApi, + this.userLimitsBackend, this.mapConfigAdapter, this.affectedTablesCache, user, diff --git a/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js b/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js index b3b3f191..fe92bb47 100644 --- a/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js +++ b/lib/cartodb/models/mapconfig/provider/create-layergroup-provider.js @@ -7,16 +7,16 @@ const QueryTables = require('cartodb-query-tables'); /** * @param {MapConfig} mapConfig * @param {String} user - * @param {UserLimitsApi} userLimitsApi + * @param {UserLimitsBackend} userLimitsBackend * @param {Object} params * @constructor * @type {CreateLayergroupMapConfigProvider} */ -function CreateLayergroupMapConfigProvider(mapConfig, user, userLimitsApi, pgConnection, affectedTablesCache, params) { +function CreateLayergroupMapConfigProvider(mapConfig, user, userLimitsBackend, pgConnection, affectedTablesCache, params) { this.mapConfig = mapConfig; this.user = user; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.pgConnection = pgConnection; this.affectedTablesCache = affectedTablesCache; this.params = params; @@ -36,7 +36,7 @@ CreateLayergroupMapConfigProvider.prototype.getMapConfig = function(callback) { step( function prepareContextLimits() { - self.userLimitsApi.getRenderLimits(self.user, self.params.api_key, this); + self.userLimitsBackend.getRenderLimits(self.user, self.params.api_key, this); }, function handleRenderLimits(err, renderLimits) { assert.ifError(err); diff --git a/lib/cartodb/models/mapconfig/provider/map-store-provider.js b/lib/cartodb/models/mapconfig/provider/map-store-provider.js index f89a79ce..d57c8d7b 100644 --- a/lib/cartodb/models/mapconfig/provider/map-store-provider.js +++ b/lib/cartodb/models/mapconfig/provider/map-store-provider.js @@ -7,15 +7,15 @@ const QueryTables = require('cartodb-query-tables'); /** * @param {MapStore} mapStore * @param {String} user - * @param {UserLimitsApi} userLimitsApi + * @param {UserLimitsBackend} userLimitsBackend * @param {Object} params * @constructor * @type {MapStoreMapConfigProvider} */ -function MapStoreMapConfigProvider(mapStore, user, userLimitsApi, pgConnection, affectedTablesCache, params) { +function MapStoreMapConfigProvider(mapStore, user, userLimitsBackend, pgConnection, affectedTablesCache, params) { this.mapStore = mapStore; this.user = user; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.pgConnection = pgConnection; this.affectedTablesCache = affectedTablesCache; this.token = params.token; @@ -38,7 +38,7 @@ MapStoreMapConfigProvider.prototype.getMapConfig = function(callback) { step( function prepareContextLimits() { - self.userLimitsApi.getRenderLimits(self.user, self.params.api_key, this); + self.userLimitsBackend.getRenderLimits(self.user, self.params.api_key, this); }, function handleRenderLimits(err, renderLimits) { assert.ifError(err); diff --git a/lib/cartodb/models/mapconfig/provider/named-map-provider.js b/lib/cartodb/models/mapconfig/provider/named-map-provider.js index 50612065..5e931679 100644 --- a/lib/cartodb/models/mapconfig/provider/named-map-provider.js +++ b/lib/cartodb/models/mapconfig/provider/named-map-provider.js @@ -15,7 +15,7 @@ function NamedMapMapConfigProvider( templateMaps, pgConnection, metadataBackend, - userLimitsApi, + userLimitsBackend, mapConfigAdapter, affectedTablesCache, owner, @@ -27,7 +27,7 @@ function NamedMapMapConfigProvider( this.templateMaps = templateMaps; this.pgConnection = pgConnection; this.metadataBackend = metadataBackend; - this.userLimitsApi = userLimitsApi; + this.userLimitsBackend = userLimitsBackend; this.mapConfigAdapter = mapConfigAdapter; this.owner = owner; @@ -125,7 +125,7 @@ NamedMapMapConfigProvider.prototype.getMapConfig = function(callback) { function prepareContextLimits(err, _mapConfig) { assert.ifError(err); mapConfig = _mapConfig; - self.userLimitsApi.getRenderLimits(self.owner, self.params.api_key, this); + self.userLimitsBackend.getRenderLimits(self.owner, self.params.api_key, this); }, function cacheAndReturnMapConfig(err, renderLimits) { self.err = err; diff --git a/test/acceptance/rate-limit.test.js b/test/acceptance/rate-limit.test.js index 5b7cdbc0..925d5028 100644 --- a/test/acceptance/rate-limit.test.js +++ b/test/acceptance/rate-limit.test.js @@ -5,7 +5,7 @@ const redis = require('redis'); const RedisPool = require('redis-mpool'); const cartodbRedis = require('cartodb-redis'); const TestClient = require('../support/test-client'); -const UserLimitsApi = require('../../lib/cartodb/backends/api/user_limits_api'); +const UserLimitsBackend = require('../../lib/cartodb/backends/user-limits'); const rateLimitMiddleware = require('../../lib/cartodb/api/middlewares/rate-limit'); const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitMiddleware; @@ -218,7 +218,7 @@ describe('rate limit middleware', function () { const redisPool = new RedisPool(global.environment.redis); const metadataBackend = cartodbRedis({ pool: redisPool }); - userLimitsApi = new UserLimitsApi(metadataBackend, { + userLimitsApi = new UserLimitsBackend(metadataBackend, { limits: { rateLimitsEnabled: global.environment.enabledFeatures.rateLimitsEnabled }