Skip to content

Commit

Permalink
Fix #272: Change the order of audit cleanup to keep data integrity (#273
Browse files Browse the repository at this point in the history
)

* Fix #272: Change the order of audit cleanup to keep data integrity
  • Loading branch information
banterCZ authored Mar 21, 2024
1 parent b2b00df commit 3d446b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ public void cleanup() {
final LocalDateTime cleanupLimit = LocalDateTime.now().minusDays(cleanupDays);
synchronized (CLEANUP_LOCK) {
transactionTemplate.executeWithoutResult(status -> {
jdbcTemplate.execute("DELETE FROM " + tableNameAudit + " WHERE timestamp_created < ?", (PreparedStatementCallback<Boolean>) ps -> {
jdbcTemplate.execute("DELETE FROM " + tableNameParam + " WHERE timestamp_created < ?", (PreparedStatementCallback<Boolean>) ps -> {
ps.setTimestamp(1, Timestamp.valueOf(cleanupLimit));
return ps.execute();
});
jdbcTemplate.execute("DELETE FROM " + tableNameParam + " WHERE timestamp_created < ?", (PreparedStatementCallback<Boolean>) ps -> {
jdbcTemplate.execute("DELETE FROM " + tableNameAudit + " WHERE timestamp_created < ?", (PreparedStatementCallback<Boolean>) ps -> {
ps.setTimestamp(1, Timestamp.valueOf(cleanupLimit));
return ps.execute();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(classes = TestApplication.class, properties = {"audit.db.table.param.enabled=true"})
@SpringBootTest(classes = TestApplication.class, properties = {
"audit.db.table.param.enabled=true",
"audit.db.cleanup.days=-1", // time shift to the future to enable cleanup test
"powerauth.audit.cleanup.delay.initial=60000" // delay the job start due to slow builds
})
@Sql(scripts = "/db_schema.sql")
class AuditParamEnabledTest {

Expand Down Expand Up @@ -122,4 +126,33 @@ void testAuditMoreParams() {
assertEquals(new JsonUtil().serializeObject(timestamp), rs3.getString("param_value"));
}

@Test
void testAuditCleanup() {
final Audit audit = auditFactory.getAudit();
audit.info("test message", AuditDetail.builder().param("my_id", "test_id").build());
audit.flush();

assertEquals(1, countAuditLogs());
assertEquals(1, countAuditParams());

audit.cleanup();

assertEquals(0, countAuditLogs());
assertEquals(0, countAuditParams());
}

private int countAuditLogs() {
return count("audit_log");
}

private int countAuditParams() {
return count("audit_param");
}

private int count(final String tableName) {
final SqlRowSet rs = jdbcTemplate.queryForRowSet("SELECT COUNT(*) FROM " + tableName);
assertTrue(rs.next());
return rs.getInt(1);
}

}
12 changes: 12 additions & 0 deletions audit-base/src/test/java/com/wultra/core/audit/base/AuditTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.wultra.core.audit.base.model.AuditDetail;
import com.wultra.core.audit.base.model.AuditLevel;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,6 +27,7 @@
import org.springframework.test.context.jdbc.Sql;

import java.sql.Timestamp;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -219,4 +221,14 @@ void testAuditTrace() {
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
}

@Test
void testScheduledFlush() {
Audit audit = auditFactory.getAudit();
audit.info("test message");

Awaitility.await()
.atMost(Duration.ofSeconds(5))
.until(() -> jdbcTemplate.queryForRowSet("SELECT * FROM audit_log").next());
}
}

0 comments on commit 3d446b5

Please sign in to comment.