Developer
Architecture overview
nr-vault follows clean architecture principles with these main components:
- Service layer
Vault- Main facade for all vault operations.Service - Crypto layer
Encryption- Envelope encryption implementation.Service Master- Master key retrieval abstraction.Key Provider - Storage layer
Secret- Database persistence.Repository Vault- Storage backend abstraction.Adapter Interface - Security layer
Access- Permission checks.Control Service Audit- Operation logging.Log Service
Extending nr-vault
Custom storage adapters
Note
nr-vault currently includes only the local database adapter. External vault adapters (HashiCorp Vault, AWS Secrets Manager, Azure Key Vault) are planned for future releases. The adapter architecture below allows you to implement your own custom adapters in the meantime.
Implement
Vault to add new storage backends:
namespace MyVendor\MyExtension\Adapter;
use Netresearch\NrVault\Adapter\VaultAdapterInterface;
use Netresearch\NrVault\Domain\Model\Secret;
final class CustomAdapter implements VaultAdapterInterface
{
public function getIdentifier(): string
{
return 'custom';
}
public function isAvailable(): bool
{
// Check if your backend is configured and reachable
}
public function store(Secret $secret): void
{
// Store secret in your backend
}
public function retrieve(string $identifier): ?Secret
{
// Retrieve secret from your backend
}
public function delete(string $identifier): void
{
// Delete from your backend
}
public function exists(string $identifier): bool
{
// Check if secret exists
}
public function list(array $filters = []): array
{
// List secret identifiers
}
public function getMetadata(string $identifier): ?array
{
// Get secret metadata
}
public function updateMetadata(string $identifier, array $metadata): void
{
// Update metadata
}
}
Register in Services.:
MyVendor\MyExtension\Adapter\CustomAdapter:
tags:
- name: nr_vault.adapter
identifier: custom
Custom master key providers
Note
nr-vault includes three built-in master key providers: typo3 (derives from TYPO3's encryption key), file (reads from filesystem), and env (reads from environment variable). The example below shows how to implement a custom provider for enterprise key management systems like HashiCorp Vault Transit or AWS KMS.
Implement
Master for custom key sources:
namespace MyVendor\MyExtension\Crypto;
use Netresearch\NrVault\Crypto\MasterKeyProviderInterface;
final class VaultKeyProvider implements MasterKeyProviderInterface
{
public function getIdentifier(): string
{
return 'hashicorp';
}
public function isAvailable(): bool
{
// Check if HashiCorp Vault is accessible
}
public function getMasterKey(): string
{
// Retrieve key from HashiCorp Vault
}
public function storeMasterKey(string $key): void
{
// Store key in HashiCorp Vault
}
public function generateMasterKey(): string
{
return random_bytes(32);
}
}
Events
nr-vault dispatches PSR-14 events for extensibility:
- SecretAccessedEvent
- Dispatched when a secret is read.
- SecretCreatedEvent
- Dispatched when a new secret is created.
- SecretRotatedEvent
- Dispatched when a secret is rotated with a new value.
- SecretUpdatedEvent
- Dispatched when a secret value is updated (without rotation).
- SecretDeletedEvent
- Dispatched when a secret is deleted.
- MasterKeyRotatedEvent
- Dispatched after master key rotation completes.
Example listener:
namespace MyVendor\MyExtension\EventListener;
use Netresearch\NrVault\Event\SecretAccessedEvent;
final class SecretAccessLogger
{
public function __invoke(SecretAccessedEvent $event): void
{
// Custom logging or alerting
$identifier = $event->getIdentifier();
$actorUid = $event->getActorUid();
}
}
Testing
Development setup
Use DDEV for local development:
ddev start
ddev install-v14
ddev vault-init
Running tests
# All tests
ddev exec composer test
# Unit tests only
ddev exec .Build/bin/phpunit -c Tests/Build/phpunit.xml --testsuite Unit
# With coverage
ddev exec .Build/bin/phpunit -c Tests/Build/phpunit.xml --coverage-html .Build/coverage
Code quality
# PHP-CS-Fixer
ddev exec composer fix
# PHPStan
ddev exec composer stan
Contributing
See CONTRIBUTING. for contribution guidelines.
- Fork the repository.
- Create a feature branch.
- Write tests for your changes.
- Ensure all tests pass.
- Submit a pull request.
API reference
VaultService
- class VaultService
-
- Fully qualified name
-
\Netresearch\\
Nr Vault\\ Service\ Vault Service
Main facade for vault operations.
- rotate ( string $identifier, string $newSecret, string $reason = '') : void
-
Rotate a secret with a new value.