From f96bc6d5f4665fe7019a1bde41389ef4eaceb9fd Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Mon, 17 Aug 2020 10:02:46 -0700 Subject: [PATCH] keep mongodb database names short --- CHANGES | 1 + src/addons.js | 61 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/CHANGES b/CHANGES index 258b9105c..96d49ef2d 100644 --- a/CHANGES +++ b/CHANGES @@ -2069,4 +2069,5 @@ * Remove IP nginx configuration that redirects to dashboard after activation * dashboard: looks for search string in app title as well * Add vaapi caps for transcoding +* Fix issue where the long mongodb database names where causing app indices of rocket.chat to overflow (> 127) diff --git a/src/addons.js b/src/addons.js index 89c57e01b..ca61ea260 100644 --- a/src/addons.js +++ b/src/addons.js @@ -1666,8 +1666,10 @@ function setupMongoDb(app, options, callback) { appdb.getAddonConfigByName(app.id, 'mongodb', '%MONGODB_PASSWORD', function (error, existingPassword) { if (error && error.reason !== BoxError.NOT_FOUND) return callback(error); + const database = hat(8 * 8); // 16 bytes. keep this short, so as to not overflow the 127 byte index length in MongoDB < 4.4 + const data = { - database: app.id, + database: database, username: app.id, password: error ? hat(4 * 128) : existingPassword, oplog: !!options.oplog @@ -1707,16 +1709,18 @@ function clearMongodb(app, options, callback) { assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); - debugApp(app, 'Clearing mongodb'); - getContainerDetails('mongodb', 'CLOUDRON_MONGODB_TOKEN', function (error, result) { if (error) return callback(error); - request.post(`https://${result.ip}:3000/databases/${app.id}/clear?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { - if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Network error clearing mongodb: ${error.message}`)); - if (response.statusCode !== 200) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error clearing mongodb. Status code: ${response.statusCode} message: ${response.body.message}`)); + appdb.getAddonConfigByName(app.id, 'mongodb', '%MONGODB_DATABASE', function (error, database) { + if (error) return callback(error); - callback(); + request.post(`https://${result.ip}:3000/databases/${database}/clear?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { + if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Network error clearing mongodb: ${error.message}`)); + if (response.statusCode !== 200) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error clearing mongodb. Status code: ${response.statusCode} message: ${response.body.message}`)); + + callback(); + }); }); }); } @@ -1726,16 +1730,19 @@ function teardownMongoDb(app, options, callback) { assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); - debugApp(app, 'Tearing down mongodb'); - getContainerDetails('mongodb', 'CLOUDRON_MONGODB_TOKEN', function (error, result) { if (error) return callback(error); - request.delete(`https://${result.ip}:3000/databases/${app.id}?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { - if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error tearing down mongodb: ${error.message}`)); - if (response.statusCode !== 200) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error tearing down mongodb. Status code: ${response.statusCode} message: ${response.body.message}`)); + appdb.getAddonConfigByName(app.id, 'mongodb', '%MONGODB_DATABASE', function (error, database) { + if (error && error.reason === BoxError.NOT_FOUND) return callback(null); + if (error) return callback(error); - appdb.unsetAddonConfig(app.id, 'mongodb', callback); + request.delete(`https://${result.ip}:3000/databases/${database}?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { + if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error tearing down mongodb: ${error.message}`)); + if (response.statusCode !== 200) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error tearing down mongodb. Status code: ${response.statusCode} message: ${response.body.message}`)); + + appdb.unsetAddonConfig(app.id, 'mongodb', callback); + }); }); }); } @@ -1750,8 +1757,12 @@ function backupMongoDb(app, options, callback) { getContainerDetails('mongodb', 'CLOUDRON_MONGODB_TOKEN', function (error, result) { if (error) return callback(error); - const url = `https://${result.ip}:3000/databases/${app.id}/backup?access_token=${result.token}`; - pipeRequestToFile(url, dumpPath('mongodb', app.id), callback); + appdb.getAddonConfigByName(app.id, 'mongodb', '%MONGODB_DATABASE', function (error, database) { + if (error) return callback(error); + + const url = `https://${result.ip}:3000/databases/${database}/backup?access_token=${result.token}`; + pipeRequestToFile(url, dumpPath('mongodb', app.id), callback); + }); }); } @@ -1767,17 +1778,21 @@ function restoreMongoDb(app, options, callback) { getContainerDetails('mongodb', 'CLOUDRON_MONGODB_TOKEN', function (error, result) { if (error) return callback(error); - const readStream = fs.createReadStream(dumpPath('mongodb', app.id)); - readStream.on('error', (error) => callback(new BoxError(BoxError.FS_ERROR, `Error reading input stream when restoring mongodb: ${error.message}`))); + appdb.getAddonConfigByName(app.id, 'mongodb', '%MONGODB_DATABASE', function (error, database) { + if (error) return callback(error); - const restoreReq = request.post(`https://${result.ip}:3000/databases/${app.id}/restore?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { - if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring mongodb: ${error.message}`)); - if (response.statusCode !== 200) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring mongodb. Status code: ${response.statusCode} message: ${response.body.message}`)); + const readStream = fs.createReadStream(dumpPath('mongodb', app.id)); + readStream.on('error', (error) => callback(new BoxError(BoxError.FS_ERROR, `Error reading input stream when restoring mongodb: ${error.message}`))); - callback(null); + const restoreReq = request.post(`https://${result.ip}:3000/databases/${database}/restore?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { + if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring mongodb: ${error.message}`)); + if (response.statusCode !== 200) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring mongodb. Status code: ${response.statusCode} message: ${response.body.message}`)); + + callback(null); + }); + + readStream.pipe(restoreReq); }); - - readStream.pipe(restoreReq); }); }