Skip to content

Commit

Permalink
Throw IMDSUnavailable when encountering IOError (#650)
Browse files Browse the repository at this point in the history
* Throw IMDSUnavailable when encountering IOError

* Set project version to 1.90.1

* Formatting
  • Loading branch information
omus committed Jul 27, 2023
1 parent 9595404 commit 5faafb2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
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

2 comments on commit 5faafb2

@omus
Copy link
Member Author

@omus omus commented on 5faafb2 Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/88488

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.90.1 -m "<description of version>" 5faafb2d363fcdd77c9f717b8976e180168bf9ea
git push origin v1.90.1

Please sign in to comment.