Skip to content

Commit 76ae78c

Browse files
authored
Added key rotation check (TappNetwork#26)
* Added key rotation check * Fix Style
1 parent 142da8f commit 76ae78c

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ AWS_SECRET_ACCESS_KEY
5656
```
5757
[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)
5858

59+
### Key Rotation
60+
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.
61+
5962
### Testing
6063

6164
``` bash

config/config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@
8888

8989
'cache-store' => 'file',
9090

91+
/*
92+
|--------------------------------------------------------------------------
93+
| Key rotation
94+
|--------------------------------------------------------------------------
95+
|
96+
| If key rotation is enabled, force retrieving config if NextRotationDate is in the past
97+
|
98+
*/
99+
100+
'key-rotation' => env('AWS_SECRETS_KEY_ROTATION', false),
101+
91102
/*
92103
|--------------------------------------------------------------------------
93104
| Debugging

src/LaravelAwsSecretsManager.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tapp\LaravelAwsSecretsManager;
44

55
use Aws\SecretsManager\SecretsManagerClient;
6+
use Carbon\Carbon;
67
use Illuminate\Support\Facades\Cache;
78
use Illuminate\Support\Facades\Log;
89

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

3536
$this->debug = config('aws-secrets-manager.debug', false);
37+
38+
$this->keyRotation = config('aws-secrets-manager.key-rotation');
3639
}
3740

3841
public function loadSecrets()
@@ -61,6 +64,16 @@ public function loadSecrets()
6164

6265
protected function checkCache()
6366
{
67+
if ($this->keyRotation) {
68+
$cachedNextRotationDate = Cache::store($this->cacheStore)->get('AWSSecretsNextRotationDate');
69+
if (
70+
blank($cachedNextRotationDate) ||
71+
$cachedNextRotationDate < Carbon::now()
72+
) {
73+
return false;
74+
}
75+
}
76+
6477
foreach ($this->configVariables as $variable => $configPath) {
6578
$val = Cache::store($this->cacheStore)->get($variable);
6679

@@ -101,6 +114,10 @@ protected function getVariables()
101114
return;
102115
}
103116

117+
if ($this->keyRotation) {
118+
$nextRotationDateToCache = null;
119+
}
120+
104121
foreach ($secrets['SecretList'] as $secret) {
105122
if (isset($secret['ARN'])) {
106123
$result = $this->client->getSecretValue([
@@ -110,6 +127,13 @@ protected function getVariables()
110127
$secretValues = json_decode($result['SecretString'], true);
111128

112129
if (is_array($secretValues) && count($secretValues) > 0) {
130+
if ($this->keyRotation) {
131+
$nextRotationDate = Carbon::instance($secret['NextRotationDate']);
132+
if ($nextRotationDate < $nextRotationDateToCache) {
133+
$nextRotationDateToCache = $nextRotationDate;
134+
}
135+
}
136+
113137
if (isset($secretValues['name']) && isset($secretValues['value'])) {
114138
$key = $secretValues['name'];
115139
$secret = $secretValues['value'];
@@ -124,6 +148,10 @@ protected function getVariables()
124148
}
125149
}
126150
}
151+
152+
if ($this->keyRotation) {
153+
$this->storeToCache('AWSSecretsNextRotationDate', $nextRotationDateToCache);
154+
}
127155
}
128156

129157
protected function updateConfigs()

0 commit comments

Comments
 (0)