From: Johannes Zellner Date: Mon, 8 Oct 2018 12:35:04 +0000 (+0200) Subject: Remove redis dependency only for few tokens X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=b3ff26fb1e2b4d07a1d7b00ee5ff6d72026c5427;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git Remove redis dependency only for few tokens --- diff --git a/.gitignore b/.gitignore index a34adba..1f7fe79 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ files/ .users.json .config.json +.tokens.json diff --git a/CHANGELOG b/CHANGELOG index c297477..35d96b7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -67,3 +67,6 @@ [5.2.0] * Disable folder listing by default * Do not render welcome screen if folder listing is enabled + +[5.3.0] +* Replacing redis token store with local json file diff --git a/CloudronManifest.json b/CloudronManifest.json index 9e16c41..a58e260 100644 --- a/CloudronManifest.json +++ b/CloudronManifest.json @@ -19,7 +19,6 @@ "icon": "logo.png", "addons": { "ldap": {}, - "redis": {}, "localstorage": {} }, "mediaLinks": [ diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 2edb3ea..1d88103 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -429,11 +429,6 @@ "integrity": "sha1-fyjS657nsVqX79ic5j3P2qPMur8=", "dev": true }, - "double-ended-queue": { - "version": "2.1.0-0", - "resolved": "https://registry.npmjs.org/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz", - "integrity": "sha1-ED01J/0xUo9AGIEwyEHv3XgmTlw=" - }, "dtrace-provider": { "version": "0.2.8", "resolved": "https://registry.npmjs.org/dtrace-provider/-/dtrace-provider-0.2.8.tgz", @@ -1300,26 +1295,6 @@ "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.9.tgz", "integrity": "sha1-PtqOZfI80qF+YTAbHwADOWr17No=" }, - "redis": { - "version": "2.8.0", - "resolved": "https://registry.npmjs.org/redis/-/redis-2.8.0.tgz", - "integrity": "sha512-M1OkonEQwtRmZv4tEWF2VgpG0JWJ8Fv1PhlgT5+B+uNq2cA3Rt1Yt/ryoR+vQNOQcIEgdCdfH0jr3bDpihAw1A==", - "requires": { - "double-ended-queue": "2.1.0-0", - "redis-commands": "1.3.1", - "redis-parser": "2.6.0" - } - }, - "redis-commands": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.3.1.tgz", - "integrity": "sha1-gdgm9F+pyLIBH0zXoP5ZfSQdRCs=" - }, - "redis-parser": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-2.6.0.tgz", - "integrity": "sha1-Uu0J2srBCPGmMcB+m2mUHnoZUEs=" - }, "reduce-component": { "version": "1.0.1", "resolved": "http://registry.npmjs.org/reduce-component/-/reduce-component-1.0.1.tgz", diff --git a/package.json b/package.json index 07e052d..159dddb 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "passport-http-bearer": "^1.0.1", "passport-ldapjs": "^1.0.3", "readline-sync": "^1.4.9", - "redis": "^2.8.0", "request": "^2.83.0", "safetydance": "^0.1.1", "serve-index": "^1.9.1", diff --git a/server.js b/server.js index f661e87..6206103 100755 --- a/server.js +++ b/server.js @@ -22,11 +22,11 @@ var express = require('express'), files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); -var rootFolder = path.resolve(__dirname, process.argv[2] || 'files'); -var configFile = path.resolve(__dirname, process.argv[3] || '.config.json'); +const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files'); +const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json'); // Ensure the root folder exists -mkdirp.sync(rootFolder); +mkdirp.sync(ROOT_FOLDER); var config = { folderListingEnabled: false @@ -41,7 +41,7 @@ function setSettings(req, res, next) { config.folderListingEnabled = !!req.body.folderListingEnabled; - fs.writeFile(configFile, JSON.stringify(config), function (error) { + fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) { if (error) return next(new HttpError(500, 'unable to save settings')); next(new HttpSuccess(201, {})); @@ -50,10 +50,11 @@ function setSettings(req, res, next) { // Load the config file try { - config = require(configFile); + console.log(`Using config file: ${CONFIG_FILE}`); + config = require(CONFIG_FILE); } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${configFile} not found`); - else console.log(`Cannot load config file ${configFile}`, e); + if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${CONFIG_FILE} not found`); + else console.log(`Cannot load config file ${CONFIG_FILE}`, e); } if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; @@ -85,7 +86,7 @@ app.use('/api', passport.initialize()); app.use('/api', passport.session()); app.use(router); app.use('/_admin', express.static(__dirname + '/frontend')); -app.use('/', express.static(rootFolder)); +app.use('/', express.static(ROOT_FOLDER)); app.use('/', function welcomePage(req, res, next) { if (config.folderListingEnabled || req.path !== '/') return next(); res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html')); @@ -94,7 +95,7 @@ app.use('/', function (req, res, next) { if (config.folderListingEnabled) return next(); res.sendFile(__dirname + '/frontend/404.html'); }); -app.use('/', serveIndex(rootFolder, { icons: true })); +app.use('/', serveIndex(ROOT_FOLDER, { icons: true })); app.use(lastMile()); var server = app.listen(3000, function () { @@ -102,5 +103,5 @@ var server = app.listen(3000, function () { var port = server.address().port; console.log('Surfer listening on http://%s:%s', host, port); - console.log('Using base path', rootFolder); + console.log('Using base path', ROOT_FOLDER); }); diff --git a/src/auth.js b/src/auth.js index 0338632..093f297 100644 --- a/src/auth.js +++ b/src/auth.js @@ -3,53 +3,47 @@ var passport = require('passport'), path = require('path'), safe = require('safetydance'), + fs = require('fs'), bcrypt = require('bcryptjs'), uuid = require('uuid/v4'), - redis = require('redis'), BearerStrategy = require('passport-http-bearer').Strategy, LdapStrategy = require('passport-ldapjs').Strategy, HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess; -var LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json'); +const LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json'); +const TOKENSTORE_FILE = path.resolve(process.env.TOKENSTORE_FILE || './.tokens.json'); var tokenStore = { data: {}, + save: function () { + try { + fs.writeFileSync(TOKENSTORE_FILE, JSON.stringify(tokenStore.data), 'utf-8'); + } catch (e) { + console.error(`Unable to save tokenstore file at ${TOKENSTORE_FILE}`, e); + } + }, get: function (token, callback) { callback(tokenStore.data[token] ? null : 'not found', tokenStore.data[token]); }, set: function (token, data, callback) { tokenStore.data[token] = data; + tokenStore.save(); callback(null); }, del: function (token, callback) { delete tokenStore.data[token]; + tokenStore.save(); callback(null); } }; -if (process.env.REDIS_URL) { - console.log('Enable redis token store'); - - var redisClient = redis.createClient(process.env.REDIS_URL); - - if (process.env.REDIS_PASSWORD) { - console.log('Using redis auth'); - redisClient.auth(process.env.REDIS_PASSWORD); - } - - // overwrite the tokenStore api - tokenStore.get = function (token, callback) { - redisClient.get(token, function (error, result) { - callback(error || null, safe.JSON.parse(result)); - }); - }; - tokenStore.set = function (token, data, callback) { - redisClient.set(token, JSON.stringify(data), callback); - }; - tokenStore.del = redisClient.del.bind(redisClient); -} else { - console.log('Use in-memory token store'); +// load token store data if any +try { + console.log(`Using tokenstore file: ${TOKENSTORE_FILE}`); + tokenStore.data = JSON.parse(fs.readFileSync(TOKENSTORE_FILE, 'utf-8')); +} catch (e) { + // start with empty token store } function issueAccessToken() { @@ -77,11 +71,11 @@ var LDAP_URL = process.env.LDAP_URL; var LDAP_USERS_BASE_DN = process.env.LDAP_USERS_BASE_DN; if (LDAP_URL && LDAP_USERS_BASE_DN) { - console.log('Enable ldap auth'); + console.log('Using ldap auth'); exports.login = [ passport.authenticate('ldap'), issueAccessToken() ]; } else { - console.log('Use local user file:', LOCAL_AUTH_FILE); + console.log(`Using local user file: ${LOCAL_AUTH_FILE}`); exports.login = [ function (req, res, next) { diff --git a/start.sh b/start.sh index 562eb84..0a13477 100755 --- a/start.sh +++ b/start.sh @@ -3,6 +3,7 @@ set -eu export NODE_ENV=production +export TOKENSTORE_FILE=/app/data/tokens.json if [[ ! -d "/app/data/surfer_root" ]]; then echo "=> Migrating root folder from /app/data to /app/data/surfer_root"