From ea7d3c64101c8400a114f7ec1be78260d4bda32b Mon Sep 17 00:00:00 2001 From: Sammy Aknan Date: Mon, 1 Sep 2025 11:39:50 -0400 Subject: [PATCH] feat: add actor based logs instead of user based logs --- build.gradle | 2 +- .../java/net/staticstudios/audit/Actor.java | 27 ++++++ .../staticstudios/audit/AuditLogEntry.java | 37 +++++--- .../audit/EncodedAuditLogEntry.java | 16 ++-- .../net/staticstudios/audit/StaticAudit.java | 84 +++++++++++-------- .../net/staticstudios/audit/LoggingTest.java | 43 +++++----- 6 files changed, 136 insertions(+), 73 deletions(-) create mode 100644 src/main/java/net/staticstudios/audit/Actor.java diff --git a/build.gradle b/build.gradle index a836592..3242755 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'net.staticstudios' -version = '1.0.0-SNAPSHOT' +version = '1.0.1-SNAPSHOT' repositories { mavenCentral() diff --git a/src/main/java/net/staticstudios/audit/Actor.java b/src/main/java/net/staticstudios/audit/Actor.java new file mode 100644 index 0000000..db5bdd5 --- /dev/null +++ b/src/main/java/net/staticstudios/audit/Actor.java @@ -0,0 +1,27 @@ +package net.staticstudios.audit; + +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +/** + * Represents an actor that can perform actions in the audit log system. + */ +public interface Actor { + + /** + * Get the type of actor, e.g., "user", "server", etc. + * + * @return the actor type + */ + @NotNull + String getActorType(); + + /** + * Get the unique identifier of the actor. + * + * @return the actor ID + */ + @NotNull + UUID getActorId(); +} diff --git a/src/main/java/net/staticstudios/audit/AuditLogEntry.java b/src/main/java/net/staticstudios/audit/AuditLogEntry.java index 8d90099..bb7df5f 100644 --- a/src/main/java/net/staticstudios/audit/AuditLogEntry.java +++ b/src/main/java/net/staticstudios/audit/AuditLogEntry.java @@ -11,7 +11,8 @@ * Represents an entry in the audit log. */ public class AuditLogEntry { - private final @NotNull UUID userId; + private final @NotNull String actorType; + private final @NotNull UUID actorId; private final @Nullable UUID sessionId; private final @NotNull String applicationGroup; private final @NotNull String applicationId; @@ -22,7 +23,8 @@ public class AuditLogEntry { /** * Creates a new AuditLogEntry. * - * @param userId the ID of the user who performed the action + * @param actorType the type of actor who performed the action (e.g., "user", "server") + * @param actorId the ID of the actor who performed the action * @param sessionId the ID of the session in which the action was performed (nullable) * @param applicationGroup the application group that logged the action * @param applicationId the application ID that logged the action @@ -30,8 +32,9 @@ public class AuditLogEntry { * @param action the action that was performed * @param data the data associated with the action */ - public AuditLogEntry(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull String applicationGroup, @NotNull String applicationId, @NotNull Instant timestamp, @NotNull Action action, @NotNull T data) { - this.userId = userId; + public AuditLogEntry(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @NotNull String applicationGroup, @NotNull String applicationId, @NotNull Instant timestamp, @NotNull Action action, @NotNull T data) { + this.actorType = actorType; + this.actorId = actorId; this.sessionId = sessionId; this.applicationGroup = applicationGroup; this.applicationId = applicationId; @@ -41,12 +44,21 @@ public AuditLogEntry(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull St } /** - * Gets the ID of the user who performed the action. + * Gets the type of actor who performed the action. * - * @return the user ID + * @return the actor type (e.g., "user", "server") */ - public @NotNull UUID getUserId() { - return userId; + public @NotNull String getActorType() { + return actorType; + } + + /** + * Gets the ID of the actor who performed the action. + * + * @return the actor ID + */ + public @NotNull UUID getActorId() { + return actorId; } /** @@ -110,8 +122,8 @@ public AuditLogEntry(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull St */ @Override public String toString() { - return String.format("AuditLogEntry{userId=%s, sessionId=%s, applicationGroup='%s', applicationId='%s', timestamp=%s, action=%s, data=%s}", - userId, sessionId, applicationGroup, applicationId, timestamp, action, data); + return String.format("AuditLogEntry{actorType=%s, actorId=%s, sessionId=%s, applicationGroup='%s', applicationId='%s', timestamp=%s, action=%s, data=%s}", + actorType, actorId, sessionId, applicationGroup, applicationId, timestamp, action, data); } /** @@ -126,7 +138,8 @@ public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) return false; AuditLogEntry that = (AuditLogEntry) obj; return Objects.equals(action, that.action) && Objects.equals(data, that.data) && - Objects.equals(userId, that.userId) && Objects.equals(sessionId, that.sessionId) && + Objects.equals(actorType, that.actorType) && + Objects.equals(actorId, that.actorId) && Objects.equals(sessionId, that.sessionId) && Objects.equals(applicationGroup, that.applicationGroup) && Objects.equals(applicationId, that.applicationId) && Objects.equals(timestamp, that.timestamp); } @@ -138,6 +151,6 @@ public boolean equals(Object obj) { */ @Override public int hashCode() { - return Objects.hash(action, data, userId, sessionId, applicationGroup, applicationId, timestamp); + return Objects.hash(action, data, actorType, actorId, sessionId, applicationGroup, applicationId, timestamp); } } diff --git a/src/main/java/net/staticstudios/audit/EncodedAuditLogEntry.java b/src/main/java/net/staticstudios/audit/EncodedAuditLogEntry.java index 425738e..57b54f9 100644 --- a/src/main/java/net/staticstudios/audit/EncodedAuditLogEntry.java +++ b/src/main/java/net/staticstudios/audit/EncodedAuditLogEntry.java @@ -7,7 +7,8 @@ import java.util.UUID; public class EncodedAuditLogEntry { - private final @NotNull UUID userId; + private final @NotNull String actorType; + private final @NotNull UUID actorId; private final @Nullable UUID sessionId; private final @NotNull String applicationGroup; private final @NotNull String applicationId; @@ -15,8 +16,9 @@ public class EncodedAuditLogEntry { private final @NotNull String actionId; private final @NotNull String encodedData; - public EncodedAuditLogEntry(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull String applicationGroup, @NotNull String applicationId, @NotNull Instant timestamp, @NotNull String actionId, @NotNull String encodedData) { - this.userId = userId; + public EncodedAuditLogEntry(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @NotNull String applicationGroup, @NotNull String applicationId, @NotNull Instant timestamp, @NotNull String actionId, @NotNull String encodedData) { + this.actorType = actorType; + this.actorId = actorId; this.sessionId = sessionId; this.applicationGroup = applicationGroup; this.applicationId = applicationId; @@ -25,8 +27,12 @@ public EncodedAuditLogEntry(@NotNull UUID userId, @Nullable UUID sessionId, @Not this.encodedData = encodedData; } - public @NotNull UUID getUserId() { - return userId; + public @NotNull String getActorType() { + return actorType; + } + + public @NotNull UUID getActorId() { + return actorId; } public @Nullable UUID getSessionId() { diff --git a/src/main/java/net/staticstudios/audit/StaticAudit.java b/src/main/java/net/staticstudios/audit/StaticAudit.java index 214c982..f71b833 100644 --- a/src/main/java/net/staticstudios/audit/StaticAudit.java +++ b/src/main/java/net/staticstudios/audit/StaticAudit.java @@ -34,7 +34,8 @@ public class StaticAudit { session_id UUID, application_group VARCHAR(255) NOT NULL, application_id VARCHAR(255) NOT NULL, - user_id UUID NOT NULL, + actor_type VARCHAR(255) NOT NULL, + actor_id UUID NOT NULL, action_id VARCHAR(255) NOT NULL, action_data JSONB ); @@ -159,55 +160,59 @@ public StaticAudit registerAction(Action action) { /** * Logs an action to the audit log. * - * @param userId The ID of the user performing the action. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The user's current session ID, if applicable. * @param timestamp The timestamp of when the action occurred. * @param action The action being performed. * @param actionData The data associated with the action. * @return The StaticAudit instance. */ - public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull Instant timestamp, @NotNull Action action, @NotNull T actionData) { - return log(userId, sessionId, timestamp, action.getActionId(), actionData); + public StaticAudit log(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @NotNull Instant timestamp, @NotNull Action action, @NotNull T actionData) { + return log(actorType, actorId, sessionId, timestamp, action.getActionId(), actionData); } /** * Logs an action to the audit log. * - * @param userId The ID of the user performing the action. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The user's current session ID, if applicable. * @param action The action being performed. * @param actionData The data associated with the action. * @return The StaticAudit instance. */ - public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull Action action, @NotNull T actionData) { - return log(userId, sessionId, Instant.now(), action.getActionId(), actionData); + public StaticAudit log(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @NotNull Action action, @NotNull T actionData) { + return log(actorType, actorId, sessionId, Instant.now(), action.getActionId(), actionData); } /** * Logs an action to the audit log. * - * @param userId The ID of the user performing the action. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The user's current session ID, if applicable. * @param actionId The ID of the action being performed. * @param actionData The data associated with the action. * @return The StaticAudit instance. */ - public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull String actionId, @NotNull Object actionData) { - return log(userId, sessionId, Instant.now(), actionId, actionData); + public StaticAudit log(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @NotNull String actionId, @NotNull Object actionData) { + return log(actorType, actorId, sessionId, Instant.now(), actionId, actionData); } /** * Logs an action to the audit log. * - * @param userId The ID of the user performing the action. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The user's current session ID, if applicable. * @param timestamp The timestamp of when the action occurred. * @param actionId The ID of the action being performed. * @param actionData The data associated with the action. * @return The StaticAudit instance. */ - public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull Instant timestamp, @NotNull String actionId, @NotNull Object actionData) { - Preconditions.checkNotNull(userId, "User ID cannot be null"); + public StaticAudit log(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @NotNull Instant timestamp, @NotNull String actionId, @NotNull Object actionData) { + Preconditions.checkNotNull(actorId, "Actor ID cannot be null"); Preconditions.checkNotNull(timestamp, "Timestamp cannot be null"); Preconditions.checkNotNull(actionId, "Action ID cannot be null"); Preconditions.checkNotNull(actionData, "Action data cannot be null"); @@ -217,16 +222,17 @@ public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull String actionDataJson = toJson(action, actionData); Preconditions.checkNotNull(actionDataJson, "Action data JSON cannot be null"); runAsync(connection -> { - @Language("SQL") String sql = "INSERT INTO %s.%s (log_id, timestamp, session_id, application_group, application_id, user_id, action_id, action_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?::jsonb)"; + @Language("SQL") String sql = "INSERT INTO %s.%s (log_id, timestamp, session_id, application_group, application_id, actor_type, actor_id, action_id, action_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?::jsonb)"; try (PreparedStatement statement = connection.prepareStatement(sql.formatted(schemaName, tableName))) { statement.setObject(1, UUID.randomUUID()); statement.setObject(2, Timestamp.from(timestamp)); statement.setObject(3, sessionId); statement.setString(4, applicationGroup); statement.setString(5, applicationId); - statement.setObject(6, userId); - statement.setString(7, actionId); - statement.setString(8, actionDataJson); + statement.setObject(6, actorType); + statement.setObject(7, actorId); + statement.setString(8, actionId); + statement.setString(9, actionDataJson); logger.trace(statement.toString()); statement.executeUpdate(); } @@ -238,7 +244,8 @@ public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull /** * Retrieves audit log entries asynchronously for a given user and optional filters. * - * @param userId The ID of the user whose logs to retrieve. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The session ID to filter by, or null for any session. * @param from The start timestamp for filtering, or null for no lower bound. * @param to The end timestamp for filtering, or null for no upper bound. @@ -246,16 +253,17 @@ public StaticAudit log(@NotNull UUID userId, @Nullable UUID sessionId, @NotNull * @param limit The maximum number of entries to retrieve. * @return A CompletableFuture containing the list of matching audit log entries. */ - public CompletableFuture>> retrieveAsync(@NotNull UUID userId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { + public CompletableFuture>> retrieveAsync(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { CompletableFuture>> future = new CompletableFuture<>(); - async(() -> future.complete(retrieve(userId, sessionId, from, to, limit, actionIds))); + async(() -> future.complete(retrieve(actorType, actorId, sessionId, from, to, limit, actionIds))); return future; } /** * Retrieves audit log entries for a given user and optional filters. * - * @param userId The ID of the user whose logs to retrieve. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The session ID to filter by, or null for any session. * @param from The start timestamp for filtering, or null for no lower bound. * @param to The end timestamp for filtering, or null for no upper bound. @@ -263,8 +271,8 @@ public CompletableFuture>> retrieveAsync(@NotNull UUID use * @param limit The maximum number of entries to retrieve. * @return The list of matching audit log entries. */ - public List> retrieve(@NotNull UUID userId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { - List encodedList = retrieveEncoded(userId, sessionId, from, to, limit, actionIds); + public List> retrieve(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { + List encodedList = retrieveEncoded(actorType, actorId, sessionId, from, to, limit, actionIds); encodedList.removeIf(encoded -> { if (!actions.containsKey(encoded.getActionId())) { logger.warn("Unknown action ID {} in audit log, skipping entry", encoded.getActionId()); @@ -285,7 +293,8 @@ public List> retrieve(@NotNull UUID userId, @Nullable UUID sess /** * Retrieves encoded audit log entries asynchronously for a given user and optional filters. * - * @param userId The ID of the user whose logs to retrieve. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The session ID to filter by, or null for any session. * @param from The start timestamp for filtering, or null for no lower bound. * @param to The end timestamp for filtering, or null for no upper bound. @@ -293,16 +302,17 @@ public List> retrieve(@NotNull UUID userId, @Nullable UUID sess * @param limit The maximum number of entries to retrieve. * @return A CompletableFuture containing the list of matching audit log entries. */ - public CompletableFuture> retrieveEncodedAsync(@NotNull UUID userId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { + public CompletableFuture> retrieveEncodedAsync(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { CompletableFuture> future = new CompletableFuture<>(); - async(() -> future.complete(retrieveEncoded(userId, sessionId, from, to, limit, actionIds))); + async(() -> future.complete(retrieveEncoded(actorType, actorId, sessionId, from, to, limit, actionIds))); return future; } /** * Retrieves encoded audit log entries for a given user and optional filters. * - * @param userId The ID of the user whose logs to retrieve. + * @param actorType The type of actor performing the action, e.g., "user", "server", etc. + * @param actorId The ID of the actor performing the action. * @param sessionId The session ID to filter by, or null for any session. * @param from The start timestamp for filtering, or null for no lower bound. * @param to The end timestamp for filtering, or null for no upper bound. @@ -310,13 +320,14 @@ public CompletableFuture> retrieveEncodedAsync(@NotNu * @param limit The maximum number of entries to retrieve. * @return The list of matching audit log entries. */ - public List retrieveEncoded(@NotNull UUID userId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { - Preconditions.checkNotNull(userId, "User ID cannot be null"); + public List retrieveEncoded(@NotNull String actorType, @NotNull UUID actorId, @Nullable UUID sessionId, @Nullable Instant from, @Nullable Instant to, int limit, String... actionIds) { + Preconditions.checkNotNull(actorType, "Actor type cannot be null"); + Preconditions.checkNotNull(actorId, "Actor ID cannot be null"); Preconditions.checkArgument(limit > 0, "Limit must be greater than 0"); List entries = new ArrayList<>(); run(connection -> { - StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM %s.%s WHERE user_id = ?"); + StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM %s.%s WHERE actor_type = ? AND actor_id = ?"); if (sessionId != null) { sqlBuilder.append(" AND session_id = ?"); } @@ -340,7 +351,8 @@ public List retrieveEncoded(@NotNull UUID userId, @Nullabl String sql = sqlBuilder.toString().formatted(schemaName, tableName); try (PreparedStatement statement = connection.prepareStatement(sql)) { int index = 1; - statement.setObject(index++, userId); + statement.setObject(index++, actorType); + statement.setObject(index++, actorId); if (sessionId != null) { statement.setObject(index++, sessionId); } @@ -359,7 +371,8 @@ public List retrieveEncoded(@NotNull UUID userId, @Nullabl while (rs.next()) { String _actionId = rs.getString("action_id"); EncodedAuditLogEntry entry = new EncodedAuditLogEntry( - (UUID) rs.getObject("user_id"), + rs.getString("actor_type"), + (UUID) rs.getObject("actor_id"), (UUID) rs.getObject("session_id"), rs.getString("application_group"), rs.getString("application_id"), @@ -385,7 +398,8 @@ private T fromJson(Action action, String json) { private AuditLogEntry createEntry(EncodedAuditLogEntry encoded) { return createEntry( - encoded.getUserId(), + encoded.getActorType(), + encoded.getActorId(), encoded.getSessionId(), encoded.getApplicationGroup(), encoded.getApplicationId(), @@ -396,9 +410,9 @@ private AuditLogEntry createEntry(EncodedAuditLogEntry encoded) { } private AuditLogEntry createEntry( - UUID userId, UUID sessionId, String applicationGroup, String applicationId, Instant timestamp, + String actorType, UUID actorId, UUID sessionId, String applicationGroup, String applicationId, Instant timestamp, Action action, String jsonData) { - return new AuditLogEntry<>(userId, sessionId, applicationGroup, applicationId, timestamp, + return new AuditLogEntry<>(actorType, actorId, sessionId, applicationGroup, applicationId, timestamp, action, fromJson(action, jsonData)); } diff --git a/src/test/java/net/staticstudios/audit/LoggingTest.java b/src/test/java/net/staticstudios/audit/LoggingTest.java index 7dbb898..7070ad3 100644 --- a/src/test/java/net/staticstudios/audit/LoggingTest.java +++ b/src/test/java/net/staticstudios/audit/LoggingTest.java @@ -14,6 +14,7 @@ public class LoggingTest extends AuditTest { private static final Instant NOW = Instant.ofEpochMilli(0); + private final String USER = "user"; private StaticAudit audit; private UUID userId; private UUID sessionId; @@ -51,15 +52,15 @@ public void tearDown() throws SQLException { @Test public void testLogging() throws SQLException { SimpleActionData data = new SimpleActionData("test"); - audit.log(userId, sessionId, action1, data); + audit.log(USER, userId, sessionId, action1, data); Connection connection = getConnection(); - @Language("SQL") String sql = "SELECT * FROM %s.%s WHERE user_id = ?"; + @Language("SQL") String sql = "SELECT * FROM %s.%s WHERE actor_id = ?"; PreparedStatement statement = connection.prepareStatement(sql.formatted(audit.getSchemaName(), audit.getTableName())); statement.setObject(1, userId); ResultSet rs = statement.executeQuery(); assertTrue(rs.next()); - assertEquals(userId, rs.getObject("user_id")); + assertEquals(userId, rs.getObject("actor_id")); assertEquals(sessionId, rs.getObject("session_id")); assertEquals(action1.getActionId(), rs.getString("action_id")); assertEquals(data, action1.fromJson(rs.getString("action_data"))); @@ -70,9 +71,9 @@ public void testRetrieving() { logMultiple(50); List> entries; - entries = audit.retrieve(userId, null, null, null, 100); + entries = audit.retrieve(USER, userId, null, null, null, 100); assertEquals(50, entries.size()); - entries = audit.retrieve(userId, null, null, null, 10); + entries = audit.retrieve(USER, userId, null, null, null, 10); assertEquals(10, entries.size()); for (int i = 0; i < 10; i++) { @@ -88,12 +89,12 @@ public void testRetrievingWithFilter() { List> entries; - entries = audit.retrieve(userId, null, null, null, 500); + entries = audit.retrieve(USER, userId, null, null, null, 500); assertEquals(140, entries.size()); - entries = audit.retrieve(userId, null, null, null, 100, action1.getActionId(), action3.getActionId()); + entries = audit.retrieve(USER, userId, null, null, null, 100, action1.getActionId(), action3.getActionId()); assertEquals(100, entries.size()); assertFalse(entries.stream().anyMatch(entry -> entry.getAction().getActionId().equals(action2.getActionId()))); - entries = audit.retrieve(userId, null, null, null, 10, action1.getActionId()); + entries = audit.retrieve(USER, userId, null, null, null, 10, action1.getActionId()); assertEquals(10, entries.size()); assertTrue(entries.stream().allMatch(entry -> entry.getAction().getActionId().equals(action1.getActionId()))); assertFalse(entries.stream().anyMatch(entry -> entry.getAction().getActionId().equals(action2.getActionId()))); @@ -104,7 +105,7 @@ public void testRetrievingWithFilter() { public void testRetrievingEncoded() { logMultiple(action1, 5); logMultiple(action2, 3); - List encodedEntries = audit.retrieveEncoded(userId, null, null, null, 20); + List encodedEntries = audit.retrieveEncoded(USER, userId, null, null, null, 20); assertEquals(8, encodedEntries.size()); assertTrue(encodedEntries.stream().anyMatch(e -> e.getActionId().equals(action1.getActionId()))); assertTrue(encodedEntries.stream().anyMatch(e -> e.getActionId().equals(action2.getActionId()))); @@ -116,19 +117,20 @@ public void testRetrievingEncodedWithUnknownAction() throws SQLException { String unknownActionId = "unknown_action"; String jsonData = "{\"data\":\"foobar\"}"; Connection connection = getConnection(); - String sql = String.format("INSERT INTO %s.%s (log_id, timestamp, session_id, application_group, application_id, user_id, action_id, action_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?::jsonb)", audit.getSchemaName(), audit.getTableName()); + String sql = String.format("INSERT INTO %s.%s (log_id, timestamp, session_id, application_group, application_id, actor_type, actor_id, action_id, action_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?::jsonb)", audit.getSchemaName(), audit.getTableName()); try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setObject(1, UUID.randomUUID()); statement.setObject(2, Timestamp.from(Instant.now())); statement.setObject(3, sessionId); statement.setString(4, audit.getApplicationGroup()); statement.setString(5, audit.getApplicationId()); - statement.setObject(6, userId); - statement.setString(7, unknownActionId); - statement.setString(8, jsonData); + statement.setString(6, USER); + statement.setObject(7, userId); + statement.setString(8, unknownActionId); + statement.setString(9, jsonData); statement.executeUpdate(); } - List encodedEntries = audit.retrieveEncoded(userId, null, null, null, 10); + List encodedEntries = audit.retrieveEncoded(USER, userId, null, null, null, 10); assertTrue(encodedEntries.stream().anyMatch(e -> e.getActionId().equals(unknownActionId))); } @@ -138,19 +140,20 @@ public void testRetrievingWithUnknownActionIsFiltered() throws SQLException { String unknownActionId = "unknown_action"; String jsonData = "{\"data\":\"foobar\"}"; Connection connection = getConnection(); - String sql = String.format("INSERT INTO %s.%s (log_id, timestamp, session_id, application_group, application_id, user_id, action_id, action_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?::jsonb)", audit.getSchemaName(), audit.getTableName()); + String sql = String.format("INSERT INTO %s.%s (log_id, timestamp, session_id, application_group, application_id, actor_type, actor_id, action_id, action_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?::jsonb)", audit.getSchemaName(), audit.getTableName()); try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setObject(1, UUID.randomUUID()); statement.setObject(2, Timestamp.from(Instant.now())); statement.setObject(3, sessionId); statement.setString(4, audit.getApplicationGroup()); statement.setString(5, audit.getApplicationId()); - statement.setObject(6, userId); - statement.setString(7, unknownActionId); - statement.setString(8, jsonData); + statement.setString(6, USER); + statement.setObject(7, userId); + statement.setString(8, unknownActionId); + statement.setString(9, jsonData); statement.executeUpdate(); } - List> entries = audit.retrieve(userId, null, null, null, 10); + List> entries = audit.retrieve(USER, userId, null, null, null, 10); assertTrue(entries.stream().noneMatch(e -> e.getAction().getActionId().equals(unknownActionId))); } @@ -162,7 +165,7 @@ private void logMultiple(Action action, int count) { for (int i = 0; i < count; i++) { SimpleActionData data = new SimpleActionData("test" + i); Instant timestamp = NOW.plusSeconds(i); - audit.log(userId, sessionId, timestamp, action, data); + audit.log(USER, userId, sessionId, timestamp, action, data); } } }