|
1 | 1 | # Vault |
2 | 2 |
|
3 | | -The Vault component securely stores secrets using pluggable storage backends and encryption with key rotation and support for additional authenticated data (AAD). |
| 3 | +The Vault component securely stores secrets using pluggable storage backends and encryption with key rotation, versioning, and additional authenticated data (AAD). |
4 | 4 |
|
5 | | -## Basic Usage |
| 5 | +## Setup |
6 | 6 |
|
7 | 7 | ```php |
8 | 8 | use SonsOfPHP\Component\Vault\Cipher\OpenSSLCipher; |
9 | 9 | use SonsOfPHP\Component\Vault\KeyRing\InMemoryKeyRing; |
| 10 | +use SonsOfPHP\Component\Vault\Marshaller\JsonMarshaller; |
10 | 11 | use SonsOfPHP\Component\Vault\Storage\InMemoryStorage; |
11 | 12 | use SonsOfPHP\Component\Vault\Vault; |
12 | 13 |
|
13 | | -$keyRing = new InMemoryKeyRing(['v1' => '32_byte_master_key_example!!'], 'v1'); |
14 | | -$vault = new Vault(new InMemoryStorage(), new OpenSSLCipher(), $keyRing); |
15 | | -$vault->set('db_password', 'secret', ['app']); |
16 | | -$secret = $vault->get('db_password', ['app']); |
| 14 | +$storage = new InMemoryStorage(); |
| 15 | +$cipher = new OpenSSLCipher(); |
| 16 | +$keyRing = new InMemoryKeyRing(['v1' => '32_byte_master_key_example!!'], 'v1'); |
| 17 | +$marshaller = new JsonMarshaller(); |
| 18 | +$vault = new Vault($storage, $cipher, $keyRing, $marshaller); |
| 19 | +``` |
| 20 | + |
| 21 | +## Storing and Retrieving Secrets |
| 22 | + |
| 23 | +```php |
| 24 | +$vault->set('db_password', 'secret'); |
| 25 | +$secret = $vault->get('db_password'); |
| 26 | +``` |
| 27 | + |
| 28 | +## Using Additional Authenticated Data |
17 | 29 |
|
18 | | -// Store a new version of the secret |
19 | | -$vault->set('db_password', 'new-secret', ['app']); |
20 | | -$oldSecret = $vault->get('db_password', ['app'], 1); // retrieve version 1 |
| 30 | +```php |
| 31 | +$vault->set('token', 'secret', ['app']); |
| 32 | +$secret = $vault->get('token', ['app']); |
| 33 | +``` |
| 34 | + |
| 35 | +## Versioned Secrets |
| 36 | + |
| 37 | +```php |
| 38 | +$vault->set('db_password', 'old-secret'); |
| 39 | +$vault->set('db_password', 'new-secret'); |
21 | 40 |
|
22 | | -// Rotate the master key |
| 41 | +// Retrieve specific version |
| 42 | +$old = $vault->get('db_password', [], 1); |
| 43 | +``` |
| 44 | + |
| 45 | +## Rotating Keys |
| 46 | + |
| 47 | +```php |
23 | 48 | $vault->rotateKey('v2', 'another_32_byte_master_key!!'); |
| 49 | +``` |
24 | 50 |
|
25 | | -// Store non-string data |
| 51 | +Secrets encrypted with previous keys remain accessible because the key ring keeps old keys. |
| 52 | + |
| 53 | +## Storing Non-String Data |
| 54 | + |
| 55 | +```php |
26 | 56 | $vault->set('config', ['user' => 'root']); |
27 | 57 | $config = $vault->get('config'); |
28 | 58 | ``` |
| 59 | + |
| 60 | +## Custom Marshallers |
| 61 | + |
| 62 | +Vault uses a marshaller to convert secrets to strings before encryption. The default `JsonMarshaller` handles arrays and scalars, but you can provide your own implementation of `MarshallerInterface` for advanced use cases. |
0 commit comments