Skip to content

Commit 1e21b24

Browse files
authored
Merge pull request #20 from ChristophEichhorn/addAdditionalSwitches
Add additional switches
2 parents d9e0e01 + 2675619 commit 1e21b24

File tree

22 files changed

+338
-167
lines changed

22 files changed

+338
-167
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ All in all, you should do the following:
2727
* adjust your logging configuration accordingly.
2828

2929

30-
Say, you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `2.1.2`):
30+
Say, you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `2.1.3`):
3131

3232
```xml
3333
<properties>
34-
<cf-logging-version>2.1.2</cf-logging-version>
34+
<cf-logging-version>2.1.3</cf-logging-version>
3535
</properties>
3636
```
3737

cf-java-logging-support-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<parent>
3333
<groupId>com.sap.hcp.cf.logging</groupId>
3434
<artifactId>cf-java-logging-support-parent</artifactId>
35-
<version>2.1.2</version>
35+
<version>2.1.3</version>
3636
<relativePath>../pom.xml</relativePath>
3737
</parent>
3838
<build>

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/Defaults.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public interface Defaults {
1414
public LongValue RESPONSE_SIZE_B = new LongValue(-1);
1515
public LongValue REQUEST_SIZE_B = new LongValue(-1);
1616
public LongValue STATUS = new LongValue(200);
17+
18+
public String REDACTED = "redacted";
1719
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.sap.hcp.cf.logging.common;
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 LogOptionalFieldsSettings {
9+
10+
private static final Logger LOGGER = LoggerFactory.getLogger(LogOptionalFieldsSettings.class);
11+
12+
private final boolean logSensitiveConnectionData;
13+
private final boolean logRemoteUserField;
14+
private final boolean logRefererField;
15+
16+
public LogOptionalFieldsSettings(String invokingClass) {
17+
this(new Environment(), invokingClass);
18+
}
19+
20+
LogOptionalFieldsSettings(Environment environment, String invokingClass) {
21+
logSensitiveConnectionData = readEnvironmentVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA, environment,
22+
invokingClass);
23+
logRemoteUserField = readEnvironmentVariable(Environment.LOG_REMOTE_USER, environment, invokingClass);
24+
logRefererField = readEnvironmentVariable(Environment.LOG_REFERER, environment, invokingClass);
25+
}
26+
27+
private static boolean readEnvironmentVariable(String environmentVariableKey, Environment environment,
28+
String invokingClass) {
29+
String tempEnvVariable = environment.getVariable(environmentVariableKey);
30+
31+
if (tempEnvVariable == null) {
32+
LOGGER.trace("Logging remote IPs is DEACTIVATED by default for {}.", invokingClass);
33+
return false;
34+
}
35+
36+
if ("true".equalsIgnoreCase(tempEnvVariable)) {
37+
LOGGER.trace("Logging field {} has been ACTIVATED via environment variable for {}", environmentVariableKey,
38+
invokingClass);
39+
return true;
40+
}
41+
42+
if ("false".equalsIgnoreCase(tempEnvVariable)) {
43+
LOGGER.trace("Logging field {} has been DEACTIVATED via environment variable for {}",
44+
environmentVariableKey, invokingClass);
45+
return false;
46+
}
47+
48+
LOGGER.debug("Logging field {} is DEACTIVATED by default for {}. Environment variable \"LogRemoteIP = {}\" could not " +
49+
"be read. It should be set to true to activate Logging of remote IPs or to false to deactivate it",
50+
environmentVariableKey, invokingClass, tempEnvVariable);
51+
return false;
52+
53+
}
54+
55+
public boolean isLogSensitiveConnectionData() {
56+
return logSensitiveConnectionData;
57+
}
58+
59+
public boolean isLogRemoteUserField() {
60+
return logRemoteUserField;
61+
}
62+
63+
public boolean isLogRefererField() {
64+
return logRefererField;
65+
}
66+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.sap.hcp.cf.logging.common;
2+
3+
public class RequestRecordConfigurator {
4+
5+
private final RequestRecord requestRecord;
6+
7+
private RequestRecordConfigurator(RequestRecord requestRecord) {
8+
this.requestRecord = requestRecord;
9+
}
10+
11+
public static RequestRecordConfigurator to(RequestRecord requestRecord) {
12+
return new RequestRecordConfigurator(requestRecord);
13+
}
14+
15+
public RequestRecordConfigurator addOptionalTag(boolean optionalFieldCanBeLogged, String fieldKey, String tag) {
16+
17+
if (!optionalFieldCanBeLogged && tag != null) {
18+
requestRecord.addTag(fieldKey, Defaults.REDACTED);
19+
}
20+
21+
if (!optionalFieldCanBeLogged && tag.equals(Defaults.UNKNOWN)) {
22+
requestRecord.addTag(fieldKey, tag);
23+
}
24+
25+
if (optionalFieldCanBeLogged) {
26+
requestRecord.addTag(fieldKey, tag);
27+
}
28+
return this;
29+
}
30+
}

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/helper/Environment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
public class Environment {
44

5-
public static final String LOG_REMOTE_IP = "LOG_REMOTE_IP";
5+
public static final String LOG_SENSITIVE_CONNECTION_DATA = "LOG_SENSITIVE_CONNECTION_DATA";
6+
public static final String LOG_REMOTE_USER = "LOG_REMOTE_USER";
7+
public static final String LOG_REFERER = "LOG_REFERER";
68

79
public String getVariable(String name) {
810
return System.getenv(name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.sap.hcp.cf.logging.common;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import org.junit.Test;
9+
10+
import com.sap.hcp.cf.logging.common.helper.Environment;
11+
12+
public class LogOptionalFieldsSettingsTest {
13+
14+
@Test
15+
public void testLogOptionalFieldsSettingsTrue() {
16+
Environment mockEnvironment = mock(Environment.class);
17+
when(mockEnvironment.getVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA)).thenReturn("true");
18+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_USER)).thenReturn("True");
19+
when(mockEnvironment.getVariable(Environment.LOG_REFERER)).thenReturn("TRUE");
20+
21+
LogOptionalFieldsSettings settings = new LogOptionalFieldsSettings(mockEnvironment, "NameOfInvokingClass");
22+
23+
assertTrue("Wrapping LOG_SENSITIVE_CONNECTION_DATA failed", settings.isLogSensitiveConnectionData());
24+
assertTrue("Wrapping LOG_REMOTE_USER failed", settings.isLogRemoteUserField());
25+
assertTrue("Wrapping LOG_REFERER failed", settings.isLogRefererField());
26+
}
27+
28+
@Test
29+
public void testLogOptionalFieldsSettingsFalse() {
30+
Environment mockEnvironment = mock(Environment.class);
31+
when(mockEnvironment.getVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA)).thenReturn("false");
32+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_USER)).thenReturn("False");
33+
when(mockEnvironment.getVariable(Environment.LOG_REFERER)).thenReturn("FALSE");
34+
35+
LogOptionalFieldsSettings settings = new LogOptionalFieldsSettings(mockEnvironment, "NameOfInvokingClass");
36+
37+
assertFalse("Wrapping LOG_SENSITIVE_CONNECTION_DATA failed", settings.isLogSensitiveConnectionData());
38+
assertFalse("Wrapping LOG_REMOTE_USER failed", settings.isLogRemoteUserField());
39+
assertFalse("Wrapping LOG_REFERER failed", settings.isLogRefererField());
40+
}
41+
42+
@Test
43+
public void testLogOptionalFieldsSettingsInvalidEnvVariable() {
44+
Environment mockEnvironment = mock(Environment.class);
45+
when(mockEnvironment.getVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA)).thenReturn("someInvalidString");
46+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_USER)).thenReturn("someInvalidString");
47+
when(mockEnvironment.getVariable(Environment.LOG_REFERER)).thenReturn("someInvalidString");
48+
49+
LogOptionalFieldsSettings settings = new LogOptionalFieldsSettings(mockEnvironment, "NameOfInvokingClass");
50+
assertFalse("Wrapping LOG_SENSITIVE_CONNECTION_DATA failed", settings.isLogSensitiveConnectionData());
51+
assertFalse("Wrapping LOG_REMOTE_USER failed", settings.isLogRemoteUserField());
52+
assertFalse("Wrapping LOG_REFERER failed", settings.isLogRefererField());
53+
}
54+
55+
@Test
56+
public void testLogOptionalFieldsSettingsEmptyString() {
57+
Environment mockEnvironment = mock(Environment.class);
58+
when(mockEnvironment.getVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA)).thenReturn("");
59+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_USER)).thenReturn("");
60+
when(mockEnvironment.getVariable(Environment.LOG_REFERER)).thenReturn("");
61+
62+
LogOptionalFieldsSettings settings = new LogOptionalFieldsSettings(mockEnvironment, "NameOfInvokingClass");
63+
assertFalse("Wrapping LOG_SENSITIVE_CONNECTION_DATA failed", settings.isLogSensitiveConnectionData());
64+
assertFalse("Wrapping LOG_REMOTE_USER failed", settings.isLogRemoteUserField());
65+
assertFalse("Wrapping LOG_REFERER failed", settings.isLogRefererField());
66+
}
67+
68+
@Test
69+
public void testLogOptionalFieldsSettingsEmptyEnvVariable() {
70+
Environment mockEnvironment = mock(Environment.class);
71+
when(mockEnvironment.getVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA)).thenReturn(null);
72+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_USER)).thenReturn(null);
73+
when(mockEnvironment.getVariable(Environment.LOG_REFERER)).thenReturn(null);
74+
75+
LogOptionalFieldsSettings settings = new LogOptionalFieldsSettings(mockEnvironment, "NameOfInvokingClass");
76+
77+
assertFalse("Wrapping LOG_SENSITIVE_CONNECTION_DATA failed", settings.isLogSensitiveConnectionData());
78+
assertFalse("Wrapping LOG_REMOTE_USER failed", settings.isLogRemoteUserField());
79+
assertFalse("Wrapping LOG_REFERER failed", settings.isLogRefererField());
80+
}
81+
82+
@Test
83+
public void testLogOptionalFieldsWithMixedSettings() {
84+
Environment mockEnvironment = mock(Environment.class);
85+
when(mockEnvironment.getVariable(Environment.LOG_SENSITIVE_CONNECTION_DATA)).thenReturn("false");
86+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_USER)).thenReturn("true");
87+
when(mockEnvironment.getVariable(Environment.LOG_REFERER)).thenReturn("True");
88+
89+
LogOptionalFieldsSettings settings = new LogOptionalFieldsSettings(mockEnvironment, "NameOfInvokingClass");
90+
91+
assertFalse("Wrapping LOG_SENSITIVE_CONNECTION_DATA failed", settings.isLogSensitiveConnectionData());
92+
assertTrue("Wrapping LOG_REMOTE_USER failed", settings.isLogRemoteUserField());
93+
assertTrue("Wrapping LOG_REFERER failed", settings.isLogRefererField());
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.sap.hcp.cf.logging.common;
2+
3+
import static com.sap.hcp.cf.logging.common.RequestRecordConfigurator.to;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import java.io.IOException;
7+
8+
import org.junit.Test;
9+
10+
import com.fasterxml.jackson.jr.ob.JSON;
11+
import com.fasterxml.jackson.jr.ob.JSONObjectException;
12+
13+
public class RequestRecordConfiguratorTest {
14+
15+
@Test
16+
public void testAddingSingleActivatedOptionalTagToRequestRecord() throws JSONObjectException, IOException {
17+
RequestRecord requestRecord = new RequestRecord("TEST");
18+
boolean canBeLogged = true;
19+
String key = "TestKey";
20+
String tag = "TestTag";
21+
22+
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
23+
requestRecord.close();
24+
25+
assertEquals(tag, getFieldFromRequestRecord(requestRecord, key));
26+
}
27+
28+
@Test
29+
public void testAddingSingleForbiddenOptionalTagToRequestRecord() throws JSONObjectException, IOException {
30+
RequestRecord requestRecord = new RequestRecord("TEST");
31+
boolean canBeLogged = false;
32+
String key = "TestKey";
33+
String tag = "TestTag";
34+
35+
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
36+
requestRecord.close();
37+
38+
assertEquals(Defaults.REDACTED, getFieldFromRequestRecord(requestRecord, key));
39+
}
40+
41+
@Test
42+
public void testAddingSingleForbiddenOptionalNullTagToRequestRecord() throws JSONObjectException, IOException {
43+
RequestRecord requestRecord = new RequestRecord("TEST");
44+
boolean canBeLogged = false;
45+
String key = "TestKey";
46+
String tag = Defaults.UNKNOWN;
47+
48+
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
49+
requestRecord.close();
50+
51+
assertEquals(Defaults.UNKNOWN, getFieldFromRequestRecord(requestRecord, key));
52+
}
53+
54+
@Test
55+
public void testAddingSingleActivatedOptionalNullTagToRequestRecord() throws JSONObjectException, IOException {
56+
RequestRecord requestRecord = new RequestRecord("TEST");
57+
boolean canBeLogged = true;
58+
String key = "TestKey";
59+
String tag = Defaults.UNKNOWN;
60+
61+
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
62+
requestRecord.close();
63+
64+
assertEquals(Defaults.UNKNOWN, getFieldFromRequestRecord(requestRecord, key));
65+
}
66+
67+
private String getFieldFromRequestRecord(RequestRecord requestRecord, String key) throws JSONObjectException,
68+
IOException {
69+
return JSON.std.mapFrom(requestRecord.toString()).get(key).toString();
70+
}
71+
}

