Use different UserSessions keys for cluster vs normal redis session store

This commit is contained in:
James Allen 2016-11-10 14:31:48 +00:00
parent c3860dd7a7
commit 593e59450b
3 changed files with 17 additions and 23 deletions

View file

@ -8,10 +8,6 @@ UserSessionsRedis = require('./UserSessionsRedis')
rclient = UserSessionsRedis.client()
module.exports = UserSessionsManager =
_sessionSetKey: (user) ->
return "UserSessions:{#{user._id}}"
# mimic the key used by the express sessions
_sessionKey: (sessionId) ->
return "sess:#{sessionId}"
@ -24,7 +20,7 @@ module.exports = UserSessionsManager =
logger.log {user_id: user._id}, "no sessionId to track, returning"
return callback(null)
logger.log {user_id: user._id, sessionId}, "onLogin handler"
sessionSetKey = UserSessionsManager._sessionSetKey(user)
sessionSetKey = UserSessionsRedis.sessionSetKey(user)
value = UserSessionsManager._sessionKey sessionId
rclient.multi()
.sadd(sessionSetKey, value)
@ -44,7 +40,7 @@ module.exports = UserSessionsManager =
logger.log {user_id: user._id}, "no sessionId to untrack, returning"
return callback(null)
logger.log {user_id: user._id, sessionId}, "onLogout handler"
sessionSetKey = UserSessionsManager._sessionSetKey(user)
sessionSetKey = UserSessionsRedis.sessionSetKey(user)
value = UserSessionsManager._sessionKey sessionId
rclient.multi()
.srem(sessionSetKey, value)
@ -58,7 +54,7 @@ module.exports = UserSessionsManager =
getAllUserSessions: (user, exclude, callback=(err, sessionKeys)->) ->
exclude = _.map(exclude, UserSessionsManager._sessionKey)
sessionSetKey = UserSessionsManager._sessionSetKey(user)
sessionSetKey = UserSessionsRedis.sessionSetKey(user)
rclient.smembers sessionSetKey, (err, sessionKeys) ->
if err?
logger.err user_id: user._id, "error getting all session keys for user from redis"
@ -94,7 +90,7 @@ module.exports = UserSessionsManager =
logger.log {}, "no user to revoke sessions for, returning"
return callback(null)
logger.log {user_id: user._id}, "revoking all existing sessions for user"
sessionSetKey = UserSessionsManager._sessionSetKey(user)
sessionSetKey = UserSessionsRedis.sessionSetKey(user)
rclient.smembers sessionSetKey, (err, sessionKeys) ->
if err?
logger.err {err, user_id: user._id, sessionSetKey}, "error getting contents of UserSessions set"
@ -123,7 +119,7 @@ module.exports = UserSessionsManager =
if !user
logger.log {}, "no user to touch sessions for, returning"
return callback(null)
sessionSetKey = UserSessionsManager._sessionSetKey(user)
sessionSetKey = UserSessionsRedis.sessionSetKey(user)
rclient.expire sessionSetKey, "#{Settings.cookieSessionLength}", (err, response) ->
if err?
logger.err {err, user_id: user._id}, "error while updating ttl on UserSessions set"
@ -135,7 +131,7 @@ module.exports = UserSessionsManager =
logger.log {}, "no user, returning"
return callback(null)
logger.log {user_id: user._id}, "checking sessions for user"
sessionSetKey = UserSessionsManager._sessionSetKey(user)
sessionSetKey = UserSessionsRedis.sessionSetKey(user)
rclient.smembers sessionSetKey, (err, sessionKeys) ->
if err?
logger.err {err, user_id: user._id, sessionSetKey}, "error getting contents of UserSessions set"

View file

@ -3,16 +3,19 @@ redis = require 'redis-sharelatex'
ioredis = require 'ioredis'
logger = require 'logger-sharelatex'
redisSessionsSettings = Settings.redis.websessions or Settings.redis.web
module.exports = Redis =
client: () ->
redisSessionsSettings = Settings.redis.websessions or Settings.redis.web
if redisSessionsSettings?.cluster?
logger.log {}, "using redis cluster for web sessions"
rclient = new ioredis.Cluster(redisSessionsSettings.cluster)
else
rclient = redis.createClient(redisSessionsSettings)
return rclient
sessionSetKey: (user) ->
if redisSessionsSettings?.cluster?
return "UserSessions:{#{user._id}}"
else
return "UserSessions:#{user._id}"

View file

@ -33,8 +33,9 @@ describe 'UserSessionsManager', ->
@rclient.expire.returns(@rclient)
@rclient.exec.callsArgWith(0, null)
@redis =
@UserSessionsRedis =
client: () => @rclient
sessionSetKey: (user) => "UserSessions:{#{user._id}}"
@logger =
err: sinon.stub()
error: sinon.stub()
@ -45,15 +46,9 @@ describe 'UserSessionsManager', ->
@UserSessionsManager = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger
"settings-sharelatex": @settings
'./UserSessionsRedis': @redis
'./UserSessionsRedis': @UserSessionsRedis
'async': Async
describe '_sessionSetKey', ->
it 'should build the correct key', ->
result = @UserSessionsManager._sessionSetKey(@user)
result.should.equal 'UserSessions:{abcd}'
describe '_sessionKey', ->
it 'should build the correct key', ->