|
| 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_excluding_substring] |
| 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\Dictionary; |
| 32 | +use Google\Cloud\Dlp\V2\CustomInfoType\Dictionary\WordList; |
| 33 | +use Google\Cloud\Dlp\V2\CustomInfoType\Regex; |
| 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, excluding a custom substring |
| 44 | + * Illustrates how to use an InspectConfig to instruct Cloud DLP to avoid matching on the name "Jimmy" in a scan that uses the specified custom regular expression 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_excluding_substring( |
| 50 | + // TODO(developer): Replace sample parameters before running the code. |
| 51 | + string $projectId, |
| 52 | + string $textToInspect = 'Name: Doe, John. Name: Example, Jimmy' |
| 53 | +): void { |
| 54 | + // Instantiate a client. |
| 55 | + $dlp = new DlpServiceClient(); |
| 56 | + $customDetectorPattern = '[A-Z][a-z]{1,15}, [A-Z][a-z]{1,15}'; |
| 57 | + |
| 58 | + $parent = "projects/$projectId/locations/global"; |
| 59 | + |
| 60 | + // Specify what content you want the service to Inspect. |
| 61 | + $item = (new ContentItem()) |
| 62 | + ->setValue($textToInspect); |
| 63 | + |
| 64 | + // Specify the type of info the inspection will look for. |
| 65 | + $customerNameDetector = (new InfoType()) |
| 66 | + ->setName('CUSTOM_NAME_DETECTOR'); |
| 67 | + $customInfoType = (new CustomInfoType()) |
| 68 | + ->setInfoType($customerNameDetector) |
| 69 | + ->setRegex((new Regex()) |
| 70 | + ->setPattern($customDetectorPattern)); |
| 71 | + |
| 72 | + // Exclude partial matches from the specified excludedSubstringList. |
| 73 | + $excludedSubstringList = (new Dictionary()) |
| 74 | + ->setWordList((new WordList()) |
| 75 | + ->setWords(['Jimmy'])); |
| 76 | + |
| 77 | + $exclusionRule = (new ExclusionRule()) |
| 78 | + ->setMatchingType(MatchingType::MATCHING_TYPE_PARTIAL_MATCH) |
| 79 | + ->setDictionary($excludedSubstringList); |
| 80 | + |
| 81 | + // Construct a ruleset that applies the exclusion rule. |
| 82 | + $inspectionRuleSet = (new InspectionRuleSet()) |
| 83 | + ->setInfoTypes([$customerNameDetector]) |
| 84 | + ->setRules([ |
| 85 | + (new InspectionRule()) |
| 86 | + ->setExclusionRule($exclusionRule), |
| 87 | + ]); |
| 88 | + |
| 89 | + // Construct the configuration for the Inspect request, including the ruleset. |
| 90 | + $inspectConfig = (new InspectConfig()) |
| 91 | + ->setCustomInfoTypes([$customInfoType]) |
| 92 | + ->setIncludeQuote(true) |
| 93 | + ->setRuleSet([$inspectionRuleSet]); |
| 94 | + |
| 95 | + // Run request |
| 96 | + $response = $dlp->inspectContent([ |
| 97 | + 'parent' => $parent, |
| 98 | + 'inspectConfig' => $inspectConfig, |
| 99 | + 'item' => $item |
| 100 | + ]); |
| 101 | + |
| 102 | + // Print the results |
| 103 | + $findings = $response->getResult()->getFindings(); |
| 104 | + if (count($findings) == 0) { |
| 105 | + printf('No findings.' . PHP_EOL); |
| 106 | + } else { |
| 107 | + printf('Findings:' . PHP_EOL); |
| 108 | + foreach ($findings as $finding) { |
| 109 | + printf(' Quote: %s' . PHP_EOL, $finding->getQuote()); |
| 110 | + printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName()); |
| 111 | + printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood())); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +# [END dlp_inspect_string_custom_excluding_substring] |
| 116 | + |
| 117 | +// The following 2 lines are only needed to run the samples |
| 118 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 119 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments