summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2016-03-08 20:18:28 -0800
committerTim Smith <tsmith@chef.io>2016-03-08 20:18:28 -0800
commit94a20df0d6ba5e6ac008850baf87c0d9098f2dba (patch)
tree99aae40e5430d55fbc81f8a6632d3f793473ec4d
parent07ba1066658d9264b09851291429dafe9ee4c861 (diff)
parentb6d002bfa7997ca1e10780efa18f0e7497b8a158 (diff)
downloadohai-94a20df0d6ba5e6ac008850baf87c0d9098f2dba.tar.gz
Merge pull request #736 from tas50/to_gce_or_not_to_gce
Update GCE metadata API version and fail better
-rw-r--r--lib/ohai/mixin/gce_metadata.rb24
-rw-r--r--spec/unit/plugins/gce_spec.rb15
2 files changed, 24 insertions, 15 deletions
diff --git a/lib/ohai/mixin/gce_metadata.rb b/lib/ohai/mixin/gce_metadata.rb
index 05127a5e..53d4ec10 100644
--- a/lib/ohai/mixin/gce_metadata.rb
+++ b/lib/ohai/mixin/gce_metadata.rb
@@ -23,11 +23,17 @@ module Ohai
# Trailing dot to host is added to avoid DNS search path
GCE_METADATA_ADDR = "metadata.google.internal." unless defined?(GCE_METADATA_ADDR)
- GCE_METADATA_URL = "/computeMetadata/v1beta1/?recursive=true" unless defined?(GCE_METADATA_URL)
+ GCE_METADATA_URL = "/computeMetadata/v1/?recursive=true" unless defined?(GCE_METADATA_URL)
def can_metadata_connect?(addr, port, timeout = 2)
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
- saddr = Socket.pack_sockaddr_in(port, addr)
+ begin
+ saddr = Socket.pack_sockaddr_in(port, addr)
+ rescue SocketError => e # occurs when non-GCE systems try to resolve metadata.google.internal
+ Ohai::Log.debug("gce mixin: can_metadata_connect? failed setting up socket: #{e}")
+ return false
+ end
+
connected = false
begin
@@ -47,24 +53,26 @@ module Ohai
end
rescue SystemCallError
end
- Ohai::Log.debug("can_metadata_connect? == #{connected}")
+ Ohai::Log.debug("gce mixin: can_metadata_connect? == #{connected}")
connected
end
- def http_client
- Net::HTTP.start(GCE_METADATA_ADDR).tap { |h| h.read_timeout = 6 }
+ # fetch the meta content with a timeout and the required header
+ def http_get(uri)
+ conn = Net::HTTP.start(GCE_METADATA_ADDR)
+ conn.read_timeout = 6
+ conn.get(uri, initheader = { "Metadata-Flavor" => "Google" })
end
def fetch_metadata(id = "")
- uri = "#{GCE_METADATA_URL}/#{id}"
- response = http_client.get(uri)
+ response = http_get("#{GCE_METADATA_URL}/#{id}")
return nil unless response.code == "200"
if json?(response.body)
data = StringIO.new(response.body)
parser = FFI_Yajl::Parser.new
parser.parse(data)
- elsif has_trailing_slash?(id) or (id == "")
+ elsif has_trailing_slash?(id) || (id == "")
temp = {}
response.body.split("\n").each do |sub_attr|
temp[sanitize_key(sub_attr)] = fetch_metadata("#{id}#{sub_attr}")
diff --git a/spec/unit/plugins/gce_spec.rb b/spec/unit/plugins/gce_spec.rb
index 4e1d4927..1d3ae838 100644
--- a/spec/unit/plugins/gce_spec.rb
+++ b/spec/unit/plugins/gce_spec.rb
@@ -25,15 +25,20 @@ describe Ohai::System, "plugin gce" do
shared_examples_for "!gce" do
it "should NOT attempt to fetch the gce metadata" do
- expect(@plugin).not_to receive(:http_client)
+ expect(@plugin).not_to receive(:http_get)
+ @plugin.run
+ end
+
+ it "should NOT set gce attributes" do
+ expect(@plugin[:gce]).to be_nil
@plugin.run
end
end
shared_examples_for "gce" do
before(:each) do
- @http_client = double("Net::HTTP client")
- allow(@plugin).to receive(:http_client).and_return(@http_client)
+ @http_get = double("Net::HTTP client")
+ allow(@plugin).to receive(:http_get).and_return(double("Net::HTTP Response", :body => '{"instance":{"hostname":"test-host"}}', :code => "200"))
allow(IO).to receive(:select).and_return([[], [1], []])
t = double("connection")
allow(t).to receive(:connect_nonblock).and_raise(Errno::EINPROGRESS)
@@ -42,10 +47,6 @@ describe Ohai::System, "plugin gce" do
end
it "should recursively fetch and properly parse json metadata" do
- expect(@http_client).to receive(:get).
- with("/computeMetadata/v1beta1/?recursive=true/").
- and_return(double("Net::HTTP Response", :body => '{"instance":{"hostname":"test-host"}}', :code => "200"))
-
@plugin.run
expect(@plugin[:gce]).not_to be_nil