Skip to content

Commit 012ce87

Browse files
authored
feat(dlp): inspect data with a custom regex (GoogleCloudPlatform#1797)
1 parent 47cbbec commit 012ce87

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

dlp/src/inspect_custom_regex.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google LLC.
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 full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/bigquery/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
// [START dlp_inspect_custom_regex]
28+
use Google\Cloud\Dlp\V2\DlpServiceClient;
29+
use Google\Cloud\Dlp\V2\ContentItem;
30+
use Google\Cloud\Dlp\V2\CustomInfoType;
31+
use Google\Cloud\Dlp\V2\CustomInfoType\Regex;
32+
use Google\Cloud\Dlp\V2\InfoType;
33+
use Google\Cloud\Dlp\V2\InspectConfig;
34+
use Google\Cloud\Dlp\V2\Likelihood;
35+
36+
/**
37+
* Inspect data with a custom regex
38+
* Regex example: Matching medical record numbers. The following sample uses a regular expression custom infoType detector that instructs Cloud DLP to match a medical record number (MRN) in the input text "Patient's MRN 444-5-22222," and then assigns each match a likelihood of POSSIBLE.
39+
*
40+
* @param string $projectId The Google Cloud project id to use as a parent resource.
41+
* @param string $textToInspect The string to inspect.
42+
*/
43+
function inspect_custom_regex(
44+
// TODO(developer): Replace sample parameters before running the code.
45+
string $projectId,
46+
string $textToInspect = 'Patients MRN 444-5-22222'
47+
): void {
48+
// Instantiate a client.
49+
$dlp = new DlpServiceClient();
50+
51+
$parent = "projects/$projectId/locations/global";
52+
53+
// Specify what content you want the service to Inspect.
54+
$item = (new ContentItem())
55+
->setValue($textToInspect);
56+
57+
// Specify the regex pattern the inspection will look for.
58+
$customRegexPattern = '[1-9]{3}-[1-9]{1}-[1-9]{5}';
59+
60+
// Construct the custom regex detector.
61+
$cMrnDetector = (new InfoType())
62+
->setName('C_MRN');
63+
$customInfoType = (new CustomInfoType())
64+
->setInfoType($cMrnDetector)
65+
->setRegex((new Regex())
66+
->setPattern($customRegexPattern))
67+
->setLikelihood(Likelihood::POSSIBLE);
68+
69+
// Construct the configuration for the Inspect request.
70+
$inspectConfig = (new InspectConfig())
71+
->setCustomInfoTypes([$customInfoType])
72+
->setIncludeQuote(true);
73+
74+
// Run request
75+
$response = $dlp->inspectContent([
76+
'parent' => $parent,
77+
'inspectConfig' => $inspectConfig,
78+
'item' => $item
79+
]);
80+
81+
// Print the results
82+
$findings = $response->getResult()->getFindings();
83+
if (count($findings) == 0) {
84+
printf('No findings.' . PHP_EOL);
85+
} else {
86+
printf('Findings:' . PHP_EOL);
87+
foreach ($findings as $finding) {
88+
printf(' Quote: %s' . PHP_EOL, $finding->getQuote());
89+
printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName());
90+
printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood()));
91+
}
92+
}
93+
}
94+
// [END dlp_inspect_custom_regex]
95+
96+
// The following 2 lines are only needed to run the samples
97+
require_once __DIR__ . '/../../testing/sample_helpers.php';
98+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/test/dlpTest.php

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

262+
public function testInspectCustomRegex()
263+
{
264+
$output = $this->runFunctionSnippet('inspect_custom_regex', [
265+
self::$projectId,
266+
'Patients MRN 444-5-22222'
267+
]);
268+
$this->assertStringContainsString('Info type: C_MRN', $output);
269+
}
270+
262271
public function testInspectStringOmitOverlap()
263272
{
264273
$output = $this->runFunctionSnippet('inspect_string_omit_overlap', [

0 commit comments

Comments
 (0)