2016-04-29 23:49:56 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
EventLogError: EventLogError,
|
|
|
|
|
|
|
|
|
|
add: add,
|
|
|
|
|
get: get,
|
2016-04-30 11:49:51 -07:00
|
|
|
getAllPaged: getAllPaged,
|
2017-11-02 14:27:07 -07:00
|
|
|
getByCreationTime: getByCreationTime,
|
2016-07-25 12:36:43 -07:00
|
|
|
cleanup: cleanup,
|
2016-04-30 11:49:51 -07:00
|
|
|
|
2017-03-07 12:44:17 -08:00
|
|
|
// keep in sync with webadmin index.js filter and CLI tool
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_ACTIVATE: 'cloudron.activate',
|
2016-06-17 17:12:55 -05:00
|
|
|
ACTION_APP_CLONE: 'app.clone',
|
2016-04-30 13:34:06 -07:00
|
|
|
ACTION_APP_CONFIGURE: 'app.configure',
|
2019-09-19 17:04:11 -07:00
|
|
|
ACTION_APP_REPAIR: 'app.repair',
|
2016-04-30 13:34:06 -07:00
|
|
|
ACTION_APP_INSTALL: 'app.install',
|
|
|
|
|
ACTION_APP_RESTORE: 'app.restore',
|
|
|
|
|
ACTION_APP_UNINSTALL: 'app.uninstall',
|
|
|
|
|
ACTION_APP_UPDATE: 'app.update',
|
2019-05-07 12:04:43 +02:00
|
|
|
ACTION_APP_UPDATE_FINISH: 'app.update.finish',
|
2017-03-02 17:13:04 +01:00
|
|
|
ACTION_APP_LOGIN: 'app.login',
|
2019-01-17 15:31:34 +01:00
|
|
|
ACTION_APP_OOM: 'app.oom',
|
2019-02-11 12:32:02 -08:00
|
|
|
ACTION_APP_UP: 'app.up',
|
2019-01-17 15:49:04 +01:00
|
|
|
ACTION_APP_DOWN: 'app.down',
|
2018-11-09 18:45:44 -08:00
|
|
|
|
2016-05-01 11:42:12 -07:00
|
|
|
ACTION_BACKUP_FINISH: 'backup.finish',
|
|
|
|
|
ACTION_BACKUP_START: 'backup.start',
|
2019-01-11 12:48:40 -08:00
|
|
|
ACTION_BACKUP_CLEANUP_START: 'backup.cleanup.start',
|
|
|
|
|
ACTION_BACKUP_CLEANUP_FINISH: 'backup.cleanup.finish',
|
2018-11-09 18:45:44 -08:00
|
|
|
|
2018-11-14 20:38:49 -08:00
|
|
|
ACTION_CERTIFICATE_NEW: 'certificate.new',
|
2016-04-30 22:27:33 -07:00
|
|
|
ACTION_CERTIFICATE_RENEWAL: 'certificate.renew',
|
2018-11-09 18:45:44 -08:00
|
|
|
|
2019-02-04 20:24:28 -08:00
|
|
|
ACTION_DASHBOARD_DOMAIN_UPDATE: 'dashboard.domain.update',
|
|
|
|
|
|
2018-11-10 00:43:46 -08:00
|
|
|
ACTION_DOMAIN_ADD: 'domain.add',
|
|
|
|
|
ACTION_DOMAIN_UPDATE: 'domain.update',
|
|
|
|
|
ACTION_DOMAIN_REMOVE: 'domain.remove',
|
|
|
|
|
|
2018-11-09 18:51:58 -08:00
|
|
|
ACTION_MAIL_ENABLED: 'mail.enabled',
|
2018-11-10 00:32:27 -08:00
|
|
|
ACTION_MAIL_DISABLED: 'mail.disabled',
|
2018-11-09 18:49:55 -08:00
|
|
|
ACTION_MAIL_MAILBOX_ADD: 'mail.box.add',
|
|
|
|
|
ACTION_MAIL_MAILBOX_REMOVE: 'mail.box.remove',
|
|
|
|
|
ACTION_MAIL_LIST_ADD: 'mail.list.add',
|
|
|
|
|
ACTION_MAIL_LIST_REMOVE: 'mail.list.remove',
|
2018-11-09 18:45:44 -08:00
|
|
|
|
2018-11-10 00:43:46 -08:00
|
|
|
ACTION_PROVISION: 'cloudron.provision',
|
|
|
|
|
ACTION_RESTORE: 'cloudron.restore', // unused
|
2016-05-02 09:39:38 -07:00
|
|
|
ACTION_START: 'cloudron.start',
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_UPDATE: 'cloudron.update',
|
2019-08-03 13:59:11 -07:00
|
|
|
ACTION_UPDATE_FINISH: 'cloudron.update.finish',
|
2018-11-09 18:45:44 -08:00
|
|
|
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_USER_ADD: 'user.add',
|
2016-04-30 23:16:37 -07:00
|
|
|
ACTION_USER_LOGIN: 'user.login',
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_USER_REMOVE: 'user.remove',
|
2018-07-05 13:51:22 -07:00
|
|
|
ACTION_USER_UPDATE: 'user.update',
|
|
|
|
|
ACTION_USER_TRANSFER: 'user.transfer',
|
2019-01-12 09:58:11 -08:00
|
|
|
|
2019-01-19 13:22:29 +01:00
|
|
|
ACTION_DYNDNS_UPDATE: 'dyndns.update',
|
|
|
|
|
|
|
|
|
|
ACTION_PROCESS_CRASH: 'system.crash'
|
2016-04-29 23:49:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
DatabaseError = require('./databaseerror.js'),
|
2016-04-30 11:49:51 -07:00
|
|
|
debug = require('debug')('box:eventlog'),
|
2016-04-29 23:49:56 -07:00
|
|
|
eventlogdb = require('./eventlogdb.js'),
|
2019-01-17 13:12:26 +01:00
|
|
|
notifications = require('./notifications.js'),
|
2016-04-29 23:49:56 -07:00
|
|
|
util = require('util'),
|
2017-08-13 17:44:31 -07:00
|
|
|
uuid = require('uuid');
|
2016-04-29 23:49:56 -07:00
|
|
|
|
2016-04-30 11:49:51 -07:00
|
|
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
|
|
2016-04-29 23:49:56 -07:00
|
|
|
function EventLogError(reason, errorOrMessage) {
|
|
|
|
|
assert.strictEqual(typeof reason, 'string');
|
|
|
|
|
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
|
|
|
|
|
|
|
|
|
|
Error.call(this);
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
|
|
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
|
this.reason = reason;
|
|
|
|
|
if (typeof errorOrMessage === 'undefined') {
|
|
|
|
|
this.message = reason;
|
|
|
|
|
} else if (typeof errorOrMessage === 'string') {
|
|
|
|
|
this.message = errorOrMessage;
|
|
|
|
|
} else {
|
|
|
|
|
this.message = 'Internal error';
|
|
|
|
|
this.nestedError = errorOrMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
util.inherits(EventLogError, Error);
|
|
|
|
|
EventLogError.INTERNAL_ERROR = 'Internal error';
|
2016-04-30 13:00:44 -07:00
|
|
|
EventLogError.NOT_FOUND = 'Not Found';
|
2016-04-29 23:49:56 -07:00
|
|
|
|
2016-05-01 20:01:34 -07:00
|
|
|
function add(action, source, data, callback) {
|
2016-04-29 23:49:56 -07:00
|
|
|
assert.strictEqual(typeof action, 'string');
|
2016-05-01 20:01:34 -07:00
|
|
|
assert.strictEqual(typeof source, 'object');
|
2016-04-29 23:49:56 -07:00
|
|
|
assert.strictEqual(typeof data, 'object');
|
2016-04-30 11:49:51 -07:00
|
|
|
assert(!callback || typeof callback === 'function');
|
|
|
|
|
|
|
|
|
|
callback = callback || NOOP_CALLBACK;
|
2016-04-29 23:49:56 -07:00
|
|
|
|
2019-02-05 15:27:43 +01:00
|
|
|
// we do only daily upserts for login actions, so they don't spam the db
|
|
|
|
|
var api = action === exports.ACTION_USER_LOGIN ? eventlogdb.upsert : eventlogdb.add;
|
|
|
|
|
api(uuid.v4(), action, source, data, function (error, id) {
|
2016-04-29 23:49:56 -07:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
2019-02-27 16:10:54 -08:00
|
|
|
notifications.onEvent(id, action, source, data, function (error) {
|
|
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null, { id: id });
|
|
|
|
|
});
|
2016-04-29 23:49:56 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(id, callback) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
eventlogdb.get(id, function (error, result) {
|
2016-04-30 13:00:44 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new EventLogError(EventLogError.NOT_FOUND, 'No such event'));
|
2016-04-29 23:49:56 -07:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 11:14:36 +01:00
|
|
|
function getAllPaged(actions, search, page, perPage, callback) {
|
|
|
|
|
assert(Array.isArray(actions));
|
2016-05-06 16:49:17 +02:00
|
|
|
assert(typeof search === 'string' || search === null);
|
|
|
|
|
assert.strictEqual(typeof page, 'number');
|
|
|
|
|
assert.strictEqual(typeof perPage, 'number');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2018-03-05 11:14:36 +01:00
|
|
|
eventlogdb.getAllPaged(actions, search, page, perPage, function (error, events) {
|
2016-05-06 16:49:17 +02:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
2017-11-02 14:27:07 -07:00
|
|
|
callback(null, events);
|
2016-05-06 16:49:17 +02:00
|
|
|
});
|
|
|
|
|
}
|
2016-07-25 12:36:43 -07:00
|
|
|
|
2017-11-02 14:27:07 -07:00
|
|
|
function getByCreationTime(creationTime, callback) {
|
|
|
|
|
assert(util.isDate(creationTime));
|
2017-07-21 16:13:44 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-11-02 14:27:07 -07:00
|
|
|
eventlogdb.getByCreationTime(creationTime, function (error, events) {
|
2017-07-21 16:13:44 +02:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
2017-11-02 14:27:07 -07:00
|
|
|
callback(null, events);
|
2017-07-21 16:13:44 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 12:36:43 -07:00
|
|
|
function cleanup(callback) {
|
|
|
|
|
callback = callback || NOOP_CALLBACK;
|
|
|
|
|
|
2016-11-18 11:32:11 +01:00
|
|
|
var d = new Date();
|
2017-11-02 14:27:07 -07:00
|
|
|
d.setDate(d.getDate() - 10); // 10 days ago
|
2016-07-25 12:36:43 -07:00
|
|
|
|
2018-03-22 20:26:45 -07:00
|
|
|
eventlogdb.delByCreationTime(d, function (error) {
|
2016-07-25 12:36:43 -07:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|