Skip to content

Commit

Permalink
Working on redaction hub client.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Dec 13, 2024
1 parent 5f7f645 commit 97377ca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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<String> 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<String> policyNames, String context, String documentId, String input, MimeType mimeType) throws Exception {
final List<ai.philterd.redactionhub.model.Span> 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 {

Expand All @@ -99,7 +127,7 @@ public FilterResponse filter(List<String> policyNames, String context, String do
}

@Override
public BinaryDocumentFilterResponse filter(List<String> policyNames, String context, String documentId, byte[] input, MimeType mimeType, MimeType outputMimeType) throws Exception {
public BinaryDocumentFilterResponse filter(final List<String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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;
}

}

This file was deleted.

0 comments on commit 97377ca

Please sign in to comment.