Skip to content

Commit 569f3aa

Browse files
owend74trondn
authored andcommitted
MB-28651: Ensure audit.log exists as much as possible
The changes documented below are to ensure that the audit.log file almost always exists and open available for writing. Note: It is not possible for the file to always exist because when we rotate the file we close the current one, rename and open the new one. The changes are as follows: Create the audit.log file immediately after a file rotation occurs (as opposed to when a new audit event is recieved). If the log file is due to rotate, but it is currently empty, instead of closing the file, keep it open and update the open_time so that the next rotation will occur at the correct time. If there is a failure writing to disk, which causes us to close (and possibly rotate) the audit file, make sure that the file exists and is re-opened immediately afterwards. Change-Id: Ib93abf1d45eb36c15b6f2dee438f9956894fff58 Reviewed-on: http://review.couchbase.org/90599 Reviewed-by: Trond Norbye <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent e0ccc57 commit 569f3aa

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

auditd/src/audit.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,12 @@ bool Audit::configure(void) {
408408
}
409409
}
410410

411-
if (!config.is_auditd_enabled()) {
411+
if (config.is_auditd_enabled()) {
412+
// If the write_event_to_disk function returns false then it is
413+
// possible the audit file has been closed. Therefore ensure
414+
// the file is open.
415+
auditfile.ensure_open();
416+
} else {
412417
// Audit is disabled, ensure that the audit file is closed
413418
auditfile.close();
414419
}

auditd/src/auditd.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ static void consume_events(void* arg) {
5252
audit.auditfile.get_seconds_to_rotation() * 1000);
5353
if (audit.filleventqueue->empty()) {
5454
// We timed out, so just rotate the files
55-
audit.auditfile.maybe_rotate_files();
55+
if (audit.auditfile.maybe_rotate_files()) {
56+
// If the file was rotated then we need to open a new
57+
// audit.log file.
58+
audit.auditfile.ensure_open();
59+
}
5660
}
5761
}
5862
/* now have producer_consumer lock!

auditd/src/auditfile.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ bool AuditFile::write_event_to_disk(cJSON *output) {
263263
} else {
264264
log_error(AuditErrorCode::MEMORY_ALLOCATION_ERROR,
265265
"failed to convert audit event");
266+
// Failed to write event to disk.
267+
return false;
266268
}
267269

268270
return ret;

auditd/src/auditfile.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class AuditFile {
4848
*/
4949
bool maybe_rotate_files(void) {
5050
if (is_open() && time_to_rotate_log()) {
51+
if (is_empty()) {
52+
// Given the audit log is empty on rotation instead of
53+
// closing and then re-opening we can just keep open and
54+
// update the open_time.
55+
open_time = auditd_time();
56+
return true;
57+
}
5158
close_and_rotate_log();
5259
return true;
5360
}
@@ -142,6 +149,10 @@ class AuditFile {
142149
void set_log_directory(const std::string &new_directory);
143150
bool is_timestamp_format_correct(std::string& str);
144151

152+
bool is_empty() const {
153+
return (current_size == 0);
154+
}
155+
145156
static time_t auditd_time();
146157

147158
FILE *file;

auditd/src/event.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,10 @@ bool Event::process(Audit& audit) {
125125

126126
Audit::log_error(AuditErrorCode::WRITE_EVENT_TO_DISK_ERROR,
127127
to_string(json_payload, false));
128+
129+
// If the write_event_to_disk function returns false then it is
130+
// possible the audit file has been closed. Therefore ensure
131+
// the file is open.
132+
audit.auditfile.ensure_open();
128133
return false;
129134
}

0 commit comments

Comments
 (0)