commit a7ed6f03098b1537f15f12299f586688e6e240d4 Author: Javier Pena Date: Fri Oct 2 12:01:04 2020 +0200 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 diff --git a/lib/puppet/provider/glance_image/openstack.rb b/lib/puppet/provider/glance_image/openstack.rb index 15f09a5..b195a55 100644 --- a/lib/puppet/provider/glance_image/openstack.rb +++ b/lib/puppet/provider/glance_image/openstack.rb @@ -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