Skip to content

Commit 3438bdb

Browse files
authored
feat(dlp): sample for Hybrid Job Trigger , kAnonymity with entity id , and DeIdentify Replace InfoType (#1900)
1 parent 2863731 commit 3438bdb

File tree

6 files changed

+764
-15
lines changed

6 files changed

+764
-15
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the samples:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Dlp;
25+
26+
# [START dlp_deidentify_replace_infotype]
27+
use Google\Cloud\Dlp\V2\DlpServiceClient;
28+
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
29+
use Google\Cloud\Dlp\V2\InfoType;
30+
use Google\Cloud\Dlp\V2\DeidentifyConfig;
31+
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
32+
use Google\Cloud\Dlp\V2\InfoTypeTransformations;
33+
use Google\Cloud\Dlp\V2\ContentItem;
34+
use Google\Cloud\Dlp\V2\InspectConfig;
35+
use Google\Cloud\Dlp\V2\ReplaceWithInfoTypeConfig;
36+
37+
/**
38+
* De-identify sensitive data by replacing with infoType.
39+
* Uses the Data Loss Prevention API to deidentify sensitive data in a string by replacing it with
40+
* the info type.
41+
*
42+
* @param string $callingProjectId The Google Cloud project id to use as a parent resource.
43+
* @param string $string The string to deidentify (will be treated as text).
44+
*/
45+
46+
function deidentify_replace_infotype(
47+
// TODO(developer): Replace sample parameters before running the code.
48+
string $callingProjectId,
49+
string $string
50+
): void {
51+
// Instantiate a client.
52+
$dlp = new DlpServiceClient();
53+
54+
$parent = "projects/$callingProjectId/locations/global";
55+
56+
// Specify what content you want the service to de-identify.
57+
$content = (new ContentItem())
58+
->setValue($string);
59+
60+
// The infoTypes of information to mask.
61+
$phoneNumberinfoType = (new InfoType())
62+
->setName('PHONE_NUMBER');
63+
$personNameinfoType = (new InfoType())
64+
->setName('PERSON_NAME');
65+
$infoTypes = [$phoneNumberinfoType, $personNameinfoType];
66+
67+
// Create the configuration object.
68+
$inspectConfig = (new InspectConfig())
69+
->setInfoTypes($infoTypes);
70+
71+
// Create the information transform configuration objects.
72+
$primitiveTransformation = (new PrimitiveTransformation())
73+
->setReplaceWithInfoTypeConfig(new ReplaceWithInfoTypeConfig());
74+
75+
$infoTypeTransformation = (new InfoTypeTransformation())
76+
->setPrimitiveTransformation($primitiveTransformation);
77+
78+
$infoTypeTransformations = (new InfoTypeTransformations())
79+
->setTransformations([$infoTypeTransformation]);
80+
81+
// Create the deidentification configuration object.
82+
$deidentifyConfig = (new DeidentifyConfig())
83+
->setInfoTypeTransformations($infoTypeTransformations);
84+
85+
// Run request.
86+
$response = $dlp->deidentifyContent([
87+
'parent' => $parent,
88+
'deidentifyConfig' => $deidentifyConfig,
89+
'item' => $content,
90+
'inspectConfig' => $inspectConfig
91+
]);
92+
93+
// Print the results.
94+
printf('Text after replace with infotype config: %s', $response->getItem()->getValue());
95+
}
96+
# [END dlp_deidentify_replace_infotype]
97+
98+
// The following 2 lines are only needed to run the samples.
99+
require_once __DIR__ . '/../../testing/sample_helpers.php';
100+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/src/inspect_gcs.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2018 Google Inc.
54
*
@@ -58,12 +57,8 @@ function inspect_gcs(
5857
int $maxFindings = 0
5958
): void {
6059
// Instantiate a client.
61-
$dlp = new DlpServiceClient([
62-
'projectId' => $callingProjectId,
63-
]);
64-
$pubsub = new PubSubClient([
65-
'projectId' => $callingProjectId,
66-
]);
60+
$dlp = new DlpServiceClient();
61+
$pubsub = new PubSubClient();
6762
$topic = $pubsub->topic($topicId);
6863

6964
// The infoTypes of information to match
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the samples:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Dlp;
25+
26+
# [START dlp_inspect_send_data_to_hybrid_job_trigger]
27+
28+
use Google\Cloud\Dlp\V2\Container;
29+
use Google\Cloud\Dlp\V2\DlpServiceClient;
30+
use Google\Cloud\Dlp\V2\ContentItem;
31+
use Google\Cloud\Dlp\V2\DlpJob\JobState;
32+
use Google\Cloud\Dlp\V2\HybridContentItem;
33+
use Google\Cloud\Dlp\V2\HybridFindingDetails;
34+
35+
/**
36+
* Inspect data hybrid job trigger.
37+
* Send data to the hybrid job or hybrid job trigger.
38+
*
39+
* @param string $callingProjectId The Google Cloud project id to use as a parent resource.
40+
* @param string $string The string to inspect (will be treated as text).
41+
*/
42+
43+
function inspect_send_data_to_hybrid_job_trigger(
44+
// TODO(developer): Replace sample parameters before running the code.
45+
string $callingProjectId,
46+
string $jobTriggerId,
47+
string $string
48+
): void {
49+
// Instantiate a client.
50+
$dlp = new DlpServiceClient();
51+
52+
$content = (new ContentItem())
53+
->setValue($string);
54+
55+
$container = (new Container())
56+
->setFullPath('10.0.0.2:logs1:app1')
57+
->setRelativePath('app1')
58+
->setRootPath('10.0.0.2:logs1')
59+
->setType('logging_sys')
60+
->setVersion('1.2');
61+
62+
$findingDetails = (new HybridFindingDetails())
63+
->setContainerDetails($container)
64+
->setLabels([
65+
'env' => 'prod',
66+
'appointment-bookings-comments' => ''
67+
]);
68+
69+
$hybridItem = (new HybridContentItem())
70+
->setItem($content)
71+
->setFindingDetails($findingDetails);
72+
73+
$parent = "projects/$callingProjectId/locations/global";
74+
$name = "projects/$callingProjectId/locations/global/jobTriggers/" . $jobTriggerId;
75+
76+
$triggerJob = null;
77+
try {
78+
$triggerJob = $dlp->activateJobTrigger($name);
79+
} catch (\InvalidArgumentException $e) {
80+
$result = $dlp->listDlpJobs($parent, ['filter' => 'trigger_name=' . $name]);
81+
foreach ($result as $job) {
82+
$triggerJob = $job;
83+
}
84+
}
85+
86+
$dlp->hybridInspectJobTrigger($name, [
87+
'hybridItem' => $hybridItem,
88+
]);
89+
90+
$numOfAttempts = 10;
91+
do {
92+
printf('Waiting for job to complete' . PHP_EOL);
93+
sleep(10);
94+
$job = $dlp->getDlpJob($triggerJob->getName());
95+
if ($job->getState() != JobState::RUNNING) {
96+
break;
97+
}
98+
$numOfAttempts--;
99+
} while ($numOfAttempts > 0);
100+
101+
// Print finding counts.
102+
printf('Job %s status: %s' . PHP_EOL, $job->getName(), JobState::name($job->getState()));
103+
switch ($job->getState()) {
104+
case JobState::DONE:
105+
$infoTypeStats = $job->getInspectDetails()->getResult()->getInfoTypeStats();
106+
if (count($infoTypeStats) === 0) {
107+
printf('No findings.' . PHP_EOL);
108+
} else {
109+
foreach ($infoTypeStats as $infoTypeStat) {
110+
printf(
111+
' Found %s instance(s) of infoType %s' . PHP_EOL,
112+
$infoTypeStat->getCount(),
113+
$infoTypeStat->getInfoType()->getName()
114+
);
115+
}
116+
}
117+
break;
118+
case JobState::FAILED:
119+
printf('Job %s had errors:' . PHP_EOL, $job->getName());
120+
$errors = $job->getErrors();
121+
foreach ($errors as $error) {
122+
var_dump($error->getDetails());
123+
}
124+
break;
125+
case JobState::PENDING:
126+
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
127+
break;
128+
default:
129+
printf('Unexpected job state. Most likely, the job is either running or has not yet started.');
130+
}
131+
}
132+
# [END dlp_inspect_send_data_to_hybrid_job_trigger]
133+
134+
// The following 2 lines are only needed to run the samples.
135+
require_once __DIR__ . '/../../testing/sample_helpers.php';
136+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)