Skip to content

Commit

Permalink
Removed JSONPath-aware related code: to be implemented seperately
Browse files Browse the repository at this point in the history
  • Loading branch information
Breus committed Nov 19, 2023
1 parent a4a5a32 commit a4b60ca
Show file tree
Hide file tree
Showing 12 changed files with 3 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static JsonMasker getMasker(JsonMaskingConfig maskingConfig) {
} else if (maskingConfig.getAlgorithmType() == JsonMaskerAlgorithmType.KEYS_CONTAIN) {
return new KeyContainsMasker(maskingConfig);
} else {
return new PathAwareKeyContainsMasker(maskingConfig);
throw new IllegalArgumentException("Unknown masking algorithm type: " + maskingConfig.getAlgorithmType());
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* Default value: KEYS_CONTAIN
*/
public enum JsonMaskerAlgorithmType {
PATH_AWARE_KEYS_CONTAIN,
SINGLE_TARGET_LOOP,
KEYS_CONTAIN,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.blaauwendraad.masker.json.config;

import dev.blaauwendraad.masker.json.path.JsonPath;

import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -18,10 +16,6 @@ public class JsonMaskingConfig {
* The target key mode specifies how to the JSON properties corresponding to the target keys are processed.
*/
private final TargetKeyMode targetKeyMode;
/**
* Specifies the set of JSON paths for which the string/number values should be masked.
*/
private final Set<JsonPath> targetJsonPaths;
/**
* Specifies the algorithm type that will be used for masking.
*/
Expand All @@ -44,15 +38,8 @@ public class JsonMaskingConfig {
* By default, the correct {@link JsonMaskerAlgorithmType} is resolved based on the input of the builder. The logic
* for this is as follows:
* <p>
* If an algorithm type override is set, this will always be the algorithm used. If this algorithm is
* JSONPath-aware, the target keys that start with "$." will be interpreted as JSONPaths. If the algorithm is not
* JSONPath-aware, all targets will be interpreted as regular targets (even if they start with "$."), in which case
* this prefix will just be interpreted as part of the target key.
* If an algorithm type override is set, this will always be the algorithm used.
* <p>
* If no algorithm type override is set, the algorithm is selected as following: If the target set contains Strings
* starting with "$.", these will be interpreted as JSONPaths, and the JSONPath-aware algorithm is used. If the
* target set does not contain JSONPaths, the {@link JsonMaskerAlgorithmType#KEYS_CONTAIN} will be chosen if the
* target set contains more than one target key or {@link JsonMaskerAlgorithmType#SINGLE_TARGET_LOOP}.
*
* @param builder the builder object
*/
Expand Down Expand Up @@ -87,36 +74,19 @@ public class JsonMaskingConfig {
targets = targets.stream().map(String::toLowerCase).collect(Collectors.toSet());
}

Set<String> jsonPathLiterals = targets.stream()
.filter(t -> t.startsWith("$."))
.collect(Collectors.toSet());
if (builder.algorithmTypeOverride != null) {
algorithmType = builder.algorithmTypeOverride;
} else if (!jsonPathLiterals.isEmpty() && builder.resolveJsonPaths) {
algorithmType = JsonMaskerAlgorithmType.PATH_AWARE_KEYS_CONTAIN;
} else if (targets.size() > 1) {
algorithmType = JsonMaskerAlgorithmType.KEYS_CONTAIN;
} else {
algorithmType = JsonMaskerAlgorithmType.SINGLE_TARGET_LOOP;
}
switch (algorithmType) {
case PATH_AWARE_KEYS_CONTAIN -> {
targetJsonPaths = resolveJsonPaths(jsonPathLiterals);
targets.removeIf(jsonPathLiterals::contains);
targetKeys = targets;
}
case KEYS_CONTAIN, SINGLE_TARGET_LOOP -> {
targetJsonPaths = Set.of();
targetKeys = targets;
}
case KEYS_CONTAIN, SINGLE_TARGET_LOOP -> targetKeys = targets;
default -> throw new IllegalStateException("Unknown JSON masking algorithm");
}
}

private Set<JsonPath> resolveJsonPaths(Set<String> targets) {
return targets.stream().map(JsonPath::from).collect(Collectors.toSet());
}

public static JsonMaskingConfig getDefault(Set<String> targets) {
return custom(targets, TargetKeyMode.MASK).build();
}
Expand Down Expand Up @@ -163,10 +133,6 @@ public Set<String> getTargetKeys() {
return targetKeys;
}

public Set<JsonPath> getTargetJsonPaths() {
return targetJsonPaths;
}

/**
* Get the obfuscation length configuration value.
*
Expand Down Expand Up @@ -202,7 +168,6 @@ public static class Builder {
private final Set<String> targets;
private final TargetKeyMode targetKeyMode;
private int maskNumberValuesWith;
private boolean resolveJsonPaths;
private JsonMaskerAlgorithmType algorithmTypeOverride;
private int obfuscationLength;
private boolean caseSensitiveTargetKeys;
Expand All @@ -213,8 +178,6 @@ public Builder(Set<String> targets, TargetKeyMode targetKeyMode) {
this.targetKeyMode = targetKeyMode;
// by default, mask number values with is -1 which means number value masking is disabled
this.maskNumberValuesWith = -1;
// by default, JSON paths are resolved, every target starting with "$." is interpreted as a JSONPath
this.resolveJsonPaths = true;
// by default, length obfuscation is disabled
this.obfuscationLength = -1;
// by default, target keys are considered case-insensitive
Expand Down Expand Up @@ -269,18 +232,6 @@ public Builder caseSensitiveTargetKeys() {
return this;
}

/**
* Disables that target keys starting with a '$' are interpreted as JSON paths
* <p>
* Default value: true (JSON path resolving is enabled)
*
* @return the builder instance
*/
public Builder disableJsonPathResolving() {
this.resolveJsonPaths = false;
return this;
}

/**
* Creates a new {@link JsonMaskingConfig} instance.
*
Expand Down
90 changes: 0 additions & 90 deletions src/main/java/dev/blaauwendraad/masker/json/path/JsonPath.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ public static List<JsonMaskerTestInstance> getJsonMaskerTestInstancesFromFile(
new SingleTargetMasker(maskingConfig)
));
}
// if (algorithmTypes.contains(JsonMaskerAlgorithmType.PATH_AWARE_KEYS_CONTAIN)) {
// testInstances.add(new JsonMaskerTestInstance(
// input,
// expectedOutput,
// new PathAwareKeyContainsMasker(maskingConfig)
// ));
// }
}
return testInstances;
}
Expand Down
19 changes: 0 additions & 19 deletions src/test/java/dev/blaauwendraad/masker/json/PrettyPrintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.blaauwendraad.masker.json.config.JsonMaskingConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Set;
Expand Down Expand Up @@ -47,22 +46,4 @@ void prettyPrintMaskingKeyContains() throws JsonProcessingException {
.textValue()
);
}

@Test
@Disabled("JSON path key contains not yet implemented")
void prettyPrintMaskingPathAwareKeyContains() throws JsonProcessingException {
ObjectNode objectNode = JsonNodeFactory.instance.objectNode().put("Test", "Value");
JsonNode jsonNode = JsonNodeFactory.instance.objectNode().set("Test1", objectNode);
String prettyString = jsonNode.toPrettyString();
JsonMasker jsonMasker = new PathAwareKeyContainsMasker(JsonMaskingConfig.getDefault(Set.of("Test")));
String mask = jsonMasker.mask(prettyString);
Assertions.assertEquals(
"*****",
JsonMapper.builder()
.build()
.readValue(mask, JsonNode.class)
.findValue("Test")
.textValue()
);
}
}
Loading

0 comments on commit a4b60ca

Please sign in to comment.