Skip to content

Commit 05f2333

Browse files
Add optional logging of remote/client IPs
Change-Id: I520abadf98e5437b705f7b4b6bbc59c23419162c
1 parent 87d1a79 commit 05f2333

File tree

5 files changed

+156
-4
lines changed

5 files changed

+156
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.sap.hcp.cf.logging.servlet.filter;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.sap.hcp.cf.logging.common.helper.Environment;
7+
8+
public class LogRemoteIPSettings {
9+
10+
private static final Logger LOGGER = LoggerFactory.getLogger(LogRemoteIPSettings.class);
11+
12+
private final String logRemoteIPSetting;
13+
14+
public LogRemoteIPSettings() {
15+
this(new Environment());
16+
}
17+
18+
LogRemoteIPSettings(Environment environment) {
19+
String tempLogRemoteIP = environment.getVariable("LogRemoteIP");
20+
21+
if (tempLogRemoteIP != null) {
22+
if (tempLogRemoteIP.equals("true") || tempLogRemoteIP.equals("True") || tempLogRemoteIP.equals("TRUE")) {
23+
tempLogRemoteIP = "true";
24+
LOGGER.info("Logging remote IPs has been ACTIVATED via environment variable");
25+
} else if (tempLogRemoteIP.equals("false") || tempLogRemoteIP.equals("False") || tempLogRemoteIP.equals(
26+
"FALSE")) {
27+
tempLogRemoteIP = "false";
28+
LOGGER.info("Logging remote IPs has been DEACTIVATED via environment variable");
29+
} else {
30+
LOGGER.error("Logging remote IPs is DEACTIVATED by default. Environment variable \"LogRemoteIP = {}\" could not " +
31+
"be read. It should be set to true to activate Logging of remote IPs or to false to deactivate it",
32+
tempLogRemoteIP);
33+
tempLogRemoteIP = "false";
34+
}
35+
} else {
36+
tempLogRemoteIP = "false";
37+
LOGGER.info("Logging remote IPs is DEACTIVATED by default.");
38+
}
39+
logRemoteIPSetting = tempLogRemoteIP;
40+
}
41+
42+
public String getLogRemoteIPSetting() {
43+
return logRemoteIPSetting;
44+
}
45+
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingFilter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ public class RequestLoggingFilter implements Filter {
3535
public static final String LOG_PROVIDER = "[SERVLET]";
3636
public static final String WRAP_RESPONSE_INIT_PARAM = "wrapResponse";
3737
public static final String WRAP_REQUEST_INIT_PARAM = "wrapRequest";
38+
public static final String LOG_REMOTE_IP = "LogRemoteIP";
3839

3940
private boolean wrapResponse = true;
4041
private boolean wrapRequest = true;
4142
private DynLogEnvironment dynLogEnvironment;
4243
private DynamicLogLevelProcessor dynamicLogLevelProcessor;
44+
protected LogRemoteIPSettings logRemoteIPSettings;
4345

4446
public RequestLoggingFilter() {
47+
logRemoteIPSettings = new LogRemoteIPSettings();
4548
dynLogEnvironment = new DynLogEnvironment();
4649
if (dynLogEnvironment.getRsaPublicKey() != null) {
4750
dynamicLogLevelProcessor = new DynamicLogLevelProcessor(dynLogEnvironment);
@@ -168,9 +171,11 @@ private void addHeaders(HttpServletRequest request, RequestRecord lrec) {
168171
: request.getRequestURI());
169172
lrec.addTag(Fields.METHOD, request.getMethod());
170173
lrec.addTag(Fields.PROTOCOL, getValue(request.getProtocol()));
171-
lrec.addTag(Fields.REMOTE_IP, getValue(request.getRemoteAddr()));
172-
lrec.addTag(Fields.REMOTE_HOST, getValue(request.getRemoteHost()));
173-
lrec.addTag(Fields.REMOTE_PORT, Integer.toString(request.getRemotePort()));
174+
if (logRemoteIPSettings.getLogRemoteIPSetting().equals("true")) {
175+
lrec.addTag(Fields.REMOTE_IP, getValue(request.getRemoteAddr()));
176+
lrec.addTag(Fields.REMOTE_HOST, getValue(request.getRemoteHost()));
177+
lrec.addTag(Fields.REMOTE_PORT, Integer.toString(request.getRemotePort()));
178+
}
174179
lrec.addTag(Fields.REMOTE_USER, getValue(request.getRemoteUser()));
175180
lrec.addTag(Fields.REFERER, getHeader(request, HttpHeaders.REFERER));
176181
lrec.addTag(Fields.X_FORWARDED_FOR, getHeader(request, HttpHeaders.X_FORWARDED_FOR));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.sap.hcp.cf.logging.servlet.filter;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
6+
7+
import org.junit.Test;
8+
9+
import com.sap.hcp.cf.logging.common.helper.Environment;
10+
11+
public class LogRemoteIPSettingsTest {
12+
13+
@Test
14+
public void testLogRemoteIPSettingsTrue() {
15+
Environment mockEnvironment = mock(Environment.class);
16+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("true");
17+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "true");
18+
}
19+
20+
@Test
21+
public void testLogRemoteIPSettingsTrueWithCapitalT() {
22+
Environment mockEnvironment = mock(Environment.class);
23+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("True");
24+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "true");
25+
}
26+
27+
@Test
28+
public void testLogRemoteIPSettingsTRUE() {
29+
Environment mockEnvironment = mock(Environment.class);
30+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("TRUE");
31+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "true");
32+
}
33+
34+
@Test
35+
public void testLogRemoteIPSettingsFalse() {
36+
Environment mockEnvironment = mock(Environment.class);
37+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("false");
38+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
39+
}
40+
41+
@Test
42+
public void testLogRemoteIPSettingsFalseWithCapitalF() {
43+
Environment mockEnvironment = mock(Environment.class);
44+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("False");
45+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
46+
}
47+
48+
@Test
49+
public void testLogRemoteIPSettingsFALSE() {
50+
Environment mockEnvironment = mock(Environment.class);
51+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("FALSE");
52+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
53+
}
54+
55+
@Test
56+
public void testLogRemoteIPSettingsInvalidEnvVariable() {
57+
Environment mockEnvironment = mock(Environment.class);
58+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("someInvalidString");
59+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
60+
}
61+
62+
@Test
63+
public void testLogRemoteIPSettingsEmptyEnvVariable() {
64+
Environment mockEnvironment = mock(Environment.class);
65+
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn(null);
66+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
67+
}
68+
69+
}

cf-java-logging-support-servlet/src/test/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingFilterTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO
136136

137137
@Test
138138
public void testWithSettings() throws IOException, ServletException {
139+
LogRemoteIPSettings mockLogRemoteIPSettings = mock(LogRemoteIPSettings.class);
140+
when(mockLogRemoteIPSettings.getLogRemoteIPSetting()).thenReturn("true");
139141
HttpServletRequest mockReq = mock(HttpServletRequest.class);
140142
HttpServletResponse mockResp = mock(HttpServletResponse.class);
141143
PrintWriter mockWriter = mock(PrintWriter.class);
@@ -147,7 +149,9 @@ public void testWithSettings() throws IOException, ServletException {
147149
when(mockReq.getHeader(HttpHeaders.X_VCAP_REQUEST_ID)).thenReturn(REQUEST_ID);
148150
when(mockReq.getHeader(HttpHeaders.REFERER)).thenReturn(REFERER);
149151
FilterChain mockFilterChain = mock(FilterChain.class);
150-
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
152+
RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter();
153+
requestLoggingFilter.logRemoteIPSettings = mockLogRemoteIPSettings;
154+
requestLoggingFilter.doFilter(mockReq, mockResp, mockFilterChain);
151155
assertThat(getField(Fields.REQUEST), is(FULL_REQUEST));
152156
assertThat(getField(Fields.CORRELATION_ID), is(REQUEST_ID));
153157
assertThat(getField(Fields.REQUEST_ID), is(REQUEST_ID));
@@ -157,6 +161,33 @@ public void testWithSettings() throws IOException, ServletException {
157161
assertThat(getField(Fields.REFERER), is(REFERER));
158162
}
159163

164+
@Test
165+
public void testWithSettingsWithoutRemoteIP() throws IOException, ServletException {
166+
LogRemoteIPSettings mockLogRemoteIPSettings = mock(LogRemoteIPSettings.class);
167+
when(mockLogRemoteIPSettings.getLogRemoteIPSetting()).thenReturn("false");
168+
HttpServletRequest mockReq = mock(HttpServletRequest.class);
169+
HttpServletResponse mockResp = mock(HttpServletResponse.class);
170+
PrintWriter mockWriter = mock(PrintWriter.class);
171+
when(mockResp.getWriter()).thenReturn(mockWriter);
172+
when(mockReq.getRequestURI()).thenReturn(REQUEST);
173+
when(mockReq.getQueryString()).thenReturn(QUERY_STRING);
174+
when(mockReq.getRemoteHost()).thenReturn(REMOTE_HOST);
175+
// will also set correlation id
176+
when(mockReq.getHeader(HttpHeaders.X_VCAP_REQUEST_ID)).thenReturn(REQUEST_ID);
177+
when(mockReq.getHeader(HttpHeaders.REFERER)).thenReturn(REFERER);
178+
FilterChain mockFilterChain = mock(FilterChain.class);
179+
RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter();
180+
requestLoggingFilter.logRemoteIPSettings = mockLogRemoteIPSettings;
181+
requestLoggingFilter.doFilter(mockReq, mockResp, mockFilterChain);
182+
assertThat(getField(Fields.REQUEST), is(FULL_REQUEST));
183+
assertThat(getField(Fields.CORRELATION_ID), is(REQUEST_ID));
184+
assertThat(getField(Fields.REQUEST_ID), is(REQUEST_ID));
185+
assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN));
186+
assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN));
187+
assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN));
188+
assertThat(getField(Fields.REFERER), is(REFERER));
189+
}
190+
160191
@Test
161192
public void testExplicitCorrelationId() throws IOException, ServletException {
162193
HttpServletRequest mockReq = mock(HttpServletRequest.class);

sample/manifest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ applications:
99
path: target/logging-sample-app-2.1.1.war
1010
env:
1111
RANDOM_SLEEP: true
12+
# Set LogRemoteIP: true to activate logging of remote IPs
13+
LogRemoteIP: false

0 commit comments

Comments
 (0)