Repository navigation
Expand file tree
/
Copy pathauth_cache.ex
More file actions
264 lines (212 loc) · 7.3 KB
/
Copy pathauth_cache.ex
File metadata and controls
264 lines (212 loc) · 7.3 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
defmodule ExAws.Config.AuthCache do
@moduledoc false
use GenServer
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html
@refresh_lead_time Application.compile_env(:ex_aws, :auth_refresh_lead_time, 300_000)
@instance_auth_key :aws_instance_auth
@pod_identity_auth_key :aws_pod_identity_auth
defmodule AuthConfigAdapter do
@moduledoc false
@doc "Compute the awscli auth information."
@callback adapt_auth_config(auth :: map, profile :: String.t(), expiration :: integer) :: any
end
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, Keyword.put(opts, :name, __MODULE__))
end
def get(config) do
:ets.lookup(__MODULE__, @instance_auth_key)
|> refresh_auth_if_required(config)
end
def get(:pod_identity, config) do
:ets.lookup(__MODULE__, @pod_identity_auth_key)
|> refresh_pod_identity_if_required(config)
end
def get(profile, expiration) do
case :ets.lookup(__MODULE__, {:awscli, profile}) do
[{{:awscli, ^profile}, auth_config}] ->
auth_config
[] ->
GenServer.call(__MODULE__, {:refresh_awscli_config, profile, expiration}, 30_000)
end
end
## Callbacks
def init(:ok) do
ets = :ets.new(__MODULE__, [:named_table, read_concurrency: true])
{:ok, ets}
end
def handle_call({:refresh_auth, config}, _from, ets) do
auth = refresh_auth(config, ets)
{:reply, auth, ets}
end
def handle_call({:refresh_pod_identity_auth, config}, _from, ets) do
auth = refresh_pod_identity_auth(config, ets)
{:reply, auth, ets}
end
def handle_call({:refresh_awscli_config, profile, expiration}, _from, ets) do
case :ets.lookup(ets, {:awscli, profile}) do
[{{:awscli, ^profile}, auth}] ->
{:reply, auth, ets}
[] ->
auth = refresh_awscli_config(profile, expiration, ets)
{:reply, auth, ets}
end
end
def handle_info({:refresh_auth, config}, ets) do
refresh_auth(config, ets)
{:noreply, ets}
end
def handle_info({:refresh_pod_identity_auth, config}, ets) do
refresh_pod_identity_auth(config, ets)
{:noreply, ets}
end
def handle_info({:refresh_awscli_config, profile, expiration}, ets) do
refresh_awscli_config(profile, expiration, ets)
{:noreply, ets}
end
def refresh_awscli_config(profile, expiration, ets) do
auth =
try do
ExAws.Config.awscli_auth_credentials(profile)
rescue
e ->
require Logger
Logger.error(
"Failed to refresh AWS CLI config for profile #{profile}: #{Exception.format(:error, e, __STACKTRACE__)}"
)
nil
end
auth =
if auth do
case ExAws.Config.awscli_auth_adapter() do
nil ->
auth
adapter ->
attempt_credentials_refresh(adapter, auth, profile, expiration)
end
else
nil
end
refresh_interval =
if auth && Map.get(auth, :expiration) do
interval = next_refresh_in(auth)
if interval > 0 do
max(interval, expiration)
else
expiration
end
else
expiration
end
Process.send_after(self(), {:refresh_awscli_config, profile, expiration}, refresh_interval)
if auth do
:ets.insert(ets, {{:awscli, profile}, auth})
end
auth
end
defp attempt_credentials_refresh(adapter, auth, profile, expiration, retries \\ 6) do
case adapter.adapt_auth_config(auth, profile, expiration) do
{:error, error} when retries == 1 ->
Process.send_after(self(), {:refresh_awscli_config, profile, expiration}, expiration)
raise "Couldn't get credentials from auth adapter after 6 retries, last error was #{inspect(error)}"
{:error, _error} ->
Process.sleep(:rand.uniform(5_000))
attempt_credentials_refresh(adapter, auth, profile, expiration, retries - 1)
# Always store a map on AuthCache
auth when is_map(auth) ->
auth
end
end
defp refresh_auth_if_required([], config) do
GenServer.call(__MODULE__, {:refresh_auth, config}, 30_000)
end
defp refresh_auth_if_required([{_key, cached_auth}], config) do
if next_refresh_in(cached_auth) > 0 do
cached_auth
else
GenServer.call(__MODULE__, {:refresh_auth, config}, 30_000)
end
end
defp refresh_auth(config, ets) do
:ets.lookup(__MODULE__, @instance_auth_key)
|> refresh_auth_if_stale(config, ets)
end
defp refresh_auth_if_stale([], config, ets) do
refresh_auth_now(config, ets)
end
defp refresh_auth_if_stale([{_key, cached_auth}], config, ets) do
if next_refresh_in(cached_auth) > @refresh_lead_time do
# we still have a valid auth token, so simply return that
cached_auth
else
refresh_auth_now(config, ets)
end
end
defp refresh_auth_if_stale(_, config, ets), do: refresh_auth_now(config, ets)
defp refresh_auth_now(config, ets) do
auth = ExAws.InstanceMeta.security_credentials(config)
:ets.insert(ets, {@instance_auth_key, auth})
Process.send_after(__MODULE__, {:refresh_auth, config}, next_refresh_in(auth))
auth
end
defp refresh_pod_identity_if_required([], config) do
GenServer.call(__MODULE__, {:refresh_pod_identity_auth, config}, 30_000)
end
defp refresh_pod_identity_if_required([{_key, cached_auth}], config) do
if next_refresh_in(cached_auth) > 0 do
cached_auth
else
GenServer.call(__MODULE__, {:refresh_pod_identity_auth, config}, 30_000)
end
end
defp refresh_pod_identity_auth(config, ets) do
:ets.lookup(__MODULE__, @pod_identity_auth_key)
|> refresh_pod_identity_if_stale(config, ets)
end
defp refresh_pod_identity_if_stale([], config, ets) do
refresh_pod_identity_now(config, ets)
end
defp refresh_pod_identity_if_stale([{_key, cached_auth}], config, ets) do
if next_refresh_in(cached_auth) > @refresh_lead_time do
cached_auth
else
refresh_pod_identity_now(config, ets)
end
end
defp refresh_pod_identity_if_stale(_, config, ets), do: refresh_pod_identity_now(config, ets)
defp refresh_pod_identity_now(config, ets) do
# Only attempt pod identity if the environment is properly configured
if ExAws.PodIdentity.available?() do
auth = ExAws.PodIdentity.security_credentials(config)
:ets.insert(ets, {@pod_identity_auth_key, auth})
Process.send_after(__MODULE__, {:refresh_pod_identity_auth, config}, next_refresh_in(auth))
auth
else
# Return empty map if pod identity is not available
%{}
end
end
defp next_refresh_in(%{expiration: expiration}) when is_integer(expiration) do
try do
expires_in_ms = (expiration - System.os_time(:second)) * 1000
# refresh lead_time before auth expires, unless the time has passed
# otherwise refresh needed now
max(0, expires_in_ms - @refresh_lead_time)
rescue
_e -> 0
end
end
defp next_refresh_in(%{expiration: expiration}) when is_binary(expiration) do
try do
expires_in_ms =
expiration
|> NaiveDateTime.from_iso8601!()
|> NaiveDateTime.diff(NaiveDateTime.utc_now(), :millisecond)
# refresh lead_time before auth expires, unless the time has passed
# otherwise refresh needed now
max(0, expires_in_ms - @refresh_lead_time)
rescue
_e -> 0
end
end
defp next_refresh_in(_), do: 0
end