|
| 1 | +# Secretary - Secrets Manager for PHP |
| 2 | + |
| 3 | +Secrets are an important aspect of most applications you can build. How you store them, and keep them "secret" is a challenge. |
| 4 | +Luckily, there are tools you can use to keep them all safe. |
| 5 | + |
| 6 | +Secretary is a tool to integrate your PHP application with these tools. |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +1. [Installation](#installation) |
| 11 | +2. [Api Documentation](#api-documentation) |
| 12 | + 1. [Initializing](#constructor) |
| 13 | + 1. [getSecret](#getSecret) |
| 14 | + 1. [putSecret](#putSecret) |
| 15 | + 1. [deleteSecret](#deleteSecret) |
| 16 | + |
| 17 | +### Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +$ composer require secretary/core |
| 21 | +``` |
| 22 | + |
| 23 | +By itself, the core is useless. You will also need to add at least one adapter: |
| 24 | + |
| 25 | +| Storage Engine | Badges | |
| 26 | +| -------------- | -------- | |
| 27 | +| [AWS Secrets Manager][aws-secrets-manager-adapter] | [](https://packagist.org/packages/secretary/php-aws-secrets-manager-adapter) [](https://packagist.org/packages/secretary/php-aws-secrets-manager-adapter) | |
| 28 | +| [HashiCorp Vault][hashicorp-vault-adapter] | [](https://packagist.org/packages/secretary/php-hashicorp-vault-adapter) [](https://packagist.org/packages/secretary/php-hashicorp-vault-adapter) | |
| 29 | + |
| 30 | +There are also miscellaneous packages that add on to Secretary |
| 31 | + |
| 32 | +| Package | Purpose | Badges | |
| 33 | +| ------- | ------- | ------ | |
| 34 | +| [PSR-6 Cache Adapter][psr-6-cache-adapter] | Allows for caching secrets using a PSR-6 Cache Interface | [](https://packagist.org/packages/secretary/php-psr-6-cache-adapter) [](https://packagist.org/packages/secretary/php-psr-6-cache-adapter) | |
| 35 | +| [PSR-16 Cache Adapter][psr-16-cache-adapter] | Allows for caching secrets using a PSR-16 Cache Interface | [](https://packagist.org/packages/secretary/php-psr-16-cache-adapter) [](https://packagist.org/packages/secretary/php-psr-16-cache-adapter) | |
| 36 | +| [Secretary Bundle][secretary-bundle] | Allows for integrating with the Symfony Framework | [](https://packagist.org/packages/secretary/php-secretary-bundle) [](https://packagist.org/packages/secretary/php-secretary-bundle) | |
| 37 | + |
| 38 | +### Api Documentation |
| 39 | + |
| 40 | +There's only one class you interface with in Secretary: [`Secretary\Manager`][Secretary\Manager::class] |
| 41 | + |
| 42 | +<a name="constructor" /> |
| 43 | + |
| 44 | +##### Secretary\Manager->__construct(AdapterInterface $adapter) |
| 45 | + |
| 46 | +Pass in your desired adapter. |
| 47 | + |
| 48 | +```php |
| 49 | +<?php |
| 50 | +use Secretary\Manager; |
| 51 | +use Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter; |
| 52 | + |
| 53 | +$manager = new Manager( |
| 54 | + new AWSSecretsManagerAdapter([ |
| 55 | + 'region' => 'us-east-1', |
| 56 | + 'credentials' => [ |
| 57 | + 'accessKeyId' => 'myAccessKeyId', |
| 58 | + 'secretAccessKey' => 'mySecretAccessKey' |
| 59 | + ] |
| 60 | + ]) |
| 61 | +); |
| 62 | +``` |
| 63 | + |
| 64 | +Optionally, you may wrap your adapter, with one of the two cache adapters. |
| 65 | + |
| 66 | +```php |
| 67 | +<?php |
| 68 | +use Secretary\Manager; |
| 69 | +use Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter; |
| 70 | + |
| 71 | +use Secretary\Adapter\Cache\PSR6CacheAdapter; |
| 72 | +use Cache\Adapter\Apc\ApcCachePool; |
| 73 | + |
| 74 | +$manager = new Manager( |
| 75 | + new PSR6CacheAdapter( |
| 76 | + new AWSSecretsManagerAdapter([ |
| 77 | + 'region' => 'us-east-1', |
| 78 | + 'credentials' => [ |
| 79 | + 'accessKeyId' => 'myAccessKeyId', |
| 80 | + 'secretAccessKey' => 'mySecretAccessKey' |
| 81 | + ] |
| 82 | + ]), |
| 83 | + new ApcCachePool() |
| 84 | + ) |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | +For mor information on the arguments and options for the adapters, view their respective documentation. |
| 89 | + |
| 90 | +<a name="getSecret" /> |
| 91 | + |
| 92 | +##### Secretary\Manager->getSecret(string $key, ?array $options): Secret |
| 93 | + |
| 94 | +Fetches a secret from the configured adapter. `$key` is the name of the secret (or path) you are trying to get. |
| 95 | + |
| 96 | +Certain adapters will take custom options as well, like VersionId and VersionStage for the AWS SecretsManager Adapter |
| 97 | + |
| 98 | +This will throw a `Secretary\SecretNotFoundException` if the secret cannot be found |
| 99 | + |
| 100 | +```php |
| 101 | +$secret = $manager->getSecret('databases/redis/dsn'); |
| 102 | +/* |
| 103 | +Secret { |
| 104 | + "path" = "databases/redis/dsn", |
| 105 | + "value" = "redis://localhost:6379" |
| 106 | +} |
| 107 | +*/ |
| 108 | +``` |
| 109 | + |
| 110 | +Some adapters also support storing a key/value map as a secret's value. |
| 111 | + |
| 112 | +```php |
| 113 | +$secret = $manager->getSecret('databases/redis'); |
| 114 | +/* |
| 115 | +Secret { |
| 116 | + "path" = "databases/redis", |
| 117 | + "value" = [ |
| 118 | + "dsn" => "redis://localhost:6379", |
| 119 | + "password" => "my_super_strong_password" |
| 120 | + ] |
| 121 | +} |
| 122 | +*/ |
| 123 | +``` |
| 124 | + |
| 125 | +<a name="putSecret" /> |
| 126 | + |
| 127 | +##### Secretary\Manager->putSecret(string $key, string|array $value, ?array $options): void |
| 128 | + |
| 129 | +Puts a secret with the given `$value`, into the storage engine, under the given `$key`. |
| 130 | + |
| 131 | +If the current adapter doesn't support arrays, and you pass one it, it will throw a `Secretary\ValueNotSupportedException`. |
| 132 | + |
| 133 | +Again, some adapters allow passing in custom options to send along with the request. |
| 134 | + |
| 135 | +```php |
| 136 | +$manager->putSecret('database/redis', 'postgres://localhost:5432'); |
| 137 | +``` |
| 138 | + |
| 139 | +And for adapters that support a key/value map as a value: |
| 140 | + |
| 141 | +```php |
| 142 | +$manager->putSecret('database/redis', ['dsn' => 'redis://localhost:6379', 'password' => 'my_super_strong_password']); |
| 143 | +``` |
| 144 | + |
| 145 | +<a name="deleteSecret" /> |
| 146 | + |
| 147 | +##### Secretary\Manager->deleteSecret(string $key, ?array $options): void |
| 148 | + |
| 149 | +Deletes a secret from the storage engine using the given `$key`. |
| 150 | + |
| 151 | +Again, some adapters allow passing in custom options to send along with the request. |
| 152 | + |
| 153 | +```php |
| 154 | +$manager->deleteSecret('database/redis'); |
| 155 | +``` |
| 156 | + |
| 157 | +##### Secretary\Manager->getAdapter(): AdapterInterface |
| 158 | + |
| 159 | +Will return the adapter that was passed to this manager during construction. |
| 160 | + |
| 161 | +aws-secrets-manager-adapter: https://github.com/secretary/php-aws-secrets-manager-adapter |
| 162 | +hashicorp-vault-adapter: https://github.com/secretary/php-hashicorp-vault-adapter |
| 163 | +psr-6-cache-adapter: https://github.com/secretary/php-psr-6-cache-adapter |
| 164 | +psr-16-cache-adapter: https://github.com/secretary/php-psr-16-cache-adapter |
| 165 | +secretary-bundle: https://github.com/secretary/php-secretary-bundle |
| 166 | +Secretary\Manager::class: https://github.com/secretary/php/blob/master/src/Core/src/Manager.php |
0 commit comments