Return ObjectID instead of string.
MODIFY
src/storage/mongodb/LoggingStorage.ts
src/storage/mongodb/LoggingStorage.ts
@@ -6,6 +6,7 @@ import { DataResult } from '../../types/DataResult';
import DatabaseUtils from './DatabaseUtils';
import DbParams from '../../types/database/DbParams';
import { Log } from '../../types/Log';
+import { ObjectID } from 'mongodb';
import Utils from '../../utils/Utils';
import cfenv from 'cfenv';
import cluster from 'cluster';
@@ -58,7 +59,7 @@ export default class LoggingStorage {
return result.result;
}
- public static async saveLog(tenantID: string, logToSave: Log): Promise<string> {
+ public static async saveLog(tenantID: string, logToSave: Log): Promise<ObjectID> {
// Check Tenant
await DatabaseUtils.checkTenant(tenantID);
// Set
@@ -80,7 +81,7 @@ export default class LoggingStorage {
// Insert
if (global.database) {
await global.database.getCollection<Log>(tenantID, 'logs').insertOne(logMDB);
- return logMDB._id.toHexString();
+ return logMDB._id;
}
}

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -14,6 +14,7 @@ import LoggingConfiguration from '../types/configuration/LoggingConfiguration';
import LoggingStorage from '../storage/mongodb/LoggingStorage';
import { OCPIResult } from '../types/ocpi/OCPIResult';
import { OCPPStatus } from '../types/ocpp/OCPPClient';
+import { ObjectID } from 'mongodb';
import { ServerAction } from '../types/Server';
import User from '../types/User';
import UserToken from '../types/UserToken';
@@ -71,53 +72,53 @@ export default class Logging {
}
// Log Debug
- public static async logDebug(log: Log): Promise<string> {
+ public static async logDebug(log: Log): Promise<ObjectID> {
log.level = LogLevel.DEBUG;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
return Logging._log(log);
}
// Log Security Debug
- public static async logSecurityDebug(log: Log): Promise<string> {
+ public static async logSecurityDebug(log: Log): Promise<ObjectID> {
log.type = LogType.SECURITY;
return Logging.logDebug(log);
}
// Log Info
- public static async logInfo(log: Log): Promise<string> {
+ public static async logInfo(log: Log): Promise<ObjectID> {
log.level = LogLevel.INFO;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
return Logging._log(log);
}
// Log Security Info
- public static async logSecurityInfo(log: Log): Promise<string> {
+ public static async logSecurityInfo(log: Log): Promise<ObjectID> {
log.type = LogType.SECURITY;
return Logging.logInfo(log);
}
// Log Warning
- public static async logWarning(log: Log): Promise<string> {
+ public static async logWarning(log: Log): Promise<ObjectID> {
log.level = LogLevel.WARNING;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
return Logging._log(log);
}
// Log Security Warning
- public static async logSecurityWarning(log: Log): Promise<string> {
+ public static async logSecurityWarning(log: Log): Promise<ObjectID> {
log.type = LogType.SECURITY;
return Logging.logWarning(log);
}
// Log Error
- public static async logError(log: Log): Promise<string> {
+ public static async logError(log: Log): Promise<ObjectID> {
log.level = LogLevel.ERROR;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
return Logging._log(log);
}
// Log Security Error
- public static async logSecurityError(log: Log): Promise<string> {
+ public static async logSecurityError(log: Log): Promise<ObjectID> {
log.type = LogType.SECURITY;
return Logging.logError(log);
}
@@ -583,7 +584,7 @@ export default class Logging {
}
// Log
- private static async _log(log: Log): Promise<string> {
+ private static async _log(log: Log): Promise<ObjectID> {
let moduleConfig = null;
const loggingConfig = Logging.getConfiguration();
// Default Log Level

MODIFY
test/api/SecurityTest.ts
test/api/SecurityTest.ts
@@ -7,6 +7,7 @@ import Constants from '../../src/utils/Constants';
import ContextDefinition from './context/ContextDefinition';
import Logging from '../../src/utils/Logging';
import MongoDBStorage from '../../src/storage/mongodb/MongoDBStorage';
+import { ObjectID } from 'mongodb';
import { ServerAction } from '../../src/types/Server';
import Tenant from '../types/Tenant';
import TestConstants from './client/utils/TestConstants';
@@ -83,7 +84,7 @@ describe('Security tests', function() {
describe('Success cases (tenant utall)', () => {
it('Check that sensitive data string (containing "=") is anonymized', async () => { // Will fail
- const logId:string = await Logging.logDebug({
+ const logId:ObjectID = await Logging.logDebug({
source: 'test',
tenantID: testData.credentials.tenantId,
action: ServerAction.HTTP_REQUEST,
@@ -92,12 +93,12 @@ describe('Security tests', function() {
method: 'test',
detailedMessages: 'repeatPassword=MyDummyPass'
});
- const read = await testData.centralService.logsApi.readById(logId);
+ const read = await testData.centralService.logsApi.readById(logId.toString());
expect(read.status).to.equal(200);
checkSensitiveDataIsObfuscated(JSON.parse(read.data.detailedMessages));
});
it('Check that sensitive data string (containing ":") is anonymized', async () => { // Will fail
- const logId:string = await Logging.logDebug({
+ const logId:ObjectID = await Logging.logDebug({
source: 'test',
tenantID: testData.credentials.tenantId,
action: ServerAction.HTTP_REQUEST,
@@ -106,13 +107,12 @@ describe('Security tests', function() {
method: 'test',
detailedMessages: 'firstName:MyName'
});
-
- const read = await testData.centralService.logsApi.readById(logId);
+ const read = await testData.centralService.logsApi.readById(logId.toString());
expect(read.status).to.equal(200);
checkSensitiveDataIsObfuscated(JSON.parse(read.data.detailedMessages));
});
it('Check that sensitive data is anonymized in object with string fields', async () => {
- const logId:string = await Logging.logDebug({
+ const logId:ObjectID = await Logging.logDebug({
source: 'test',
tenantID: testData.credentials.tenantId,
action: ServerAction.HTTP_REQUEST,
@@ -137,13 +137,12 @@ describe('Security tests', function() {
'token':'test'
}
});
-
- const read = await testData.centralService.logsApi.readById(logId);
+ const read = await testData.centralService.logsApi.readById(logId.toString());
expect(read.status).to.equal(200);
checkSensitiveDataIsObfuscated(JSON.parse(read.data.detailedMessages));
});
it('Check that sensitive data is anonymized in object with query string fields', async () => { // Passes because query strings are treated correctly in object fields
- const logId:string = await Logging.logDebug({
+ const logId:ObjectID = await Logging.logDebug({
source: 'test',
tenantID: testData.credentials.tenantId,
action: ServerAction.HTTP_REQUEST,
@@ -156,13 +155,12 @@ describe('Security tests', function() {
'password': 'password=testtesttest'
}
});
-
- const read = await testData.centralService.logsApi.readById(logId);
+ const read = await testData.centralService.logsApi.readById(logId.toString());
expect(read.status).to.equal(200);
checkSensitiveDataIsObfuscated(JSON.parse(read.data.detailedMessages));
});
it('Check that sensitive data is anonymized in array with strings', async () => { // Will fail
- const logId:string = await Logging.logDebug({
+ const logId:ObjectID = await Logging.logDebug({
source: 'test',
tenantID: testData.credentials.tenantId,
action: ServerAction.HTTP_REQUEST,
@@ -187,7 +185,7 @@ describe('Security tests', function() {
'token=test'
]
});
- const read = await testData.centralService.logsApi.readById(logId);
+ const read = await testData.centralService.logsApi.readById(logId.toString());
expect(read.status).to.equal(200);
checkSensitiveDataIsObfuscated(JSON.parse(read.data.detailedMessages));
});

Return ObjectID instead of string.