|
1 | | -# nightfall-java-sdk |
| 1 | +# Nightfall Java SDK |
| 2 | + |
| 3 | +**Embed Nightfall scanning and detection functionality into Java applications** |
| 4 | + |
| 5 | +<!-- TODO add badges [](https://travis-ci.org/joemccann/dillinger) |
| 6 | +--> |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +This SDK provides Java bindings for the Nightfall API. It allows you to add functionality to your applications to |
| 11 | +scan plain text and files in order to detect different categories of information. You can leverage any of |
| 12 | +the detectors in Nightfall's pre-built library, or you may programmatically define your own custom detectors. |
| 13 | + |
| 14 | +Additionally, this library provides convenience features such as encapsulating the steps to chunk and upload files. |
| 15 | + |
| 16 | +To obtain an API Key, login to the [Nightfall dashboard](https://app.nightfall.ai/) and click the section |
| 17 | +titled "Manage API Keys". |
| 18 | + |
| 19 | +See our [developer documentation](https://docs.nightfall.ai/docs/entities-and-terms-to-know) for more details about |
| 20 | +integrating with the Nightfall API. |
| 21 | + |
| 22 | +## Dependencies |
| 23 | + |
| 24 | +The Nightfall Java SDK requires Java 8 or later. |
| 25 | + |
| 26 | +For a full list of external dependencies please consult `pom.xml`. |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +### Maven |
| 31 | +Add the following to your project's `pom.xml`: |
| 32 | + |
| 33 | +``` xml |
| 34 | +<dependency> |
| 35 | + <groupId>ai.nightfall</groupId> |
| 36 | + <artifactId>scan-api</artifactId> |
| 37 | + <version>1.0.0</version> |
| 38 | +</dependency> |
| 39 | +``` |
| 40 | + |
| 41 | +### Gradle |
| 42 | +Add the following to your project's `dependencies`: |
| 43 | + |
| 44 | +``` |
| 45 | +implementation group: 'ai.nightfall.api', name: 'scan', version: '1.0' |
| 46 | +``` |
| 47 | + |
| 48 | +### Building Locally |
| 49 | + |
| 50 | +Alternatively, if you would like to build the project yourself: |
| 51 | +1. Clone this git repository |
| 52 | +2. Run `mvn package` from the top-level directory. |
| 53 | +3. The `build` directory should contain two artifacts: `api-$VERSION.jar` and `api-$VERSION-shaded.jar`. The former |
| 54 | +contains *only* the compiled source files of this project, whereas the latter includes the compiled dependencies. |
| 55 | +4. Take whichever jar you prefer from the `build/` directory and add it to your project's classpath. |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +### Scanning Plain Text |
| 60 | + |
| 61 | +Nightfall provides pre-built detector types, covering data types ranging from PII to PHI to credentials. The following |
| 62 | +snippet shows an example of how to scan using pre-built detectors. |
| 63 | + |
| 64 | +#### Sample Code |
| 65 | + |
| 66 | +```java |
| 67 | +// By default, the client reads the API key from the environment variable NIGHTFALL_API_KEY |
| 68 | +try (NightfallClient c = NightfallClient.Builder.defaultClient()) { |
| 69 | + |
| 70 | + // Define some detectors to use to scan your data |
| 71 | + Detector creditCard = new Detector("CREDIT_CARD_NUMBER"); |
| 72 | + Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER"); |
| 73 | + |
| 74 | + // 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"); |
| 78 | + |
| 79 | + List<String> payload = Arrays.asList("hello world", "my SSN is 678-99-8212", "4242-4242-4242-4242"); |
| 80 | + ScanTextConfig config = ScanTextConfig.fromDetectionRuleUUIDs(Arrays.asList(rule), 20); |
| 81 | + ScanTextRequest req = new ScanTextRequest(payload, config); |
| 82 | + |
| 83 | + ScanTextResponse response = c.scan(req); |
| 84 | + System.out.println("findings: " + response.getFindings()); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Scanning Files |
| 89 | + |
| 90 | +Scanning common file types like PDF's or office documents typically requires cumbersome text |
| 91 | +extraction methods like OCR. |
| 92 | + |
| 93 | +Rather than implementing this functionality yourself, the Nightfall API allows you to upload the |
| 94 | +original files, and then we'll handle the heavy lifting. |
| 95 | + |
| 96 | +The file upload process is implemented as a series of requests to upload the file in chunks. The library |
| 97 | +provides a single method that wraps the steps required to upload your file. Please refer to the |
| 98 | +[API Reference](https://docs.nightfall.ai/reference) for more details. |
| 99 | + |
| 100 | +The file is uploaded synchronously, but as files can be arbitrarily large, the scan itself is conducted asynchronously. |
| 101 | +The results from the scan are delivered by webhook; for more information about setting up a webhook server, refer to |
| 102 | +[the docs](https://docs.nightfall.ai/docs/creating-a-webhook-server). |
| 103 | + |
| 104 | +#### Sample Code |
| 105 | + |
| 106 | +```java |
| 107 | +// By default, the client reads the API key from the environment variable NIGHTFALL_API_KEY |
| 108 | +try (NightfallClient c = NightfallClient.Builder.defaultClient()) { |
| 109 | + |
| 110 | + // Define some detectors to use to scan your data |
| 111 | + Detector creditCard = new Detector("CREDIT_CARD_NUMBER"); |
| 112 | + Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER"); |
| 113 | + |
| 114 | + // 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"); |
| 118 | + |
| 119 | + // File scans are conducted asynchronously, so provide a webhook route to an HTTPS server to send results to. |
| 120 | + String webhookResponseListenerURL = "https://my-service.com/nightfall/listener"; |
| 121 | + ScanPolicy policy = ScanPolicy.fromDetectionRules(Arrays.asList(rule), webhookResponseListenerURL); |
| 122 | + ScanFileRequest req = new ScanFileRequest(policy, "my request metadata"); |
| 123 | + |
| 124 | + // Upload the data to the API, then trigger the async scan |
| 125 | + File file = new File("./super-secret-credit-cards.pdf"); |
| 126 | + try (InputStream stream = new FileInputStream(file)) { |
| 127 | + ScanFileResponse response = c.scanFile(req, stream, file.length()); |
| 128 | + System.out.println("started scan: " + response.toString()); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +## Contributing |
| 135 | + |
| 136 | +Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature |
| 137 | +or change. Please adhere to the linting criteria defined in `checkstyle.xml`, and be sure to add unit |
| 138 | +tests for any new functionality you add. |
| 139 | + |
| 140 | +Refer to `CONTRIBUTING.md` for the full details. |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +This code is licensed under the terms of the MIT License. See [here](https://opensource.org/licenses/MIT) |
| 145 | +for more information. |
| 146 | + |
| 147 | +Java is licensed by Oracle. See [here](https://www.oracle.com/java/technologies/javase/jdk-faqs.html) |
| 148 | +for more information. |
0 commit comments