Repository navigation
Expand file tree
/
Copy pathAwsCredentialCache.php
More file actions
229 lines (199 loc) · 6.2 KB
/
Copy pathAwsCredentialCache.php
File metadata and controls
229 lines (199 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace Illuminate\Queue;
use Aws\Credentials\CredentialsInterface;
use Closure;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\RejectedPromise;
use Illuminate\Contracts\Cache\Lock;
use Illuminate\Contracts\Cache\LockProvider;
use Throwable;
class AwsCredentialCache
{
/**
* The number of seconds before expiration that credentials should be refreshed.
*
* @var int
*/
protected const REFRESH_WINDOW = 60;
/**
* The number of seconds the credential refresh lock should be maintained.
*
* @var int
*/
protected const LOCK_SECONDS = 15;
/**
* The number of seconds to wait for another process to refresh credentials.
*
* @var int
*/
protected const LOCK_WAIT_SECONDS = 5;
/**
* The resolver for the backing cache repository.
*
* @var \Closure(): \Illuminate\Contracts\Cache\Repository
*/
protected $repository;
/**
* The resolver for the fallback cache repository, if any.
*
* @var \Closure(): \Illuminate\Contracts\Cache\Repository|null
*/
protected $fallback;
/**
* Create a new AWS credential cache.
*
* @param \Closure(): \Illuminate\Contracts\Cache\Repository $repository
* @param \Closure(): \Illuminate\Contracts\Cache\Repository|null $fallback
*/
public function __construct(Closure $repository, ?Closure $fallback = null)
{
$this->repository = $repository;
$this->fallback = $fallback;
}
/**
* Resolve credentials while sharing a single refresh across processes.
*
* @param string $key
* @param callable $provider
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function resolve($key, callable $provider)
{
if ($credentials = $this->freshCredentials($key)) {
return Create::promiseFor($credentials);
}
foreach ($this->repositories() as $repository) {
try {
$store = $repository()->getStore();
if (! $store instanceof LockProvider) {
continue;
}
$lock = $store->lock($key.':refresh', static::LOCK_SECONDS);
$lock->block(static::LOCK_WAIT_SECONDS);
} catch (Throwable) {
continue;
}
if ($credentials = $this->freshCredentials($key)) {
$this->release($lock);
return Create::promiseFor($credentials);
}
return $this->resolveAndCache($key, $provider, $lock);
}
return $this->resolveAndCache($key, $provider);
}
/**
* Get fresh credentials from the available cache repositories.
*
* @param string $key
* @return \Aws\Credentials\CredentialsInterface|null
*/
protected function freshCredentials($key)
{
foreach ($this->repositories() as $repository) {
try {
$credentials = $repository()->get($key);
if ($credentials instanceof CredentialsInterface &&
(is_null($credentials->getExpiration()) ||
$credentials->getExpiration() - time() > static::REFRESH_WINDOW)) {
return $credentials;
}
} catch (Throwable) {
//
}
}
}
/**
* Resolve and cache credentials before releasing the refresh lock.
*
* @param string $key
* @param callable $provider
* @param \Illuminate\Contracts\Cache\Lock|null $lock
* @return \GuzzleHttp\Promise\PromiseInterface
*/
protected function resolveAndCache($key, callable $provider, ?Lock $lock = null)
{
try {
$promise = $provider();
} catch (Throwable $e) {
$this->release($lock);
throw $e;
}
return $promise->then(
function (CredentialsInterface $credentials) use ($key, $lock) {
$expiration = $credentials->getExpiration();
if (is_null($expiration)) {
$this->put($key, $credentials);
} elseif (($ttl = $expiration - time() - static::REFRESH_WINDOW) > 0) {
$this->put($key, $credentials, $ttl);
} else {
$this->forget($key);
}
$this->release($lock);
return $credentials;
},
function ($reason) use ($lock) {
$this->release($lock);
return new RejectedPromise($reason);
},
);
}
/**
* Store the credentials, silently ignoring unavailable cache stores.
*
* @param string $key
* @param \Aws\Credentials\CredentialsInterface $credentials
* @param int $ttl
* @return void
*/
protected function put($key, CredentialsInterface $credentials, $ttl = 0)
{
foreach ($this->repositories() as $repository) {
try {
$ttl > 0
? $repository()->put($key, $credentials, (int) $ttl)
: $repository()->forever($key, $credentials);
} catch (Throwable) {
//
}
}
}
/**
* Remove the cached credentials, silently ignoring unavailable cache stores.
*
* @param string $key
* @return void
*/
protected function forget($key)
{
foreach ($this->repositories() as $repository) {
try {
$repository()->forget($key);
} catch (Throwable) {
//
}
}
}
/**
* Get the cache repository resolvers in order of preference.
*
* @return array<int, \Closure(): \Illuminate\Contracts\Cache\Repository>
*/
protected function repositories()
{
return array_filter([$this->repository, $this->fallback]);
}
/**
* Release the credential refresh lock without affecting credential resolution.
*
* @param \Illuminate\Contracts\Cache\Lock|null $lock
* @return void
*/
protected function release(?Lock $lock)
{
try {
$lock?->release();
} catch (Throwable) {
//
}
}
}