Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw IMDSUnavailable when encountering IOError #650

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AWS"
uuid = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
license = "MIT"
version = "1.90.0"
version = "1.90.1"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
10 changes: 9 additions & 1 deletion src/IMDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function _http_request(args...; status_exception=true, kwargs...)
# and connections will fail. On EC2 instances where IMDS is disabled a HTTP 403 is
# returned.
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#instance-metadata-returns
if e isa ConnectError || e isa StatusError && e.status == 403
if is_connection_exception(e) || e isa StatusError && e.status == 403
throw(IMDSUnavailable())

#! format: off
Expand All @@ -118,6 +118,14 @@ function _http_request(args...; status_exception=true, kwargs...)
return response
end

is_connection_exception(e::ConnectError) = true
is_connection_exception(e::Exception) = false

# https://github.com/JuliaCloud/AWS.jl/issues/649
function is_connection_exception(e::HTTP.Exceptions.RequestError)
return e.error == Base.IOError("read: connection timed out (ETIMEDOUT)", -110)
end

"""
get([session::Session], path::AbstractString) -> Union{String, Nothing}

Expand Down
15 changes: 15 additions & 0 deletions test/IMDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ function _imds_patch(router::HTTP.Router=HTTP.Router(); listening=true, enabled=
end

@testset "IMDS" begin
@testset "is_connection_exception" begin
url = "http://169.254.169.254/latest/api/token"
connect_timeout = HTTP.ConnectionPool.ConnectTimeout("169.254.169.254", 80)
e = HTTP.Exceptions.ConnectError(url, connect_timeout)
@test IMDS.is_connection_exception(e)

request = HTTP.Request("PUT", "/latest/api/token", [], HTTP.nobody)
io_error = Base.IOError("read: connection timed out (ETIMEDOUT)", -110)
e = HTTP.Exceptions.RequestError(request, io_error)
@test IMDS.is_connection_exception(e)

e = ErrorException("non-connection error")
@test !IMDS.is_connection_exception(e)
end

@testset "refresh_token!" begin
# Running outside of an EC2 instance
apply(_imds_patch(; listening=false)) do
Expand Down
Loading