make auditsource a class

this allows us to use AuditSource for the class and auditSource for
the instances!
This commit is contained in:
Girish Ramakrishnan
2021-09-30 09:50:30 -07:00
parent 339fdfbea1
commit 445c83c8b9
22 changed files with 138 additions and 131 deletions
+30 -30
View File
@@ -56,7 +56,7 @@ exports = module.exports = {
const apps = require('../apps.js'),
assert = require('assert'),
auditSource = require('../auditsource.js'),
AuditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
debug = require('debug')('box:routes/apps'),
HttpError = require('connect-lastmile').HttpError,
@@ -167,7 +167,7 @@ async function install(req, res, next) {
data.appStoreId = result.appStoreId;
data.manifest = result.manifest;
[error, result] = await safe(apps.install(data, auditSource.fromRequest(req)));
[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 }));
@@ -179,7 +179,7 @@ async function setAccessRestriction(req, res, next) {
if (typeof req.body.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object'));
const [error] = await safe(apps.setAccessRestriction(req.app, req.body.accessRestriction, auditSource.fromRequest(req)));
const [error] = await safe(apps.setAccessRestriction(req.app, req.body.accessRestriction, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -191,7 +191,7 @@ async function setOperators(req, res, next) {
if (typeof req.body.operators !== 'object') return next(new HttpError(400, 'operators must be an object'));
const [error] = await safe(apps.setOperators(req.app, req.body.operators, auditSource.fromRequest(req)));
const [error] = await safe(apps.setOperators(req.app, req.body.operators, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -203,7 +203,7 @@ async function setCrontab(req, res, next) {
if (req.body.crontab !== null && typeof req.body.crontab !== 'string') return next(new HttpError(400, 'crontab must be a string'));
const [error] = await safe(apps.setCrontab(req.app, req.body.crontab, auditSource.fromRequest(req)));
const [error] = await safe(apps.setCrontab(req.app, req.body.crontab, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -215,7 +215,7 @@ async function setLabel(req, res, next) {
if (typeof req.body.label !== 'string') return next(new HttpError(400, 'label must be a string'));
const [error] = await safe(apps.setLabel(req.app, req.body.label, auditSource.fromRequest(req)));
const [error] = await safe(apps.setLabel(req.app, req.body.label, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -228,7 +228,7 @@ async function setTags(req, res, next) {
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'));
const [error] = await safe(apps.setTags(req.app, req.body.tags, auditSource.fromRequest(req)));
const [error] = await safe(apps.setTags(req.app, req.body.tags, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -240,7 +240,7 @@ async function setIcon(req, res, next) {
if (req.body.icon !== null && typeof req.body.icon !== 'string') return next(new HttpError(400, 'icon is null or a base-64 image string'));
const [error] = await safe(apps.setIcon(req.app, req.body.icon || null /* empty string means null */, auditSource.fromRequest(req)));
const [error] = await safe(apps.setIcon(req.app, req.body.icon || null /* empty string means null */, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -252,7 +252,7 @@ async function setMemoryLimit(req, res, next) {
if (typeof req.body.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit is not a number'));
const [error, result] = await safe(apps.setMemoryLimit(req.app, req.body.memoryLimit, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setMemoryLimit(req.app, req.body.memoryLimit, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -264,7 +264,7 @@ function setCpuShares(req, res, next) {
if (typeof req.body.cpuShares !== 'number') return next(new HttpError(400, 'cpuShares is not a number'));
apps.setCpuShares(req.app, req.body.cpuShares, auditSource.fromRequest(req), function (error, result) {
apps.setCpuShares(req.app, req.body.cpuShares, AuditSource.fromRequest(req), function (error, result) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -277,7 +277,7 @@ async function setAutomaticBackup(req, res, next) {
if (typeof req.body.enable !== 'boolean') return next(new HttpError(400, 'enable must be a boolean'));
const [error] = await safe(apps.setAutomaticBackup(req.app, req.body.enable, auditSource.fromRequest(req)));
const [error] = await safe(apps.setAutomaticBackup(req.app, req.body.enable, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -289,7 +289,7 @@ async function setAutomaticUpdate(req, res, next) {
if (typeof req.body.enable !== 'boolean') return next(new HttpError(400, 'enable must be a boolean'));
const [error] = await safe(apps.setAutomaticUpdate(req.app, req.body.enable, auditSource.fromRequest(req)));
const [error] = await safe(apps.setAutomaticUpdate(req.app, req.body.enable, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -303,7 +303,7 @@ async function setReverseProxyConfig(req, res, next) {
if (req.body.csp !== null && typeof req.body.csp !== 'string') return next(new HttpError(400, 'csp is not a string'));
const [error] = await safe(apps.setReverseProxyConfig(req.app, req.body, auditSource.fromRequest(req)));
const [error] = await safe(apps.setReverseProxyConfig(req.app, req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -322,7 +322,7 @@ async function setCertificate(req, res, next) {
if (req.body.cert && !req.body.key) return next(new HttpError(400, 'key must be provided'));
if (!req.body.cert && req.body.key) return next(new HttpError(400, 'cert must be provided'));
const [error] = await safe(apps.setCertificate(req.app, req.body, auditSource.fromRequest(req)));
const [error] = await safe(apps.setCertificate(req.app, req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
@@ -335,7 +335,7 @@ async function setEnvironment(req, res, next) {
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'));
const [error, result] = await safe(apps.setEnvironment(req.app, req.body.env, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setEnvironment(req.app, req.body.env, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -347,7 +347,7 @@ async function setDebugMode(req, res, next) {
if (req.body.debugMode !== null && typeof req.body.debugMode !== 'object') return next(new HttpError(400, 'debugMode must be an object'));
const [error, result] = await safe(apps.setDebugMode(req.app, req.body.debugMode, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setDebugMode(req.app, req.body.debugMode, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -363,7 +363,7 @@ async function setMailbox(req, res, next) {
if (typeof req.body.mailboxDomain !== 'string') return next(new HttpError(400, 'mailboxDomain must be a string'));
}
const [error, result] = await safe(apps.setMailbox(req.app, req.body, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setMailbox(req.app, req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -392,7 +392,7 @@ async 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'));
const [error, result] = await safe(apps.setLocation(req.app, req.body, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setLocation(req.app, req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -404,7 +404,7 @@ async function setDataDir(req, res, next) {
if (req.body.dataDir !== null && typeof req.body.dataDir !== 'string') return next(new HttpError(400, 'dataDir must be a string'));
const [error, result] = await safe(apps.setDataDir(req.app, req.body.dataDir, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setDataDir(req.app, req.body.dataDir, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -426,7 +426,7 @@ async function repair(req, res, next) {
if (!data.dockerImage || typeof data.dockerImage !== 'string') return next(new HttpError(400, 'dockerImage must be a string'));
}
const [error, result] = await safe(apps.repair(req.app, data, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.repair(req.app, data, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -440,7 +440,7 @@ async function restore(req, res, next) {
if (!data.backupId || typeof data.backupId !== 'string') return next(new HttpError(400, 'backupId must be non-empty string'));
const [error, result] = await safe(apps.restore(req.app, data.backupId, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.restore(req.app, data.backupId, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -470,7 +470,7 @@ async function importApp(req, res, next) {
}
}
const [error, result] = await safe(apps.importApp(req.app, data, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.importApp(req.app, data, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -480,7 +480,7 @@ async function exportApp(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.app, 'object');
const [error, result] = await safe(apps.exportApp(req.app, {}, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.exportApp(req.app, {}, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -500,7 +500,7 @@ async 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'));
const [error, result] = await safe(apps.clone(req.app, data, req.user, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.clone(req.app, data, req.user, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { id: result.id, taskId: result.taskId }));
@@ -518,7 +518,7 @@ async function backup(req, res, next) {
async function uninstall(req, res, next) {
assert.strictEqual(typeof req.app, 'object');
const [error, result] = await safe(apps.uninstall(req.app, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.uninstall(req.app, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -527,7 +527,7 @@ async function uninstall(req, res, next) {
async function start(req, res, next) {
assert.strictEqual(typeof req.app, 'object');
const [error, result] = await safe(apps.start(req.app, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.start(req.app, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -536,7 +536,7 @@ async function start(req, res, next) {
async function stop(req, res, next) {
assert.strictEqual(typeof req.app, 'object');
const [error, result] = await safe(apps.stop(req.app, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.stop(req.app, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -545,7 +545,7 @@ async function stop(req, res, next) {
async function restart(req, res, next) {
assert.strictEqual(typeof req.app, 'object');
const [error, result] = await safe(apps.restart(req.app, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.restart(req.app, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -574,7 +574,7 @@ async function update(req, res, next) {
data.appStoreId = appStoreId;
data.manifest = manifest;
[error, result] = await safe(apps.updateApp(req.app, data, auditSource.fromRequest(req)));
[error, result] = await safe(apps.updateApp(req.app, data, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));
@@ -812,7 +812,7 @@ async function setMounts(req, res, next) {
if (typeof m.readOnly !== 'boolean') return next(new HttpError(400, 'readOnly must be a boolean'));
}
const [error, result] = await safe(apps.setMounts(req.app, req.body.mounts, auditSource.fromRequest(req)));
const [error, result] = await safe(apps.setMounts(req.app, req.body.mounts, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId: result.taskId }));