Skip to content

Commit

Permalink
The pattern should now be included in the payload to the pseudonymize…
Browse files Browse the repository at this point in the history
…Field endpoint. (#111)
  • Loading branch information
bjornandre authored May 2, 2024
1 parent cdadb5c commit 76c184f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public HttpResponse<Flowable<byte[]>> pseudonymizeField(
try {
PseudoFieldRequest req = Json.toObject(PseudoFieldRequest.class, request);
log.info(Strings.padEnd(String.format("*** Pseudonymize field: %s ", req.getName()), 80, '*'));
PseudoField pseudoField = new PseudoField(req.getName(), req.getPseudoFunc(), req.getKeyset());
PseudoField pseudoField = new PseudoField(req.getName(), req.getPattern(), req.getPseudoFunc(), req.getKeyset());

final String correlationId = MDC.get("CorrelationID");

Expand Down Expand Up @@ -109,7 +109,7 @@ public HttpResponse<Flowable<byte[]>> depseudonymizeField(
try {
DepseudoFieldRequest req = Json.toObject(DepseudoFieldRequest.class, request);
log.info(Strings.padEnd(String.format("*** Depseudonymize field: %s ", req.getName()), 80, '*'));
PseudoField pseudoField = new PseudoField(req.getName(), req.getPseudoFunc(), req.getKeyset());
PseudoField pseudoField = new PseudoField(req.getName(), req.getPattern(), req.getPseudoFunc(), req.getKeyset());

final String correlationId = MDC.get("CorrelationID");

Expand Down Expand Up @@ -139,8 +139,8 @@ public HttpResponse<Flowable<byte[]>> repseudonymizeField(
try {
RepseudoFieldRequest req = Json.toObject(RepseudoFieldRequest.class, request);
log.info(Strings.padEnd(String.format("*** Repseudonymize field: %s ", req.getName()), 80, '*'));
PseudoField sourcePseudoField = new PseudoField(req.getName(), req.getSourcePseudoFunc(), req.getSourceKeyset());
PseudoField targetPseudoField = new PseudoField(req.getName(), req.getTargetPseudoFunc(), req.getTargetKeyset());
PseudoField sourcePseudoField = new PseudoField(req.getName(), req.getPattern(), req.getSourcePseudoFunc(), req.getSourceKeyset());
PseudoField targetPseudoField = new PseudoField(req.getName(), req.getPattern(), req.getTargetPseudoFunc(), req.getTargetKeyset());

final String correlationId = MDC.get("CorrelationID");
return HttpResponse.ok(
Expand Down Expand Up @@ -436,6 +436,7 @@ public static class PseudoFieldRequest {
private String pseudoFunc;
private EncryptedKeysetWrapper keyset;
private String name;
private String pattern;
private List<String> values;
}

Expand All @@ -448,6 +449,7 @@ public static class DepseudoFieldRequest {
private String pseudoFunc;
private EncryptedKeysetWrapper keyset;
private String name;
private String pattern;
private List<String> values;
}

Expand All @@ -462,6 +464,7 @@ public static class RepseudoFieldRequest {
private EncryptedKeysetWrapper sourceKeyset;
private EncryptedKeysetWrapper targetKeyset;
private String name;
private String pattern;
private List<String> values;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,26 @@ public class PseudoField {
* a default pseudo configuration is used.
*
* @param name The name of the field.
* @param pattern The pattern that matched the field.
* @param pseudoFunc The pseudo function definition.
* @param keyset The encrypted keyset to be used for pseudonymization.
*/
public PseudoField(String name, String pseudoFunc, EncryptedKeysetWrapper keyset) {
public PseudoField(String name, String pattern, String pseudoFunc, EncryptedKeysetWrapper keyset) {
this.name = name;

pseudoConfig = new PseudoConfig();

// For backwards compatibility
if (pattern == null) {
pattern = "**";
}
if (pseudoFunc == null) {
pseudoFunc = DEFAULT_PSEUDO_FUNC;
}
if (keyset != null) {
pseudoConfig.getKeysets().add(keyset);
}
pseudoConfig.getRules().add(new PseudoFuncRule(name, "**", pseudoFunc));
pseudoConfig.getRules().add(new PseudoFuncRule(name, pattern, pseudoFunc));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void processWithNullValues() throws JSONException {
return Collections.singletonMap("testField", "processedValue " + originalValue);
});

PseudoField pseudoField = new PseudoField("testField", null, null);
PseudoField pseudoField = new PseudoField("testField", "**", null, null);
List<String> values = Arrays.asList("v1", null, "v2");

String want = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class PseudoFieldTest {

@Test
void UsesDefaultPseudoConfigWhenNoKeysetIsSupplied() {
PseudoField pseudoField = new PseudoField(null, null, null);
PseudoField pseudoField = new PseudoField(null, "**", null, null);
assertEquals(PseudoField.getDEFAULT_PSEUDO_FUNC(), pseudoField.getPseudoConfig().getRules().get(0).getFunc());
}

@Test
void usesCustomPseudoFuncWhenPseudoFuncIsSupplied() {
int keySetPrimaryKey = 12345;

PseudoField pseudoSIDField = new PseudoField(null, String.format("map-sid(keyId=%s)", keySetPrimaryKey), null);
PseudoField pseudoSIDField = new PseudoField(null, "**", String.format("map-sid(keyId=%s)", keySetPrimaryKey), null);

assertEquals(String.format("map-sid(keyId=%s)", keySetPrimaryKey),
pseudoSIDField.getPseudoConfig().getRules().get(0).getFunc());
Expand All @@ -55,7 +55,7 @@ void usesCustomPseudoFuncWhenPseudoFuncIsSupplied() {
@Test
void setCustomKeysetWhenKeysetIsSupplied() {
EncryptedKeysetWrapper encryptedKeysetWrapper = mock(EncryptedKeysetWrapper.class);
PseudoField pseudoField = new PseudoField(null, null, encryptedKeysetWrapper);
PseudoField pseudoField = new PseudoField(null, "**", null, encryptedKeysetWrapper);

assertEquals(encryptedKeysetWrapper,
pseudoField.getPseudoConfig().getKeysets().get(0));
Expand Down Expand Up @@ -97,7 +97,7 @@ void processWithNullValues() throws JSONException {
return Collections.singletonMap("testField", "processedValue " + originalValue);
});

PseudoField pseudoField = new PseudoField("testField", null, null);
PseudoField pseudoField = new PseudoField("testField", "**", null, null);
List<String> values = Arrays.asList("v1", null, "v2");

String want = """
Expand Down Expand Up @@ -153,7 +153,7 @@ void preprocessorWithNullValues() {
when(recordMapProcessor.hasPreprocessors()).thenReturn(true);
when(recordMapProcessor.init(any())).thenReturn(Collections.singletonMap("testField", "initializedValue"));

PseudoField pseudoField = new PseudoField("testField", null, null);
PseudoField pseudoField = new PseudoField("testField", "**", null, null);
List<String> values = Arrays.asList("v1", null, "v2");

Completable result = pseudoField.getPreprocessor(values, recordMapProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ void processWithNullValues() throws JSONException {
return Collections.singletonMap("testField", "processedValue " + originalValue);
});

PseudoField sourcePseudoField = new PseudoField("testField", null, null);
PseudoField targetPseudoField = new PseudoField("testField", null, null);
PseudoField sourcePseudoField = new PseudoField("testField", "**", null, null);
PseudoField targetPseudoField = new PseudoField("testField", "**", null, null);
List<String> values = Arrays.asList("v1", null, "v2");

String want = """
Expand Down

0 comments on commit 76c184f

Please sign in to comment.