From 8788f8f56550a5fde4b15dc8306d72a446e59049 Mon Sep 17 00:00:00 2001 From: Dan Caseley Date: Tue, 28 Jun 2022 22:12:32 +0100 Subject: [PATCH] WIP --- .../rest/controller/MUCRoomController.java | 51 +++++++++++++++---- .../rest/controller/MessageController.java | 2 + .../rest/controller/MsgArchiveController.java | 2 + .../plugin/rest/entity/GroupEntity.java | 2 +- .../rest/entity/MUCInvitationEntity.java | 5 ++ .../plugin/rest/entity/MUCRoomEntity.java | 29 +++++++++++ .../plugin/rest/entity/MessageEntity.java | 5 ++ .../plugin/rest/utils/LoggingUtils.java | 49 +++++++++++++++++- 8 files changed, 133 insertions(+), 12 deletions(-) diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MUCRoomController.java b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MUCRoomController.java index 55c02ad22..16fcb212f 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MUCRoomController.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MUCRoomController.java @@ -25,6 +25,7 @@ import org.jivesoftware.openfire.plugin.rest.entity.*; import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType; import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException; +import org.jivesoftware.openfire.plugin.rest.utils.LoggingUtils; import org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils; import org.jivesoftware.openfire.plugin.rest.utils.UserUtils; import org.jivesoftware.util.AlreadyExistsException; @@ -36,7 +37,6 @@ import org.xmpp.packet.Presence; import javax.annotation.Nonnull; -import javax.servlet.ServletException; import javax.ws.rs.core.Response; import java.lang.reflect.InvocationTargetException; import java.util.*; @@ -166,7 +166,11 @@ protected static MUCRoom getRoom(@Nonnull final String serviceName, @Nonnull fin */ public MUCRoomEntities getChatRooms(String serviceName, String channelType, String roomSearch, boolean expand) throws ServiceException { - log("Get the chat rooms"); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_LIST_ROOMS, + "serviceName", serviceName, + "channelType", channelType, + "roomSearch", roomSearch, + "expand", expand); final MultiUserChatService service = getService(serviceName); Set roomNames = service.getAllRoomNames(); @@ -207,7 +211,10 @@ public MUCRoomEntities getChatRooms(String serviceName, String channelType, Stri * the service exception */ public MUCRoomEntity getChatRoom(String roomName, String serviceName, boolean expand) throws ServiceException { - log("Get the chat room: " + roomName); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_ROOM, + "roomName", roomName, + "serviceName", serviceName, + "expand", expand); final MUCRoom chatRoom = getRoom(serviceName, roomName); return convertToMUCRoomEntity(chatRoom, expand); } @@ -223,7 +230,9 @@ public MUCRoomEntity getChatRoom(String roomName, String serviceName, boolean ex * the service exception */ public void deleteChatRoom(String roomName, String serviceName) throws ServiceException { - log("Delete the chat room: " + roomName); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_DELETE_ROOM, + "roomName", roomName, + "serviceName", serviceName); final MUCRoom chatRoom = getRoom(serviceName, roomName); chatRoom.destroyRoom(null, null); } @@ -239,7 +248,9 @@ public void deleteChatRoom(String roomName, String serviceName) throws ServiceEx * the service exception */ public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) throws ServiceException { - log("Create a chat room: " + mucRoomEntity.getRoomName()); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_CREATE_ROOM, + "serviceName", serviceName, + "mucRoomEntity", mucRoomEntity); try { createRoom(mucRoomEntity, serviceName); } catch (NotAllowedException | ForbiddenException e) { @@ -268,7 +279,10 @@ public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) thro */ public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity) throws ServiceException { - log("Update a chat room: " + mucRoomEntity.getRoomName()); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_UPDATE_ROOM, + "roomName", roomName, + "serviceName", serviceName, + "mucRoomEntity", mucRoomEntity); try { // If the room name is different throw exception if (!roomName.equals(mucRoomEntity.getRoomName())) { @@ -409,7 +423,9 @@ private boolean equalToAffiliations(MUCRoom room, MUCRoomEntity mucRoomEntity) { */ public ParticipantEntities getRoomParticipants(String roomName, String serviceName) throws ServiceException { - log("Get room participants for room: " + roomName); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_PARTICIPANT_LIST, + "roomName", roomName, + "serviceName", serviceName); ParticipantEntities participantEntities = new ParticipantEntities(); List participants = new ArrayList<>(); @@ -439,7 +455,9 @@ public ParticipantEntities getRoomParticipants(String roomName, String serviceNa */ public OccupantEntities getRoomOccupants(String roomName, String serviceName) throws ServiceException { - log("Get room occupants for room: " + roomName); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_OCCUPANT_LIST, + "roomName", roomName, + "serviceName", serviceName); OccupantEntities occupantEntities = new OccupantEntities(); List occupants = new ArrayList<>(); @@ -469,7 +487,9 @@ public OccupantEntities getRoomOccupants(String roomName, String serviceName) th * @return the room chat history */ public MUCRoomMessageEntities getRoomHistory(String roomName, String serviceName) throws ServiceException { - log("Get room history for room: " + roomName); + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_ROOM_HISTORY, + "roomName", roomName, + "serviceName", serviceName); MUCRoomMessageEntities mucRoomMessageEntities = new MUCRoomMessageEntities(); List listMessages = new ArrayList<>(); @@ -517,6 +537,11 @@ public MUCRoomMessageEntities getRoomHistory(String roomName, String serviceName */ public void inviteUser(String serviceName, String roomName, String jid, MUCInvitationEntity mucInvitationEntity) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_INVITE_USER, + "roomName", roomName, + "serviceName", serviceName, + "jid", jid, + "invitation" , mucInvitationEntity); MUCRoom room = getRoom(serviceName, roomName); try { @@ -664,6 +689,7 @@ private void setRoles(MUCRoom room, MUCRoomEntity mucRoomEntity) throws Forbidde * the service exception */ public void addAdmin(String serviceName, String roomName, String jid) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_ADMIN, serviceName, roomName, jid); MUCRoom room = getRoom(serviceName, roomName); try { room.addAdmin(UserUtils.checkAndGetJID(jid), room.getRole()); @@ -687,6 +713,7 @@ public void addAdmin(String serviceName, String roomName, String jid) throws Ser * the service exception */ public void addOwner(String serviceName, String roomName, String jid) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OWNER, serviceName, roomName, jid); MUCRoom room = getRoom(serviceName, roomName); try { room.addOwner(UserUtils.checkAndGetJID(jid), room.getRole()); @@ -708,6 +735,7 @@ public void addOwner(String serviceName, String roomName, String jid) throws Ser * the service exception */ public void addMember(String serviceName, String roomName, String jid) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_MEMBER, serviceName, roomName, jid); MUCRoom room = getRoom(serviceName, roomName); try { room.addMember(UserUtils.checkAndGetJID(jid), null, room.getRole()); @@ -729,6 +757,7 @@ public void addMember(String serviceName, String roomName, String jid) throws Se * the service exception */ public void addOutcast(String serviceName, String roomName, String jid) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OUTCAST, serviceName, roomName, jid); MUCRoom room = getRoom(serviceName, roomName); try { room.addOutcast(UserUtils.checkAndGetJID(jid), null, room.getRole()); @@ -753,6 +782,7 @@ public void addOutcast(String serviceName, String roomName, String jid) throws S */ public Collection getByAffiliation(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_LIST_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation); final MUCRoom room = getRoom(serviceName, roomName); switch (affiliation) { case admin: @@ -789,6 +819,7 @@ public Collection getByAffiliation(@Nonnull final String serviceName, @Nonn */ public void replaceAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation, @Nonnull final Collection jids) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_REPLACE_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation, jids); final Collection replacements = new HashSet<>(); // Input validation. @@ -864,6 +895,7 @@ public void replaceAffiliatedUsers(@Nonnull final String serviceName, @Nonnull f */ public void addAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation, @Nonnull final Collection jids) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation, jids); final Collection additions = new HashSet<>(); // Input validation. @@ -927,6 +959,7 @@ public void addAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final * the service exception */ public void deleteAffiliation(String serviceName, String roomName, String jid) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_REMOVE_AFFILIATED_USER_OR_GROUP_FOR_AFFILIATION, serviceName, roomName, jid); MUCRoom room = getRoom(serviceName, roomName); try { JID userJid = UserUtils.checkAndGetJID(jid); diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MessageController.java b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MessageController.java index 25e16fbb4..305ba597d 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MessageController.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MessageController.java @@ -22,6 +22,7 @@ import org.jivesoftware.openfire.plugin.rest.entity.MessageEntity; import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType; import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException; +import org.jivesoftware.openfire.plugin.rest.utils.LoggingUtils; /** * The Class MessageController. @@ -48,6 +49,7 @@ public static MessageController getInstance() { * the service exception */ public void sendBroadcastMessage(MessageEntity messageEntity) throws ServiceException { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MESSAGE_BROADCAST, messageEntity); if (messageEntity.getBody() != null && !messageEntity.getBody().isEmpty()) { SessionManager.getInstance().sendServerMessage(null, messageEntity.getBody()); } else { diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java index 41536ee11..6bab0ee41 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java @@ -22,6 +22,7 @@ import java.sql.SQLException; import org.jivesoftware.database.DbConnectionManager; +import org.jivesoftware.openfire.plugin.rest.utils.LoggingUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xmpp.packet.JID; @@ -64,6 +65,7 @@ private MsgArchiveController() { * @return the total number of user unread messages. */ public int getUnReadMessagesCount(JID jid) { + LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MESSAGE_ARCHIVE_UNREAD_COUNT, jid); int messageCount = 0; Connection con = null; PreparedStatement pstmt = null; diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/entity/GroupEntity.java b/src/java/org/jivesoftware/openfire/plugin/rest/entity/GroupEntity.java index 90b50d669..df4a52a89 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/entity/GroupEntity.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/entity/GroupEntity.java @@ -17,9 +17,9 @@ package org.jivesoftware.openfire.plugin.rest.entity; import com.fasterxml.jackson.annotation.JsonProperty; -import com.google.common.base.MoreObjects; import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Schema; +import org.glassfish.jersey.internal.guava.MoreObjects; import org.jivesoftware.util.StringUtils; import java.util.List; diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCInvitationEntity.java b/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCInvitationEntity.java index 623226131..290143384 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCInvitationEntity.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCInvitationEntity.java @@ -36,4 +36,9 @@ public void setReason(String reason) { this.reason = reason; } + @Override + public String toString() { + return "MUCInvitationEntity [reason=" + reason + "]"; + } + } diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCRoomEntity.java b/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCRoomEntity.java index 78d07d16e..7dfc7e687 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCRoomEntity.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCRoomEntity.java @@ -17,6 +17,7 @@ package org.jivesoftware.openfire.plugin.rest.entity; import com.fasterxml.jackson.annotation.JsonProperty; +import org.glassfish.jersey.internal.guava.MoreObjects; import java.util.Date; import java.util.List; @@ -350,4 +351,32 @@ public void setAdminGroups(List adminGroups) { this.adminGroups = adminGroups; } + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("roomName", roomName) + .add("description", description) + .add("persistent", persistent) + .add("publicRoom", publicRoom) + .add("registrationEnabled", registrationEnabled) + .add("canAnyoneDiscoverJID", canAnyoneDiscoverJID) + .add("canOccupantsChangeSubject", canOccupantsChangeSubject) + .add("canOccupantsInvite", canOccupantsInvite) + .add("canChangeNickname", canChangeNickname) + .add("logEnabled", logEnabled) + .add("loginRestrictedToNickname", loginRestrictedToNickname) + .add("membersOnly", membersOnly) + .add("moderated", moderated) + .add("broadcastPresenceRoles", broadcastPresenceRoles) + .add("owners", owners) + .add("ownerGroups", ownerGroups) + .add("members", members) + .add("memberGroups", memberGroups) + .add("outcasts", outcasts) + .add("outcastGroups", outcastGroups) + .add("admins", admins) + .add("adminGroups", adminGroups) + .toString(); + } + } diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/entity/MessageEntity.java b/src/java/org/jivesoftware/openfire/plugin/rest/entity/MessageEntity.java index 3458cc1af..1acbc6ac2 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/entity/MessageEntity.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/entity/MessageEntity.java @@ -53,4 +53,9 @@ public String getBody() { public void setBody(String body) { this.body = body; } + + @Override + public String toString() { + return "MessageEntity [body=" + body + "]"; + } } diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/utils/LoggingUtils.java b/src/java/org/jivesoftware/openfire/plugin/rest/utils/LoggingUtils.java index 518ec59e9..4c876d9d4 100644 --- a/src/java/org/jivesoftware/openfire/plugin/rest/utils/LoggingUtils.java +++ b/src/java/org/jivesoftware/openfire/plugin/rest/utils/LoggingUtils.java @@ -22,7 +22,6 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Arrays; public class LoggingUtils { private static final Logger AUDIT_LOG = LoggerFactory.getLogger("RestAPI-Plugin-Audit"); @@ -41,7 +40,34 @@ public enum AuditEvent { GROUPS_DELETE, //JustMarried - USER_CHANGE_NAME + USER_CHANGE_NAME, + + //Messages + MESSAGE_BROADCAST, + + //Message Archive + MESSAGE_ARCHIVE_UNREAD_COUNT, + + //MUC + // - MUC Affiliations + MUC_LIST_AFFILIATED_USERS_FOR_AFFILIATION, + MUC_REPLACE_AFFILIATED_USERS_FOR_AFFILIATION, + MUC_REMOVE_AFFILIATED_USER_OR_GROUP_FOR_AFFILIATION, + MUC_ADD_AFFILIATED_USERS_FOR_AFFILIATION, + MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_ADMIN, + MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_MEMBER, + MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OUTCAST, + MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OWNER, + // - MUC Rooms + MUC_LIST_ROOMS, + MUC_GET_ROOM, + MUC_DELETE_ROOM, + MUC_CREATE_ROOM, + MUC_UPDATE_ROOM, + MUC_GET_PARTICIPANT_LIST, + MUC_GET_OCCUPANT_LIST, + MUC_GET_ROOM_HISTORY, + MUC_INVITE_USER, ; } @@ -55,11 +81,14 @@ public static void auditEvent(AuditEvent event, Object... parameters){ String logMessage = "Event: " + event; logMessage += " - "; logMessage += "Parameters: + " + parameterString; + logMessage += " - "; + logMessage += "Caller: " + getCaller(); AUDIT_LOG.info(logMessage); }; } private static String parseParameters(Object[] parameters) { + //TODO: Does this belong here? ArrayList parsed = new ArrayList<>(); for (Object obj: parameters) { if(obj == null){ @@ -74,4 +103,20 @@ private static String parseParameters(Object[] parameters) { } return parsed.toString(); } + + /* + * Returns the name and method of the calling class. + */ + private static String getCaller() { + try { + StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + for (StackTraceElement element : stackTrace) { + if(element.getClassName().equals(LoggingUtils.class.getName())){ + continue; + } + return element.getClassName() + "." + element.getMethodName(); + } + } catch (Exception ignored) {} + return "unknown"; + } }