2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let rclient_secondary
|
2020-08-11 05:35:08 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
2021-03-18 04:39:39 -04:00
|
|
|
const request = require('request').defaults({ timeout: 30 * 1000 })
|
2019-05-29 05:21:06 -04:00
|
|
|
const RedisWrapper = require('../../infrastructure/RedisWrapper')
|
|
|
|
const rclient = RedisWrapper.client('clsi_cookie')
|
|
|
|
if (Settings.redis.clsi_cookie_secondary != null) {
|
|
|
|
rclient_secondary = RedisWrapper.client('clsi_cookie_secondary')
|
|
|
|
}
|
|
|
|
const Cookie = require('cookie')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
|
|
|
|
const clsiCookiesEnabled =
|
|
|
|
(Settings.clsiCookie != null ? Settings.clsiCookie.key : undefined) != null &&
|
|
|
|
Settings.clsiCookie.key.length !== 0
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
module.exports = function (backendGroup) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return {
|
|
|
|
buildKey(project_id) {
|
|
|
|
if (backendGroup != null) {
|
|
|
|
return `clsiserver:${backendGroup}:${project_id}`
|
|
|
|
} else {
|
|
|
|
return `clsiserver:${project_id}`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getServerId(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, serverId) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return rclient.get(this.buildKey(project_id), (err, serverId) => {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (serverId == null || serverId === '') {
|
|
|
|
return this._populateServerIdViaRequest(project_id, callback)
|
|
|
|
} else {
|
|
|
|
return callback(null, serverId)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_populateServerIdViaRequest(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, serverId) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const url = `${Settings.apis.clsi.url}/project/${project_id}/status`
|
2021-03-29 05:19:42 -04:00
|
|
|
return request.post(url, (err, res, body) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2020-08-11 05:35:08 -04:00
|
|
|
OError.tag(err, 'error getting initial server id for project', {
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2020-08-11 05:35:08 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return this.setServerId(project_id, res, function (err, serverId) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ err, project_id },
|
|
|
|
'error setting server id via populate request'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return callback(err, serverId)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_parseServerIdFromResponse(response) {
|
|
|
|
const cookies = Cookie.parse(
|
|
|
|
(response.headers['set-cookie'] != null
|
|
|
|
? response.headers['set-cookie'][0]
|
|
|
|
: undefined) || ''
|
|
|
|
)
|
|
|
|
return cookies != null ? cookies[Settings.clsiCookie.key] : undefined
|
|
|
|
},
|
|
|
|
|
|
|
|
setServerId(project_id, response, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, serverId) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (!clsiCookiesEnabled) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const serverId = this._parseServerIdFromResponse(response)
|
|
|
|
if (serverId == null) {
|
|
|
|
// We don't get a cookie back if it hasn't changed
|
|
|
|
return rclient.expire(
|
|
|
|
this.buildKey(project_id),
|
|
|
|
Settings.clsiCookie.ttl,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (rclient_secondary != null) {
|
|
|
|
this._setServerIdInRedis(rclient_secondary, project_id, serverId)
|
|
|
|
}
|
|
|
|
return this._setServerIdInRedis(rclient, project_id, serverId, err =>
|
|
|
|
callback(err, serverId)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
_setServerIdInRedis(rclient, project_id, serverId, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-03-18 04:39:39 -04:00
|
|
|
rclient.setex(
|
|
|
|
this.buildKey(project_id),
|
|
|
|
Settings.clsiCookie.ttl,
|
|
|
|
serverId,
|
|
|
|
callback
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
clearServerId(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (!clsiCookiesEnabled) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
return rclient.del(this.buildKey(project_id), callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getCookieJar(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, jar) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (!clsiCookiesEnabled) {
|
|
|
|
return callback(null, request.jar())
|
|
|
|
}
|
|
|
|
return this._getServerId(project_id, (err, serverId) => {
|
|
|
|
if (err != null) {
|
2020-08-11 05:35:08 -04:00
|
|
|
OError.tag(err, 'error getting server id', {
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2020-08-11 05:35:08 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const serverCookie = request.cookie(
|
|
|
|
`${Settings.clsiCookie.key}=${serverId}`
|
|
|
|
)
|
|
|
|
const jar = request.jar()
|
|
|
|
jar.setCookie(serverCookie, Settings.apis.clsi.url)
|
|
|
|
return callback(null, jar)
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|