use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testXForwardedHeaders.
@Test
public void testXForwardedHeaders() throws UnsupportedEncodingException, MalformedURLException {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
assertEquals("wrong originating IP", "172.16.150.11", remoteIp.getOrigIP());
assertEquals("wrong originating port", "8080", remoteIp.getOrigPort().toString());
assertEquals("wrong originating protocol", "IMAP", remoteIp.getOrigProto());
assertEquals("wrong request IP", "172.16.150.11", remoteIp.getRequestIP());
assertEquals("wrong request port", "8080", remoteIp.getRequestPort().toString());
assertEquals("wrong client IP", "192.168.1.1", remoteIp.getClientIP());
assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testNonTrustedClientIPRemoteIP.
@Test
public void testNonTrustedClientIPRemoteIP() throws UnsupportedEncodingException, MalformedURLException {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "10.10.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
// we should ignore X-Forwarded-XXX headers from non-trusted clients
assertNull("originating IP should be null", remoteIp.getOrigIP());
assertNull("originating port should be null", remoteIp.getOrigPort());
assertNull("originating protocol should be null", remoteIp.getOrigProto());
assertEquals("wrong request IP", "10.10.1.1", remoteIp.getRequestIP());
assertEquals("wrong request port", "80", remoteIp.getRequestPort().toString());
assertEquals("wrong client IP", "10.10.1.1", remoteIp.getClientIP());
assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class TestRemoteIP method testTrustedIPLogString.
@Test
public void testTrustedIPLogString() throws Exception {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
remoteIp.addToLoggingContext();
String updatedLogContext = ZimbraLog.getContextString();
assertTrue(updatedLogContext.indexOf("oip=172.16.150.11") > -1);
assertTrue(updatedLogContext.indexOf("oport=8080") > -1);
assertTrue(updatedLogContext.indexOf("ip=172.16.150.11") > -1);
assertTrue(updatedLogContext.indexOf("port=8080") > -1);
assertTrue(updatedLogContext.indexOf("oproto=IMAP") > -1);
}
use of com.zimbra.common.util.RemoteIP in project zm-mailbox by Zimbra.
the class ZimbraServlet method addRemoteIpToLoggingContext.
public static void addRemoteIpToLoggingContext(HttpServletRequest req) {
RemoteIP remoteIp = new RemoteIP(req, getTrustedIPs());
remoteIp.addToLoggingContext();
}
Aggregations