From 97377cad58897b7327152895e4fdbc166920f238 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Fri, 13 Dec 2024 13:11:51 -0500 Subject: [PATCH] Working on redaction hub client. --- .../philter/PhilterConfiguration.java | 8 +++ .../philter/services/PhilterService.java | 68 +++++++++++++------ .../services/RedactionHubFilterResponse.java | 27 ++++++++ .../services/SignedFilterResponse.java | 25 ------- 4 files changed, 83 insertions(+), 45 deletions(-) create mode 100644 philter-services/src/main/java/ai/philterd/philter/services/RedactionHubFilterResponse.java delete mode 100644 philter-services/src/main/java/ai/philterd/philter/services/SignedFilterResponse.java diff --git a/philter-configuration/src/main/java/ai/philterd/philter/PhilterConfiguration.java b/philter-configuration/src/main/java/ai/philterd/philter/PhilterConfiguration.java index 2f5a084..3ddf66c 100644 --- a/philter-configuration/src/main/java/ai/philterd/philter/PhilterConfiguration.java +++ b/philter-configuration/src/main/java/ai/philterd/philter/PhilterConfiguration.java @@ -59,6 +59,14 @@ public boolean redactionHubIgnoreSsl() { return Boolean.parseBoolean(getProperty("redaction.hub.ignore.ssl", "false")); } + public boolean redactionHubSigningEnabled() { + return Boolean.parseBoolean(getProperty("redaction.hub.signing.enabled", "false")); + } + + public boolean redactionHubIndexingEnabled() { + return Boolean.parseBoolean(getProperty("redaction.hub.indexing.enabled", "false")); + } + public String redactionHubCertificateName() { return getProperty("redaction.hub.certificate.name", ""); } diff --git a/philter-services/src/main/java/ai/philterd/philter/services/PhilterService.java b/philter-services/src/main/java/ai/philterd/philter/services/PhilterService.java index ea79d92..f5dfa11 100644 --- a/philter-services/src/main/java/ai/philterd/philter/services/PhilterService.java +++ b/philter-services/src/main/java/ai/philterd/philter/services/PhilterService.java @@ -18,6 +18,7 @@ import ai.philterd.phileas.metrics.PhilterMetricsService; import ai.philterd.phileas.model.configuration.PhileasConfiguration; import ai.philterd.phileas.model.enums.MimeType; +import ai.philterd.phileas.model.objects.Span; import ai.philterd.phileas.model.policy.Policy; import ai.philterd.phileas.model.responses.BinaryDocumentFilterResponse; import ai.philterd.phileas.model.responses.FilterResponse; @@ -27,20 +28,25 @@ import ai.philterd.phileas.services.PhileasFilterService; import ai.philterd.philter.PhilterConfiguration; import ai.philterd.redactionhub.client.RedactionHubClient; +import ai.philterd.redactionhub.model.RedactedObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.List; @Component public class PhilterService implements FilterService { private final FilterService phileasFilterService; + private final RedactionHubClient redactionHubClient; private final String certificateName; + private final boolean signingEnabled; + private final boolean indexingEnabled; @Autowired - public PhilterService(PhileasConfiguration phileasConfiguration) throws Exception { + public PhilterService(final PhileasConfiguration phileasConfiguration) throws Exception { final PhilterConfiguration philterConfiguration = new PhilterConfiguration("philter.properties", "Philter"); final PhilterMetricsService philterMetricsService = new PhilterMetricsService(philterConfiguration); @@ -49,46 +55,68 @@ public PhilterService(PhileasConfiguration phileasConfiguration) throws Exceptio if(philterConfiguration.redactionHubEnabled()) { this.redactionHubClient = new RedactionHubClient(philterConfiguration.redactionHubApiKey(), philterConfiguration.redactionHubBaseUrl(), philterConfiguration.redactionHubTimeOut(), philterConfiguration.redactionHubIgnoreSsl()); this.certificateName = philterConfiguration.redactionHubCertificateName(); + this.signingEnabled = philterConfiguration.redactionHubSigningEnabled(); + this.indexingEnabled = philterConfiguration.redactionHubIndexingEnabled(); } else { this.redactionHubClient = null; this.certificateName = null; + this.signingEnabled = false; + this.indexingEnabled = false; } } @Override - public FilterResponse filter(Policy policy, String context, String documentId, String input, MimeType mimeType) throws Exception { - - final FilterResponse filterResponse = phileasFilterService.filter(policy, context, documentId, input, mimeType); + public FilterResponse filter(final Policy policy, final String context, final String documentId, final String input, final MimeType mimeType) throws Exception { + return filter(List.of(policy.getName()), context, documentId, input, mimeType); + } - if(redactionHubClient != null) { + @Override + public FilterResponse filter(final List policyNames, final String context, final String documentId, final String input, final MimeType mimeType) throws Exception { - final String signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName); + final FilterResponse filterResponse = phileasFilterService.filter(policyNames, context, documentId, input, mimeType); - return new SignedFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(), - filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes(), - signature); + String indexedId = null; + String signature = null; - } else { + if(signingEnabled) { - return filterResponse; + signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName); } - } + if(indexingEnabled) { - @Override - public FilterResponse filter(List policyNames, String context, String documentId, String input, MimeType mimeType) throws Exception { + final List spans = new ArrayList<>(); - final FilterResponse filterResponse = phileasFilterService.filter(policyNames, context, documentId, input, mimeType); + // Convert the Phileas spans to Redaction Hub spans. + for(final Span span : filterResponse.getExplanation().appliedSpans()) { + + final ai.philterd.redactionhub.model.Span spanObject = new ai.philterd.redactionhub.model.Span(span.getCharacterStart(), + span.getCharacterEnd(), span.getFilterType().toString(), span.getReplacement(), span.getConfidence(), + span.getText(), span.getReplacement(), span.getSalt()); - if(redactionHubClient != null) { + spans.add(spanObject); + } + + final RedactedObject redactedObject = new RedactedObject(); + redactedObject.setText(filterResponse.getFilteredText()); + redactedObject.setRedacted(filterResponse.getFilteredText()); + redactedObject.setSpans(spans); + redactedObject.setContext(filterResponse.getContext()); + redactedObject.setDocumentId(filterResponse.getDocumentId()); + redactedObject.setCertificate(certificateName); + redactedObject.setSignature(signature); + + indexedId = redactionHubClient.index(redactedObject); + + } - final String signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName); + if(signingEnabled || indexingEnabled) { - return new SignedFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(), + return new RedactionHubFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(), filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes(), - signature); + signature, indexedId); } else { @@ -99,7 +127,7 @@ public FilterResponse filter(List policyNames, String context, String do } @Override - public BinaryDocumentFilterResponse filter(List policyNames, String context, String documentId, byte[] input, MimeType mimeType, MimeType outputMimeType) throws Exception { + public BinaryDocumentFilterResponse filter(final List policyNames, final String context, final String documentId, final byte[] input, final MimeType mimeType, final MimeType outputMimeType) throws Exception { return phileasFilterService.filter(policyNames, context, documentId, input, mimeType, outputMimeType); } diff --git a/philter-services/src/main/java/ai/philterd/philter/services/RedactionHubFilterResponse.java b/philter-services/src/main/java/ai/philterd/philter/services/RedactionHubFilterResponse.java new file mode 100644 index 0000000..111279d --- /dev/null +++ b/philter-services/src/main/java/ai/philterd/philter/services/RedactionHubFilterResponse.java @@ -0,0 +1,27 @@ +package ai.philterd.philter.services; + +import ai.philterd.phileas.model.objects.Explanation; +import ai.philterd.phileas.model.responses.FilterResponse; + +import java.util.Map; + +public class RedactionHubFilterResponse extends FilterResponse { + + private final String signature; + private final String indexedId; + + public RedactionHubFilterResponse(String filteredText, String context, String documentId, int piece, Explanation explanation, Map attributes, String signature, String indexedId) { + super(filteredText, context, documentId, piece, explanation, attributes); + this.signature = signature; + this.indexedId = indexedId; + } + + public String getSignature() { + return signature; + } + + public String getIndexedId() { + return indexedId; + } + +} diff --git a/philter-services/src/main/java/ai/philterd/philter/services/SignedFilterResponse.java b/philter-services/src/main/java/ai/philterd/philter/services/SignedFilterResponse.java deleted file mode 100644 index 3eb5b21..0000000 --- a/philter-services/src/main/java/ai/philterd/philter/services/SignedFilterResponse.java +++ /dev/null @@ -1,25 +0,0 @@ -package ai.philterd.philter.services; - -import ai.philterd.phileas.model.objects.Explanation; -import ai.philterd.phileas.model.responses.FilterResponse; - -import java.util.Map; - -public class SignedFilterResponse extends FilterResponse { - - private String signature; - - public SignedFilterResponse(String filteredText, String context, String documentId, int piece, Explanation explanation, Map attributes, String signature) { - super(filteredText, context, documentId, piece, explanation, attributes); - this.signature = signature; - } - - public String getSignature() { - return signature; - } - - public void setSignature(String signature) { - this.signature = signature; - } - -}