|
| 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_inspect_bigquery_with_sampling] |
| 28 | + |
| 29 | +use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 30 | +use Google\Cloud\Dlp\V2\BigQueryOptions; |
| 31 | +use Google\Cloud\Dlp\V2\InfoType; |
| 32 | +use Google\Cloud\Dlp\V2\InspectConfig; |
| 33 | +use Google\Cloud\Dlp\V2\StorageConfig; |
| 34 | +use Google\Cloud\Dlp\V2\BigQueryTable; |
| 35 | +use Google\Cloud\Dlp\V2\DlpJob\JobState; |
| 36 | +use Google\Cloud\Dlp\V2\Action; |
| 37 | +use Google\Cloud\Dlp\V2\Action\PublishToPubSub; |
| 38 | +use Google\Cloud\Dlp\V2\BigQueryOptions\SampleMethod; |
| 39 | +use Google\Cloud\Dlp\V2\FieldId; |
| 40 | +use Google\Cloud\Dlp\V2\InspectJobConfig; |
| 41 | +use Google\Cloud\PubSub\PubSubClient; |
| 42 | + |
| 43 | +/** |
| 44 | + * Inspect BigQuery for sensitive data with sampling. |
| 45 | + * The following examples demonstrate using the Cloud Data Loss Prevention |
| 46 | + * API to scan a 1000-row subset of a BigQuery table. The scan starts from |
| 47 | + * a random row. |
| 48 | + * |
| 49 | + * @param string $callingProjectId The project ID to run the API call under. |
| 50 | + * @param string $topicId The Pub/Sub topic ID to notify once the job is completed. |
| 51 | + * @param string $subscriptionId The Pub/Sub subscription ID to use when listening for job. |
| 52 | + * @param string $projectId The Google Cloud Project ID. |
| 53 | + * @param string $datasetId The BigQuery Dataset ID. |
| 54 | + * @param string $tableId The BigQuery Table ID to be inspected. |
| 55 | + */ |
| 56 | +function inspect_bigquery_with_sampling( |
| 57 | + string $callingProjectId, |
| 58 | + string $topicId, |
| 59 | + string $subscriptionId, |
| 60 | + string $projectId, |
| 61 | + string $datasetId, |
| 62 | + string $tableId |
| 63 | +): void { |
| 64 | + // Instantiate a client. |
| 65 | + $dlp = new DlpServiceClient(); |
| 66 | + $pubsub = new PubSubClient(); |
| 67 | + $topic = $pubsub->topic($topicId); |
| 68 | + |
| 69 | + // Specify the BigQuery table to be inspected. |
| 70 | + $bigqueryTable = (new BigQueryTable()) |
| 71 | + ->setProjectId($projectId) |
| 72 | + ->setDatasetId($datasetId) |
| 73 | + ->setTableId($tableId); |
| 74 | + |
| 75 | + $bigQueryOptions = (new BigQueryOptions()) |
| 76 | + ->setTableReference($bigqueryTable) |
| 77 | + ->setRowsLimit(1000) |
| 78 | + ->setSampleMethod(SampleMethod::RANDOM_START) |
| 79 | + ->setIdentifyingFields([ |
| 80 | + (new FieldId()) |
| 81 | + ->setName('name') |
| 82 | + ]); |
| 83 | + |
| 84 | + $storageConfig = (new StorageConfig()) |
| 85 | + ->setBigQueryOptions($bigQueryOptions); |
| 86 | + |
| 87 | + // Specify the type of info the inspection will look for. |
| 88 | + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types |
| 89 | + $personNameInfoType = (new InfoType()) |
| 90 | + ->setName('PERSON_NAME'); |
| 91 | + $infoTypes = [$personNameInfoType]; |
| 92 | + |
| 93 | + // Specify how the content should be inspected. |
| 94 | + $inspectConfig = (new InspectConfig()) |
| 95 | + ->setInfoTypes($infoTypes) |
| 96 | + ->setIncludeQuote(true); |
| 97 | + |
| 98 | + // Specify the action that is triggered when the job completes. |
| 99 | + $pubSubAction = (new PublishToPubSub()) |
| 100 | + ->setTopic($topic->name()); |
| 101 | + |
| 102 | + $action = (new Action()) |
| 103 | + ->setPubSub($pubSubAction); |
| 104 | + |
| 105 | + // Configure the long running job we want the service to perform. |
| 106 | + $inspectJob = (new InspectJobConfig()) |
| 107 | + ->setInspectConfig($inspectConfig) |
| 108 | + ->setStorageConfig($storageConfig) |
| 109 | + ->setActions([$action]); |
| 110 | + |
| 111 | + // Listen for job notifications via an existing topic/subscription. |
| 112 | + $subscription = $topic->subscription($subscriptionId); |
| 113 | + |
| 114 | + // Submit request |
| 115 | + $parent = "projects/$callingProjectId/locations/global"; |
| 116 | + $job = $dlp->createDlpJob($parent, [ |
| 117 | + 'inspectJob' => $inspectJob |
| 118 | + ]); |
| 119 | + |
| 120 | + // Poll Pub/Sub using exponential backoff until job finishes |
| 121 | + // Consider using an asynchronous execution model such as Cloud Functions |
| 122 | + $attempt = 1; |
| 123 | + $startTime = time(); |
| 124 | + do { |
| 125 | + foreach ($subscription->pull() as $message) { |
| 126 | + if ( |
| 127 | + isset($message->attributes()['DlpJobName']) && |
| 128 | + $message->attributes()['DlpJobName'] === $job->getName() |
| 129 | + ) { |
| 130 | + $subscription->acknowledge($message); |
| 131 | + // Get the updated job. Loop to avoid race condition with DLP API. |
| 132 | + do { |
| 133 | + $job = $dlp->getDlpJob($job->getName()); |
| 134 | + } while ($job->getState() == JobState::RUNNING); |
| 135 | + break 2; // break from parent do while |
| 136 | + } |
| 137 | + } |
| 138 | + printf('Waiting for job to complete' . PHP_EOL); |
| 139 | + // Exponential backoff with max delay of 60 seconds |
| 140 | + sleep(min(60, pow(2, ++$attempt))); |
| 141 | + } while (time() - $startTime < 600); // 10 minute timeout |
| 142 | + |
| 143 | + // Print finding counts |
| 144 | + printf('Job %s status: %s' . PHP_EOL, $job->getName(), JobState::name($job->getState())); |
| 145 | + switch ($job->getState()) { |
| 146 | + case JobState::DONE: |
| 147 | + $infoTypeStats = $job->getInspectDetails()->getResult()->getInfoTypeStats(); |
| 148 | + if (count($infoTypeStats) === 0) { |
| 149 | + printf('No findings.' . PHP_EOL); |
| 150 | + } else { |
| 151 | + foreach ($infoTypeStats as $infoTypeStat) { |
| 152 | + printf( |
| 153 | + ' Found %s instance(s) of infoType %s' . PHP_EOL, |
| 154 | + $infoTypeStat->getCount(), |
| 155 | + $infoTypeStat->getInfoType()->getName() |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + break; |
| 160 | + case JobState::FAILED: |
| 161 | + printf('Job %s had errors:' . PHP_EOL, $job->getName()); |
| 162 | + $errors = $job->getErrors(); |
| 163 | + foreach ($errors as $error) { |
| 164 | + var_dump($error->getDetails()); |
| 165 | + } |
| 166 | + break; |
| 167 | + case JobState::PENDING: |
| 168 | + printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL); |
| 169 | + break; |
| 170 | + default: |
| 171 | + printf('Unexpected job state. Most likely, the job is either running or has not yet started.'); |
| 172 | + } |
| 173 | +} |
| 174 | +# [END dlp_inspect_bigquery_with_sampling] |
| 175 | + |
| 176 | +// The following 2 lines are only needed to run the samples |
| 177 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 178 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments