|
| 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_bigquery_send_to_scc] |
| 27 | +use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 28 | +use Google\Cloud\Dlp\V2\InfoType; |
| 29 | +use Google\Cloud\Dlp\V2\InspectConfig; |
| 30 | +use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits; |
| 31 | +use Google\Cloud\Dlp\V2\StorageConfig; |
| 32 | +use Google\Cloud\Dlp\V2\Likelihood; |
| 33 | +use Google\Cloud\Dlp\V2\Action; |
| 34 | +use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc; |
| 35 | +use Google\Cloud\Dlp\V2\BigQueryOptions; |
| 36 | +use Google\Cloud\Dlp\V2\BigQueryTable; |
| 37 | +use Google\Cloud\Dlp\V2\InspectJobConfig; |
| 38 | +use Google\Cloud\Dlp\V2\DlpJob\JobState; |
| 39 | + |
| 40 | +/** |
| 41 | + * (BIGQUERY) Send Cloud DLP scan results to Security Command Center. |
| 42 | + * Using Cloud Data Loss Prevention to scan specific Google Cloud resources and send data to Security Command Center. |
| 43 | + * |
| 44 | + * @param string $callingProjectId The project ID to run the API call under. |
| 45 | + * @param string $projectId The ID of the Project. |
| 46 | + * @param string $datasetId The ID of the BigQuery Dataset. |
| 47 | + * @param string $tableId The ID of the BigQuery Table to be inspected. |
| 48 | + */ |
| 49 | +function inspect_bigquery_send_to_scc( |
| 50 | + // TODO(developer): Replace sample parameters before running the code. |
| 51 | + string $callingProjectId, |
| 52 | + string $projectId, |
| 53 | + string $datasetId, |
| 54 | + string $tableId |
| 55 | +): void { |
| 56 | + // Instantiate a client. |
| 57 | + $dlp = new DlpServiceClient(); |
| 58 | + |
| 59 | + // Construct the items to be inspected. |
| 60 | + $bigqueryTable = (new BigQueryTable()) |
| 61 | + ->setProjectId($projectId) |
| 62 | + ->setDatasetId($datasetId) |
| 63 | + ->setTableId($tableId); |
| 64 | + $bigQueryOptions = (new BigQueryOptions()) |
| 65 | + ->setTableReference($bigqueryTable); |
| 66 | + |
| 67 | + $storageConfig = (new StorageConfig()) |
| 68 | + ->setBigQueryOptions(($bigQueryOptions)); |
| 69 | + |
| 70 | + // Specify the type of info the inspection will look for. |
| 71 | + $infoTypes = [ |
| 72 | + (new InfoType())->setName('EMAIL_ADDRESS'), |
| 73 | + (new InfoType())->setName('PERSON_NAME'), |
| 74 | + (new InfoType())->setName('LOCATION'), |
| 75 | + (new InfoType())->setName('PHONE_NUMBER') |
| 76 | + ]; |
| 77 | + |
| 78 | + // Specify how the content should be inspected. |
| 79 | + $inspectConfig = (new InspectConfig()) |
| 80 | + ->setMinLikelihood(likelihood::UNLIKELY) |
| 81 | + ->setLimits((new FindingLimits()) |
| 82 | + ->setMaxFindingsPerRequest(100)) |
| 83 | + ->setInfoTypes($infoTypes) |
| 84 | + ->setIncludeQuote(true); |
| 85 | + |
| 86 | + // Specify the action that is triggered when the job completes. |
| 87 | + $action = (new Action()) |
| 88 | + ->setPublishSummaryToCscc(new PublishSummaryToCscc()); |
| 89 | + |
| 90 | + // Configure the inspection job we want the service to perform. |
| 91 | + $inspectJobConfig = (new InspectJobConfig()) |
| 92 | + ->setInspectConfig($inspectConfig) |
| 93 | + ->setStorageConfig($storageConfig) |
| 94 | + ->setActions([$action]); |
| 95 | + |
| 96 | + // Send the job creation request and process the response. |
| 97 | + $parent = "projects/$callingProjectId/locations/global"; |
| 98 | + $job = $dlp->createDlpJob($parent, [ |
| 99 | + 'inspectJob' => $inspectJobConfig |
| 100 | + ]); |
| 101 | + |
| 102 | + $numOfAttempts = 10; |
| 103 | + do { |
| 104 | + printf('Waiting for job to complete' . PHP_EOL); |
| 105 | + sleep(10); |
| 106 | + $job = $dlp->getDlpJob($job->getName()); |
| 107 | + if ($job->getState() == JobState::DONE) { |
| 108 | + break; |
| 109 | + } |
| 110 | + $numOfAttempts--; |
| 111 | + } while ($numOfAttempts > 0); |
| 112 | + |
| 113 | + // Print finding counts. |
| 114 | + printf('Job %s status: %s' . PHP_EOL, $job->getName(), JobState::name($job->getState())); |
| 115 | + switch ($job->getState()) { |
| 116 | + case JobState::DONE: |
| 117 | + $infoTypeStats = $job->getInspectDetails()->getResult()->getInfoTypeStats(); |
| 118 | + if (count($infoTypeStats) === 0) { |
| 119 | + printf('No findings.' . PHP_EOL); |
| 120 | + } else { |
| 121 | + foreach ($infoTypeStats as $infoTypeStat) { |
| 122 | + printf( |
| 123 | + ' Found %s instance(s) of infoType %s' . PHP_EOL, |
| 124 | + $infoTypeStat->getCount(), |
| 125 | + $infoTypeStat->getInfoType()->getName() |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + break; |
| 130 | + case JobState::FAILED: |
| 131 | + printf('Job %s had errors:' . PHP_EOL, $job->getName()); |
| 132 | + $errors = $job->getErrors(); |
| 133 | + foreach ($errors as $error) { |
| 134 | + var_dump($error->getDetails()); |
| 135 | + } |
| 136 | + break; |
| 137 | + case JobState::PENDING: |
| 138 | + printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL); |
| 139 | + break; |
| 140 | + default: |
| 141 | + printf('Unexpected job state. Most likely, the job is either running or has not yet started.'); |
| 142 | + } |
| 143 | +} |
| 144 | +# [END dlp_inspect_bigquery_send_to_scc] |
| 145 | +// The following 2 lines are only needed to run the samples |
| 146 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 147 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments