Skip to content

Commit

Permalink
Add multiple censors (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsargent authored Oct 29, 2022
1 parent 38cecc5 commit be83a1e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 27 deletions.
3 changes: 2 additions & 1 deletion docs/guide/censor.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ If you are working with a componentized framework, you'll want to use the `censo
<file>file3.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<jsonGeneratorDecorator class="com.tersesystems.logback.censor.CensoringJsonGeneratorDecorator">
<censor-ref ref="json-censor"/>
<censor-ref ref="first-censor"/>
<censor-ref ref="second-censor"/>
</jsonGeneratorDecorator>
</encoder>
</appender>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
package com.tersesystems.logback.censor;

public interface CensorAttachable {
void setCensor(CensorContextAware censor);
void addCensor(CensorContextAware censor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* <p>Note that this does not filter out marker text or additional information related to the event,
* i.e. it does not filter out exception text.
*
* Note also that the censor converter only picks out one censor from the list.
*
* <pre>{@code
* <conversionRule conversionWord="censor"
* converterClass="com.tersesystems.logback.censor.CensorConverter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void begin(InterpretationContext ec, String tagName, Attributes attribute
+ censorAttachable
+ "at "
+ getLineNumber(ec));
censorAttachable.setCensor(censor);
censorAttachable.addCensor(censor);
}

public void end(InterpretationContext ec, String n) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class CensoringJsonGeneratorDecorator extends ContextAwareBase
implements CensorAttachable, JsonGeneratorDecorator, LifeCycle {

private CensorContextAware censor;
private final List<CensorContextAware> censors = new ArrayList<>();
private boolean started;

@Override
Expand All @@ -38,13 +38,12 @@ public JsonGenerator decorate(JsonGenerator generator) {
substitutionDelegate, new CensoringTokenFilter(), true, true);
}

public CensorContextAware getCensor() {
return censor;
public List<CensorContextAware> getCensors() {
return censors;
}

@Override
public void setCensor(CensorContextAware censor) {
this.censor = censor;
public void addCensor(CensorContextAware censor) {
this.censors.add(censor);
}

@Override
Expand All @@ -55,6 +54,7 @@ public void start() {
@Override
public void stop() {
filterKeys.clear();
censors.clear();
started = false;
}

Expand All @@ -63,7 +63,7 @@ public boolean isStarted() {
return started;
}

private List<String> filterKeys = new ArrayList<>();
private final List<String> filterKeys = new ArrayList<>();

public void addFilterKey(String filterKey) {
this.filterKeys.add(filterKey);
Expand All @@ -85,7 +85,7 @@ public TokenFilter includeProperty(String name) {
}

private boolean shouldFilter(String name) {
return filterKeys != null && filterKeys.contains(name);
return filterKeys.contains(name);
}

@Override
Expand All @@ -101,11 +101,13 @@ public CensoringJsonGeneratorDelegate(JsonGenerator d) {
}

private String censorSensitiveMessage(String original) {
if (censor != null) {
return String.valueOf(censor.censorText(original));
} else {
return original;
String value = original;
final List<CensorContextAware> censors = getCensors();
for (CensorContextAware censor : censors) {
value = String.valueOf(censor.censorText(value));
}

return value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void testJsonTest3() throws JoranException {
ch.qos.logback.classic.Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
TestAppender test = (TestAppender) root.getAppender("TEST3");
assertThat(test).isNotNull();
byte[] bytes = test.getEncoder().encode(createLoggingEvent(root, "hunter3"));
assertThat(new String(bytes, StandardCharsets.UTF_8)).contains("\"message\":\"[CENSOR3]\"");
byte[] bytes = test.getEncoder().encode(createLoggingEvent(root, "hunter3 hunter4"));
assertThat(new String(bytes, StandardCharsets.UTF_8)).contains("\"message\":\"[CENSOR3] [CENSOR4]\"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ public class CensoringJsonGeneratorDecoratorTest {
@Test
public void basicCensor() throws Exception {
LoggerContext context = new LoggerContext();
RegexCensor censor = new RegexCensor();
censor.setContext(context);
censor.setReplacementText("*******");
censor.setRegex("hunter2");
censor.start();
RegexCensor censor1 = new RegexCensor();
censor1.setContext(context);
censor1.setReplacementText("*******");
censor1.setRegex("hunter2");
censor1.start();

RegexCensor censor2 = new RegexCensor();
censor2.setContext(context);
censor2.setReplacementText("!!!!!!");
censor2.setRegex("message");
censor2.start();


CensoringJsonGeneratorDecorator decorator = new CensoringJsonGeneratorDecorator();
decorator.setContext(context);
decorator.setCensor(censor);
decorator.addCensor(censor1);
decorator.addCensor(censor2);
decorator.start();

StringWriter writer = new StringWriter();
Expand All @@ -44,7 +52,7 @@ public void basicCensor() throws Exception {
generator.writeEndObject();
generator.flush();

assertThat(writer.toString()).isEqualTo("{\"message\":\"My ******* message\"}");
assertThat(writer.toString()).isEqualTo("{\"message\":\"My ******* !!!!!!\"}");
}

@Test
Expand Down Expand Up @@ -76,7 +84,7 @@ public void prettyPrintCensor() throws Exception {

CensoringJsonGeneratorDecorator decorator = new CensoringPrettyPrintingJsonGeneratorDecorator();
decorator.setContext(context);
decorator.setCensor(censor);
decorator.addCensor(censor);
decorator.start();

StringWriter writer = new StringWriter();
Expand Down
10 changes: 8 additions & 2 deletions logback-censor/src/test/resources/test3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@

<conversionRule conversionWord="censor" converterClass="com.tersesystems.logback.censor.CensorConverter" />

<censor name="json-censor" class="com.tersesystems.logback.censor.RegexCensor">
<censor name="hunter3-censor" class="com.tersesystems.logback.censor.RegexCensor">
<regex>hunter3</regex>
<replacementText>[CENSOR3]</replacementText>
</censor>

<censor name="hunter4-censor" class="com.tersesystems.logback.censor.RegexCensor">
<regex>hunter4</regex>
<replacementText>[CENSOR4]</replacementText>
</censor>

<appender name="TEST3" class="com.tersesystems.logback.censor.TestAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<jsonGeneratorDecorator class="com.tersesystems.logback.censor.CensoringJsonGeneratorDecorator">
<censor-ref ref="json-censor"/>
<censor-ref ref="hunter3-censor"/>
<censor-ref ref="hunter4-censor"/>
</jsonGeneratorDecorator>
</encoder>
</appender>
Expand Down

0 comments on commit be83a1e

Please sign in to comment.