Search in sources :

Example 6 with RemoteIP

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());
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 7 with RemoteIP

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());
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 8 with RemoteIP

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);
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 9 with RemoteIP

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();
}
Also used : RemoteIP(com.zimbra.common.util.RemoteIP)

Aggregations

RemoteIP (com.zimbra.common.util.RemoteIP)9 HashMap (java.util.HashMap)6 MockHttpServletRequest (com.zimbra.cs.service.MockHttpServletRequest)5 URL (java.net.URL)5 Test (org.junit.Test)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ServiceException (com.zimbra.common.service.ServiceException)1 Element (com.zimbra.common.soap.Element)1 BufferStream (com.zimbra.common.util.BufferStream)1 EOFException (java.io.EOFException)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HttpSession (javax.servlet.http.HttpSession)1 HttpSessionBindingListener (javax.servlet.http.HttpSessionBindingListener)1