Change tenant id to tenant object for logging storage
MODIFY
src/scheduler/tasks/LoggingDatabaseTableCleanupTask.ts
src/scheduler/tasks/LoggingDatabaseTableCleanupTask.ts
@@ -16,7 +16,7 @@ const MODULE_NAME = 'LoggingDatabaseTableCleanupTask';
export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
public async run(name: string, config: TaskConfig): Promise<void> {
// Delete
- await this.deleteLogs(Constants.DEFAULT_TENANT, config);
+ await this.deleteLogs(Constants.DEFAULT_TENANT_OBJECT, config);
// Delete Perfs Records
await this.deletePerformanceRecords(Constants.DEFAULT_TENANT, config);
// Call for all Tenants
@@ -25,29 +25,29 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
async processTenant(tenant: Tenant, config: LoggingDatabaseTableCleanupTaskConfig): Promise<void> {
// Delete Logs
- await this.deleteLogs(tenant.id, config);
+ await this.deleteLogs(tenant, config);
}
- private async deleteLogs(tenantID: string, config: LoggingDatabaseTableCleanupTaskConfig) {
+ private async deleteLogs(tenant: Tenant, config: LoggingDatabaseTableCleanupTaskConfig) {
// Get the lock
- const logsCleanUpLock = LockingManager.createExclusiveLock(tenantID, LockEntity.LOGGING, 'cleanup');
+ const logsCleanUpLock = LockingManager.createExclusiveLock(tenant.id, LockEntity.LOGGING, 'cleanup');
if (await LockingManager.acquire(logsCleanUpLock)) {
try {
// Delete Standard Logs
const deleteUpToDate = moment().subtract(config.retentionPeriodWeeks, 'w').toDate();
// Delete
- let result = await LoggingStorage.deleteLogs(tenantID, deleteUpToDate);
+ let result = await LoggingStorage.deleteLogs(tenant, deleteUpToDate);
// Ok?
if (result.acknowledged) {
await Logging.logSecurityInfo({
- tenantID: tenantID,
+ tenantID: tenant.id,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
message: `${result.deletedCount} Log(s) have been deleted successfully before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
});
} else {
await Logging.logError({
- tenantID: tenantID,
+ tenantID: tenant.id,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
message: `An error occurred when deleting Logs before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`,
@@ -57,18 +57,18 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
// Delete Security Logs
const securityDeleteUpToDate: Date = moment().subtract(config.securityRetentionPeriodWeeks, 'w').startOf('week').toDate();
// Delete
- result = await LoggingStorage.deleteSecurityLogs(tenantID, securityDeleteUpToDate);
+ result = await LoggingStorage.deleteSecurityLogs(tenant, securityDeleteUpToDate);
// Ok?
if (result.acknowledged) {
await Logging.logSecurityInfo({
- tenantID: tenantID,
+ tenantID: tenant.id,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
message: `${result.deletedCount} Security Log(s) have been deleted before '${moment(securityDeleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
});
} else {
await Logging.logSecurityError({
- tenantID: tenantID,
+ tenantID: tenant.id,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
message: `An error occurred when deleting Security Logs before '${moment(securityDeleteUpToDate).format('DD/MM/YYYY h:mm A')}'`,
@@ -76,7 +76,7 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
});
}
} catch (error) {
- await Logging.logActionExceptionMessage(tenantID, ServerAction.LOGS_CLEANUP, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.LOGS_CLEANUP, error);
} finally {
await LockingManager.release(logsCleanUpLock);
}

MODIFY
src/storage/mongodb/LoggingStorage.ts
src/storage/mongodb/LoggingStorage.ts
@@ -5,13 +5,14 @@ import Constants from '../../utils/Constants';
import DatabaseUtils from './DatabaseUtils';
import DbParams from '../../types/database/DbParams';
import { Log } from '../../types/Log';
+import Tenant from '../../types/Tenant';
import Utils from '../../utils/Utils';
import cluster from 'cluster';
export default class LoggingStorage {
- public static async deleteLogs(tenantID: string, deleteUpToDate: Date): Promise<DeletedResult> {
+ public static async deleteLogs(tenant: Tenant, deleteUpToDate: Date): Promise<DeletedResult> {
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Build filter
const filters: FilterParams = {};
// Do Not Delete Security Logs
@@ -25,15 +26,15 @@ export default class LoggingStorage {
return;
}
// Delete Logs
- const result = await global.database.getCollection<Log>(tenantID, 'logs')
+ const result = await global.database.getCollection<Log>(tenant.id, 'logs')
.deleteMany(filters);
// Return the result
return { acknowledged: result.acknowledged, deletedCount: result.deletedCount };
}
- public static async deleteSecurityLogs(tenantID: string, deleteUpToDate: Date): Promise<DeletedResult> {
+ public static async deleteSecurityLogs(tenant: Tenant, deleteUpToDate: Date): Promise<DeletedResult> {
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Build filter
const filters: FilterParams = {};
// Delete Only Security Logs
@@ -47,7 +48,7 @@ export default class LoggingStorage {
return;
}
// Delete Logs
- const result = await global.database.getCollection<Log>(tenantID, 'logs')
+ const result = await global.database.getCollection<Log>(tenant.id, 'logs')
.deleteMany(filters);
// Return the result
return { acknowledged: result.acknowledged, deletedCount: result.deletedCount };
@@ -77,19 +78,19 @@ export default class LoggingStorage {
}
}
- public static async getLog(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID, projectFields: string[]): Promise<Log> {
- const logsMDB = await LoggingStorage.getLogs(tenantID, {
+ public static async getLog(tenant: Tenant, id: string = Constants.UNKNOWN_OBJECT_ID, projectFields: string[]): Promise<Log> {
+ const logsMDB = await LoggingStorage.getLogs(tenant, {
logIDs: [id]
}, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return logsMDB.count === 1 ? logsMDB.result[0] : null;
}
- public static async getLogs(tenantID: string, params: {
+ public static async getLogs(tenant: Tenant, params: {
startDateTime?: Date; endDateTime?: Date; levels?: string[]; sources?: string[]; type?: string; actions?: string[];
hosts?: string[]; userIDs?: string[]; search?: string; logIDs?: string[];
} = {}, dbParams: DbParams, projectFields: string[]): Promise<DataResult<Log>> {
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Clone before updating the values
dbParams = Utils.cloneObject(dbParams);
// Check Limit
@@ -160,7 +161,7 @@ export default class LoggingStorage {
// Always limit the nbr of record to avoid perfs issues
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
- const loggingsCountMDB = await global.database.getCollection<any>(tenantID, 'logs')
+ const loggingsCountMDB = await global.database.getCollection<any>(tenant.id, 'logs')
.aggregate([...aggregation, { $count: 'count' }])
.toArray();
// Check if only the total count is requested
@@ -189,7 +190,7 @@ export default class LoggingStorage {
});
// Add Users
DatabaseUtils.pushUserLookupInAggregation({
- tenantID,
+ tenantID: tenant.id,
aggregation: aggregation,
asField: 'user',
localField: 'userID',
@@ -200,7 +201,7 @@ export default class LoggingStorage {
{ $project: { name: 1, firstName: 1 } }
]);
DatabaseUtils.pushUserLookupInAggregation({
- tenantID,
+ tenantID: tenant.id,
aggregation: aggregation,
asField: 'actionOnUser',
localField: 'actionOnUserID',
@@ -221,7 +222,7 @@ export default class LoggingStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const loggingsMDB = await global.database.getCollection<Log>(tenantID, 'logs')
+ const loggingsMDB = await global.database.getCollection<Log>(tenant.id, 'logs')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Ok

Change tenant id to tenant object for logging storage