Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concurrent writing to audit file #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions lib/AuditManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require("path");
const enums_1 = require("./enums");
const helper_1 = require("./helper");
const crypto = require("crypto");
const sleep = require("sleep-promise");
class AuditManager {
constructor(config, emitter) {
this.config = config;
Expand Down Expand Up @@ -52,36 +53,46 @@ class AuditManager {
helper_1.Logger.debug("file already in the log", name);
return;
}
var time = Date.now();
this.config.files.push({
date: time,
name: name,
hash: crypto.createHash(this.config.hashType).update(name + "LOG_FILE" + time).digest("hex")
});
helper_1.Logger.debug(`added file ${name} to log`);
if (this.config.keepSettings && this.config.keepSettings.amount) {
if (this.config.keepSettings.type == enums_1.KeepLogFiles.days) {
let date = Date.now() - 86400 * this.config.keepSettings.amount * 1000;
let files = this.config.files.filter((logEntry) => {
if (logEntry.date >= date) {
return true;
}
this.removeLog(logEntry);
// add a random sleep to avoid concurrent writes to the same file
sleep(Math.random() * 1000).then(() => {
// read audit file again before updating it (mainly for nodejs cluster mode with concurrent writes from workers)
this.loadLog();
// check if file name is already present in the audit file and don't
// include it in case is already there (mainly for nodejs cluster mode with concurrent writes from workers)
var fileIsPresent = this.config.files.some((file) => file.name === name);
if (!fileIsPresent) {
var time = Date.now();
this.config.files.push({
date: time,
name: name,
hash: crypto.createHash(this.config.hashType).update(name + "LOG_FILE" + time).digest("hex")
});
this.config.files = files;
helper_1.Logger.debug(`added file ${name} to log`);
}
else if (this.config.files.length > this.config.keepSettings.amount) {
var filesToKeep = this.config.files.splice(-this.config.keepSettings.amount);
if (this.config.files.length > 0) {
this.config.files.filter((logEntry) => {
if (this.config.keepSettings && this.config.keepSettings.amount) {
if (this.config.keepSettings.type == enums_1.KeepLogFiles.days) {
let date = Date.now() - 86400 * this.config.keepSettings.amount * 1000;
let files = this.config.files.filter((logEntry) => {
if (logEntry.date >= date) {
return true;
}
this.removeLog(logEntry);
return false;
});
this.config.files = files;
}
else if (this.config.files.length > this.config.keepSettings.amount) {
var filesToKeep = this.config.files.splice(-this.config.keepSettings.amount);
if (this.config.files.length > 0) {
this.config.files.filter((logEntry) => {
this.removeLog(logEntry);
return false;
});
}
this.config.files = filesToKeep;
}
this.config.files = filesToKeep;
}
}
this.writeLog();
this.writeLog();
});
}
removeLog(logEntry) {
if (logEntry.hash === crypto.createHash(this.config.hashType).update(logEntry.name + "LOG_FILE" + logEntry.date).digest("hex")) {
Expand Down
37 changes: 6 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
],
"author": "Roger Castells",
"license": "MIT",
"dependencies": {},
"dependencies": {
"sleep-promise": "^9.1.0"
},
"devDependencies": {
"@types/node": "^18.11.3",
"typescript": "^4.8.4"
Expand Down
65 changes: 38 additions & 27 deletions src/AuditManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Logger, makeDirectory } from "./helper";
import { AuditConfigFile, AuditEntry, AuditSettings } from "./types";
import * as crypto from "crypto"
import { EventEmitter } from "stream";
import * as sleep from "sleep-promise";

export default class AuditManager {

Expand Down Expand Up @@ -59,38 +60,48 @@ export default class AuditManager {
return
}

var time = Date.now();
this.config.files.push({
date: time,
name: name,
hash: crypto.createHash(this.config.hashType).update(name + "LOG_FILE" + time).digest("hex")
});
Logger.debug(`added file ${name} to log`)
// add a random sleep to avoid concurrent writes to the same file
sleep(Math.random() * 1000).then(() => {
// read audit file again before updating it (mainly for nodejs cluster mode with concurrent writes from workers)
this.loadLog();
// check if file name is already present in the audit file and don't
// include it in case is already there (mainly for nodejs cluster mode with concurrent writes from workers)
var fileIsPresent = this.config.files.some((file) => file.name === name);
if (!fileIsPresent) {
var time = Date.now();
this.config.files.push({
date: time,
name: name,
hash: crypto.createHash(this.config.hashType).update(name + "LOG_FILE" + time).digest("hex")
});
Logger.debug(`added file ${name} to log`)
}

if (this.config.keepSettings && this.config.keepSettings.amount){
if(this.config.keepSettings.type == KeepLogFiles.days){
let date = Date.now() - 86400*this.config.keepSettings.amount*1000
let files = this.config.files.filter((logEntry) => {
if (logEntry.date >= date) {
return true
}
this.removeLog(logEntry)
})

this.config.files = files
} else if (this.config.files.length > this.config.keepSettings.amount){
var filesToKeep = this.config.files.splice(-this.config.keepSettings.amount);
if(this.config.files.length > 0){
this.config.files.filter((logEntry) => {
this.removeLog(logEntry);
return false;
if (this.config.keepSettings && this.config.keepSettings.amount){
if(this.config.keepSettings.type == KeepLogFiles.days){
let date = Date.now() - 86400*this.config.keepSettings.amount*1000
let files = this.config.files.filter((logEntry) => {
if (logEntry.date >= date) {
return true
}
this.removeLog(logEntry)
})

this.config.files = files
} else if (this.config.files.length > this.config.keepSettings.amount){
var filesToKeep = this.config.files.splice(-this.config.keepSettings.amount);
if(this.config.files.length > 0){
this.config.files.filter((logEntry) => {
this.removeLog(logEntry);
return false;
})
}
this.config.files = filesToKeep
}
this.config.files = filesToKeep
}
}

this.writeLog()
this.writeLog()
});
}

private removeLog(logEntry: AuditEntry){
Expand Down