Installation:
composer require bentools/shh-bundle
Add to config/bundles.php:
return [
// ...
Bentools\ShhBundle\ShhBundle::class => ['all' => true],
];
Configuration:
Define secrets in config/packages/shh.yaml:
shh:
secrets:
- name: "DB_PASSWORD"
path: "%kernel.project_dir%/var/secrets/db_password"
permissions: 0600
First Use Case:
Inject Shh\SecretManager and retrieve a secret:
use Bentools\ShhBundle\SecretManager;
public function __construct(private SecretManager $secretManager) {}
public function getDbPassword()
{
return $this->secretManager->get('DB_PASSWORD');
}
Secret Retrieval:
SecretManager in services/controllers.$this->secretManager->get('SECRET_KEY', true); // Force refresh
Environment Integration:
.env if file-based secret is missing:
shh:
fallback_to_env: true
Dynamic Secrets:
# Update secret file (permissions 600)
echo "new_password" > var/secrets/db_password
Validation:
$this->secretManager->get('API_KEY', validate: true);
Secret Generation:
$this->secretManager->generate('NEW_SECRET', 32); // 32-character random string
Encrypted Secrets:
Use Shh\EncryptedSecretManager for encrypted storage (requires defuse/php-encryption):
shh:
encryption_key: "%env(ENCRYPTION_KEY)%"
Custom Storage:
Implement SecretStorageInterface for databases/cloud storage:
class MyStorage implements SecretStorageInterface {
public function read(string $name): string { ... }
public function write(string $name, string $value): void { ... }
}
Permissions:
0600 permissions. Use:
chmod 600 var/secrets/*
var/log/shh.log for permission errors.Caching:
php bin/console cache:clear
Environment Fallback:
fallback_to_env: true, ensure .env secrets are not committed to version control.Symfony Debug Toolbar:
phpinfo() or logs, but avoid logging them explicitly:
// ❌ Avoid
error_log("Secret: " . $this->secretManager->get('DB_PASSWORD'));
// ✅ Safe
error_log("Database connected.");
php bin/console debug:shh
shh.log_level: debug in config for detailed logs.Custom Secret Formats:
Override Shh\SecretManager to support JSON/YAML secrets:
class JsonSecretManager extends SecretManager {
protected function decode(string $content): string {
return json_decode($content, true)['secret'];
}
}
Secret Events: Listen for secret updates:
use Bentools\ShhBundle\Event\SecretUpdatedEvent;
public function onSecretUpdated(SecretUpdatedEvent $event) {
// Log or trigger actions
}
Register in services.yaml:
services:
App\Listener\SecretListener:
tags:
- { name: kernel.event_listener, event: shh.secret_updated, method: onSecretUpdated }
Multi-Environment Secrets:
Use %env(default::SECRET_NAME)% in shh.yaml for environment-specific defaults.
Path Resolution:
Use %kernel.project_dir% for relative paths (e.g., var/secrets/).
Absolute paths are supported but less portable.
Secret Naming:
Avoid names conflicting with Symfony’s %env() variables (e.g., APP_ENV).
How can I help you explore Laravel packages today?