merge appdb.js into apps.js

This commit is contained in:
Girish Ramakrishnan
2021-08-20 09:19:44 -07:00
parent b6f2d6d620
commit 77f5cb183b
39 changed files with 1599 additions and 2495 deletions

View File

@@ -2,7 +2,7 @@
exports = module.exports = {
getApp,
getApps,
listByUser,
getAppIcon,
install,
uninstall,
@@ -59,16 +59,15 @@ var apps = require('../apps.js'),
users = require('../users.js'),
WebSocket = require('ws');
function load(req, res, next) {
async function load(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
apps.get(req.params.id, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await apps.get(req.params.id);
if (error) return next(BoxError.toHttpError(error));
req.resource = result;
req.resource = result;
next();
});
next();
}
function getApp(req, res, next) {
@@ -77,29 +76,27 @@ function getApp(req, res, next) {
next(new HttpSuccess(200, apps.removeInternalFields(req.resource)));
}
function getApps(req, res, next) {
async function listByUser(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
apps.getAllByUser(req.user, function (error, allApps) {
if (error) return next(BoxError.toHttpError(error));
let [error, result] = await safe(apps.listByUser(req.user));
if (error) return next(BoxError.toHttpError(error));
allApps = allApps.map(apps.removeRestrictedFields);
result = result.map(apps.removeRestrictedFields);
next(new HttpSuccess(200, { apps: allApps }));
});
next(new HttpSuccess(200, { apps: result }));
}
function getAppIcon(req, res, next) {
async function getAppIcon(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
apps.getIcon(req.resource, { original: req.query.original }, function (error, icon) {
if (error) return next(BoxError.toHttpError(error));
const [error, icon] = await safe(apps.getIcon(req.resource, { original: req.query.original }));
if (error) return next(BoxError.toHttpError(error));
res.send(icon);
});
res.send(icon);
}
function install(req, res, next) {
async function install(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
const data = req.body;
@@ -147,85 +144,79 @@ function install(req, res, next) {
if ('skipDnsSetup' in req.body && typeof req.body.skipDnsSetup !== 'boolean') return next(new HttpError(400, 'skipDnsSetup must be boolean'));
if ('enableMailbox' in req.body && typeof req.body.enableMailbox !== 'boolean') return next(new HttpError(400, 'enableMailbox must be boolean'));
apps.downloadManifest(data.appStoreId, data.manifest, function (error, appStoreId, manifest) {
if (error) return next(BoxError.toHttpError(error));
let [error, result] = await safe(apps.downloadManifest(data.appStoreId, data.manifest));
if (error) return next(BoxError.toHttpError(error));
if (safe.query(manifest, 'addons.docker') && req.user.role !== users.ROLE_OWNER) return next(new HttpError(403, '"owner" role is required to install app with docker addon'));
if (safe.query(result.manifest, 'addons.docker') && req.user.role !== users.ROLE_OWNER) return next(new HttpError(403, '"owner" role is required to install app with docker addon'));
data.appStoreId = appStoreId;
data.manifest = manifest;
apps.install(data, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
data.appStoreId = result.appStoreId;
data.manifest = result.manifest;
next(new HttpSuccess(202, { id: result.id, taskId: result.taskId }));
});
});
[error, result] = await safe(apps.install(data, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { id: result.id, taskId: result.taskId }));
}
function setAccessRestriction(req, res, next) {
async function setAccessRestriction(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (typeof req.body.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object'));
apps.setAccessRestriction(req.resource, req.body.accessRestriction, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(apps.setAccessRestriction(req.resource, req.body.accessRestriction, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
function setLabel(req, res, next) {
async function setLabel(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (typeof req.body.label !== 'string') return next(new HttpError(400, 'label must be a string'));
apps.setLabel(req.resource, req.body.label, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(apps.setLabel(req.resource, req.body.label, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
function setTags(req, res, next) {
async function setTags(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (!Array.isArray(req.body.tags)) return next(new HttpError(400, 'tags must be an array'));
if (req.body.tags.some((t) => typeof t !== 'string')) return next(new HttpError(400, 'tags array must contain strings'));
apps.setTags(req.resource, req.body.tags, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(apps.setTags(req.resource, req.body.tags, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
function setIcon(req, res, next) {
async function setIcon(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (req.body.icon !== null && typeof req.body.icon !== 'string') return next(new HttpError(400, 'icon is null or a base-64 image string'));
apps.setIcon(req.resource, req.body.icon || null /* empty string means null */, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(apps.setIcon(req.resource, req.body.icon || null /* empty string means null */, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
function setMemoryLimit(req, res, next) {
async function setMemoryLimit(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (typeof req.body.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit is not a number'));
apps.setMemoryLimit(req.resource, req.body.memoryLimit, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setMemoryLimit(req.resource, req.body.memoryLimit, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function setCpuShares(req, res, next) {
@@ -241,30 +232,28 @@ function setCpuShares(req, res, next) {
});
}
function setAutomaticBackup(req, res, next) {
async function setAutomaticBackup(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (typeof req.body.enable !== 'boolean') return next(new HttpError(400, 'enable must be a boolean'));
apps.setAutomaticBackup(req.resource, req.body.enable, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(apps.setAutomaticBackup(req.resource, req.body.enable, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
function setAutomaticUpdate(req, res, next) {
async function setAutomaticUpdate(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (typeof req.body.enable !== 'boolean') return next(new HttpError(400, 'enable must be a boolean'));
apps.setAutomaticUpdate(req.resource, req.body.enable, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(apps.setAutomaticUpdate(req.resource, req.body.enable, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
async function setReverseProxyConfig(req, res, next) {
@@ -300,34 +289,32 @@ async function setCertificate(req, res, next) {
next(new HttpSuccess(200, {}));
}
function setEnvironment(req, res, next) {
async function setEnvironment(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (!req.body.env || typeof req.body.env !== 'object') return next(new HttpError(400, 'env must be an object'));
if (Object.keys(req.body.env).some((key) => typeof req.body.env[key] !== 'string')) return next(new HttpError(400, 'env must contain values as strings'));
apps.setEnvironment(req.resource, req.body.env, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setEnvironment(req.resource, req.body.env, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function setDebugMode(req, res, next) {
async function setDebugMode(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (req.body.debugMode !== null && typeof req.body.debugMode !== 'object') return next(new HttpError(400, 'debugMode must be an object'));
apps.setDebugMode(req.resource, req.body.debugMode, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setDebugMode(req.resource, req.body.debugMode, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function setMailbox(req, res, next) {
async function setMailbox(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
@@ -337,14 +324,13 @@ function setMailbox(req, res, next) {
if (typeof req.body.mailboxDomain !== 'string') return next(new HttpError(400, 'mailboxDomain must be a string'));
}
apps.setMailbox(req.resource, req.body, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setMailbox(req.resource, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function setLocation(req, res, next) {
async function setLocation(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
@@ -367,27 +353,25 @@ function setLocation(req, res, next) {
if ('overwriteDns' in req.body && typeof req.body.overwriteDns !== 'boolean') return next(new HttpError(400, 'overwriteDns must be boolean'));
if ('skipDnsSetup' in req.body && typeof req.body.skipDnsSetup !== 'boolean') return next(new HttpError(400, 'skipDnsSetup must be boolean'));
apps.setLocation(req.resource, req.body, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setLocation(req.resource, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function setDataDir(req, res, next) {
async function setDataDir(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (req.body.dataDir !== null && typeof req.body.dataDir !== 'string') return next(new HttpError(400, 'dataDir must be a string'));
apps.setDataDir(req.resource, req.body.dataDir, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setDataDir(req.resource, req.body.dataDir, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function repair(req, res, next) {
async function repair(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
@@ -403,11 +387,10 @@ function repair(req, res, next) {
if (!data.dockerImage || typeof data.dockerImage !== 'string') return next(new HttpError(400, 'dockerImage must be a string'));
}
apps.repair(req.resource, data, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.repair(req.resource, data, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
async function restore(req, res, next) {
@@ -424,11 +407,11 @@ async function restore(req, res, next) {
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function importApp(req, res, next) {
async function importApp(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
var data = req.body;
const data = req.body;
if ('backupId' in data) { // if not provided, we import in-place
if (typeof data.backupId !== 'string') return next(new HttpError(400, 'backupId must be string'));
@@ -448,25 +431,23 @@ function importApp(req, res, next) {
}
}
apps.importApp(req.resource, data, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.importApp(req.resource, data, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function exportApp(req, res, next) {
async function exportApp(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
apps.exportApp(req.resource, {}, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.exportApp(req.resource, {}, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function clone(req, res, next) {
async function clone(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
@@ -480,68 +461,62 @@ function clone(req, res, next) {
if ('overwriteDns' in req.body && typeof req.body.overwriteDns !== 'boolean') return next(new HttpError(400, 'overwriteDns must be boolean'));
if ('skipDnsSetup' in req.body && typeof req.body.skipDnsSetup !== 'boolean') return next(new HttpError(400, 'skipDnsSetup must be boolean'));
apps.clone(req.resource, data, req.user, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.clone(req.resource, data, req.user, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { id: result.id, taskId: result.taskId }));
});
next(new HttpSuccess(201, { id: result.id, taskId: result.taskId }));
}
function backup(req, res, next) {
async function backup(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
apps.backup(req.resource, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.backup(req.resource));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function uninstall(req, res, next) {
async function uninstall(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
apps.uninstall(req.resource, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.uninstall(req.resource, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function start(req, res, next) {
async function start(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
apps.start(req.resource, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.start(req.resource, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function stop(req, res, next) {
async function stop(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
apps.stop(req.resource, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.stop(req.resource, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function restart(req, res, next) {
async function restart(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
apps.restart(req.resource, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.restart(req.resource, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}
function update(req, res, next) {
async function update(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
var data = req.body;
const data = req.body;
// atleast one
if ('manifest' in data && typeof data.manifest !== 'object') return next(new HttpError(400, 'manifest must be an object'));
@@ -551,19 +526,19 @@ function update(req, res, next) {
if ('skipBackup' in data && typeof data.skipBackup !== 'boolean') return next(new HttpError(400, 'skipBackup must be a boolean'));
if ('force' in data && typeof data.force !== 'boolean') return next(new HttpError(400, 'force must be a boolean'));
apps.downloadManifest(data.appStoreId, data.manifest, function (error, appStoreId, manifest) {
if (error) return next(BoxError.toHttpError(error));
let [error, result] = await safe(apps.downloadManifest(data.appStoreId, data.manifest));
if (error) return next(BoxError.toHttpError(error));
if (safe.query(manifest, 'addons.docker') && req.user.role !== users.ROLE_OWNER) return next(new HttpError(403, '"owner" role is required to update app with docker addon'));
const { appStoreId, manifest } = result;
data.appStoreId = appStoreId;
data.manifest = manifest;
apps.update(req.resource, data, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
if (safe.query(manifest, 'addons.docker') && req.user.role !== users.ROLE_OWNER) return next(new HttpError(403, '"owner" role is required to update app with docker addon'));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
});
data.appStoreId = appStoreId;
data.manifest = manifest;
[error, result] = await safe(apps.updateApp(req.resource, data, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
}
// this route is for streaming logs
@@ -789,7 +764,7 @@ function downloadFile(req, res, next) {
});
}
function setMounts(req, res, next) {
async function setMounts(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
@@ -800,9 +775,8 @@ function setMounts(req, res, next) {
if (typeof m.readOnly !== 'boolean') return next(new HttpError(400, 'readOnly must be a boolean'));
}
apps.setMounts(req.resource, req.body.mounts, auditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(apps.setMounts(req.resource, req.body.mounts, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
});
next(new HttpSuccess(202, { taskId: result.taskId }));
}