From 59727edb93372738b4c7a5278862b3047d727efd Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Fri, 13 Dec 2024 12:12:12 -0500 Subject: [PATCH] Working on adding redaction hub client. --- .../api/controllers/ExplainApiController.java | 2 +- .../api/controllers/FilterApiController.java | 67 +++---------------- .../philter/PhilterConfiguration.java | 26 +++++++ philter-services/pom.xml | 5 ++ .../philter/services/PhilterService.java | 52 ++++++++++++-- .../services/SignedFilterResponse.java | 25 +++++++ pom.xml | 3 +- 7 files changed, 118 insertions(+), 62 deletions(-) create mode 100644 philter-services/src/main/java/ai/philterd/philter/services/SignedFilterResponse.java diff --git a/philter-api/src/main/java/ai/philterd/philter/api/controllers/ExplainApiController.java b/philter-api/src/main/java/ai/philterd/philter/api/controllers/ExplainApiController.java index b17c581..b0cfdb1 100644 --- a/philter-api/src/main/java/ai/philterd/philter/api/controllers/ExplainApiController.java +++ b/philter-api/src/main/java/ai/philterd/philter/api/controllers/ExplainApiController.java @@ -37,7 +37,7 @@ public ExplainApiController(FilterService filterService, Gson gson) { final FilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.TEXT_PLAIN); return ResponseEntity.status(HttpStatus.OK) - .header("x-document-id", response.documentId()) + .header("x-document-id", response.getDocumentId()) .contentType(MediaType.APPLICATION_JSON) .body(gson.toJson(response)); diff --git a/philter-api/src/main/java/ai/philterd/philter/api/controllers/FilterApiController.java b/philter-api/src/main/java/ai/philterd/philter/api/controllers/FilterApiController.java index 48b3e42..3fee6ff 100644 --- a/philter-api/src/main/java/ai/philterd/philter/api/controllers/FilterApiController.java +++ b/philter-api/src/main/java/ai/philterd/philter/api/controllers/FilterApiController.java @@ -1,10 +1,9 @@ package ai.philterd.philter.api.controllers; -import java.util.Arrays; -import java.util.List; -import java.util.logging.Filter; - -import ai.philterd.philter.services.PhilterService; +import ai.philterd.phileas.model.enums.MimeType; +import ai.philterd.phileas.model.responses.BinaryDocumentFilterResponse; +import ai.philterd.phileas.model.responses.FilterResponse; +import ai.philterd.phileas.model.services.FilterService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -18,10 +17,8 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import ai.philterd.phileas.model.enums.MimeType; -import ai.philterd.phileas.model.responses.BinaryDocumentFilterResponse; -import ai.philterd.phileas.model.responses.FilterResponse; -import ai.philterd.phileas.model.services.FilterService; +import java.util.Collections; +import java.util.List; @Controller public class FilterApiController extends AbstractController { @@ -45,7 +42,7 @@ public FilterApiController(FilterService filterService) { LOGGER.info("Received uploaded binary PDF file to be returned as ZIP."); - final List policies = Arrays.asList(policyName); + final List policies = Collections.singletonList(policyName); final BinaryDocumentFilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.APPLICATION_PDF, MimeType.IMAGE_JPEG); return ResponseEntity.status(HttpStatus.OK) @@ -63,7 +60,7 @@ public FilterApiController(FilterService filterService) { LOGGER.info("Received uploaded binary PDF file to be returned as PDF."); - final List policies = Arrays.asList(policyName); + final List policies = Collections.singletonList(policyName); final BinaryDocumentFilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.APPLICATION_PDF, MimeType.APPLICATION_PDF); return ResponseEntity.status(HttpStatus.OK) @@ -79,55 +76,13 @@ public FilterApiController(FilterService filterService) { @RequestParam(value="p", defaultValue="default") String policyName, @RequestBody String body) throws Exception { - final List policies = Arrays.asList(policyName); + final List policies = Collections.singletonList(policyName); final FilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.TEXT_PLAIN); - return ResponseEntity.status(HttpStatus.OK) - .header("x-document-id", response.documentId()) - .body(response.filteredText()); - - } - - /*@RequestMapping(value="/api/filter", method=RequestMethod.POST, produces = MediaType.IMAGE_JPEG_VALUE, consumes = MediaType.IMAGE_JPEG_VALUE) - public @ResponseBody ResponseEntity filterImageFacialRecognition( - @RequestParam(value="c", defaultValue="none") String context, - @RequestParam(value="d", defaultValue="") String documentId, - @RequestParam(value="p", defaultValue="default") String policyName, - @RequestBody byte[] body) throws Exception { - - final BinaryDocumentFilterResponse response = filterService.filter(policyName, context, documentId, body, MimeType.IMAGE_JPEG, MimeType.IMAGE_JPEG); - return ResponseEntity.status(HttpStatus.OK) .header("x-document-id", response.getDocumentId()) - .body(response.getDocument()); - - }*/ - - /*@RequestMapping(value="/api/filter", method=RequestMethod.POST, produces = "text/csv", consumes = "text/csv") - public @ResponseBody ResponseEntity filterTextCsvAsTextCsv( - @RequestParam(value="c", defaultValue="none") String context, - @RequestParam(value="d", defaultValue="") String documentId, - @RequestParam(value="p", defaultValue="default") String policyName, - @RequestBody String body) throws Exception { + .body(response.getFilteredText()); - if(!statusUp) { - statusUp = getPythonRESTServiceStatus(phileasConfiguration.philterNerEndpoint()); - } - - if(statusUp) { - - final FilterResponse response = filterService.filter(policyName, context, documentId, body, MimeType.CSV); - - return ResponseEntity.status(HttpStatus.OK) - .header("x-document-id", response.getDocumentId()) - .body(response.getFilteredText()); - - } else { - - throw new ServiceUnavailableException("Philter is still initializing. If message persists refer to Philter's log file."); - - } - - }*/ + } } 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 a14e41e..5de8ca9 100644 --- a/philter-configuration/src/main/java/ai/philterd/philter/PhilterConfiguration.java +++ b/philter-configuration/src/main/java/ai/philterd/philter/PhilterConfiguration.java @@ -22,6 +22,32 @@ public PhilterConfiguration(Properties properties, String applicationName) throw this.applicationName = applicationName; } + // Redaction Hub + + public boolean redactionHubEnabled() { + return Boolean.parseBoolean(getProperty("redaction.hub.enabled", "false")); + } + + public String redactionHubApiKey() { + return getProperty("redaction.hub.api.key", ""); + } + + public String redactionHubBaseUrl() { + return getProperty("redaction.hub.base.url", ""); + } + + public int redactionHubTimeOut() { + return Integer.parseInt(getProperty("redaction.hub.timeout", "3000")); + } + + public boolean redactionHubIgnoreSsl() { + return Boolean.parseBoolean(getProperty("redaction.hub.ignore.ssl", "false")); + } + + public String redactionHubCertificateName() { + return getProperty("redaction.hub.certificate.name", ""); + } + // Metrics public String metricsPrefix() { diff --git a/philter-services/pom.xml b/philter-services/pom.xml index 9f790b2..ef5ee41 100644 --- a/philter-services/pom.xml +++ b/philter-services/pom.xml @@ -35,6 +35,11 @@ philter-metrics ${project.version} + + ai.philterd + redaction-hub-client + ${redaction.hub.version} + org.springframework spring-web 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 d4be8cb..557ffc6 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 @@ -11,32 +11,76 @@ import ai.philterd.phileas.model.services.PolicyService; import ai.philterd.phileas.services.PhileasFilterService; import ai.philterd.philter.PhilterConfiguration; +import ai.philterd.redactionhub.client.RedactionHubClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.io.IOException; import java.util.List; @Component public class PhilterService implements FilterService { private final FilterService phileasFilterService; + private final RedactionHubClient redactionHubClient; + private final String certificateName; @Autowired - public PhilterService(PhileasConfiguration phileasConfiguration) throws IOException { + public PhilterService(PhileasConfiguration phileasConfiguration) throws Exception { + final PhilterConfiguration philterConfiguration = new PhilterConfiguration("philter.properties", "Philter"); final PhilterMetricsService philterMetricsService = new PhilterMetricsService(philterConfiguration); this.phileasFilterService = new PhileasFilterService(phileasConfiguration, philterMetricsService); + + if(philterConfiguration.redactionHubEnabled()) { + this.redactionHubClient = new RedactionHubClient(philterConfiguration.redactionHubApiKey(), philterConfiguration.redactionHubBaseUrl(), philterConfiguration.redactionHubTimeOut(), philterConfiguration.redactionHubIgnoreSsl()); + this.certificateName = philterConfiguration.redactionHubCertificateName(); + } else { + this.redactionHubClient = null; + this.certificateName = null; + } + } @Override public FilterResponse filter(Policy policy, String context, String documentId, String input, MimeType mimeType) throws Exception { - return phileasFilterService.filter(policy, context, documentId, input, mimeType); + + final FilterResponse filterResponse = phileasFilterService.filter(policy, context, documentId, input, mimeType); + + if(redactionHubClient != null) { + + final String signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName); + + return new SignedFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(), + filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes(), + signature); + + } else { + + return filterResponse; + + } + } @Override public FilterResponse filter(List policyNames, String context, String documentId, String input, MimeType mimeType) throws Exception { - return phileasFilterService.filter(policyNames, context, documentId, input, mimeType); + + final FilterResponse filterResponse = phileasFilterService.filter(policyNames, context, documentId, input, mimeType); + + if(redactionHubClient != null) { + + final String signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName); + + return new SignedFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(), + filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes(), + signature); + + } else { + + return filterResponse; + + } + } @Override 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 new file mode 100644 index 0000000..3eb5b21 --- /dev/null +++ b/philter-services/src/main/java/ai/philterd/philter/services/SignedFilterResponse.java @@ -0,0 +1,25 @@ +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; + } + +} diff --git a/pom.xml b/pom.xml index 80220fc..76e5fbe 100644 --- a/pom.xml +++ b/pom.xml @@ -93,8 +93,9 @@ 4.13.2 1.13.3 1.10.19 - 2.9.1 + 2.10.0-SNAPSHOT 1.4.0 + 1.0.0-SNAPSHOT 3.3.1 6.1.10