Follow redirects in glance_image provider

When glance_image receives an URL, and that URL has a redirect,
the provider does not follow the redirect and the generated image
contains just the HTML text from the initial HTTP output.

This commit fixes that behavior by making the Net.HTTP request follow
redirects.

Change-Id: Iaf82f68f6371259852eb3232a4012847ed8307ff
This commit is contained in:
Javier Pena 2020-10-02 12:01:04 +02:00 committed by Takashi Kajinami
parent da9e1a7f4e
commit a7ed6f0309
1 changed files with 24 additions and 12 deletions

View File

@ -38,18 +38,9 @@ Puppet::Type.type(:glance_image).provide(
location = "--file=#{@resource[:source]}"
else
temp_file = Tempfile.new('puppet-glance-image')
uri = URI(@resource[:source])
Net::HTTP.start(uri.host, uri.port, proxy_host, proxy_port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
open temp_file.path, 'w' do |io|
response.read_body do |segment|
io.write(segment)
end
end
end
response = fetch(@resource[:source], proxy_host, proxy_port)
open temp_file.path, 'w' do |io|
io.write(response.read_body)
end
location = "--file=#{temp_file.path}"
@ -221,4 +212,25 @@ Puppet::Type.type(:glance_image).provide(
return self.string2hash(input)
end
end
def fetch(uri_str, proxy_host = nil, proxy_port = nil, limit = 10)
raise RuntimeError, 'Too many HTTP redirections' if limit == 0
uri = URI(uri_str)
Net::HTTP.start(uri.host, uri.port, proxy_host, proxy_port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
case response
when Net::HTTPSuccess then
response
when Net::HTTPRedirection then
location = response['location']
return fetch(location, proxy_host, proxy_port, limit - 1)
else
response.value
end
end
end
end
end