csharpru/vault-php
PHP client for HashiCorp Vault with a simple API for reading/writing secrets, authentication, and managing Vault endpoints. Works well in Laravel or any PHP app for integrating secure secret storage and retrieval.
Installation
composer require csharpru/vault-php
Add to config/services.php:
'vault' => [
'url' => env('VAULT_ADDR', 'http://127.0.0.1:8200'),
'token' => env('VAULT_TOKEN'),
'timeout' => 5.0,
],
First Use Case: Reading a Secret
use Csharpru\Vault\Client;
$client = new Client(config('services.vault'));
$secret = $client->read('secret/data/myapp/config');
echo $secret['data']['data']['api_key'];
Key Files
Dynamic Secret Rotation
// Fetch and auto-rotate DB credentials
$client->read('secret/data/db/creds');
// Later, trigger rotation via KV v2 API
$client->write('transit/rotate/db-key', ['key' => 'db-key']);
Policy-Based Access
// Assign a policy to a token
$client->write('auth/token/create', [
'policies' => ['app-policy'],
'ttl' => '1h',
]);
Environment-Specific Config
// Load config per environment (dev/staging/prod)
$env = app()->environment();
$config = $client->read("secret/data/app/config/{$env}");
Laravel Service Provider Bind the client to the container for dependency injection:
$this->app->singleton(Client::class, function ($app) {
return new Client($app['config']['services.vault']);
});
Caching Secrets Use Laravel’s cache to avoid repeated Vault calls:
$secret = Cache::remember('vault_db_creds', now()->addHours(1), function () {
return $client->read('secret/data/db/creds');
});
Error Handling Wrap Vault calls in a try-catch for HTTP/token errors:
try {
$client->read('secret/nonexistent');
} catch (\Csharpru\Vault\Exception\VaultException $e) {
Log::error("Vault error: " . $e->getMessage());
abort(500);
}
Token Management
VAULT_TOKEN env vars or dynamic token generation via auth/token/create.KV v1 vs. v2
secret/data/...).vault secrets enable -path=secret -version=2 kv
Rate Limiting
TLS/HTTPS
$client = new Client([
'url' => 'https://vault.example.com',
'ca_cert' => file_get_contents('/path/to/ca.crt'),
]);
Enable Debugging
Set the debug option to log HTTP requests:
$client = new Client([
'url' => env('VAULT_ADDR'),
'debug' => true, // Logs requests to storage/logs/vault.log
]);
Common Errors
| Error | Cause | Solution |
|---|---|---|
InvalidResponseException |
Malformed Vault response | Check Vault server logs |
ConnectionException |
Network issues | Verify VAULT_ADDR and firewall rules |
PermissionDeniedException |
Insufficient token permissions | Reassign policies via auth/token/roles |
Custom Middleware Add request/response filters:
$client->setMiddleware(function ($request) {
$request->setHeader('X-Custom-Header', 'value');
});
Mocking for Tests
Use the MockClient for unit tests:
$mock = new \Csharpru\Vault\MockClient();
$mock->setResponse('secret/data/test', ['data' => ['key' => 'value']]);
Async Operations For long-running jobs (e.g., key rotation), use Laravel Queues:
dispatch(new RotateVaultKey($client, 'transit/keys/db-key'));
How can I help you explore Laravel packages today?