summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsawanoboly <sawanoboriyu@higanworks.com>2020-09-30 13:44:41 +0900
committersawanoboly <sawanoboriyu@higanworks.com>2020-09-30 14:13:34 +0900
commit15bfc271b6146a865dbb984405695fdf2adc2f1f (patch)
treee37d5192056e853ca12885c0691db6445db63187
parenta63a306873feafacc0dd61bdaa21484bbc8d2e5b (diff)
downloadohai-15bfc271b6146a865dbb984405695fdf2adc2f1f.tar.gz
Update ec2_metadata to use IMDSV2
get token once Signed-off-by: sawanoboly <sawanoboriyu@higanworks.com> add new context: when metadata service is disabled
-rw-r--r--lib/ohai/mixin/ec2_metadata.rb2
-rw-r--r--spec/unit/mixin/ec2_metadata_spec.rb7
2 files changed, 8 insertions, 1 deletions
diff --git a/lib/ohai/mixin/ec2_metadata.rb b/lib/ohai/mixin/ec2_metadata.rb
index 4de7ed74..309fd983 100644
--- a/lib/ohai/mixin/ec2_metadata.rb
+++ b/lib/ohai/mixin/ec2_metadata.rb
@@ -84,7 +84,7 @@ module Ohai
end
def v2_token
- http_client.put("/latest/api/token/", nil, { 'X-aws-ec2-metadata-token-ttl-seconds': "60" })&.body
+ @v2_token ||= http_client.put('/latest/api/token', nil, { 'X-aws-ec2-metadata-token-ttl-seconds': "60" })&.body
end
# Get metadata for a given path and API version
diff --git a/spec/unit/mixin/ec2_metadata_spec.rb b/spec/unit/mixin/ec2_metadata_spec.rb
index 5d5286b8..89c3a5c7 100644
--- a/spec/unit/mixin/ec2_metadata_spec.rb
+++ b/spec/unit/mixin/ec2_metadata_spec.rb
@@ -73,6 +73,13 @@ describe Ohai::Mixin::Ec2Metadata do
expect { mixin.best_api_version }.to raise_error(RuntimeError)
end
end
+
+ context "when metadata service is disabled" do
+ let(:response) { double("Net::HTTP::PUT Response", body: "403 - Forbidden", code: "403") }
+ it "raises an error" do
+ expect { mixin.best_api_version }.to raise_error(RuntimeError)
+ end
+ end
end
describe "#metadata_get" do