Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishbowler committed Nov 17, 2022
1 parent 56e4a4c commit 62833f2
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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;
Expand Down Expand Up @@ -154,6 +155,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 = MUCServiceController.getService(serviceName);
Collection<MUCRoomSearchInfo> roomsInfo = service.getAllRoomSearchInfo();

Expand Down Expand Up @@ -196,7 +202,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);
}
Expand All @@ -212,7 +221,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);
}
Expand All @@ -227,6 +238,9 @@ public void deleteChatRoom(String roomName, String serviceName) throws ServiceEx
*/
public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity, boolean sendInvitations) throws ServiceException {
log("Create a chat room: " + mucRoomEntity.getRoomName());
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_CREATE_ROOM,
"serviceName", serviceName,
"mucRoomEntity", mucRoomEntity);
try {
createRoom(mucRoomEntity, serviceName, sendInvitations);
} catch (NotAllowedException | ForbiddenException e) {
Expand Down Expand Up @@ -296,7 +310,10 @@ public RoomCreationResultEntities createMultipleChatRooms(String serviceName, MU
*/
public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity, boolean sendInvitations)
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 (!JID.nodeprep(roomName).equals(mucRoomEntity.getRoomName())) {
Expand Down Expand Up @@ -493,7 +510,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<ParticipantEntity> participants = new ArrayList<>();

Expand Down Expand Up @@ -523,7 +542,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<OccupantEntity> occupants = new ArrayList<>();

Expand Down Expand Up @@ -553,7 +574,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<MUCRoomMessageEntity> listMessages = new ArrayList<>();

Expand Down Expand Up @@ -602,6 +625,11 @@ public MUCRoomMessageEntities getRoomHistory(String roomName, String serviceName
*/
public void inviteUsersAndOrGroups(String serviceName, String roomName, MUCInvitationsEntity mucInvitationsEntity)
throws ServiceException {
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_INVITE_USER,
"roomName", roomName,
"serviceName", serviceName,
"jid", jid,
"invitation" , mucInvitationEntity);
MUCRoom room = getRoom(serviceName, roomName);

// First determine where to send all the invitations
Expand Down Expand Up @@ -987,6 +1015,7 @@ private static Collection<JID> setRoles(MUCRoom room, MUCRoomEntity mucRoomEntit
*/
public Collection<JID> 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:
Expand Down Expand Up @@ -1025,6 +1054,7 @@ public Collection<JID> getByAffiliation(@Nonnull final String serviceName, @Nonn
*/
public void replaceAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation, boolean sendInvitations, @Nonnull final String... jids) throws ServiceException
{
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_REPLACE_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation, jids);
final Collection<JID> replacements = new HashSet<>();

// Input validation.
Expand Down Expand Up @@ -1110,6 +1140,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, boolean sendInvitations, @Nonnull final String... jids) throws ServiceException
{
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation, jids);
final Collection<JID> additions = new HashSet<>();

// Input validation.
Expand Down Expand Up @@ -1183,6 +1214,7 @@ public void addAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final
* the service exception
*/
public void deleteAffiliation(String serviceName, String roomName, MUCRole.Affiliation affiliation, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public String getReason() {
public void setReason(String reason) {
this.reason = reason;
}
@Override
public String toString() {
return "MUCInvitationEntity [reason=" + reason + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import org.glassfish.jersey.internal.guava.MoreObjects;
import org.xmpp.packet.JID;

import java.util.Date;
Expand Down Expand Up @@ -363,4 +364,32 @@ public void setAdminGroups(List<String> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public String getBody() {
public void setBody(String body) {
this.body = body;
}

@Override
public String toString() {
return "MessageEntity [body=" + body + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
;
}

Expand All @@ -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<String> parsed = new ArrayList<>();
for (Object obj: parameters) {
if(obj == null){
Expand All @@ -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";
}
}

0 comments on commit 62833f2

Please sign in to comment.