google/cloud-secret-manager
Idiomatic PHP client for Google Cloud Secret Manager. Install via Composer, authenticate with Google Cloud credentials, then use SecretManagerServiceClient to create, access, and manage secrets over REST or gRPC with robust error handling.
.env files or local storage. It aligns with Laravel’s dependency injection (DI) container and service providers, enabling runtime secret resolution.env:production, tenant:acme) to dynamically fetch configurations for staging, production, or tenant-specific Laravel apps without redeployment. This is ideal for SaaS platforms or multi-tenant architectures.config('services.db.password') → Secret Manager fetch). Example:
$this->app->bind('secret-manager', function () {
return new \Google\Cloud\SecretManager\V1\SecretManagerServiceClient();
});
.env with a custom SecretManagerEnvProvider that fetches values at runtime:
$secret = $secretManager->accessSecretVersion(['name' => $secretPath]);
putenv("DB_PASSWORD={$secret->payload->data}");
config() helper to fetch secrets on-demand:
config(['services.stripe.key' => $secretManager->getSecret('stripe-key')]);
cache()->remember('db_password', 3600, fn() => $secretManager->getSecret('db-password'))).| Risk Area | Mitigation Strategy |
|---|---|
| Authentication Setup | Use GCP Workload Identity Federation or service accounts to avoid hardcoding credentials. Document IAM roles (e.g., roles/secretmanager.secretAccessor) for Laravel deployments. |
| Latency for Secrets | Cache secrets in Redis with TTL-based invalidation to avoid repeated API calls. Monitor cache miss rates. |
| Breaking Changes | The package is GA (v2.x), but Laravel integrations should use dependency injection to isolate changes. Test with PHP 8.1+. |
| gRPC vs. REST | Default to REST for simplicity; enable gRPC only for high-throughput microservices. |
| Secret Rotation | Use Laravel Task Scheduler or Cloud Scheduler to trigger rotations via the package’s updateSecret method. |
| Error Handling | Wrap API calls in try-catch blocks and log ApiException (e.g., ex->getMessage()). Integrate with Laravel’s Monolog. |
| Multi-Region Access | Configure Global Load Balancer or Multi-Region Endpoints for low-latency access. |
roles/secretmanager.secretAccessor) sufficient, or do we need custom roles?env:prod, tenant:acme).env files?.env usage in favor of Secret Manager?| Laravel Component | Integration Strategy |
|---|---|
| Configuration | Replace .env with runtime secret fetching via config() helper or DI container. |
| Database | Inject secrets (e.g., DB_PASSWORD) via Service Provider or Eloquent events. |
| API Clients | Use Laravel’s HTTP client to fetch secrets before making external calls. |
| Queue Workers | Inject secrets via Laravel’s app() helper or environment variables. |
| CI/CD Pipelines | Use GCP Workload Identity or temporary credentials in GitHub Actions/GitLab CI. |
| Logging | Integrate Monolog with Cloud Audit Logs for compliance tracking. |
| Caching | Cache secrets in Redis with TTL-based invalidation (e.g., 5-minute cache). |
| Service Providers | Bind SecretManagerServiceClient to Laravel’s DI container for reuse. |
| Task Scheduling | Use Laravel Task Scheduler to trigger secret rotations. |
| Testing | Mock SecretManagerServiceClient in PHPUnit for unit tests. |
Phase 1: Pilot Project
.env with runtime secret injection for 2–3 critical secrets (e.g., DB_PASSWORD, STRIPE_KEY).Phase 2: CI/CD Integration
Phase 3: Full Rollout
.env usage in favor of runtime injection.Phase 4: Optimization
ApiException).| Compatibility Factor | Details |
|---|---|
| PHP Version | Supports PHP 8.1+ (Laravel 9+). Tested with PHP 8.4. |
| Laravel Version | Compatible with Laravel 9/10 (uses Composer autoloading). |
| GCP Services | Works with Cloud Run, GKE, App Engine, Compute Engine. |
| Authentication | Supports service accounts, Workload Identity, and application default credentials. |
| Protocol | Defaults to REST; supports gRPC for high performance. |
| Caching | Integrates with Redis, Memcached via Laravel Cache. |
| IAM Roles | Requires roles/secretmanager.secretAccessor (or custom roles). |
| Multi-Region |
How can I help you explore Laravel packages today?