From 1f693c6c781de6f3388dede7fe4c6d99eb82f659 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Mon, 16 Dec 2013 18:24:35 +0100 Subject: [PATCH] Add 'user_from_host' directive to generalize username extraction Closes #100 Default extractor is backward compatible --- NEWS.md | 1 + config/environments/development.js.example | 3 +++ config/environments/production.js.example | 3 +++ config/environments/staging.js.example | 3 +++ config/environments/test.js.example | 3 +++ lib/cartodb/server_options.js | 20 +++++++++++++++++++- 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 8f235148..26ae1167 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ ------------------- * Update cartodb-redis dependency to "~0.2.0" +* Add 'user_from_host' directive to generalize username extraction (#100) 1.5.2 -- 2013-12-05 ------------------- diff --git a/config/environments/development.js.example b/config/environments/development.js.example index 03b352e5..97941dd7 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -2,6 +2,9 @@ var config = { environment: 'development' ,port: 8181 ,host: '127.0.0.1' + // Regular expression pattern to extract username + // from hostname. Must have a single grabbing block. + ,user_from_host: '^(.*)\\.localhost' // Maximum number of connections for one process // 128 is a good value with a limit of 1024 open file descriptors ,maxConnections:128 diff --git a/config/environments/production.js.example b/config/environments/production.js.example index db418ca7..e64ad4ea 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -2,6 +2,9 @@ var config = { environment: 'production' ,port: 8181 ,host: '127.0.0.1' + // Regular expression pattern to extract username + // from hostname. Must have a single grabbing block. + ,user_from_host: '^(.*)\\.cartodb\\.com$' // Maximum number of connections for one process // 128 is a good value with a limit of 1024 open file descriptors ,maxConnections:128 diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index a2918b74..8695ccfc 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -2,6 +2,9 @@ var config = { environment: 'production' ,port: 8181 ,host: '127.0.0.1' + // Regular expression pattern to extract username + // from hostname. Must have a single grabbing block. + ,user_from_host: '^(.*)\\.cartodb\\.com$' // Maximum number of connections for one process // 128 is a good value with a limit of 1024 open file descriptors ,maxConnections:128 diff --git a/config/environments/test.js.example b/config/environments/test.js.example index d22fba80..f9bd3929 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -2,6 +2,9 @@ var config = { environment: 'test' ,port: 8888 ,host: '127.0.0.1' + // Regular expression pattern to extract username + // from hostname. Must have a single grabbing block. + ,user_from_host: '(.*)' // Maximum number of connections for one process // 128 is a good value with a limit of 1024 open file descriptors ,maxConnections:128 diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index afa8043d..a3366e8f 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -305,8 +305,26 @@ module.exports = function(){ /* X-Cache-Channel generation } */ + me.re_userFromHost = new RegExp( + global.environment.user_from_host || + '^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com" + ); + me.userByReq = function(req) { - return cartoData.userFromHostname(req.headers.host); + var host = req.headers.host; + var mat = host.match(this.re_userFromHost); + if ( ! mat ) { + console.error("ERROR: user pattern '" + this.re_userFromHost + + "' does not match hostname '" + host + "'"); + return; + } + // console.log("Matches: "); console.dir(mat); + if ( ! mat.length === 2 ) { + console.error("ERROR: pattern '" + this.re_userFromHost + + "' gave unexpected matches against '" + host + "': " + mat); + return; + } + return mat[1]; } // Set db authentication parameters to those of the given username