A Laravel package to retrieve key management from AWS Secrets Manager
Communication via AWS Secrets Manager
may incur unnecessary charges.
So we developed a package that simply caches.
Installation
You can install the package via composer:
composer require getsolaris/laravel-aws-secretsmanager
You can publish the config file with:
php artisan vendor:publish --provider="Getsolaris\LaravelAwsSecretsManager\AwsSecretsManagerServiceProvider" --tag="config"
Usage
You can choose cache driver and cache ttl
default cache driver is filesystem
(storage/framework/cache/data
)
# .env
CACHE_DRIVER=redis
CACHE_TTL=86400
# aws configuration
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
Required permissions: secretsmanager:GetSecretValue
If the secret is encrypted using a customer-managed key instead of the AWS managed key aws/secretsmanager
Example
createSecret
namespace App\Services;
use Getsolaris\LaravelAwsSecretsManager\AwsSecretsManager;
class FacebookApiService extends Service
{
protected AwsSecretsManager $client;
public function __construct()
{
$this->client = new AwsSecretsManager();
}
/**
* @param string $secretId
* @return array
* @throws \Exception
*/
public function createFacebookSecret(): \Aws\Result
{
$appId = env('FACEBOOK_APP_ID', 'test_app_id_123');
$appSecret = env('FACEBOOK_APP_SECRET', 'test_app_secret_123');
$createSecret = new CreateSecretDto(
Name: 'prod/facebook/secret',
SecretString: [
'app_id' => $appId,
'app_secret' => $appSecret,
],
);
$createSecret = new CreateSecretDto([
'Name' => 'prod/facebook/secret',
'SecretString' => [
'app_id' => $appId,
'app_secret' => $appSecret,
],
]);
return $this->client->createSecret($createSecret);
}
}
getSecret
namespace App\Services;
use Getsolaris\LaravelAwsSecretsManager\AwsSecretsManager;
class FacebookApiService extends Service
{
protected AwsSecretsManager $client;
public function __construct()
{
$this->client = new AwsSecretsManager();
}
/**
* @param string $secretId
* @return array
* @throws \Exception
*/
public function getFacebookSecret(): \Aws\Result
{
return $this->client->getSecret('prod/facebook/secret');
}
}
getSecretValue
namespace App\Services;
use Getsolaris\LaravelAwsSecretsManager\AwsSecretsManager;
class FacebookApiService extends Service
{
protected AwsSecretsManager $client;
public function __construct()
{
$this->client = new AwsSecretsManager();
}
/**
* @param string $secretId
* @return array
* @throws \Exception
*/
public function getFacebookSecretValue(): array
{
return $this->client->getSecretValue('prod/facebook/secret');
}
}
Resource
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.