$ composer require stefna/secrets-manager-core
The core only provides the basic functionality and some basic inmemory providers.
We provide a couple of providers that can be installed separately
Provider | Badges |
---|---|
JSON File | Coming soon |
Psr-6 | Coming soon |
Aws Secrets Manager | Coming soon |
Aws Parameter Store | Coming soon |
Pass in your desired provider.
<?php
use Stefna\SecretsManager\Manager;
use Stefna\SecretsManager\Provider\ArrayProvider;
$manager = new Manager(
ArrayProvider::fromArray([
'key' => 'value',
'key2' => new \Stefna\SecretsManager\Values\Secret('key2', ['mixed' => 'value'], ['stage' => 'dev'])
])
);
You can also chain providers
<?php
use Stefna\SecretsManager\Manager;
use Stefna\SecretsManager\Provider\ArrayProvider;
use Stefna\SecretsManager\Provider\ChainProvider;
use Stefna\SecretsManager\Provider\JsonProvider\JsonProvider;
$defaultSecretsProvider = ArrayProvider::fromArray([
'key' => 'value',
'key2' => new \Stefna\SecretsManager\Values\Secret('key2', ['mixed' => 'value'], ['stage' => 'dev'])
]);
$manager = new Manager(
new ChainProvider(
new JsonProvider('secrets.json'),
$defaultSecretsProvider // if secrets are missing in JsonProvider will fallback to defaultProvider
)
);
Fetches a secret from the configured provider, $key
is the name of the secret (or path) you are trying to get.
This will throw a Stefna\SecretsManager\SecretNotFoundException
if secret is not found
use Stefna\SecretsManager\Manager;
use Stefna\SecretsManager\Provider\ArrayProvider;
$manager = new Manager(ArrayProvider::fromArray([
'databases/redis/dsn' => 'redis://localhost:6379',
]));
$secret = $manager->getSecret('databases/redis/dsn');
$secret->getValue() === 'redis://localhost:6379';
Puts a secret with the given $value
, into the storage engine, under the given $key
.
If the current adapter doesn't support arrays, and you pass one it, it will throw a Stefna\SecretsManager\ValueNotSupportedException
.
Again, some adapters allow passing in custom options to send along with the request.
$manager->putSecret('database/redis', 'postgres://localhost:5432');
And for adapters that support a key/value map as a value:
$manager->putSecret('database/redis', ['dsn' => 'redis://localhost:6379', 'password' => 'my_super_strong_password']);
Deletes a secret from the provider using the given $secret
.
use Stefna\SecretsManager\Manager;
use Stefna\SecretsManager\Provider\ArrayProvider;
$manager = new Manager(ArrayProvider::fromArray([
'databases/redis/dsn' => 'redis://localhost:6379',
]));
$secret = $manager->getSecret('databases/redis/dsn');
$manager->deleteSecret($secret);
Will return provider currently in use
Secrets are immutable and will throw exception if you try to modify it.
The class implements ArrayAccess to allow ease of reading secrets stored in assoc array.
Returns the key for the secret
use Stefna\SecretsManager\Manager;
$manager = new Manager($provider);
$secret = $manager->getSecret('database/redis');
$secret->getKey() === 'database/redis';
Returns the value for the secret. If the secret is a key/value map it can be used as an array
use Stefna\SecretsManager\Manager;
use Stefna\SecretsManager\Provider\ArrayProvider;
$manager = new Manager(ArrayProvider::fromArray([
'databases/redis/dsn' => 'redis://localhost:6379',
]));
$secret = $manager->getSecret('dabase/redis/dsn');
$secret->getValue() === 'redis://localhost:6379';
Array like access
use Stefna\SecretsManager\Manager;
use Stefna\SecretsManager\Provider\ArrayProvider;
$manager = new Manager(ArrayProvider::fromArray([
'database' => new \Stefna\SecretsManager\Values\Secret('database', [
'user' => 'test',
'name' => 'testDb',
]),
]));
$secret = $manager->getSecret('database');
$secret['user'] === 'test';