cf-java-logging-support-jersey/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath>../pom.xml</relativePath>
1010
<groupId>com.sap.hcp.cf.logging</groupId>
1111
<artifactId>cf-java-logging-support-parent</artifactId>
12-
<version>2.1.2</version>
12+
<version>2.1.3</version>
1313
</parent>
1414

1515
<name>cf-java-logging-support-jersey</name>

cf-java-logging-support-jersey/src/main/java/com/sap/hcp/cf/logging/jersey/filter/RequestHandler.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package com.sap.hcp.cf.logging.jersey.filter;
22

3+
import static com.sap.hcp.cf.logging.common.RequestRecordConfigurator.to;
4+
35
import java.net.URI;
46

57
import com.sap.hcp.cf.logging.common.Defaults;
68
import com.sap.hcp.cf.logging.common.Fields;
79
import com.sap.hcp.cf.logging.common.HttpHeaders;
810
import com.sap.hcp.cf.logging.common.LogContext;
11+
import com.sap.hcp.cf.logging.common.LogOptionalFieldsSettings;
912
import com.sap.hcp.cf.logging.common.LongValue;
1013
import com.sap.hcp.cf.logging.common.RequestRecord;
1114

1215
public class RequestHandler {
16+
final LogOptionalFieldsSettings logOptionalFieldsSettings;
17+
18+
public RequestHandler() {
19+
String invokingClass = this.getClass().getName().toString();
20+
logOptionalFieldsSettings = new LogOptionalFieldsSettings(invokingClass);
21+
}
1322

1423
public RequestRecord handle(RequestContextAdapter adapter) {
1524

@@ -39,15 +48,22 @@ public RequestRecord handle(RequestContextAdapter adapter) {
3948
}
4049

4150
private void addHeaders(RequestContextAdapter adapter, RequestRecord lrec) {
51+
4252
lrec.addTag(Fields.REQUEST, getValue(getRequestUri(adapter)));
4353
lrec.addTag(Fields.METHOD, getValue(adapter.getMethod()));
44-
lrec.addTag(Fields.REMOTE_IP, getValue(adapter.getUri().getAuthority()));
45-
lrec.addTag(Fields.REMOTE_HOST, getValue(adapter.getUri().getHost()));
46-
lrec.addTag(Fields.REMOTE_PORT, Integer.toString(adapter.getUri().getPort()));
47-
lrec.addTag(Fields.REMOTE_USER, getValue(adapter.getUser()));
48-
lrec.addTag(Fields.REFERER, getHeader(adapter, HttpHeaders.REFERER));
49-
lrec.addTag(Fields.X_FORWARDED_FOR, getHeader(adapter, HttpHeaders.X_FORWARDED_FOR));
5054

55+
boolean isSensitiveConnectionData = logOptionalFieldsSettings.isLogSensitiveConnectionData();
56+
57+
to(lrec).addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_IP, getValue(adapter.getUri().getAuthority()))
58+
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_HOST, getValue(adapter.getUri().getHost()))
59+
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_PORT, Integer.toString(adapter.getUri()
60+
.getPort()))
61+
.addOptionalTag(isSensitiveConnectionData, Fields.X_FORWARDED_FOR, getHeader(adapter,
62+
HttpHeaders.X_FORWARDED_FOR))
63+
.addOptionalTag(logOptionalFieldsSettings.isLogRemoteUserField(), Fields.REMOTE_USER, getValue(adapter
64+
.getUser()))
65+
.addOptionalTag(logOptionalFieldsSettings.isLogRefererField(), Fields.REFERER, getHeader(adapter,
66+
HttpHeaders.REFERER));
5167
lrec.addContextTag(Fields.REQUEST_ID, getHeader(adapter, HttpHeaders.X_VCAP_REQUEST_ID));
5268

5369
lrec.addValue(Fields.REQUEST_SIZE_B, new LongValue(adapter.getRequestSize()));

0 commit comments

Comments
 (0)