Skip to content

Added key rotation check #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added key rotation check
  • Loading branch information
Matteo Di Ninno committed Apr 18, 2024
commit 0f802cd73fa786f9ca5bed5edfb655cacdae062c
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ AWS_SECRET_ACCESS_KEY
```
[https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html](https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html)

### Key Rotation
If key rotation is enabled, the most recent next rotation date is cached and if it's in the past we force getting the secrets.

### Testing

``` bash
Expand Down
11 changes: 11 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@

'cache-store' => 'file',

/*
|--------------------------------------------------------------------------
| Key rotation
|--------------------------------------------------------------------------
|
| If key rotation is enabled, force retrieving config if NextRotationDate is in the past
|
*/

'key-rotation' => env('AWS_SECRETS_KEY_ROTATION', false),

/*
|--------------------------------------------------------------------------
| Debugging
Expand Down
28 changes: 28 additions & 0 deletions src/LaravelAwsSecretsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tapp\LaravelAwsSecretsManager;

use Aws\Api\DateTimeResult;
use Aws\SecretsManager\SecretsManagerClient;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

Expand Down Expand Up @@ -33,6 +35,8 @@ public function __construct()
$this->enabledEnvironments = config('aws-secrets-manager.enabled-environments', []);

$this->debug = config('aws-secrets-manager.debug', false);

$this->keyRotation = config('aws-secrets-manager.key-rotation');
}

public function loadSecrets()
Expand Down Expand Up @@ -61,6 +65,15 @@ public function loadSecrets()

protected function checkCache()
{
if ($this->keyRotation) {
$cachedNextRotationDate = Cache::store($this->cacheStore)->get('AWSSecretsNextRotationDate');
if (
blank($cachedNextRotationDate) ||
$cachedNextRotationDate < Carbon::now()
)
return false;
}

foreach ($this->configVariables as $variable => $configPath) {
$val = Cache::store($this->cacheStore)->get($variable);

Expand Down Expand Up @@ -101,6 +114,10 @@ protected function getVariables()
return;
}

if ($this->keyRotation) {
$nextRotationDateToCache = null;
}

foreach ($secrets['SecretList'] as $secret) {
if (isset($secret['ARN'])) {
$result = $this->client->getSecretValue([
Expand All @@ -110,6 +127,13 @@ protected function getVariables()
$secretValues = json_decode($result['SecretString'], true);

if (is_array($secretValues) && count($secretValues) > 0) {
if ($this->keyRotation) {
$nextRotationDate = Carbon::instance($secret['NextRotationDate']);
if ($nextRotationDate < $nextRotationDateToCache) {
$nextRotationDateToCache = $nextRotationDate;
}
}

if (isset($secretValues['name']) && isset($secretValues['value'])) {
$key = $secretValues['name'];
$secret = $secretValues['value'];
Expand All @@ -124,6 +148,10 @@ protected function getVariables()
}
}
}

if ($this->keyRotation) {
$this->storeToCache("AWSSecretsNextRotationDate", $nextRotationDateToCache);
}
}

protected function updateConfigs()
Expand Down