Skip to content

Commit 6813a44

Browse files
authored
Add classes for redaction support (#17)
1 parent 723627e commit 6813a44

File tree

14 files changed

+534
-22
lines changed

14 files changed

+534
-22
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
BUILD_DIR=build
33

4-
VERSION=0.1
4+
VERSION=1.0.0
55

6-
NAME=dlp-scan
7-
ARTIFACT=dlp-scan-$(VERSION).jar
8-
SHADED=dlp-scan-$(VERSION)-shaded.jar
6+
NAME=scan-api
7+
ARTIFACT=$(NAME)-$(VERSION).jar
8+
SHADED=$(NAME)-$(VERSION)-shaded.jar
99

1010
all: jar
1111

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ try (NightfallClient c = NightfallClient.Builder.defaultClient()) {
6969

7070
// Define some detectors to use to scan your data
7171
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
72+
creditCard.setMinConfidence(Confidence.LIKELY);
73+
creditCard.setMinNumFindings(1);
7274
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
75+
ssn.setMinConfidence(Confidence.POSSIBLE);
76+
ssn.setMinNumFindings(1);
7377

7478
// A rule contains a set of detectors to scan with
75-
DetectionRule rule = new DetectionRule();
76-
rule.setDetectors(Arrays.asList(creditCard, ssn));
77-
rule.setLogicalOp("ANY");
79+
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
7880

7981
List<String> payload = Arrays.asList("hello world", "my SSN is 678-99-8212", "4242-4242-4242-4242");
8082
ScanTextConfig config = ScanTextConfig.fromDetectionRuleUUIDs(Arrays.asList(rule), 20);
@@ -109,12 +111,14 @@ try (NightfallClient c = NightfallClient.Builder.defaultClient()) {
109111

110112
// Define some detectors to use to scan your data
111113
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
114+
creditCard.setMinConfidence(Confidence.LIKELY);
115+
creditCard.setMinNumFindings(1);
112116
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
117+
ssn.setMinConfidence(Confidence.POSSIBLE);
118+
ssn.setMinNumFindings(1);
113119

114120
// A rule contains a set of detectors to scan with
115-
DetectionRule rule = new DetectionRule();
116-
rule.setDetectors(Arrays.asList(creditCard, ssn));
117-
rule.setLogicalOp("ANY");
121+
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
118122

119123
// File scans are conducted asynchronously, so provide a webhook route to an HTTPS server to send results to.
120124
String webhookResponseListenerURL = "https://my-service.com/nightfall/listener";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ai.nightfall.scan.model;
2+
3+
/**
4+
* Confidence describes the certainty that a piece of content matches a detector.
5+
*/
6+
public enum Confidence {
7+
VERY_UNLIKELY,
8+
UNLIKELY,
9+
POSSIBLE,
10+
LIKELY,
11+
VERY_LIKELY
12+
}

src/main/java/ai/nightfall/scan/model/ConfidenceAdjustment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
*/
1010
public class ConfidenceAdjustment {
1111
@JsonProperty("fixedConfidence")
12-
private String fixedConfidence;
12+
private Confidence fixedConfidence;
1313

1414
/**
1515
* Create a ConfidenceAdjustment object.
1616
*
1717
* @param fixedConfidence the confidence to adjust to if external criteria are met.
1818
*/
19-
public ConfidenceAdjustment(String fixedConfidence) {
19+
public ConfidenceAdjustment(Confidence fixedConfidence) {
2020
this.fixedConfidence = fixedConfidence;
2121
}
2222

@@ -25,7 +25,7 @@ public ConfidenceAdjustment(String fixedConfidence) {
2525
*
2626
* @return the confidence to adjust to
2727
*/
28-
public String getFixedConfidence() {
28+
public Confidence getFixedConfidence() {
2929
return fixedConfidence;
3030
}
3131
}

src/main/java/ai/nightfall/scan/model/DetectionRule.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,35 @@ public class DetectionRule {
1616
private List<Detector> detectors;
1717

1818
@JsonProperty("logicalOp")
19-
private String logicalOp;
19+
private LogicalOp logicalOp;
2020

2121
@JsonProperty("name")
2222
private String name;
2323

24+
/**
25+
* Create a detection rule with the provided detectors and logicalOp.
26+
*
27+
* @param detectors a list of detectors
28+
* @param logicalOp a logical op: ANY or ALL
29+
*/
30+
public DetectionRule(List<Detector> detectors, LogicalOp logicalOp) {
31+
this.detectors = detectors;
32+
this.logicalOp = logicalOp;
33+
}
34+
35+
/**
36+
* Create a detection rule with the provided detectors and logicalOp.
37+
*
38+
* @param detectors a list of detectors
39+
* @param logicalOp a logical op: ANY or ALL
40+
* @param name a name for the detection rule
41+
*/
42+
public DetectionRule(List<Detector> detectors, LogicalOp logicalOp, String name) {
43+
this.detectors = detectors;
44+
this.logicalOp = logicalOp;
45+
this.name = name;
46+
}
47+
2448
/**
2549
* Get the list of detectors.
2650
*
@@ -44,7 +68,7 @@ public void setDetectors(List<Detector> detectors) {
4468
*
4569
* @return the logical op
4670
*/
47-
public String getLogicalOp() {
71+
public LogicalOp getLogicalOp() {
4872
return logicalOp;
4973
}
5074

@@ -53,7 +77,7 @@ public String getLogicalOp() {
5377
*
5478
* @param logicalOp a logical op; valid values <code>ANY</code> or <code>ALL</code>
5579
*/
56-
public void setLogicalOp(String logicalOp) {
80+
public void setLogicalOp(LogicalOp logicalOp) {
5781
this.logicalOp = logicalOp;
5882
}
5983

src/main/java/ai/nightfall/scan/model/Detector.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.nightfall.scan.model;
22

3+
import ai.nightfall.scan.model.redaction.RedactionConfig;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45

56
import java.util.List;
@@ -12,7 +13,7 @@
1213
public class Detector {
1314

1415
@JsonProperty("minConfidence")
15-
private String minConfidence;
16+
private Confidence minConfidence;
1617

1718
@JsonProperty("minNumFindings")
1819
private int minNumFindings;
@@ -41,6 +42,9 @@ public class Detector {
4142
@JsonProperty("exclusionRules")
4243
private List<ExclusionRule> exclusionRules;
4344

45+
@JsonProperty("redactionConfig")
46+
private RedactionConfig redactionConfig;
47+
4448
/**
4549
* Create an instance of a detector based on a pre-built Nightfall detector.
4650
*
@@ -87,17 +91,16 @@ public Detector(UUID detectorUUID) {
8791
*
8892
* @return the minimum confidence threshold required in order for a finding to be triggered
8993
*/
90-
public String getMinConfidence() {
94+
public Confidence getMinConfidence() {
9195
return minConfidence;
9296
}
9397

9498
/**
9599
* Set the minimum confidence.
96100
*
97-
* @param minConfidence the minimum confidence threshold. Valid values: <code>VERY_UNLIKELY</code>,
98-
* <code>UNLIKELY</code>, <code>POSSIBLE</code>, <code>LIKELY</code>, <code>VERY_LIKELY</code>.
101+
* @param minConfidence the minimum confidence threshold.
99102
*/
100-
public void setMinConfidence(String minConfidence) {
103+
public void setMinConfidence(Confidence minConfidence) {
101104
this.minConfidence = minConfidence;
102105
}
103106

@@ -222,6 +225,26 @@ public void setExclusionRules(List<ExclusionRule> exclusionRules) {
222225
this.exclusionRules = exclusionRules;
223226
}
224227

228+
/**
229+
* Returns the redaction configuration to-be-applied to this detector. This configuration is currently only
230+
* supported for scanning plaintext, not for file scanning.
231+
*
232+
* @return the redaction configuration
233+
*/
234+
public RedactionConfig getRedactionConfig() {
235+
return redactionConfig;
236+
}
237+
238+
/**
239+
* Sets the redaction configuration to-be-applied to this detector. This configuration is currently only
240+
* supported for scanning plaintext, not for file scanning.
241+
*
242+
* @param redactionConfig the redaction configuration
243+
*/
244+
public void setRedactionConfig(RedactionConfig redactionConfig) {
245+
this.redactionConfig = redactionConfig;
246+
}
247+
225248
@Override
226249
public String toString() {
227250
return "Detector{"
@@ -235,6 +258,7 @@ public String toString() {
235258
+ ", wordList=" + wordList
236259
+ ", contextRules=" + contextRules
237260
+ ", exclusionRules=" + exclusionRules
261+
+ ", redactionConfig=" + redactionConfig
238262
+ '}';
239263
}
240264
}

src/main/java/ai/nightfall/scan/model/Finding.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class Finding {
1212
@JsonProperty("finding")
1313
private String finding;
1414

15+
@JsonProperty("redactedFinding")
16+
private String redactedFinding;
17+
1518
@JsonProperty("beforeContext")
1619
private String beforeContext;
1720

@@ -27,6 +30,9 @@ public class Finding {
2730
@JsonProperty("location")
2831
private Location location;
2932

33+
@JsonProperty("redactedLocation")
34+
private Location redactedLocation;
35+
3036
@JsonProperty("matchedDetectionRuleUUIDs")
3137
private List<UUID> matchedDetectionRuleUUIDs;
3238

@@ -42,6 +48,16 @@ public String getFinding() {
4248
return finding;
4349
}
4450

51+
/**
52+
* Returns the redacted finding. This will only be non-empty if redaction configuration was provided
53+
* as part of the request payload.
54+
*
55+
* @return the redacted finding
56+
*/
57+
public String getRedactedFinding() {
58+
return redactedFinding;
59+
}
60+
4561
/**
4662
* Get the preceding context.
4763
*
@@ -81,12 +97,22 @@ public String getConfidence() {
8197
/**
8298
* Get the location of the finding.
8399
*
84-
* @return the location where the data was in the original input file
100+
* @return the location where the data was in the original content
85101
*/
86102
public Location getLocation() {
87103
return location;
88104
}
89105

106+
/**
107+
* Get the location that the redacted finding would occupy in the original content if it were to replace
108+
* the finding.
109+
*
110+
* @return the location that the data occupies in its redacted form with regard to the original content
111+
*/
112+
public Location getRedactedLocation() {
113+
return redactedLocation;
114+
}
115+
90116
/**
91117
* Get the list of matched detection rule UUIDs.
92118
*
@@ -109,11 +135,13 @@ public List<String> getMatchedDetectionRules() {
109135
public String toString() {
110136
return "Finding{"
111137
+ "finding='" + finding + '\''
138+
+ ", redactedFinding='" + redactedFinding + '\''
112139
+ ", beforeContext='" + beforeContext + '\''
113140
+ ", afterContext='" + afterContext + '\''
114141
+ ", detector=" + detector
115142
+ ", confidence='" + confidence + '\''
116143
+ ", location=" + location
144+
+ ", redactedLocation=" + redactedLocation
117145
+ ", matchedDetectionRuleUUIDs=" + matchedDetectionRuleUUIDs
118146
+ ", matchedDetectionRules=" + matchedDetectionRules
119147
+ '}';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ai.nightfall.scan.model;
2+
3+
/**
4+
* A modifier that is used to decide when a finding should be surfaced in the context of a detection rule.
5+
*
6+
* <p>When <code>ALL</code> is specified, all detectors in a detection rule must trigger a match in order for the
7+
* finding to be reported. This is the equivalent of a logical "AND" operator.
8+
*
9+
* <p>When <code>ANY</code> is specified, only one of the detectors in a detection rule must trigger a match in order
10+
* for the finding to be reported. This is the equivalent of a logical "OR" operator.
11+
*/
12+
public enum LogicalOp {
13+
ANY,
14+
ALL
15+
}

src/main/java/ai/nightfall/scan/model/ScanTextResponse.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class ScanTextResponse {
1313
@JsonProperty("findings")
1414
private List<List<Finding>> findings;
1515

16+
@JsonProperty("redactedPayload")
17+
private List<String> redactedPayload;
18+
1619
/**
1720
* Get the findings.
1821
*
@@ -21,4 +24,22 @@ public class ScanTextResponse {
2124
public List<List<Finding>> getFindings() {
2225
return findings;
2326
}
27+
28+
/**
29+
* Return the original request content with the configured redactions applied. If redaction was not
30+
* applied for a given input string, the string at a given index in the array will be empty.
31+
*
32+
* @return the original content with the configured redactions applied
33+
*/
34+
public List<String> getRedactedPayload() {
35+
return redactedPayload;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "ScanTextResponse{"
41+
+ "findings=" + findings
42+
+ ", redactedPayload=" + redactedPayload
43+
+ '}';
44+
}
2445
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ai.nightfall.scan.model.redaction;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
/**
6+
* An object that specifies how findings should be encrypted when returned by the API. Currently, encryption
7+
* is only supported for RSA public keys.
8+
*/
9+
public class CryptoConfig {
10+
11+
@JsonProperty("publicKey")
12+
private String publicKey;
13+
14+
/**
15+
* Builds a configuration object with the provided PEM-formatted RSA public key.
16+
*
17+
* @param publicKey a PEM-formatted RSA public key.
18+
*/
19+
public CryptoConfig(String publicKey) {
20+
this.publicKey = publicKey;
21+
}
22+
23+
/**
24+
* Get the public key.
25+
*
26+
* @return the public key
27+
*/
28+
public String getPublicKey() {
29+
return publicKey;
30+
}
31+
32+
/**
33+
* Set the public key.
34+
*
35+
* @param publicKey the public key
36+
*/
37+
public void setPublicKey(String publicKey) {
38+
this.publicKey = publicKey;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "CryptoConfig{"
44+
+ "publicKey='" + publicKey + '\''
45+
+ '}';
46+
}
47+
}

0 commit comments

Comments
 (0)