Skip to content

Commit

Permalink
WIP: Simple fixes following review
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishbowler committed Nov 18, 2022
1 parent 62833f2 commit de2600c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
28 changes: 17 additions & 11 deletions src/java/org/jivesoftware/openfire/plugin/rest/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,7 @@ public void filter(ContainerRequestContext containerRequest) throws IOException

if (!plugin.getAllowedIPs().isEmpty()) {
// Get client's IP address
String ipAddress = httpRequest.getHeader("x-forwarded-for");
if (ipAddress == null) {
ipAddress = httpRequest.getHeader("X_FORWARDED_FOR");
if (ipAddress == null) {
ipAddress = httpRequest.getHeader("X-Forward-For");
if (ipAddress == null) {
ipAddress = httpRequest.getRemoteAddr();
}
}
}
String ipAddress = getClientIPAddressForRequest(httpRequest);
if (!plugin.getAllowedIPs().contains(ipAddress)) {
LOG.warn("REST API rejected service for IP address: " + ipAddress);
throw new WebApplicationException(Status.UNAUTHORIZED);
Expand All @@ -106,7 +97,8 @@ public void filter(ContainerRequestContext containerRequest) throws IOException
String auth = containerRequest.getHeaderString("authorization");

if (auth == null) {
LOG.warn("REST API request with no Authorization header rejected");
LOG.warn("REST API request with no Authorization header rejected. [Request IP: {}, Request URI: {}]",
getClientIPAddressForRequest(httpRequest), containerRequest.getUriInfo().getRequestUri());
throw new WebApplicationException(Status.UNAUTHORIZED);
}

Expand Down Expand Up @@ -153,4 +145,18 @@ private boolean isStatusEndpoint(String path){
path.equals("/plugins/restapi/v1/system/readiness") ||
path.startsWith("/plugins/restapi/v1/system/readiness/");
}

private String getClientIPAddressForRequest(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null) {
ipAddress = request.getHeader("X_FORWARDED_FOR");
if (ipAddress == null) {
ipAddress = request.getHeader("X-Forward-For");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
}
}
return ipAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
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;
import java.util.Optional;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
Expand Down Expand Up @@ -178,14 +174,13 @@ public void setMembers(List<String> members) {
@Override
public String toString() {

return MoreObjects.toStringHelper(this)
.add("name", this.name)
.add("description", this.description)
.add( "admins", this.admins)
.add("members", this.members)
.add("shared", this.shared)
.toString();

return "GroupEntity [" +
"name='" + name +
", description='" + description +
", admins=" + admins +
", members=" + members +
", shared=" + shared +
"]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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 @@ -366,30 +365,30 @@ public void setAdminGroups(List<String> 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();
return "MUCRoomEntity ["
+ "roomName=" + roomName
+ ", naturalName=" + naturalName
+ ", description=" + description
+ ", persistent=" + persistent
+ ", publicRoom=" + publicRoom
+ ", canAnyoneDiscoverJID=" + canAnyoneDiscoverJID
+ ", canOccupantsChangeSubject=" + canOccupantsChangeSubject
+ ", canOccupantsInvite=" + canOccupantsInvite
+ ", canChangeNickname=" + canChangeNickname
+ ", logEnabled=" + logEnabled
+ ", loginRestrictedToNickname=" + loginRestrictedToNickname
+ ", membersOnly=" + membersOnly
+ ", moderated=" + moderated
+ ", registrationEnabled=" + registrationEnabled
+ ", broadcastPresenceRoles=" + broadcastPresenceRoles
+ ", owners=" + owners
+ ", ownerGroups=" + ownerGroups
+ ", members=" + members
+ ", memberGroups=" + memberGroups
+ ", outcasts=" + outcasts
+ ", outcastGroups=" + outcastGroups
+ ", admins=" + admins
+ ", adminGroups=" + adminGroups
+ "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.jivesoftware.openfire.plugin.rest.utils;

import org.eclipse.jetty.util.log.Log;
import org.jivesoftware.openfire.plugin.rest.RESTServicePlugin;
import org.jivesoftware.util.JiveGlobals;
import org.slf4j.Logger;
Expand All @@ -25,6 +26,7 @@

public class LoggingUtils {
private static final Logger AUDIT_LOG = LoggerFactory.getLogger("RestAPI-Plugin-Audit");
private static final Logger LOG = LoggerFactory.getLogger(LoggingUtils.class);

public enum AuditEvent {
//Clustering
Expand Down Expand Up @@ -116,7 +118,9 @@ private static String getCaller() {
}
return element.getClassName() + "." + element.getMethodName();
}
} catch (Exception ignored) {}
} catch (Exception e) {
LOG.error("Unable to get caller of the logger. This should be impossible.", e);
}
return "unknown";
}
}

0 comments on commit de2600c

Please sign in to comment.