fix: Pass "Metadata-Flavor" header to metadata server when checking for GCE

This commit is contained in:
Spring_MT 2019-08-14 02:28:08 +09:00 committed by Daniel Azuma
parent 464d2650d0
commit efb9b7897a
2 changed files with 5 additions and 1 deletions

View File

@ -62,7 +62,8 @@ module Google
# is available # is available
def on_gce? options = {} def on_gce? options = {}
c = options[:connection] || Faraday.default_connection c = options[:connection] || Faraday.default_connection
resp = c.get COMPUTE_CHECK_URI do |req| headers = { "Metadata-Flavor" => "Google" }
resp = c.get COMPUTE_CHECK_URI, nil, headers do |req|
# Comment from: oauth2client/client.py # Comment from: oauth2client/client.py
# #
# Note: the explicit `timeout` below is a workaround. The underlying # Note: the explicit `timeout` below is a workaround. The underlying

View File

@ -97,6 +97,7 @@ describe Google::Auth::GCECredentials do
describe "#on_gce?" do describe "#on_gce?" do
it "should be true when Metadata-Flavor is Google" do it "should be true when Metadata-Flavor is Google" do
stub = stub_request(:get, "http://169.254.169.254") stub = stub_request(:get, "http://169.254.169.254")
.with(headers: { "Metadata-Flavor" => "Google" })
.to_return(status: 200, .to_return(status: 200,
headers: { "Metadata-Flavor" => "Google" }) headers: { "Metadata-Flavor" => "Google" })
expect(GCECredentials.on_gce?({}, true)).to eq(true) expect(GCECredentials.on_gce?({}, true)).to eq(true)
@ -105,6 +106,7 @@ describe Google::Auth::GCECredentials do
it "should be false when Metadata-Flavor is not Google" do it "should be false when Metadata-Flavor is not Google" do
stub = stub_request(:get, "http://169.254.169.254") stub = stub_request(:get, "http://169.254.169.254")
.with(headers: { "Metadata-Flavor" => "Google" })
.to_return(status: 200, .to_return(status: 200,
headers: { "Metadata-Flavor" => "NotGoogle" }) headers: { "Metadata-Flavor" => "NotGoogle" })
expect(GCECredentials.on_gce?({}, true)).to eq(false) expect(GCECredentials.on_gce?({}, true)).to eq(false)
@ -113,6 +115,7 @@ describe Google::Auth::GCECredentials do
it "should be false if the response is not 200" do it "should be false if the response is not 200" do
stub = stub_request(:get, "http://169.254.169.254") stub = stub_request(:get, "http://169.254.169.254")
.with(headers: { "Metadata-Flavor" => "Google" })
.to_return(status: 404, .to_return(status: 404,
headers: { "Metadata-Flavor" => "NotGoogle" }) headers: { "Metadata-Flavor" => "NotGoogle" })
expect(GCECredentials.on_gce?({}, true)).to eq(false) expect(GCECredentials.on_gce?({}, true)).to eq(false)