lib/specinfra/ec2_metadata.rb in specinfra-2.89.0 vs lib/specinfra/ec2_metadata.rb in specinfra-2.90.0

- old
+ new

@@ -3,14 +3,17 @@ class Ec2Metadata def initialize(host_inventory) @host_inventory = host_inventory @base_uri = 'http://169.254.169.254/latest/meta-data/' + @token_uri = 'http://169.254.169.254/latest/api/token' + @token = '' @metadata = {} end def get + @token = get_token @metadata = get_metadata self end def [](key) @@ -62,11 +65,11 @@ private def get_metadata(path='') metadata = {} - keys = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}").stdout.split("\n") + keys = @host_inventory.backend.run_command("curl -H \"X-aws-ec2-metadata-token: #{@token}\" -s #{@base_uri}#{path}").stdout.split("\n") keys.each do |key| if key =~ %r{/$} metadata[key[0..-2]] = get_metadata(path + key) else @@ -82,10 +85,19 @@ metadata end def get_endpoint(path) - ret = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}") + ret = @host_inventory.backend.run_command("curl -H \"X-aws-ec2-metadata-token: #{@token}\" -s #{@base_uri}#{path}") + if ret.success? + ret.stdout + else + nil + end + end + + def get_token + ret = @host_inventory.backend.run_command("curl -X PUT -H \"X-aws-ec2-metadata-token-ttl-seconds: 21600\" -s #{@token_uri}") if ret.success? ret.stdout else nil end