summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2021-01-25 16:34:30 -0800
committerTim Smith <tsmith84@gmail.com>2021-01-25 16:34:30 -0800
commit710fad57de7255b432c4f4d7026c155d51570c3e (patch)
treea6e3460dde3f97eb5bb689e64c90a8bf052a759a
parent1fb0823fc28a6b7833bf806b5e6671fcb035b8b9 (diff)
downloadohai-710fad57de7255b432c4f4d7026c155d51570c3e.tar.gz
Fix fetching metadata on AWS compatible clouds
They're not going to have a IMDSv2 token to fetch and they were failing hard. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/mixin/ec2_metadata.rb15
-rw-r--r--lib/ohai/plugins/rackspace.rb4
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/ohai/mixin/ec2_metadata.rb b/lib/ohai/mixin/ec2_metadata.rb
index d183b962..c8816fd7 100644
--- a/lib/ohai/mixin/ec2_metadata.rb
+++ b/lib/ohai/mixin/ec2_metadata.rb
@@ -84,8 +84,21 @@ module Ohai
end
end
+ #
+ # Fetch an API token for use querying AWS IMDSv2 or return nil if no token if found
+ # AWS like systems (think OpenStack) will not respond with a token here
+ #
+ # @return [NilClass, String] API token or nil
+ #
def v2_token
- @v2_token ||= http_client.put("/latest/api/token", nil, { 'X-aws-ec2-metadata-token-ttl-seconds': "60" })&.body
+ @v2_token ||= begin
+ request = http_client.put("/latest/api/token", nil, { 'X-aws-ec2-metadata-token-ttl-seconds': "60" })
+ if request.code == "404" # not on AWS
+ nil
+ else
+ request.body
+ end
+ end
end
# Get metadata for a given path and API version
diff --git a/lib/ohai/plugins/rackspace.rb b/lib/ohai/plugins/rackspace.rb
index efe63e40..469290b6 100644
--- a/lib/ohai/plugins/rackspace.rb
+++ b/lib/ohai/plugins/rackspace.rb
@@ -162,8 +162,8 @@ Ohai.plugin(:Rackspace) do
unless rackspace[:public_ip].nil?
rackspace[:public_hostname] = begin
Resolv.getname(rackspace[:public_ip])
- rescue Resolv::ResolvError, Resolv::ResolvTimeout
- rackspace[:public_ip]
+ rescue Resolv::ResolvError, Resolv::ResolvTimeout
+ rackspace[:public_ip]
end
end
rackspace[:local_ipv4] = rackspace[:private_ip]