3
3
namespace Tapp \LaravelAwsSecretsManager ;
4
4
5
5
use Aws \SecretsManager \SecretsManagerClient ;
6
+ use Carbon \Carbon ;
6
7
use Illuminate \Support \Facades \Cache ;
7
8
use Illuminate \Support \Facades \Log ;
8
9
@@ -33,6 +34,8 @@ public function __construct()
33
34
$ this ->enabledEnvironments = config ('aws-secrets-manager.enabled-environments ' , []);
34
35
35
36
$ this ->debug = config ('aws-secrets-manager.debug ' , false );
37
+
38
+ $ this ->keyRotation = config ('aws-secrets-manager.key-rotation ' );
36
39
}
37
40
38
41
public function loadSecrets ()
@@ -61,6 +64,16 @@ public function loadSecrets()
61
64
62
65
protected function checkCache ()
63
66
{
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
+
64
77
foreach ($ this ->configVariables as $ variable => $ configPath ) {
65
78
$ val = Cache::store ($ this ->cacheStore )->get ($ variable );
66
79
@@ -101,6 +114,10 @@ protected function getVariables()
101
114
return ;
102
115
}
103
116
117
+ if ($ this ->keyRotation ) {
118
+ $ nextRotationDateToCache = null ;
119
+ }
120
+
104
121
foreach ($ secrets ['SecretList ' ] as $ secret ) {
105
122
if (isset ($ secret ['ARN ' ])) {
106
123
$ result = $ this ->client ->getSecretValue ([
@@ -110,6 +127,13 @@ protected function getVariables()
110
127
$ secretValues = json_decode ($ result ['SecretString ' ], true );
111
128
112
129
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
+
113
137
if (isset ($ secretValues ['name ' ]) && isset ($ secretValues ['value ' ])) {
114
138
$ key = $ secretValues ['name ' ];
115
139
$ secret = $ secretValues ['value ' ];
@@ -124,6 +148,10 @@ protected function getVariables()
124
148
}
125
149
}
126
150
}
151
+
152
+ if ($ this ->keyRotation ) {
153
+ $ this ->storeToCache ('AWSSecretsNextRotationDate ' , $ nextRotationDateToCache );
154
+ }
127
155
}
128
156
129
157
protected function updateConfigs ()
0 commit comments