Skip to content

Commit baefcd5

Browse files
authored
feat(dlp): create an exception list for de-identification (GoogleCloudPlatform#1795)
1 parent 8fa91d0 commit baefcd5

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
# [START dlp_deidentify_exception_list]
28+
use Google\Cloud\Dlp\V2\ContentItem;
29+
use Google\Cloud\Dlp\V2\DlpServiceClient;
30+
use Google\Cloud\Dlp\V2\CustomInfoType\Dictionary;
31+
use Google\Cloud\Dlp\V2\CustomInfoType\Dictionary\WordList;
32+
use Google\Cloud\Dlp\V2\InfoType;
33+
use Google\Cloud\Dlp\V2\DeidentifyConfig;
34+
use Google\Cloud\Dlp\V2\ExclusionRule;
35+
use Google\Cloud\Dlp\V2\InspectConfig;
36+
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
37+
use Google\Cloud\Dlp\V2\ReplaceWithInfoTypeConfig;
38+
use Google\Cloud\Dlp\V2\InfoTypeTransformations;
39+
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
40+
use Google\Cloud\Dlp\V2\InspectionRule;
41+
use Google\Cloud\Dlp\V2\InspectionRuleSet;
42+
use Google\Cloud\Dlp\V2\MatchingType;
43+
44+
/**
45+
* Create an exception list for de-identification
46+
* Create an exception list for a regular custom dictionary detector.
47+
*
48+
* @param string $callingProjectId The project ID to run the API call under
49+
* @param string $textToDeIdentify The String you want the service to DeIdentify
50+
*/
51+
function deidentify_exception_list(
52+
// TODO(developer): Replace sample parameters before running the code.
53+
string $callingProjectId,
54+
string $textToDeIdentify = '[email protected] accessed customer record of [email protected]'
55+
): void {
56+
// Instantiate a client.
57+
$dlp = new DlpServiceClient();
58+
59+
// Specify what content you want the service to DeIdentify.
60+
$contentItem = (new ContentItem())
61+
->setValue($textToDeIdentify);
62+
63+
// Construct the custom word list to be detected.
64+
$wordList = (new Dictionary())
65+
->setWordList((new WordList())
66+
67+
68+
// Specify the exclusion rule and build-in info type the inspection will look for.
69+
$exclusionRule = (new ExclusionRule())
70+
->setMatchingType(MatchingType::MATCHING_TYPE_FULL_MATCH)
71+
->setDictionary($wordList);
72+
73+
$emailAddress = (new InfoType())
74+
->setName('EMAIL_ADDRESS');
75+
$inspectionRuleSet = (new InspectionRuleSet())
76+
->setInfoTypes([$emailAddress])
77+
->setRules([
78+
(new InspectionRule())
79+
->setExclusionRule($exclusionRule)
80+
]);
81+
82+
$inspectConfig = (new InspectConfig())
83+
->setInfoTypes([$emailAddress])
84+
->setRuleSet([$inspectionRuleSet]);
85+
86+
// Define type of deidentification as replacement.
87+
$primitiveTransformation = (new PrimitiveTransformation())
88+
->setReplaceWithInfoTypeConfig(new ReplaceWithInfoTypeConfig());
89+
90+
// Associate de-identification type with info type.
91+
$transformation = (new InfoTypeTransformation())
92+
->setInfoTypes([$emailAddress])
93+
->setPrimitiveTransformation($primitiveTransformation);
94+
95+
// Construct the configuration for the de-id request and list all desired transformations.
96+
$deidentifyConfig = (new DeidentifyConfig())
97+
->setInfoTypeTransformations(
98+
(new InfoTypeTransformations())
99+
->setTransformations([$transformation])
100+
);
101+
102+
// Send the request and receive response from the service
103+
$parent = "projects/$callingProjectId/locations/global";
104+
$response = $dlp->deidentifyContent([
105+
'parent' => $parent,
106+
'deidentifyConfig' => $deidentifyConfig,
107+
'inspectConfig' => $inspectConfig,
108+
'item' => $contentItem
109+
]);
110+
111+
// Print the results
112+
printf('Text after replace with infotype config: %s', $response->getItem()->getValue());
113+
}
114+
# [END dlp_deidentify_exception_list]
115+
116+
// The following 2 lines are only needed to run the samples
117+
require_once __DIR__ . '/../../testing/sample_helpers.php';
118+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/test/dlpTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ public function testJobs()
259259
$this->assertStringContainsString('Successfully deleted job ' . $jobId, $output);
260260
}
261261

262+
public function testDeIdentifyExceptionList()
263+
{
264+
$output = $this->runFunctionSnippet('deidentify_exception_list', [
265+
self::$projectId,
266+
'[email protected] accessed customer record of [email protected]'
267+
]);
268+
$this->assertStringContainsString('[EMAIL_ADDRESS]', $output);
269+
$this->assertStringContainsString('[email protected]', $output);
270+
$this->assertStringNotContainsString('[email protected]', $output);
271+
}
272+
262273
public function testDeidentifySimpleWordList()
263274
{
264275
$output = $this->runFunctionSnippet('deidentify_simple_word_list', [

0 commit comments

Comments
 (0)