google/cloud-dlp
Idiomatic PHP client for Google Cloud Data Loss Prevention (DLP). Inspect, classify, and manage sensitive data using REST or gRPC. Install with Composer via google/cloud-dlp and authenticate with Google Cloud credentials.
Installation:
composer require google/cloud-dlp
Ensure your composer.json includes the package under require.
Authentication: Configure credentials via environment variables (recommended) or service account JSON:
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account.json');
Or use the GOOGLE_CLOUD_PROJECT environment variable for project-specific auth.
First Use Case: Scan a text content for sensitive data (e.g., PII):
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\InspectContentRequest;
use Google\Cloud\Dlp\V2\ContentItem;
$client = new DlpServiceClient();
$request = (new InspectContentRequest())
->setItem((new ContentItem())
->setData('John Doe, SSN: 123-45-6789')
)
->setInspectConfig((new \Google\Cloud\Dlp\V2\InspectConfig())
->setInfoTypes(['PERSON_NAME', 'US_SOCIAL_SECURITY_NUMBER'])
);
$response = $client->inspectContent($request);
print_r($response->getFindings());
DlpServiceClient (primary entry point).inspectContent(): Scan text/bytes for sensitive data.redactContent(): Mask sensitive data in text/bytes.deidentifyContent(): Transform data to remove PII.createInspectJob(): Asynchronous scanning for large datasets.getColumnDataProfile(): Analyze structured data (e.g., BigQuery tables).$client = new DlpServiceClient();
$request = (new InspectContentRequest())
->setItem((new ContentItem())->setData($textData))
->setInspectConfig((new \Google\Cloud\Dlp\V2\InspectConfig())
->setInfoTypes(['CREDIT_CARD_NUMBER', 'EMAIL_ADDRESS'])
);
$findings = $client->inspectContent($request)->getFindings();
foreach ($findings as $finding) {
echo "Found: " . $finding->getInfoType()->getName() . "\n";
}
Use createInspectJob() for batch processing (e.g., GCS files):
$job = $client->createInspectJob((new \Google\Cloud\Dlp\V2\CreateInspectJobRequest())
->setParent('projects/YOUR_PROJECT')
->setInspectJob((new \Google\Cloud\Dlp\V2\InspectJob())
->setInspectConfig((new \Google\Cloud\Dlp\V2\InspectConfig())
->setInfoTypes(['PHONE_NUMBER'])
)
->setStorageConfig((new \Google\Cloud\Dlp\V2\GcsContent())
->setFileUri('gs://your-bucket/file.csv')
)
)
);
echo "Job ID: " . $job->getName() . "\n";
Profile a BigQuery table to identify sensitive columns:
$profile = $client->getColumnDataProfile((new \Google\Cloud\Dlp\V2\GetColumnDataProfileRequest())
->setName('projects/YOUR_PROJECT/locations/US/columnDataProfiles/123')
);
foreach ($profile->getColumnDataProfileResult()->getColumnStats() as $stat) {
echo "Column: " . $stat->getColumnName() . " | Sensitive: " . ($stat->getSensitive() ? 'Yes' : 'No') . "\n";
}
Mask sensitive data in text:
$deidResponse = $client->deidentifyContent((new \Google\Cloud\Dlp\V2\DeidentifyContentRequest())
->setItem((new ContentItem())->setData('SSN: 123-45-6789'))
->setDeidentifyConfig((new \Google\Cloud\Dlp\V2\DeidentifyConfig())
->setInfoTypeTransformations([
(new \Google\Cloud\Dlp\V2\InfoTypeTransformation())
->setInfoTypes(['US_SOCIAL_SECURITY_NUMBER'])
->setPrimitiveTransformation((new \Google\Cloud\Dlp\V2\PrimitiveTransformation())
->setReplaceWithInfoTypeConfig((new \Google\Cloud\Dlp\V2\ReplaceWithInfoTypeConfig())
->setNewInfoType('OTHER')
)
)
])
)
);
echo "Redacted: " . $deidResponse->getItem()->getData() . "\n";
Use GcsContent or BigQueryTable for source data:
$gcsConfig = (new \Google\Cloud\Dlp\V2\GcsContent())
->setFileUri('gs://your-bucket/large-file.json');
$request = (new InspectContentRequest())
->setItem((new ContentItem())->setData($gcsConfig))
->setInspectConfig(...);
Service Provider Binding:
Bind the client in AppServiceProvider:
public function register()
{
$this->app->singleton(DlpServiceClient::class, function () {
return new DlpServiceClient();
});
}
Facade for Cleaner Syntax:
Create a facade (app/Facades/Dlp.php):
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
class Dlp extends Facade
{
protected static function getFacadeAccessor()
{
return DlpServiceClient::class;
}
}
Usage:
$findings = \App\Facades\Dlp::inspectContent($request);
Job Queues for Async Tasks:
Dispatch DLP jobs to queues (e.g., InspectJob creation):
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class DlpInspectJob implements ShouldQueue
{
use Queueable;
public function handle()
{
$client = app(DlpServiceClient::class);
$client->createInspectJob($this->request);
}
}
Caching Responses:
Cache getColumnDataProfile results (e.g., for frequent table scans):
$profile = Cache::remember("dlp_profile_{$tableId}", now()->addHours(1), function () use ($client, $tableId) {
return $client->getColumnDataProfile(...);
});
Authentication Issues:
GOOGLE_APPLICATION_CREDENTIALS with incorrect permissions (e.g., missing dlp.user role).roles/dlp.user for scanning.roles/dlp.admin for managing jobs/profiles.google-cloud-auth for debugging:
use Google\Auth\Credentials\Credentials;
$credentials = Credentials::fromStream('/path/to/key.json');
$client = new DlpServiceClient(['credentials' => $credentials]);
Quotas and Limits:
inspectContent).createInspectJob for large datasets or request quota increases.gRPC vs REST:
pecl install grpc
Data Size Limits:
inspectContent has a 10MB payload limit.createInspectJobHow can I help you explore Laravel packages today?