lib/ohai/plugins/ec2.rb in ohai-16.5.6 vs lib/ohai/plugins/ec2.rb in ohai-16.6.5
- old
+ new
@@ -95,12 +95,12 @@
# return the contents of a file if the file exists
# @param path[String] abs path to the file
# @return [String] contents of the file if it exists
def file_val_if_exists(path)
- if ::File.exist?(path)
- ::File.read(path)
+ if file_exist?(path)
+ file_read(path)
end
end
# a single check that combines all the various detection methods for EC2
# @return [Boolean] Does the system appear to be on EC2
@@ -120,14 +120,20 @@
logger.trace("Plugin EC2: looks_like_ec2? == true")
ec2 Mash.new
fetch_metadata.each do |k, v|
# fetch_metadata returns IAM security credentials, including the IAM user's
# secret access key. We'd rather not have ohai send this information
- # to the server.
- # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories
- next if k == "iam" && !hint?("iam")
-
- ec2[k] = v
+ # to the server. If the instance is associated with an IAM role we grab
+ # only the "info" key and the IAM role name.
+ # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html
+ if k == "iam" && !hint?("iam")
+ ec2[:iam] = v.select { |key, value| key == "info" }
+ if v["security-credentials"] && v["security-credentials"].keys.length == 1
+ ec2[:iam]["role_name"] = v["security-credentials"].keys[0]
+ end
+ else
+ ec2[k] = v
+ end
end
ec2[:userdata] = fetch_userdata
ec2[:account_id] = fetch_dynamic_data["accountId"]
ec2[:availability_zone] = fetch_dynamic_data["availabilityZone"]
ec2[:region] = fetch_dynamic_data["region"]