Skip to content
Closed

Dlp #1909

Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
bcc7dcb
add dlp
russelmrcl Jun 23, 2025
497a43c
add comments
russelmrcl Jun 23, 2025
f0119d3
wip
russelmrcl Jun 23, 2025
5f84eb6
refactor code
russelmrcl Jun 23, 2025
e2536fe
wip
russelmrcl Jun 23, 2025
6ebb9c4
wip
russelmrcl Jun 23, 2025
9d2e722
wip
russelmrcl Jun 23, 2025
699b226
docs: minor
predic8 Jun 23, 2025
7ffee70
wip
russelmrcl Jun 23, 2025
559a62b
wip
russelmrcl Jun 23, 2025
de3ed95
wip
russelmrcl Jun 23, 2025
4c3ff8f
wip: dlp
russelmrcl Jun 26, 2025
8cfb161
wip: dlp
russelmrcl Jun 26, 2025
b17b14a
wip: dlp
russelmrcl Jun 26, 2025
099ec70
add test
russelmrcl Jun 26, 2025
02d75ec
wip
russelmrcl Jun 30, 2025
2be464b
add strategy pattern
russelmrcl Jun 30, 2025
450204b
wip test
russelmrcl Jun 30, 2025
75b56d9
wip
russelmrcl Jun 30, 2025
8adedb9
wip
russelmrcl Jun 30, 2025
e7cc895
refactor code
russelmrcl Jun 30, 2025
0f673bc
add tests
russelmrcl Jun 30, 2025
6a0bed1
wip
russelmrcl Jun 30, 2025
469256e
resolve conversations
russelmrcl Jun 30, 2025
53bc2f8
convert to json parse
russelmrcl Jun 30, 2025
7d04071
Merge branch 'master' into dlp
christiangoerdes Jun 30, 2025
ff15443
add path
russelmrcl Jun 30, 2025
ea1e179
wip
russelmrcl Jun 30, 2025
dce7806
add mask
russelmrcl Jun 30, 2025
e2eaa48
wip
russelmrcl Jul 7, 2025
13711b1
wip
russelmrcl Jul 17, 2025
7aa7e95
wip
russelmrcl Jul 17, 2025
60a13df
fix
russelmrcl Jul 17, 2025
0bf1268
refactor
russelmrcl Jul 17, 2025
21c3489
wip
russelmrcl Jul 17, 2025
20ed318
refactor code
russelmrcl Jul 21, 2025
c20c70d
improve log
russelmrcl Jul 21, 2025
528cc14
add docs
russelmrcl Jul 21, 2025
8d75f7c
edit docs
russelmrcl Jul 21, 2025
c3ec071
wip
russelmrcl Aug 1, 2025
e9a6286
refactor code
russelmrcl Aug 1, 2025
8b1a278
refactor code
russelmrcl Aug 1, 2025
77022dd
refactor code
russelmrcl Aug 26, 2025
5008ee7
Merge branch 'master' into dlp
russelmrcl Sep 11, 2025
e58e11d
Merge branch 'master' into dlp
christiangoerdes Nov 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.predic8.membrane.core.interceptor.dlp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class CsvFieldConfiguration implements FieldConfiguration {

private static final Logger log = LoggerFactory.getLogger(CsvFieldConfiguration.class);

@Override
public Map<String, String> getFields(String fileName) {
try (InputStream inputStream = CsvFieldConfiguration.class.getClassLoader().getResourceAsStream(fileName)) {
Map<String, String> riskDict = new HashMap<>();
if (inputStream == null) {
log.error("Could not find file: {}", fileName);
throw new NullPointerException("InputStream is null. File not found: " + fileName);
}

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
boolean isHeader = true;

while ((line = reader.readLine()) != null) {
if (isHeader) {
isHeader = false;
continue;
}

String[] parts = line.split(",", -1);
if (parts.length >= 3) {
String field = parts[0].trim().toLowerCase();
String riskLevel = parts[2].trim();
riskDict.put(field, riskLevel);
} else {
log.warn("Invalid CSV line: {}", line);
}
}
return riskDict;
} catch (IOException e) {
throw new RuntimeException("Failed to load risk data from " + fileName, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.predic8.membrane.core.http.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class DLP {

private static final Logger log = LoggerFactory.getLogger(DLP.class);
private static final JsonFactory JSON_FACTORY = new JsonFactory();

private final Map<String, String> riskDict;

public DLP(Map<String, String> riskDict) {
this.riskDict = riskDict;
}

public RiskReport analyze(Message msg) {
try (JsonParser parser = createParser(msg)) {
Deque<String> contextStack = new ArrayDeque<>();
RiskReport report = new RiskReport();

String currentField = null;

while (parser.nextToken() != null) {
JsonToken token = parser.currentToken();

switch (token) {
case FIELD_NAME -> currentField = parser.currentName();

case START_OBJECT, START_ARRAY -> {
if (currentField != null) {
contextStack.addLast(currentField);
currentField = null;
}
}

case END_OBJECT, END_ARRAY -> {
if (!contextStack.isEmpty()) contextStack.removeLast();
}

default -> {
if (currentField != null) {
String fullPath = buildFullPath(contextStack, currentField).toLowerCase();
report.recordField(fullPath, Optional.ofNullable(riskDict.get(fullPath))
.orElse(riskDict.getOrDefault(currentField.toLowerCase(), "unclassified"))
.toLowerCase());
currentField = null;
}
}
}
}
return report;
} catch (IOException e) {
log.error("Parse Error: {}", e.getMessage());
throw new RuntimeException(e);
}
}

private JsonParser createParser(Message msg) throws IOException {
return JSON_FACTORY.createParser(new InputStreamReader(msg.getBodyAsStreamDecoded(), Optional.ofNullable(msg.getCharset()).orElse(StandardCharsets.UTF_8.name())));
}

private String buildFullPath(Deque<String> stack, String field) {
List<String> path = new ArrayList<>(stack);
path.add(field);
return String.join(".", path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Message;
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.Outcome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;

@MCElement(name = "dlp")
public class DLPInterceptor extends AbstractInterceptor {

private static final Logger log = LoggerFactory.getLogger(DLPInterceptor.class);
private DLP dlp;
private String fieldsConfig;
private String action = "report";
private Fields fields;

@Override
public void init() {
super.init();
dlp = new DLP(new CsvFieldConfiguration().getFields(fieldsConfig));
}

@Override
public Outcome handleRequest(Exchange exc) {
return handleInternal(exc.getRequest());
}

@Override
public Outcome handleResponse(Exchange exc) {
return handleInternal(exc.getResponse());
}

public Outcome handleInternal(Message msg) {
if (dlp == null) {
log.warn("DLP not initialized.");
return CONTINUE;
}

RiskReport report = dlp.analyze(msg);
log.info("DLP Risk Analysis: {}", report.getLogReport());

if (fields != null && !fields.getFields().isEmpty()) {
for (Field f : fields.getFields()) {
f.handleAction(msg);
}
} else {
report.getMatchedFields().keySet().forEach(name -> {
Field f = new Field();
f.setName(name);
f.setAction(action);
f.handleAction(msg);
});
}
return CONTINUE;
}

public String getFieldsConfig() {
return fieldsConfig;
}

@MCAttribute
public void setFieldsConfig(String fieldsConfig) {
this.fieldsConfig = fieldsConfig;
}

public String getAction() {
return action;
}

@MCAttribute
public void setAction(String action) {
this.action = action;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

🧩 Verification correct

Add validation for the action parameter.

The setter should validate that the action is one of the supported values.

Apply this diff to add validation:

 @MCAttribute
 public void setAction(String action) {
+    if (action != null && !action.matches("(?i)filter|mask|report")) {
+        throw new IllegalArgumentException("Invalid action: " + action + ". Must be one of: filter, mask, report");
+    }
     this.action = action;
 }

Run the following script to verify the valid actions in the Field class:


🏁 Script executed:

#!/bin/bash
# Description: Verify the valid actions supported by the Field class

# Search for action handling in Field class
ast-grep --pattern 'class Field {
  $$$
}'

# Search for action-related constants or enums
rg -A 5 "action.*=.*\"(filter|mask|report)\""

Length of output: 703


Add validation for the action parameter.

The setter should validate that the action is one of the supported values.

Apply this diff to add validation:

 @MCAttribute
 public void setAction(String action) {
+    if (action != null && !action.matches("(?i)filter|mask|report")) {
+        throw new IllegalArgumentException("Invalid action: " + action + ". Must be one of: filter, mask, report");
+    }
     this.action = action;
 }

Run the following script to verify the valid actions in the Field class:

#!/bin/bash
# Description: Verify the valid actions supported by the Field class

# Search for action handling in Field class
ast-grep --pattern 'class Field {
  $$$
}'

# Search for action-related constants or enums
rg -A 5 "action.*=.*\"(filter|mask|report)\""
🤖 Prompt for AI Agents
In
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/DLPInterceptor.java
around lines 77 to 80, the setAction method lacks validation for the action
parameter. Modify the setter to check if the provided action string matches one
of the supported values (e.g., "filter", "mask", "report") before assigning it.
If the action is invalid, throw an IllegalArgumentException or handle the error
appropriately to prevent unsupported actions from being set.


public Fields getFields() {
return fields;
}

@MCChildElement
public void setFields(Fields fields) {
this.fields = fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.http.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@MCElement(name = "field")
public class Field {

private static final Logger log = LoggerFactory.getLogger(Field.class);

private String name;
private String action;

@MCAttribute
public void setName(String name) {
this.name = name;
}

@MCAttribute
public void setAction(String action) {
this.action = action.toLowerCase();
}

public String getName() {
return name;
}

public String getAction() {
return action;
}

public void handleAction(Message msg) {
String json = msg.getBodyAsStringDecoded();
String modified = json;

switch (action) {
case "filter" -> modified = filter(json);
case "mask" -> modified = mask(json);
case "report" -> modified = "";
default -> log.warn("Unknown DLP action: {}", action);
}

msg.setBodyContent(modified.getBytes());
}

private String filter(String json) {
return json.replaceAll("\"(" + name + ")\"\\s*:\\s*\".*?\"\\s*,?", "");
}

private String mask(String json) {
return json.replaceAll("\"(" + name + ")\"\\s*:\\s*(\".*?\"|-?\\d+(\\.\\d+)?|true|false|null)", "\"$1\":\"****\"");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.predic8.membrane.core.interceptor.dlp;

import java.util.Map;

public interface FieldConfiguration {
Map<String, String> getFields(String fileName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;

import java.util.ArrayList;
import java.util.List;

@MCElement(name = "fields")
public class Fields {

private List<Field> fields = new ArrayList<>();

@MCChildElement
public Fields setFields(List<Field> fields) {
this.fields = fields;
return this;
}

public List<Field> getFields() {
return fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.predic8.membrane.core.interceptor.dlp;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RiskReport {

private static final List<String> LEVELS = List.of("high", "medium", "low", "unclassified");

private final Map<String, String> matchedFields = new LinkedHashMap<>();
private final Map<String, Integer> riskCounts = new HashMap<>();
private final Map<String, Map<String, Integer>> riskDetails = new HashMap<>();

public void recordField(String field, String riskLevel) {
matchedFields.put(field, riskLevel);
riskCounts.merge(riskLevel, 1, Integer::sum);
riskDetails.computeIfAbsent(riskLevel, r -> new LinkedHashMap<>()).merge(field, 1, Integer::sum);
}

public Map<String, Object> getLogReport() {
Map<String, Object> out = new LinkedHashMap<String, Object>();
LEVELS.forEach(level -> {
out.put(level + "_risk", riskCounts.getOrDefault(level, 0));
riskDetails.computeIfPresent(level, (k, v) -> {
out.put(level + "_details", v);
return v;
});
});
return out;
}

Map<String, String> getMatchedFields() {
return matchedFields;
}

Map<String, Integer> getRiskCounts() {
return riskCounts;
}
}
Loading