diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableBucketing.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableBucketing.java new file mode 100644 index 00000000000..055c19c7658 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableBucketing.java @@ -0,0 +1,121 @@ +/* + * Copyright 2020 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_deidentify_table_bucketing] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.FixedSizeBucketingConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; + +public class DeIdentifyTableBucketing { + + public static void deIdentifyTableBucketing() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + deIdentifyTableBucketing(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableBucketing(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + FixedSizeBucketingConfig fixedSizeBucketingConfig = + FixedSizeBucketingConfig.newBuilder() + .setBucketSize(10) + .setLowerBound(Value.newBuilder().setIntegerValue(0).build()) + .setUpperBound(Value.newBuilder().setIntegerValue(100).build()) + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setFixedSizeBucketingConfig(fixedSizeBucketingConfig) + .build(); + + // Specify field to be encrypted. + FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build(); + + // Associate the encryption with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addFields(fieldId) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_bucketing] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionInfoTypes.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionInfoTypes.java new file mode 100644 index 00000000000..0737f97b903 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionInfoTypes.java @@ -0,0 +1,163 @@ +/* + * Copyright 2020 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_deidentify_table_condition_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordCondition; +import com.google.privacy.dlp.v2.RecordCondition.Condition; +import com.google.privacy.dlp.v2.RecordCondition.Conditions; +import com.google.privacy.dlp.v2.RecordCondition.Expressions; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.RelationalOperator; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class DeIdentifyTableConditionInfoTypes { + + public static void deIdentifyTableConditionInfoTypes() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + + deIdentifyTableConditionInfoTypes(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableConditionInfoTypes(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + // Select type of info to be replaced. + InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); + // Specify that findings should be replaced with corresponding info type name. + ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = + ReplaceWithInfoTypeConfig.getDefaultInstance(); + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); + // Associate info type with the replacement strategy + InfoTypeTransformation infoTypeTransformation = + InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + InfoTypeTransformations infoTypeTransformations = + InfoTypeTransformations.newBuilder() + .addTransformations(infoTypeTransformation) + .build(); + + // Specify fields to be de-identified. + List fieldIds = Stream.of("PATIENT", "FACTOID") + .map(id -> FieldId.newBuilder().setName(id).build()) + .collect(Collectors.toList()); + + // Specify when the above fields should be de-identified. + Condition condition = Condition.newBuilder() + .setField(FieldId.newBuilder().setName("AGE").build()) + .setOperator(RelationalOperator.GREATER_THAN) + .setValue(Value.newBuilder().setIntegerValue(89).build()) + .build(); + // Apply the condition to records + RecordCondition recordCondition = RecordCondition.newBuilder() + .setExpressions(Expressions.newBuilder() + .setConditions(Conditions.newBuilder() + .addConditions(condition) + .build()) + .build()) + .build(); + + // Associate the de-identification and conditions with the specified fields. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setInfoTypeTransformations(infoTypeTransformations) + .addAllFields(fieldIds) + .setCondition(recordCondition) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_condition_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionMasking.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionMasking.java new file mode 100644 index 00000000000..71083fe6276 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionMasking.java @@ -0,0 +1,140 @@ +/* + * Copyright 2020 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_deidentify_table_condition_masking] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.CharacterMaskConfig; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordCondition; +import com.google.privacy.dlp.v2.RecordCondition.Condition; +import com.google.privacy.dlp.v2.RecordCondition.Conditions; +import com.google.privacy.dlp.v2.RecordCondition.Expressions; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.RelationalOperator; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; + +public class DeIdentifyTableConditionMasking { + + public static void deIdentifyTableConditionMasking() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + deIdentifyTableConditionMasking(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableConditionMasking(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + CharacterMaskConfig characterMaskConfig = + CharacterMaskConfig.newBuilder() + .setMaskingCharacter("*") + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setCharacterMaskConfig(characterMaskConfig) + .build(); + + // Specify field to be de-identified. + FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build(); + + // Specify when the above field should be de-identified. + Condition condition = Condition.newBuilder() + .setField(FieldId.newBuilder().setName("AGE").build()) + .setOperator(RelationalOperator.GREATER_THAN) + .setValue(Value.newBuilder().setIntegerValue(89).build()) + .build(); + // Apply the condition to records + RecordCondition recordCondition = RecordCondition.newBuilder() + .setExpressions(Expressions.newBuilder() + .setConditions(Conditions.newBuilder() + .addConditions(condition) + .build()) + .build()) + .build(); + + // Associate the de-identification and conditions with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addFields(fieldId) + .setCondition(recordCondition) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_condition_masking] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableInfoTypes.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableInfoTypes.java new file mode 100644 index 00000000000..56070bdb678 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableInfoTypes.java @@ -0,0 +1,142 @@ +/* + * Copyright 2020 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_deidentify_table_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class DeIdentifyTableInfoTypes { + + public static void deIdentifyTableInfoTypes() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + + deIdentifyTableInfoTypes(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableInfoTypes(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + // Select type of info to be replaced. + InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); + // Specify that findings should be replaced with corresponding info type name. + ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = + ReplaceWithInfoTypeConfig.getDefaultInstance(); + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); + // Associate info type with the replacement strategy + InfoTypeTransformation infoTypeTransformation = + InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + InfoTypeTransformations infoTypeTransformations = + InfoTypeTransformations.newBuilder() + .addTransformations(infoTypeTransformation) + .build(); + + // Specify fields to be de-identified. + List fieldIds = Stream.of("PATIENT", "FACTOID") + .map(id -> FieldId.newBuilder().setName(id).build()) + .collect(Collectors.toList()); + + // Associate the de-identification and conditions with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setInfoTypeTransformations(infoTypeTransformations) + .addAllFields(fieldIds) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableRowSuppress.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableRowSuppress.java new file mode 100644 index 00000000000..db573ee3b9f --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableRowSuppress.java @@ -0,0 +1,120 @@ +/* + * Copyright 2020 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_deidentify_table_row_suppress] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.RecordCondition; +import com.google.privacy.dlp.v2.RecordCondition.Condition; +import com.google.privacy.dlp.v2.RecordCondition.Conditions; +import com.google.privacy.dlp.v2.RecordCondition.Expressions; +import com.google.privacy.dlp.v2.RecordSuppression; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.RelationalOperator; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; + +public class DeIdentifyTableRowSuppress { + + public static void deIdentifyTableRowSuppress() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + deIdentifyTableRowSuppress(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableRowSuppress(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify when the content should be de-identified. + Condition condition = Condition.newBuilder() + .setField(FieldId.newBuilder().setName("AGE").build()) + .setOperator(RelationalOperator.GREATER_THAN) + .setValue(Value.newBuilder().setIntegerValue(89).build()).build(); + // Apply the condition to record suppression. + RecordSuppression recordSuppressions = + RecordSuppression.newBuilder() + .setCondition(RecordCondition.newBuilder() + .setExpressions(Expressions.newBuilder() + .setConditions(Conditions.newBuilder().addConditions(condition).build()) + .build()) + .build()) + .build(); + // Use record suppression as the only transformation + RecordTransformations transformations = + RecordTransformations.newBuilder() + .addRecordSuppressions(recordSuppressions) + .build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_row_suppress] diff --git a/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java b/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java index f9f5f1c0f2c..3c999d80654 100644 --- a/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java +++ b/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java @@ -18,6 +18,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.StringContains.containsString; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import com.google.privacy.dlp.v2.FieldId; @@ -163,6 +164,283 @@ public void testReIdentifyTableWithFpe() throws IOException { assertThat(output, containsString("Table after re-identification:")); } + @Test + public void testDeIdentifyTableBucketing() throws IOException { + // Transform a column based on the value of another column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("90:100").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("20:30").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("70:80").build()) + .build()) + .build(); + + Table table = DeIdentifyTableBucketing.deIdentifyTableBucketing(PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output, containsString("Table after de-identification:")); + assertEquals(expectedTable, table); + } + + @Test + public void testDeIdentifyTableConditionMasking() throws IOException { + // Transform a column based on the value of another column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("**").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + Table table = DeIdentifyTableConditionMasking.deIdentifyTableConditionMasking( + PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output, containsString("Table after de-identification:")); + assertEquals(expectedTable, table); + } + + @Test + public void testDeIdentifyTableInfoTypes() throws IOException { + // Transform findings found in column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "[PERSON_NAME] name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in [PERSON_NAME] novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME] loved cats.").build()) + .build()) + .build(); + + Table table = DeIdentifyTableInfoTypes.deIdentifyTableInfoTypes(PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output, containsString("Table after de-identification:")); + assertEquals(expectedTable, table); + } + + @Test + public void testDeIdentifyTableRowSuppress() throws IOException { + // Suppress a row based on the content of a column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + Table table = DeIdentifyTableRowSuppress.deIdentifyTableRowSuppress( + PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output, containsString("Table after de-identification:")); + assertEquals(expectedTable, table); + } + + @Test + public void testDeIdentifyTableConditionsInfoTypes() throws IOException { + // Transform findings only when specific conditions are met on another field + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "[PERSON_NAME] name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + + Table table = DeIdentifyTableConditionInfoTypes.deIdentifyTableConditionInfoTypes( + PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output, containsString("Table after de-identification:")); + assertEquals(expectedTable, table); + } + @Test public void testDeIdentifyWithDateShift() throws IOException { Path inputFile = Paths.get("src/test/resources/dates.csv");