-
Notifications
You must be signed in to change notification settings - Fork 155
Dlp #1909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Dlp #1909
Changes from 27 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
bcc7dcb
add dlp
russelmrcl 497a43c
add comments
russelmrcl f0119d3
wip
russelmrcl 5f84eb6
refactor code
russelmrcl e2536fe
wip
russelmrcl 6ebb9c4
wip
russelmrcl 9d2e722
wip
russelmrcl 699b226
docs: minor
predic8 7ffee70
wip
russelmrcl 559a62b
wip
russelmrcl de3ed95
wip
russelmrcl 4c3ff8f
wip: dlp
russelmrcl 8cfb161
wip: dlp
russelmrcl b17b14a
wip: dlp
russelmrcl 099ec70
add test
russelmrcl 02d75ec
wip
russelmrcl 2be464b
add strategy pattern
russelmrcl 450204b
wip test
russelmrcl 75b56d9
wip
russelmrcl 8adedb9
wip
russelmrcl e7cc895
refactor code
russelmrcl 0f673bc
add tests
russelmrcl 6a0bed1
wip
russelmrcl 469256e
resolve conversations
russelmrcl 53bc2f8
convert to json parse
russelmrcl 7d04071
Merge branch 'master' into dlp
christiangoerdes ff15443
add path
russelmrcl ea1e179
wip
russelmrcl dce7806
add mask
russelmrcl e2eaa48
wip
russelmrcl 13711b1
wip
russelmrcl 7aa7e95
wip
russelmrcl 60a13df
fix
russelmrcl 0bf1268
refactor
russelmrcl 21c3489
wip
russelmrcl 20ed318
refactor code
russelmrcl c20c70d
improve log
russelmrcl 528cc14
add docs
russelmrcl 8d75f7c
edit docs
russelmrcl c3ec071
wip
russelmrcl e9a6286
refactor code
russelmrcl 8b1a278
refactor code
russelmrcl 77022dd
refactor code
russelmrcl 5008ee7
Merge branch 'master' into dlp
russelmrcl e58e11d
Merge branch 'master' into dlp
christiangoerdes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/CsvFieldConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 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.*; | ||
|
|
||
| /** | ||
| * Loads field risk mappings from CSV file with format: | ||
| * field_name,description,risk_level | ||
| * where risk_level should be one of: high, medium, low, unclassified | ||
| */ | ||
| public class CsvFieldConfiguration implements FieldConfiguration { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(CsvFieldConfiguration.class); | ||
|
|
||
| // Optional: define allowed risk levels | ||
| private static final Set<String> ALLOWED_RISK_LEVELS = Set.of("high", "medium", "low", "unclassified"); | ||
|
|
||
| @Override | ||
| public Map<String, String> getFields(String fileName) { | ||
| try (InputStream inputStream = CsvFieldConfiguration.class.getClassLoader().getResourceAsStream(fileName)) { | ||
| if (inputStream == null) { | ||
| log.error("Could not find file: {}", fileName); | ||
| throw new NullPointerException("InputStream is null. File not found: " + fileName); | ||
| } | ||
|
|
||
| Map<String, String> riskDict = new HashMap<>(); | ||
| BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
| String line; | ||
| boolean isHeader = true; | ||
|
|
||
| while ((line = reader.readLine()) != null) { | ||
| line = line.trim(); | ||
| if (isHeader) { | ||
| isHeader = false; | ||
| continue; | ||
| } | ||
| if (line.isEmpty() || line.startsWith("#")) continue; | ||
|
|
||
| String[] parts = line.split(",", -1); | ||
| if (parts.length >= 2) { | ||
| String field = parts[0].trim().toLowerCase(Locale.ROOT); | ||
| String riskLevel = parts[parts.length - 1].trim().toLowerCase(Locale.ROOT); | ||
|
|
||
| if (!ALLOWED_RISK_LEVELS.contains(riskLevel)) { | ||
| log.warn("Unknown risk level '{}' for field '{}'", riskLevel, field); | ||
| } | ||
|
|
||
| riskDict.put(field, riskLevel); | ||
| } else { | ||
| log.warn("Invalid CSV line (too few columns): {}", line); | ||
| } | ||
| } | ||
|
|
||
| return riskDict; | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to load risk data from " + fileName, e); | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/DLPAnalyzer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.predic8.membrane.core.interceptor.dlp; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonFactoryBuilder; | ||
| import com.fasterxml.jackson.core.StreamReadConstraints; | ||
| import com.fasterxml.jackson.core.json.JsonReadFeature; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.predic8.membrane.core.http.Message; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.*; | ||
|
|
||
| public class DLPAnalyzer { | ||
|
|
||
| private static final JsonFactory JSON_FACTORY = new JsonFactoryBuilder() | ||
| .configure(JsonReadFeature.ALLOW_TRAILING_COMMA, true) | ||
| .configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS, false) | ||
| .streamReadConstraints(StreamReadConstraints.builder() | ||
| .maxNestingDepth(64) | ||
| .maxStringLength(16 * 1024) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| private static final ObjectMapper MAPPER = new ObjectMapper(JSON_FACTORY); | ||
|
|
||
| private final Map<String, String> riskDict; | ||
|
|
||
| public DLPAnalyzer(Map<String, String> riskDict) { | ||
| this.riskDict = Map.copyOf(riskDict); | ||
| } | ||
|
|
||
| public RiskReport analyze(Message msg) { | ||
| try (InputStream is = msg.getBodyAsStreamDecoded()) { | ||
| RiskReport report = new RiskReport(); | ||
| traverse(MAPPER.readTree(is), new ArrayDeque<>(), report); | ||
| return report; | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to analyse message", e); | ||
| } | ||
| } | ||
|
|
||
| private void traverse(JsonNode node, Deque<String> path, RiskReport report) { | ||
| if (node.isObject()) { | ||
| node.fieldNames().forEachRemaining(fieldName -> { | ||
| path.addLast(fieldName); | ||
| traverse(node.get(fieldName), path, report); | ||
| path.removeLast(); | ||
| }); | ||
| } else if (node.isArray()) { | ||
| for (JsonNode child : node) { | ||
| traverse(child, path, report); | ||
| } | ||
| } else { | ||
| String fullPath = String.join(".", path).toLowerCase(Locale.ROOT); | ||
| String simpleName = path.isEmpty() ? "" : path.getLast().toLowerCase(Locale.ROOT); | ||
| report.recordField(fullPath, classify(fullPath, simpleName)); | ||
| } | ||
| } | ||
|
|
||
| private String classify(String fullPath, String simpleName) { | ||
| return Optional.ofNullable(riskDict.get(fullPath)) | ||
| .or(() -> Optional.ofNullable(riskDict.get(simpleName))) | ||
| .orElse("unclassified") | ||
| .toLowerCase(Locale.ROOT); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/DLPInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| 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 java.nio.charset.StandardCharsets; | ||
|
|
||
| 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 DLPAnalyzer dlpAnalyzer; | ||
| private String fieldsConfig; | ||
| private String action = "report"; | ||
| private Fields fields; | ||
| private Filter filter; | ||
|
|
||
| @Override | ||
| public void init() { | ||
| if (fieldsConfig != null) { | ||
| dlpAnalyzer = new DLPAnalyzer(new CsvFieldConfiguration().getFields(fieldsConfig)); | ||
| } else { | ||
| dlpAnalyzer = new DLPAnalyzer(java.util.Map.of()); | ||
| } | ||
| super.init(); | ||
| } | ||
|
|
||
| @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) { | ||
|
|
||
| RiskReport report = dlpAnalyzer.analyze(msg); | ||
| log.info("DLP Risk Analysis: {}", report.getLogReport()); | ||
| msg.setBodyContent(filter.apply(msg.getBodyAsStringDecoded()).getBytes(StandardCharsets.UTF_8)); | ||
| 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; | ||
| } | ||
|
|
||
| public Fields getFields() { | ||
| return fields; | ||
| } | ||
|
|
||
| @MCChildElement | ||
| public void setFields(Fields fields) { | ||
| this.fields = fields; | ||
| } | ||
|
|
||
| public Filter getFilter() { | ||
| return filter; | ||
| } | ||
|
|
||
| @MCChildElement(order = 1) | ||
| public void setFilter(Filter filter) { | ||
| this.filter = filter; | ||
| } | ||
|
|
||
| } | ||
50 changes: 50 additions & 0 deletions
50
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/Field.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 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; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| @MCElement(name = "field") | ||
| public class Field { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(Field.class); | ||
|
|
||
| private String name; | ||
| private Action action = Action.REPORT; | ||
| private Pattern compiled = Pattern.compile(".*"); | ||
|
|
||
| @MCAttribute | ||
| public void setName(String name) { | ||
| this.name = Objects.requireNonNull(name, "field name must not be null"); | ||
| this.compiled = Pattern.compile(name, Pattern.CASE_INSENSITIVE); | ||
| } | ||
|
|
||
| @MCAttribute | ||
| public void setAction(String action) { | ||
| this.action = Action.valueOf(action.toUpperCase(Locale.ROOT)); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getAction() { | ||
| return action.name().toLowerCase(Locale.ROOT); | ||
| } | ||
|
|
||
| public void handleAction(Message msg) { | ||
| try { | ||
| FieldActionStrategy.of(action.name().toLowerCase(Locale.ROOT)).apply(msg, compiled); | ||
| } catch (Exception e) { | ||
| log.error("DLP field action '{}' failed: {}", name, e); | ||
| } | ||
| } | ||
|
|
||
| private enum Action {MASK, FILTER, REPORT} | ||
| } |
19 changes: 19 additions & 0 deletions
19
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/FieldActionStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.predic8.membrane.core.interceptor.dlp; | ||
|
|
||
| import com.predic8.membrane.core.http.Message; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public interface FieldActionStrategy { | ||
| void apply(Message msg, Pattern pattern); | ||
|
|
||
| static FieldActionStrategy of(String action) { | ||
| return switch (action.toLowerCase(Locale.ROOT)) { | ||
| case "mask" -> new MaskField(); | ||
| case "filter" -> new FilterField(); | ||
| case "report" -> new ReportField(); | ||
| default -> throw new IllegalArgumentException("Unknown action: " + action); | ||
| }; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/FieldConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
23 changes: 23 additions & 0 deletions
23
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/Fields.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
core/src/main/java/com/predic8/membrane/core/interceptor/dlp/Filter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.predic8.membrane.core.interceptor.dlp; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.jayway.jsonpath.DocumentContext; | ||
| import com.jayway.jsonpath.JsonPath; | ||
| import com.predic8.membrane.annot.MCChildElement; | ||
| import com.predic8.membrane.annot.MCElement; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @MCElement(name = "filter") | ||
| public class Filter { | ||
|
|
||
| private List<Path> paths = new ArrayList<>(); | ||
|
|
||
| public String apply(String json) { | ||
| try { | ||
| DocumentContext context = JsonPath.parse(json); | ||
| for (Path p : paths) { | ||
| try { | ||
| context.delete(p.getJsonpath()); | ||
| } catch (Exception e) { | ||
| } | ||
russelmrcl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| System.out.println(context.jsonString()); | ||
russelmrcl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return context.jsonString(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("DLP Filter failed to apply JSONPath deletions", e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @MCChildElement | ||
| public Filter setPaths(List<Path> paths) { | ||
| this.paths = paths; | ||
| return this; | ||
| } | ||
|
|
||
| public List<Path> getPaths() { | ||
| return paths; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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:
🤖 Prompt for AI Agents