summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-01-25 19:14:53 -0800
committerGitHub <noreply@github.com>2021-01-25 19:14:53 -0800
commit3925dc53873b81560d276774131d3262de076ecb (patch)
tree46ca6b93bca037ecbec91b337e454df064ae2f70
parent1fb0823fc28a6b7833bf806b5e6671fcb035b8b9 (diff)
parent6579776fab7ae3b089002bc8e059c1b63cdcab20 (diff)
downloadohai-3925dc53873b81560d276774131d3262de076ecb.tar.gz
Merge pull request #1599 from chef/fix_openstack
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/mixin/ec2_metadata.rb22
-rw-r--r--lib/ohai/plugins/rackspace.rb4
2 files changed, 23 insertions, 3 deletions
diff --git a/lib/ohai/mixin/ec2_metadata.rb b/lib/ohai/mixin/ec2_metadata.rb
index d183b962..8f181769 100644
--- a/lib/ohai/mixin/ec2_metadata.rb
+++ b/lib/ohai/mixin/ec2_metadata.rb
@@ -48,6 +48,13 @@ module Ohai
EC2_ARRAY_DIR ||= %w{network/interfaces/macs}.freeze
EC2_JSON_DIR ||= %w{iam}.freeze
+ #
+ # The latest metadata version in EC2_SUPPORTED_VERSIONS that this instance supports
+ # in AWS supported metadata versions are determined at instance start so we need to be
+ # cautious here in case an instance has been running for a long time
+ #
+ # @return [String] the version
+ #
def best_api_version
@api_version ||= begin
logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}/ to determine the latest supported metadata release")
@@ -84,8 +91,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]