Skip to content

Commit c8fc7fb

Browse files
authored
feat(dlp): inspect a string for sensitive data, omitting custom matches (GoogleCloudPlatform#1802)
1 parent 4f00dc1 commit c8fc7fb

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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_string_custom_omit_overlap]
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\ExclusionType;
32+
use Google\Cloud\Dlp\V2\CustomInfoType\Regex;
33+
use Google\Cloud\Dlp\V2\ExcludeInfoTypes;
34+
use Google\Cloud\Dlp\V2\ExclusionRule;
35+
use Google\Cloud\Dlp\V2\InfoType;
36+
use Google\Cloud\Dlp\V2\InspectConfig;
37+
use Google\Cloud\Dlp\V2\InspectionRule;
38+
use Google\Cloud\Dlp\V2\InspectionRuleSet;
39+
use Google\Cloud\Dlp\V2\Likelihood;
40+
use Google\Cloud\Dlp\V2\MatchingType;
41+
42+
/**
43+
* Inspect a string for sensitive data, omitting custom matches
44+
* Omit scan matches from a PERSON_NAME detector scan that overlap with a custom detector.
45+
*
46+
* @param string $projectId The Google Cloud project id to use as a parent resource.
47+
* @param string $textToInspect The string to inspect.
48+
*/
49+
function inspect_string_custom_omit_overlap(
50+
// TODO(developer): Replace sample parameters before running the code.
51+
string $projectId,
52+
string $textToInspect = 'Name: Jane Doe. Name: Larry Page.'
53+
): void {
54+
// Instantiate a client.
55+
$dlp = new DlpServiceClient();
56+
57+
$parent = "projects/$projectId/locations/global";
58+
59+
// Specify what content you want the service to Inspect.
60+
$item = (new ContentItem())
61+
->setValue($textToInspect);
62+
63+
// Specify the type of info the inspection will look for.
64+
$vipDetector = (new InfoType())
65+
->setName('VIP_DETECTOR');
66+
$pattern = 'Larry Page|Sergey Brin';
67+
$customInfoType = (new CustomInfoType())
68+
->setInfoType($vipDetector)
69+
->setRegex((new Regex())
70+
->setPattern($pattern))
71+
->setExclusionType(ExclusionType::EXCLUSION_TYPE_EXCLUDE);
72+
73+
// Exclude matches that also match the custom infotype.
74+
$exclusionRule = (new ExclusionRule())
75+
->setMatchingType(MatchingType::MATCHING_TYPE_FULL_MATCH)
76+
->setExcludeInfoTypes((new ExcludeInfoTypes())
77+
->setInfoTypes([$customInfoType->getInfoType()])
78+
);
79+
80+
// Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype.
81+
$personName = (new InfoType())
82+
->setName('PERSON_NAME');
83+
$inspectionRuleSet = (new InspectionRuleSet())
84+
->setInfoTypes([$personName])
85+
->setRules([
86+
(new InspectionRule())
87+
->setExclusionRule($exclusionRule),
88+
]);
89+
90+
// Construct the configuration for the Inspect request, including the ruleset.
91+
$inspectConfig = (new InspectConfig())
92+
->setInfoTypes([$personName])
93+
->setCustomInfoTypes([$customInfoType])
94+
->setIncludeQuote(true)
95+
->setRuleSet([$inspectionRuleSet]);
96+
97+
// Run request
98+
$response = $dlp->inspectContent([
99+
'parent' => $parent,
100+
'inspectConfig' => $inspectConfig,
101+
'item' => $item
102+
]);
103+
104+
// Print the results
105+
$findings = $response->getResult()->getFindings();
106+
if (count($findings) == 0) {
107+
printf('No findings.' . PHP_EOL);
108+
} else {
109+
printf('Findings:' . PHP_EOL);
110+
foreach ($findings as $finding) {
111+
printf(' Quote: %s' . PHP_EOL, $finding->getQuote());
112+
printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName());
113+
printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood()));
114+
}
115+
}
116+
}
117+
// [END dlp_inspect_string_custom_omit_overlap]
118+
119+
// The following 2 lines are only needed to run the samples
120+
require_once __DIR__ . '/../../testing/sample_helpers.php';
121+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/test/dlpTest.php

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

262+
public function testInspectStringCustomOmitOverlap()
263+
{
264+
$output = $this->runFunctionSnippet('inspect_string_custom_omit_overlap', [
265+
self::$projectId,
266+
'Name: Jane Doe. Name: Larry Page.'
267+
]);
268+
269+
$this->assertStringContainsString('Info type: PERSON_NAME', $output);
270+
$this->assertStringContainsString('Jane Doe', $output);
271+
$this->assertStringNotContainsString('Larry Page', $output);
272+
}
273+
262274
public function testInspectPhoneNumber()
263275
{
264276
$output = $this->runFunctionSnippet('inspect_phone_number', [

0 commit comments

Comments
 (0)