Skip to content

Add SaveFindingsToGcs output proto conversion sample for DLP #10137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dlp/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<groupId>com.google.cloud</groupId>
<scope>import</scope>
<type>pom</type>
<version>26.32.0</version>
<version>26.64.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand All @@ -48,6 +48,10 @@
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dlp.snippets;

// [START dlp_process_inspect_findings_saved_to_gcs]

import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.SaveToGcsFindingsOutput;
import com.google.protobuf.ByteString;
import com.google.protobuf.TextFormat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

public class ProcessInspectFindingsSavedToGcs {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String inputPath = "src/test/resources/save_to_gcs_findings.txt";
processFindingsGcsFile(inputPath);
}

// Processes a file containing findings from a DLP inspect job.
public static void processFindingsGcsFile(String inputPath)
throws IOException {
SaveToGcsFindingsOutput.Builder builder = SaveToGcsFindingsOutput.newBuilder();
try (Reader reader =
new InputStreamReader(new FileInputStream(inputPath), StandardCharsets.UTF_8)) {
TextFormat.merge(reader, builder);
}
SaveToGcsFindingsOutput output = builder.build();

// Parse the converted proto and process results
System.out.println("Findings: " + output.getFindingsCount());
for (Finding f : output.getFindingsList()) {
System.out.println("\tInfo type: " + f.getInfoType().getName());
System.out.println("\tLikelihood: " + f.getLikelihood());
}
}
}
// [END dlp_process_inspect_findings_saved_to_gcs]
10 changes: 10 additions & 0 deletions dlp/snippets/src/test/java/dlp/snippets/InspectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -624,4 +624,14 @@ public void testInspectWithStoredInfotype() throws Exception {
assertThat(output).contains("InfoType: STORED_TYPE");
}
}

@Test
public void testProcessInspectFindingsSavedToGcs() throws Exception {
ProcessInspectFindingsSavedToGcs.processFindingsGcsFile(
"src/test/resources/save_to_gcs_findings.txt");
String output = bout.toString();
assertThat(output).contains("Findings: 2");
assertThat(output).contains("Info type: PERSON_NAME");
assertThat(output).contains("Likelihood: LIKELY");
}
}
110 changes: 110 additions & 0 deletions dlp/snippets/src/test/resources/save_to_gcs_findings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

findings {
info_type {
name: "PERSON_NAME"
sensitivity_score {
score: SENSITIVITY_MODERATE
}
}
likelihood: LIKELY
location {
byte_range {
start: 1208
end: 1216
}
content_locations {
container_name: "gs://fake_test_bucket/file.txt"
document_location {
}
container_timestamp {
seconds: 1728939753
nanos: 301000000
}
container_version: "1728939753176395"
}
container {
type: "Google Cloud Storage"
project_id: "fake-project-id"
full_path: "gs://fake_test_bucket/file.txt"
root_path: "fake_test_bucket"
relative_path: "file.txt"
update_time {
seconds: 1728939753
nanos: 301000000
}
version: "1728939753176395"
}
}
create_time {
seconds: 1741889947
nanos: 947000000
}
resource_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
job_create_time {
seconds: 1741889652
nanos: 348000000
}
job_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
finding_id: "2025-03-13T18:21:18.454889Z3148393127282654372"
}
findings {
info_type {
name: "PERSON_NAME"
sensitivity_score {
score: SENSITIVITY_MODERATE
}
}
likelihood: POSSIBLE
location {
byte_range {
start: 19872
end: 19879
}
content_locations {
container_name: "gs://fake_test_bucket/file.txt"
document_location {
}
container_timestamp {
seconds: 1728939753
nanos: 301000000
}
container_version: "1728939753176395"
}
container {
type: "Google Cloud Storage"
project_id: "fake-project-id"
full_path: "gs://fake_test_bucket/file.txt"
root_path: "fake_test_bucket"
relative_path: "file.txt"
update_time {
seconds: 1728939753
nanos: 301000000
}
version: "1728939753176395"
}
}
create_time {
seconds: 1741889947
nanos: 948000000
}
resource_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
job_create_time {
seconds: 1741889652
nanos: 348000000
}
job_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
finding_id: "2025-03-13T18:21:18.506689Z2134257296577089402"
}