Skip to content

Commit 6784eff

Browse files
committed
Formatting
1 parent 4bad754 commit 6784eff

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/IMDS.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ function refresh_token!(session::Session, duration::Integer=session.duration)
9999
return session
100100
end
101101

102-
function request(session::Session, method::AbstractString, path::AbstractString; status_exception=true)
102+
function request(
103+
session::Session, method::AbstractString, path::AbstractString; status_exception=true
104+
)
103105
# Only allow the token to be refreshed once per call to `IMDS.request`.
104106
allow_refresh = true
105107

@@ -111,7 +113,9 @@ function request(session::Session, method::AbstractString, path::AbstractString;
111113
allow_refresh = false
112114
end
113115
headers = HTTP.Header[]
114-
!isempty(session.token) && HTTP.setheader(headers, "X-aws-ec2-metadata-token" => session.token)
116+
if !isempty(session.token)
117+
HTTP.setheader(headers, "X-aws-ec2-metadata-token" => session.token)
118+
end
115119

116120
# Only using the IPv4 endpoint as the IPv6 endpoint has to be explicitly enabled and
117121
# does not disable IPv4 support.
@@ -146,9 +150,7 @@ function _http_request(args...; status_exception=true, kwargs...)
146150
#
147151
# Additionally, we set a low connect timeout to have faster responses when
148152
# attempting to connect to IMDS outside of EC2.
149-
@mock HTTP.request(
150-
args...; connect_timeout=1, kwargs..., status_exception=true,
151-
)
153+
@mock HTTP.request(args...; connect_timeout=1, kwargs..., status_exception=true)
152154
catch e
153155
# When running outside of an EC2 instance the link-local address will be unavailable
154156
# and connections will fail. On EC2 instances where IMDS is disabled a HTTP 403 is

test/unit/IMDS.jl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,24 @@ end
5656
# Use Mocking to re-route requests to 169.254.169.254 without having to actually start an
5757
# HTTP.jl server. Should result in faster running tests.
5858
function _imds_patch(
59-
router::HTTP.Router=HTTP.Router(); listening=true, enabled=true,
59+
router::HTTP.Router=HTTP.Router();
60+
listening=true,
61+
enabled=true,
6062
num_requests::Threads.Atomic{Int}=Threads.Atomic{Int}(),
6163
)
6264
num_requests[] = 0
6365

6466
patch = @patch function HTTP.request(
65-
method, url, headers=[], body=HTTP.nobody; status_exception=true, retry::Bool=true,
66-
retries::Int=4, retry_delays=ExponentialBackOff(; n=retries, factor=3),
67-
retry_check=(args...) -> false, kwargs...
67+
method,
68+
url,
69+
headers=[],
70+
body=HTTP.nobody;
71+
status_exception=true,
72+
retry::Bool=true,
73+
retries::Int=4,
74+
retry_delays=ExponentialBackOff(; n=retries, factor=3),
75+
retry_check=(args...) -> false,
76+
kwargs...,
6877
)
6978
uri = HTTP.URI(url)
7079
if uri.host != "169.254.169.254"
@@ -88,26 +97,25 @@ function _imds_patch(
8897
# When `status_exception=false` retries do not occur as an exception needs to
8998
# be raised for them to work. This replicates how `HTTP.request` works.
9099
if status_exception && resp.status >= 300
91-
ex = HTTP.Exceptions.StatusError(
92-
resp.status, req.method, req.target, resp
93-
)
100+
ex = HTTP.Exceptions.StatusError(resp.status, req.method, req.target, resp)
94101
throw(ex)
95102
end
96103
return resp
97104
end
98105

99106
retry_request = Base.retry(
100-
handler,
107+
handler;
101108
delays=retry_delays,
102109
check=(s, ex) -> begin
103110
resp_body = get(req.context, :response_body, nothing)
104111
resp = !isnothing(resp_body) ? req.response : nothing
105112
retry = (
106-
(HTTP.RetryRequest.isrecoverable(ex) && HTTP.retryable(req)) ||
107-
(HTTP.retryablebody(req) && retry_check(s, ex, req, resp, resp_body))
113+
(HTTP.RetryRequest.isrecoverable(ex) && HTTP.retryable(req)) || (
114+
HTTP.retryablebody(req) && retry_check(s, ex, req, resp, resp_body)
115+
)
108116
)
109117
return retry
110-
end
118+
end,
111119
)
112120

113121
return retry_request(req)
@@ -187,9 +195,7 @@ end
187195
io_error = Base.IOError("read: connection timed out (ETIMEDOUT)", -110)
188196
throw(HTTP.Exceptions.RequestError(request, io_error))
189197
end
190-
router = Router([
191-
Route("PUT", "/latest/api/token", connection_timeout),
192-
])
198+
router = Router([Route("PUT", "/latest/api/token", connection_timeout)])
193199
apply(_imds_patch(router)) do
194200
# Set some initial values so we can tell what gets modified
195201
session = IMDS.Session("bar", -1, -1)
@@ -203,7 +209,9 @@ end
203209
end
204210

205211
# IMDSv1 is available
206-
router = Router([response_route("PUT", "/latest/api/token", HTTP.Response(404))])
212+
router = Router([
213+
response_route("PUT", "/latest/api/token", HTTP.Response(404))
214+
])
207215
apply(_imds_patch(router)) do
208216
# Set some initial values so we can tell what gets modified
209217
session = IMDS.Session("bar", -1, -1)

0 commit comments

Comments
 (0)