-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add dependency to tink 1.7.0 * Add Daead encryption function * Add simple redact function * Add Map function
- Loading branch information
Showing
13 changed files
with
434 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
69 changes: 69 additions & 0 deletions
69
src/main/java/no/ssb/dapla/dlp/pseudo/func/daead/DaeadFunc.java
This file contains 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,69 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.daead; | ||
|
||
import com.google.crypto.tink.DeterministicAead; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.dapla.dlp.pseudo.func.*; | ||
import no.ssb.dapla.dlp.pseudo.func.util.FromString; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.GeneralSecurityException; | ||
import java.util.Base64; | ||
|
||
@Slf4j | ||
public class DaeadFunc extends AbstractPseudoFunc { | ||
|
||
// FIXME: Replace this with something real | ||
private static final byte[] DAEAD_STAMP_BYTES = "".getBytes(StandardCharsets.UTF_8); | ||
private final DaeadFuncConfigService configService = new DaeadFuncConfigService(); | ||
private final DaeadFuncConfig config; | ||
|
||
public DaeadFunc(@NonNull PseudoFuncConfig genericConfig) { | ||
super(genericConfig.getFuncDecl()); | ||
this.config = configService.resolve(genericConfig); | ||
} | ||
|
||
private DeterministicAead daead() { | ||
return config.getDaead(); | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput apply(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
input.getValues().forEach(in -> { | ||
String plain = String.valueOf(in); | ||
try { | ||
byte[] ciphertext = daead().encryptDeterministically(plain.getBytes(StandardCharsets.UTF_8), DAEAD_STAMP_BYTES); | ||
output.add(Base64.getEncoder().encodeToString(ciphertext)); | ||
} | ||
catch (GeneralSecurityException e) { | ||
throw new DaeadPseudoFuncException("DAEAD apply error. func=" + getFuncDecl() + ", contentType=" + input.getParamMetadata(), e); | ||
} | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput restore(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
input.getValues().forEach(in -> { | ||
byte[] ciphertext = Base64.getDecoder().decode(String.valueOf(in)); | ||
try { | ||
byte[] plaintext = daead().decryptDeterministically(ciphertext, DAEAD_STAMP_BYTES); | ||
output.add(FromString.convert(new String(plaintext), in.getClass())); | ||
} | ||
catch (GeneralSecurityException e) { | ||
throw new DaeadPseudoFuncException("DAEAD restore error. func=" + getFuncDecl() + ", contentType=" + input.getParamMetadata(), e); | ||
} | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
public static class DaeadPseudoFuncException extends PseudoFuncException { | ||
public DaeadPseudoFuncException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/no/ssb/dapla/dlp/pseudo/func/daead/DaeadFuncConfig.java
This file contains 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,21 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.daead; | ||
|
||
import com.google.crypto.tink.DeterministicAead; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@Value | ||
@Builder | ||
public class DaeadFuncConfig { | ||
private final String dataEncryptionKeyId; | ||
private final String base64EncodedWrappedDataEncryptionKey; | ||
private final DeterministicAead daead; | ||
|
||
@UtilityClass | ||
public static class Param { | ||
public static final String DEK_ID = "dataEncryptionKeyId"; | ||
public static final String WDEK = "wrappedDataEncryptionKey"; | ||
public static final String DAEAD = "deterministicAead"; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/no/ssb/dapla/dlp/pseudo/func/daead/DaeadFuncConfigService.java
This file contains 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 no.ssb.dapla.dlp.pseudo.func.daead; | ||
|
||
import com.google.crypto.tink.DeterministicAead; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
|
||
import static no.ssb.dapla.dlp.pseudo.func.daead.DaeadFuncConfig.Param.DAEAD; | ||
|
||
@Slf4j | ||
public class DaeadFuncConfigService { | ||
|
||
public DaeadFuncConfig resolve(PseudoFuncConfig cfg) { | ||
|
||
return DaeadFuncConfig.builder() | ||
.daead(cfg.getRequired(DAEAD, DeterministicAead.class)) | ||
.build(); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/no/ssb/dapla/dlp/pseudo/func/map/MapFunc.java
This file contains 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,54 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.map; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.dapla.dlp.pseudo.func.AbstractPseudoFunc; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput; | ||
|
||
import java.util.ServiceLoader; | ||
|
||
@Slf4j | ||
public class MapFunc extends AbstractPseudoFunc { | ||
private final MapFuncConfig config; | ||
private final MapFuncConfigService mapFuncConfigService = new MapFuncConfigService(); | ||
private final Mapper mapper; | ||
|
||
public MapFunc(PseudoFuncConfig genericConfig) { | ||
super(genericConfig.getFuncDecl()); | ||
this.config = mapFuncConfigService.resolve(genericConfig); | ||
// TODO: Filter Service Implementation by some annotation (to choose the implementation that is used) | ||
this.mapper = ServiceLoader.load(Mapper.class) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException(getClass().getSimpleName() + " requires a " + Mapper.class.getName() + " implementation to be present on the classpath")); | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput apply(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
|
||
for (Object inputValue : input.getValues()) { | ||
String plain = String.valueOf(inputValue); | ||
final Object pseudonymized = mapper.map(plain); | ||
//output.add(FromString.convert(pseudonymized, inputValue.getClass())); | ||
output.add(pseudonymized); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput restore(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
|
||
for (Object inputValue : input.getValues()) { | ||
String mapped = String.valueOf(inputValue); | ||
final Object clear = mapper.restore(mapped); | ||
//output.add(FromString.convert(clear, inputValue.getClass())); | ||
output.add(clear); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/no/ssb/dapla/dlp/pseudo/func/map/MapFuncConfig.java
This file contains 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,16 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.map; | ||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@Value | ||
@Builder | ||
public class MapFuncConfig { | ||
private final String context; | ||
|
||
@UtilityClass | ||
public static class Param { | ||
public static final String CONTEXT = "context"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/no/ssb/dapla/dlp/pseudo/func/map/MapFuncConfigService.java
This file contains 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,16 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.map; | ||
|
||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
|
||
public class MapFuncConfigService { | ||
|
||
public MapFuncConfig resolve(PseudoFuncConfig genericConfig) { | ||
|
||
String context = genericConfig.getRequired(MapFuncConfig.Param.CONTEXT, String.class); | ||
|
||
return MapFuncConfig.builder() | ||
.context(context) | ||
.build(); | ||
} | ||
|
||
} |
This file contains 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,9 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.map; | ||
|
||
public interface Mapper { | ||
|
||
Object map(Object data); | ||
|
||
Object restore(Object mapped); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/no/ssb/dapla/dlp/pseudo/func/redact/RedactFunc.java
This file contains 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,44 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.redact; | ||
|
||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.dapla.dlp.pseudo.func.AbstractPseudoFunc; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput; | ||
|
||
@Slf4j | ||
public class RedactFunc extends AbstractPseudoFunc { | ||
private final RedactFuncConfigService configService = new RedactFuncConfigService(); | ||
private final RedactFuncConfig config; | ||
|
||
public RedactFunc(@NonNull PseudoFuncConfig genericConfig) { | ||
super(genericConfig.getFuncDecl()); | ||
this.config = configService.resolve(genericConfig); | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput apply(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
input.getValues().forEach(in -> { | ||
String plain = String.valueOf(in); | ||
if (config.getRegex() != null) { | ||
output.add(plain.replaceAll(config.getRegex(), config.getPlaceholder())); | ||
} | ||
else { | ||
output.add(config.getPlaceholder()); | ||
} | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput restore(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
input.getValues().forEach(in -> output.add(in)); | ||
|
||
return output; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/no/ssb/dapla/dlp/pseudo/func/redact/RedactFuncConfig.java
This file contains 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,18 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.redact; | ||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@Value | ||
@Builder | ||
public class RedactFuncConfig { | ||
private final String placeholder; | ||
private final String regex; | ||
|
||
@UtilityClass | ||
public static class Param { | ||
public static final String PLACEHOLDER = "placeholder"; | ||
public static final String REGEX = "regex"; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/no/ssb/dapla/dlp/pseudo/func/redact/RedactFuncConfigService.java
This file contains 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 no.ssb.dapla.dlp.pseudo.func.redact; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
|
||
import static no.ssb.dapla.dlp.pseudo.func.redact.RedactFuncConfig.Param.PLACEHOLDER; | ||
import static no.ssb.dapla.dlp.pseudo.func.redact.RedactFuncConfig.Param.REGEX; | ||
|
||
@Slf4j | ||
public class RedactFuncConfigService { | ||
|
||
public RedactFuncConfig resolve(PseudoFuncConfig cfg) { | ||
return RedactFuncConfig.builder() | ||
.placeholder(cfg.get(PLACEHOLDER, String.class).orElse("***")) | ||
.regex(cfg.get(REGEX, String.class).orElse(null)) | ||
.build(); | ||
} | ||
|
||
} |
Oops, something went wrong.