diff --git a/dlp/snippets/pom.xml b/dlp/snippets/pom.xml index 651f4165dcc..0c2c4483faf 100644 --- a/dlp/snippets/pom.xml +++ b/dlp/snippets/pom.xml @@ -32,7 +32,7 @@ com.google.cloud import pom - 26.32.0 + 26.64.0 @@ -48,6 +48,10 @@ com.google.cloud google-cloud-pubsub + + com.google.protobuf + protobuf-java + junit junit diff --git a/dlp/snippets/src/main/java/dlp/snippets/ProcessInspectFindingsSavedToGcs.java b/dlp/snippets/src/main/java/dlp/snippets/ProcessInspectFindingsSavedToGcs.java new file mode 100644 index 00000000000..a50ab25e28e --- /dev/null +++ b/dlp/snippets/src/main/java/dlp/snippets/ProcessInspectFindingsSavedToGcs.java @@ -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] \ No newline at end of file diff --git a/dlp/snippets/src/test/java/dlp/snippets/InspectTests.java b/dlp/snippets/src/test/java/dlp/snippets/InspectTests.java index b5b79dd6cc2..ae0230a1e36 100644 --- a/dlp/snippets/src/test/java/dlp/snippets/InspectTests.java +++ b/dlp/snippets/src/test/java/dlp/snippets/InspectTests.java @@ -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"); + } } diff --git a/dlp/snippets/src/test/resources/save_to_gcs_findings.txt b/dlp/snippets/src/test/resources/save_to_gcs_findings.txt new file mode 100644 index 00000000000..6192d7704aa --- /dev/null +++ b/dlp/snippets/src/test/resources/save_to_gcs_findings.txt @@ -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" +} \ No newline at end of file