Skip to content

Commit

Permalink
Validate the pattern that is sendt to the pseudonymizeField endpoint. (
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornandre authored May 2, 2024
1 parent 76c184f commit 2b9fbb6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions doc/requests/examples-field.http
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Authorization: Bearer {{keycloak_token}}
{
"request": {
"name": "fnr",
"pattern": "/fnr",
"values": [
"11854898347",
"01839899544",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public class PseudoController {
public HttpResponse<Flowable<byte[]>> pseudonymizeField(
@Schema(implementation = PseudoFieldRequest.class) String request
) {
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.getPattern(), req.getPseudoFunc(), req.getKeyset());
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.getPattern(), req.getPseudoFunc(), req.getKeyset());

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

Expand All @@ -106,10 +106,10 @@ public HttpResponse<Flowable<byte[]>> pseudonymizeField(
public HttpResponse<Flowable<byte[]>> depseudonymizeField(
@Schema(implementation = DepseudoFieldRequest.class) String request
) {
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.getPattern(), req.getPseudoFunc(), req.getKeyset());
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.getPattern(), req.getPseudoFunc(), req.getKeyset());

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

Expand All @@ -136,11 +136,11 @@ public HttpResponse<Flowable<byte[]>> depseudonymizeField(
public HttpResponse<Flowable<byte[]>> repseudonymizeField(
@Schema(implementation = RepseudoFieldRequest.class) String request
) {
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.getPattern(), req.getSourcePseudoFunc(), req.getSourceKeyset());
PseudoField targetPseudoField = new PseudoField(req.getName(), req.getPattern(), req.getTargetPseudoFunc(), req.getTargetKeyset());
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.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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import no.ssb.dlp.pseudo.core.PseudoOperation;
import no.ssb.dlp.pseudo.core.field.FieldDescriptor;
import no.ssb.dlp.pseudo.core.func.PseudoFuncRule;
import no.ssb.dlp.pseudo.core.map.RecordMapProcessor;
import no.ssb.dlp.pseudo.core.tink.model.EncryptedKeysetWrapper;
Expand Down Expand Up @@ -58,6 +59,11 @@ public PseudoField(String name, String pattern, String pseudoFunc, EncryptedKeys
if (keyset != null) {
pseudoConfig.getKeysets().add(keyset);
}
final boolean validPattern = new FieldDescriptor(name).globMatches(pattern);
if (!validPattern) {
throw new IllegalArgumentException(String.format("The pattern '%s' will not match the field name '%s'. " +
"Are you sure you didn't mean to use '/%s'?", pattern, name, pattern));
}
pseudoConfig.getRules().add(new PseudoFuncRule(name, pattern, pseudoFunc));
}

Expand Down

0 comments on commit 2b9fbb6

Please sign in to comment.