33namespace Tapp \LaravelAwsSecretsManager ;
44
55use Aws \SecretsManager \SecretsManagerClient ;
6+ use Carbon \Carbon ;
67use Illuminate \Support \Facades \Cache ;
78use 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