Skip to content

Commit

Permalink
Improve EC2 detection to handle new non-Xen c5 and m5 instances, See #24
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Mar 5, 2018
1 parent 06cf314 commit e33de2d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/AWSCredentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
export AWSCredentials,
localhost_is_lambda,
localhost_is_ec2,
localhost_maybe_ec2,
aws_user_arn,
aws_account_number

@deprecate localhost_is_ec2() localhost_maybe_ec2()


"""
When you interact with AWS, you specify your [AWS Security Credentials](http://docs.aws.amazon.com/general/latest/gr/aws-security-credentials.html) to verify who you are and whether you have permission to access the resources that you are requesting. AWS uses the security credentials to authenticate and authorize your requests.
Expand Down Expand Up @@ -75,7 +79,7 @@ function AWSCredentials()

creds = ecs_instance_credentials()

elseif localhost_is_ec2()
elseif localhost_maybe_ec2()

creds = ec2_instance_credentials()

Expand Down Expand Up @@ -103,15 +107,19 @@ localhost_is_lambda() = haskey(ENV, "LAMBDA_TASK_ROOT")
Is Julia running on an EC2 virtual machine?
"""

function localhost_is_ec2()
function localhost_maybe_ec2()

if localhost_is_lambda()
return false
end

@static if VERSION < v"0.7.0-DEV" ? is_unix() : Sys.isunix()
return isfile("/sys/hypervisor/uuid") &&
String(read("/sys/hypervisor/uuid",3)) == "ec2"
String(read("/sys/hypervisor/uuid",3)) == "ec2" ||
isfile("/sys/devices/virtual/dmi/id/product_uuid")
# product_uuid is not world readable!
# https://patchwork.kernel.org/patch/6461521/
# https://github.com/JuliaCloud/AWSCore.jl/issues/24
end

return false
Expand Down Expand Up @@ -169,7 +177,7 @@ for `key`.

function ec2_metadata(key)

@assert localhost_is_ec2()
@assert localhost_maybe_ec2()

String(http_get("http://169.254.169.254/latest/meta-data/$key").body)
end
Expand All @@ -185,7 +193,7 @@ for EC2 virtual machine.

function ec2_instance_credentials()

@assert localhost_is_ec2()
@assert localhost_maybe_ec2()

info = ec2_metadata("iam/info")
info = JSON.parse(info, dicttype=Dict{String,String})
Expand Down

0 comments on commit e33de2d

Please sign in to comment.