diff --git a/src/apphealthmonitor.js b/src/apphealthmonitor.js index 034bf54ff..fb3d3e8b3 100644 --- a/src/apphealthmonitor.js +++ b/src/apphealthmonitor.js @@ -38,14 +38,14 @@ async function setHealth(app, health) { debug(`setHealth: ${app.id} (${app.fqdn}) switched from ${lastHealth} to healthy`); // do not send mails for dev apps - if (!app.debugMode) eventlog.add(eventlog.ACTION_APP_UP, AuditSource.HEALTH_MONITOR, { app: app }); + if (!app.debugMode) await eventlog.add(eventlog.ACTION_APP_UP, AuditSource.HEALTH_MONITOR, { app: app }); } } else if (Math.abs(now - healthTime) > UNHEALTHY_THRESHOLD) { if (lastHealth === apps.HEALTH_HEALTHY) { debug(`setHealth: marking ${app.id} (${app.fqdn}) as unhealthy since not seen for more than ${UNHEALTHY_THRESHOLD/(60 * 1000)} minutes`); // do not send mails for dev apps - if (!app.debugMode) eventlog.add(eventlog.ACTION_APP_DOWN, AuditSource.HEALTH_MONITOR, { app: app }); + if (!app.debugMode) await eventlog.add(eventlog.ACTION_APP_DOWN, AuditSource.HEALTH_MONITOR, { app: app }); } } else { debug(`setHealth: ${app.id} (${app.fqdn}) waiting for ${(UNHEALTHY_THRESHOLD - Math.abs(now - healthTime))/1000} to update health`); diff --git a/src/appstore.js b/src/appstore.js index b8a404968..01218fb91 100644 --- a/src/appstore.js +++ b/src/appstore.js @@ -408,7 +408,7 @@ async function createTicket(info, auditSource) { if (response.status === 422) throw new BoxError(BoxError.LICENSE_ERROR, response.body.message); if (response.status !== 201) throw new BoxError(BoxError.EXTERNAL_ERROR, util.format('Bad response: %s %s', response.status, response.text)); - eventlog.add(eventlog.ACTION_SUPPORT_TICKET, auditSource, info); + await eventlog.add(eventlog.ACTION_SUPPORT_TICKET, auditSource, info); return { message: `An email was sent to ${constants.SUPPORT_EMAIL}. We will get back shortly!` }; } diff --git a/src/backups.js b/src/backups.js index 9b98cb3dd..3507745ee 100644 --- a/src/backups.js +++ b/src/backups.js @@ -268,15 +268,15 @@ async function startCleanupTask(auditSource) { const taskId = await tasks.add(tasks.TASK_CLEAN_BACKUPS, []); - tasks.startTask(taskId, {}, (error, result) => { // result is { removedBoxBackupIds, removedAppBackupIds, removedMailBackupIds, missingBackupIds } - eventlog.add(eventlog.ACTION_BACKUP_CLEANUP_FINISH, auditSource, { + tasks.startTask(taskId, {}, async (error, result) => { // result is { removedBoxBackupIds, removedAppBackupIds, removedMailBackupIds, missingBackupIds } + await safe(eventlog.add(eventlog.ACTION_BACKUP_CLEANUP_FINISH, auditSource, { taskId, errorMessage: error ? error.message : null, removedBoxBackupIds: result ? result.removedBoxBackupIds : [], removedMailBackupIds: result ? result.removedMailBackupIds : [], removedAppBackupIds: result ? result.removedAppBackupIds : [], missingBackupIds: result ? result.missingBackupIds : [] - }); + }), { debug }); }); return taskId; diff --git a/src/domains.js b/src/domains.js index 729ef05ae..9b678bc19 100644 --- a/src/domains.js +++ b/src/domains.js @@ -165,7 +165,7 @@ async function add(domain, data, auditSource) { await reverseProxy.setFallbackCertificate(domain, fallbackCertificate); - eventlog.add(eventlog.ACTION_DOMAIN_ADD, auditSource, { domain, zoneName, provider }); + await eventlog.add(eventlog.ACTION_DOMAIN_ADD, auditSource, { domain, zoneName, provider }); safe(mail.onDomainAdded(domain)); // background } @@ -247,7 +247,7 @@ async function setConfig(domain, data, auditSource) { await reverseProxy.setFallbackCertificate(domain, fallbackCertificate); - eventlog.add(eventlog.ACTION_DOMAIN_UPDATE, auditSource, { domain, zoneName, provider }); + await eventlog.add(eventlog.ACTION_DOMAIN_UPDATE, auditSource, { domain, zoneName, provider }); } async function setWellKnown(domain, wellKnown, auditSource) { @@ -262,7 +262,7 @@ async function setWellKnown(domain, wellKnown, auditSource) { if (error && error.reason === BoxError.NOT_FOUND) throw new BoxError(BoxError.NOT_FOUND, 'Domain not found'); if (error) throw new BoxError(BoxError.DATABASE_ERROR, error); - eventlog.add(eventlog.ACTION_DOMAIN_UPDATE, auditSource, { domain, wellKnown }); + await eventlog.add(eventlog.ACTION_DOMAIN_UPDATE, auditSource, { domain, wellKnown }); } async function del(domain, auditSource) { @@ -287,7 +287,7 @@ async function del(domain, auditSource) { if (error) throw error; if (results[1].affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Domain not found'); - eventlog.add(eventlog.ACTION_DOMAIN_REMOVE, auditSource, { domain }); + await eventlog.add(eventlog.ACTION_DOMAIN_REMOVE, auditSource, { domain }); safe(mail.onDomainRemoved(domain)); } diff --git a/src/mail.js b/src/mail.js index 733a337af..a243d8ab6 100644 --- a/src/mail.js +++ b/src/mail.js @@ -1171,7 +1171,7 @@ async function addMailbox(name, domain, data, auditSource) { if (error && error.code === 'ER_NO_REFERENCED_ROW_2' && error.sqlMessage.includes('mailboxes_domain_constraint')) throw new BoxError(BoxError.NOT_FOUND, `no such domain '${domain}'`); if (error) throw error; - eventlog.add(eventlog.ACTION_MAIL_MAILBOX_ADD, auditSource, { name, domain, ownerId, ownerType, active }); + await eventlog.add(eventlog.ACTION_MAIL_MAILBOX_ADD, auditSource, { name, domain, ownerId, ownerType, active }); } async function updateMailbox(name, domain, data, auditSource) { @@ -1196,7 +1196,7 @@ async function updateMailbox(name, domain, data, auditSource) { const result = await database.query('UPDATE mailboxes SET ownerId = ?, ownerType = ?, active = ?, enablePop3 = ? WHERE name = ? AND domain = ?', [ ownerId, ownerType, active, enablePop3, name, domain ]); if (result.affectedRows === 0) throw new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'); - eventlog.add(eventlog.ACTION_MAIL_MAILBOX_UPDATE, auditSource, { name, domain, oldUserId: mailbox.userId, ownerId, ownerType, active }); + await eventlog.add(eventlog.ACTION_MAIL_MAILBOX_UPDATE, auditSource, { name, domain, oldUserId: mailbox.userId, ownerId, ownerType, active }); } async function removeSolrIndex(mailbox) { @@ -1233,7 +1233,7 @@ async function delMailbox(name, domain, options, auditSource) { const [error] = await safe(removeSolrIndex(mailbox)); if (error) debug(`delMailbox: failed to remove solr index: ${error.message}`); - eventlog.add(eventlog.ACTION_MAIL_MAILBOX_REMOVE, auditSource, { name, domain }); + await eventlog.add(eventlog.ACTION_MAIL_MAILBOX_REMOVE, auditSource, { name, domain }); } async function getAlias(name, domain) { @@ -1348,7 +1348,7 @@ async function addList(name, domain, data, auditSource) { if (error && error.code === 'ER_DUP_ENTRY') throw new BoxError(BoxError.ALREADY_EXISTS, 'mailbox already exists'); if (error) throw error; - eventlog.add(eventlog.ACTION_MAIL_LIST_ADD, auditSource, { name, domain, members, membersOnly, active }); + await eventlog.add(eventlog.ACTION_MAIL_LIST_ADD, auditSource, { name, domain, members, membersOnly, active }); } async function updateList(name, domain, data, auditSource) { @@ -1375,7 +1375,7 @@ async function updateList(name, domain, data, auditSource) { [ JSON.stringify(members), membersOnly, active, name, domain ]); if (result.affectedRows === 0) throw new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'); - eventlog.add(eventlog.ACTION_MAIL_LIST_UPDATE, auditSource, { name, domain, oldMembers: result.members, members, membersOnly, active }); + await eventlog.add(eventlog.ACTION_MAIL_LIST_UPDATE, auditSource, { name, domain, oldMembers: result.members, members, membersOnly, active }); } async function delList(name, domain, auditSource) { @@ -1387,7 +1387,7 @@ async function delList(name, domain, auditSource) { const result = await database.query('DELETE FROM mailboxes WHERE ((name=? AND domain=?) OR (aliasName = ? AND aliasDomain=?))', [ name, domain, name, domain ]); if (result.affectedRows === 0) throw new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'); - eventlog.add(eventlog.ACTION_MAIL_LIST_REMOVE, auditSource, { name, domain }); + await eventlog.add(eventlog.ACTION_MAIL_LIST_REMOVE, auditSource, { name, domain }); } // resolves the members of a list. i.e the lists and aliases diff --git a/src/provision.js b/src/provision.js index 807def63b..2f86787dc 100644 --- a/src/provision.js +++ b/src/provision.js @@ -142,7 +142,7 @@ async function activate(username, password, email, displayName, ip, auditSource) const token = { clientId: tokens.ID_WEBADMIN, identifier: ownerId, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS }; const result = await tokens.add(token); - eventlog.add(eventlog.ACTION_ACTIVATE, auditSource, {}); + await eventlog.add(eventlog.ACTION_ACTIVATE, auditSource, {}); setImmediate(() => safe(cloudron.onActivated({}), { debug })); diff --git a/src/routes/cloudron.js b/src/routes/cloudron.js index 8d649a9be..0c6b9b665 100644 --- a/src/routes/cloudron.js +++ b/src/routes/cloudron.js @@ -63,7 +63,7 @@ async function login(req, res, next) { [error, token] = await safe(tokens.add({ clientId: type, identifier: req.user.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS })); if (error) return next(new HttpError(500, error)); - eventlog.add(eventlog.ACTION_USER_LOGIN, auditSource, { userId: req.user.id, user: users.removePrivateFields(req.user) }); + await eventlog.add(eventlog.ACTION_USER_LOGIN, auditSource, { userId: req.user.id, user: users.removePrivateFields(req.user) }); if (!req.user.ghost) safe(users.notifyLoginLocation(req.user, ip, userAgent, auditSource), { debug }); @@ -73,7 +73,7 @@ async function login(req, res, next) { async function logout(req, res) { assert.strictEqual(typeof req.access_token, 'string'); - eventlog.add(eventlog.ACTION_USER_LOGOUT, AuditSource.fromRequest(req), { userId: req.user.id, user: users.removePrivateFields(req.user) }); + await eventlog.add(eventlog.ACTION_USER_LOGOUT, AuditSource.fromRequest(req), { userId: req.user.id, user: users.removePrivateFields(req.user) }); await safe(tokens.delByAccessToken(req.access_token)); res.redirect('/login.html'); diff --git a/src/updater.js b/src/updater.js index 0e314418a..0288957a4 100644 --- a/src/updater.js +++ b/src/updater.js @@ -195,15 +195,15 @@ async function updateToLatest(options, auditSource) { const [taskError, taskId] = await safe(tasks.add(tasks.TASK_UPDATE, [ boxUpdateInfo, options ])); if (taskError) throw taskError; - eventlog.add(eventlog.ACTION_UPDATE, auditSource, { taskId, boxUpdateInfo }); + await eventlog.add(eventlog.ACTION_UPDATE, auditSource, { taskId, boxUpdateInfo }); - tasks.startTask(taskId, { timeout: 20 * 60 * 60 * 1000 /* 20 hours */, nice: 15, memoryLimit }, (error) => { + tasks.startTask(taskId, { timeout: 20 * 60 * 60 * 1000 /* 20 hours */, nice: 15, memoryLimit }, async (error) => { locker.unlock(locker.OP_BOX_UPDATE); debug('Update failed with error', error); const timedOut = error.code === tasks.ETIMEOUT; - eventlog.add(eventlog.ACTION_UPDATE_FINISH, auditSource, { taskId, errorMessage: error.message, timedOut }); + await safe(eventlog.add(eventlog.ACTION_UPDATE_FINISH, auditSource, { taskId, errorMessage: error.message, timedOut })); }); return taskId; diff --git a/src/users.js b/src/users.js index b4faef128..e0f850a22 100644 --- a/src/users.js +++ b/src/users.js @@ -253,7 +253,7 @@ async function add(email, data, auditSource) { if (!auditSource.userId) auditSource.userId = user.id; if (!auditSource.username) auditSource.username= user.username; - eventlog.add(eventlog.ACTION_USER_ADD, auditSource, { userId: user.id, email: user.email, user: removePrivateFields(user) }); + await eventlog.add(eventlog.ACTION_USER_ADD, auditSource, { userId: user.id, email: user.email, user: removePrivateFields(user) }); return user.id; } @@ -405,7 +405,7 @@ async function del(user, auditSource) { if (error) throw error; if (result[3].affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'User not found'); - await safe(eventlog.add(eventlog.ACTION_USER_REMOVE, auditSource, { userId: user.id, user: removePrivateFields(user) })); + await eventlog.add(eventlog.ACTION_USER_REMOVE, auditSource, { userId: user.id, user: removePrivateFields(user) }); } async function list() { @@ -600,7 +600,7 @@ async function update(user, data, auditSource) { const newUser = _.extend({}, user, data); - eventlog.add(eventlog.ACTION_USER_UPDATE, auditSource, { + await eventlog.add(eventlog.ACTION_USER_UPDATE, auditSource, { userId: user.id, user: removePrivateFields(newUser), roleChanged: newUser.role !== user.role, diff --git a/src/volumes.js b/src/volumes.js index e0364ed9d..60620e37a 100644 --- a/src/volumes.js +++ b/src/volumes.js @@ -100,7 +100,7 @@ async function add(volume, auditSource) { if (error && error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('PRIMARY') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'id already exists'); if (error) throw error; - eventlog.add(eventlog.ACTION_VOLUME_ADD, auditSource, { id, name, hostPath: volume.hostPath }); + await eventlog.add(eventlog.ACTION_VOLUME_ADD, auditSource, { id, name, hostPath: volume.hostPath }); // in theory, we only need to do this mountpoint volumes. but for some reason a restart is required to detect new "mounts" safe(services.rebuildService('sftp', auditSource), { debug }); @@ -114,7 +114,7 @@ async function remount(volume, auditSource) { assert.strictEqual(typeof volume, 'object'); assert.strictEqual(typeof auditSource, 'object'); - eventlog.add(eventlog.ACTION_VOLUME_REMOUNT, auditSource, { volume }); + await eventlog.add(eventlog.ACTION_VOLUME_REMOUNT, auditSource, { volume }); if (!mounts.isManagedProvider(volume.mountType)) throw new BoxError(BoxError.NOT_SUPPORTED, 'Volume does not support remount'); @@ -163,7 +163,7 @@ async function del(volume, auditSource) { if (error) throw error; if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Volume not found'); - eventlog.add(eventlog.ACTION_VOLUME_REMOVE, auditSource, { volume }); + await eventlog.add(eventlog.ACTION_VOLUME_REMOVE, auditSource, { volume }); if (volume.mountType === 'mountpoint' || volume.mountType === 'filesystem') { safe(services.rebuildService('sftp', auditSource), { debug });