Skip to content

Commit cb5a9ee

Browse files
authored
Fix issues with DLP readme and samples used in it (GoogleCloudPlatform#3008)
Fixes some problems with the DLP readme and the examples referenced from it. * Fix `pom.xml` so we produce the expected jar with dependencies. * Fix package names in CLI examples to match what actually exists. * Add notes to README reminding users they need to replace the projectId vars in the samples before running * Make InfoTypesList example runnable * Fix RedactImageFile example to use an existing image file by default, update the readme to match the sample code that exists. Internal bug: b/156125412 - [X] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [X] `pom.xml` parent set to latest `shared-configuration` - [X] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [X] Tests pass (`mvn -P lint clean verify`) * (Note- `Checkstyle` passing is required; `Spotbugs`, `ErrorProne`, `PMD`, etc. `ERROR`'s are advisory only) - [X] Please **merge** this PR for me once it is approved.
1 parent 9b2ead7 commit cb5a9ee

File tree

5 files changed

+52
-26
lines changed

5 files changed

+52
-26
lines changed

dlp/README.md

+14-17
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ An [InfoType identifier](https://cloud.google.com/dlp/docs/infotypes-categories)
2727

2828
[InfoTypes](https://cloud.google.com/dlp/docs/infotypes-reference#global) are updated periodically. Use the API to retrieve the most current InfoTypes.
2929
```
30-
java -cp dlp/target/dlp-samples-1.0-jar-with-dependencies.jar com.example.dlp.Metadata
30+
java -cp target/dlp-samples-1.0-jar-with-dependencies.jar dlp.snippets.InfoTypesList
3131
```
3232

3333
## Run the quickstart
3434

3535
The Quickstart demonstrates using the DLP API to identify an InfoType in a given string.
36+
37+
Note that you will need to set the `projectId` in
38+
[QuickStart.java](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/dlp/src/main/java/dlp/snippets/QuickStart.java)
39+
for this to work correctly. If you forget this, you will see a `PERMISSION_DENIED` error.
40+
3641
```
37-
java -cp dlp/target/dlp-samples-1.0-jar-with-dependencies.jar dlp.snippets.QuickStart
42+
java -cp target/dlp-samples-1.0-jar-with-dependencies.jar dlp.snippets.QuickStart
3843
```
3944

4045
## Inspect data for sensitive elements
@@ -47,23 +52,15 @@ Optional flags are explained in [this resource](https://cloud.google.com/dlp/doc
4752
## Automatic redaction of sensitive data from images
4853
[Automatic redaction](https://cloud.google.com/dlp/docs/redacting-sensitive-data-images) produces an output image with sensitive data matches removed.
4954

50-
```
51-
Commands:
52-
-f <string> Source image file
53-
-o <string> Destination image file
54-
Options:
55-
--help Show help
56-
-minLikelihood choices: "LIKELIHOOD_UNSPECIFIED", "VERY_UNLIKELY", "UNLIKELY", "POSSIBLE", "LIKELY", "VERY_LIKELY"]
57-
[default: "LIKELIHOOD_UNSPECIFIED"]
58-
specifies the minimum reporting likelihood threshold.
59-
60-
-infoTypes set of infoTypes to search for [eg. PHONE_NUMBER US_PASSPORT]
61-
```
62-
6355
### Example
64-
- Redact phone numbers and email addresses from `test.png`:
56+
- Redact phone numbers and email addresses from `src/test/resources/test.png`:
57+
58+
Note that you will need to set the `projectId` in
59+
[RedactImageFile.java](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/dlp/src/main/java/dlp/snippets/RedactImageFilet.java)
60+
for this to work correctly. If you forget this, you will see a `PERMISSION_DENIED` error.
61+
6562
```
66-
java -cp dlp/target/dlp-samples-1.0-jar-with-dependencies.jar com.example.dlp.Redact -f src/test/resources/test.png -o test-redacted.png -infoTypes PHONE_NUMBER EMAIL_ADDRESS
63+
java -cp target/dlp-samples-1.0-jar-with-dependencies.jar dlp.snippets.RedactImageFile
6764
```
6865

6966
## Integration tests

dlp/pom.xml

+23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@
3838
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3939
</properties>
4040

41+
<build>
42+
<plugins>
43+
<plugin>
44+
<artifactId>maven-assembly-plugin</artifactId>
45+
<version>3.0.0</version>
46+
<configuration>
47+
<descriptorRefs>
48+
<descriptorRef>jar-with-dependencies</descriptorRef>
49+
</descriptorRefs>
50+
</configuration>
51+
<executions>
52+
<execution>
53+
<id>make-assembly</id> <!-- this is used for inheritance merges -->
54+
<phase>package</phase> <!-- bind to the packaging phase -->
55+
<goals>
56+
<goal>single</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
4164
<dependencyManagement>
4265
<dependencies>
4366
<dependency>

dlp/src/main/java/dlp/snippets/InfoTypesList.java

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
public class InfoTypesList {
2828

29+
public static void main(String[] args) throws IOException {
30+
listInfoTypes();
31+
}
32+
2933
// Lists the types of sensitive information the DLP API supports.
3034
public static void listInfoTypes() throws IOException {
3135
// Initialize client that will be used to send requests. This client only needs to be created

dlp/src/main/java/dlp/snippets/RedactImageFile.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434

3535
class RedactImageFile {
3636

37-
public static void redactImageFile() {
37+
public static void main(String[] args) {
3838
// TODO(developer): Replace these variables before running the sample.
39-
String projectId = "your-project-id";
40-
String filePath = "path/to/image.png";
41-
redactImageFile(projectId, filePath);
39+
String projectId = "my-project-id";
40+
String inputPath = "src/test/resources/test.png";
41+
String outputPath = "redacted.png";
42+
redactImageFile(projectId, inputPath, outputPath);
4243
}
4344

44-
static void redactImageFile(String projectId, String filePath) {
45+
static void redactImageFile(String projectId, String inputPath, String outputPath) {
4546
// Initialize client that will be used to send requests. This client only needs to be created
4647
// once, and can be reused for multiple requests. After completing all of your requests, call
4748
// the "close" method on the client to safely clean up any remaining background resources.
@@ -50,7 +51,7 @@ static void redactImageFile(String projectId, String filePath) {
5051
ProjectName project = ProjectName.of(projectId);
5152

5253
// Specify the content to be inspected.
53-
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
54+
ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
5455
ByteContentItem byteItem =
5556
ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build();
5657

@@ -78,7 +79,6 @@ static void redactImageFile(String projectId, String filePath) {
7879
RedactImageResponse response = dlp.redactImage(request);
7980

8081
// Parse the response and process results.
81-
String outputPath = "redacted.png";
8282
FileOutputStream redacted = new FileOutputStream(outputPath);
8383
redacted.write(response.getRedactedImage().toByteArray());
8484
redacted.close();

dlp/src/test/java/dlp/snippets/RedactTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
public class RedactTests {
3838

3939
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
40+
private static final String INPUT_FILE = "src/test/resources/test.png";
41+
private static final String OUTPUT_FILE = "redacted.png";
4042
private ByteArrayOutputStream bout;
4143

4244
private static void requireEnvVar(String varName) {
@@ -65,13 +67,13 @@ public void tearDown() {
6567

6668
@After
6769
public void cleanUp() throws IOException {
68-
Path outputFile = Paths.get("redacted.png");
70+
Path outputFile = Paths.get(OUTPUT_FILE);
6971
Files.delete(outputFile);
7072
}
7173

7274
@Test
7375
public void testRedactImage() {
74-
RedactImageFile.redactImageFile(PROJECT_ID, "src/test/resources/test.png");
76+
RedactImageFile.redactImageFile(PROJECT_ID, INPUT_FILE, OUTPUT_FILE);
7577

7678
String output = bout.toString();
7779
assertThat(output, containsString("Redacted image written"));

0 commit comments

Comments
 (0)