diff --git a/src/api/app/controllers/statistics/maintenance_statistics_controller.rb b/src/api/app/controllers/statistics/maintenance_statistics_controller.rb index 60de14874c1..8ae27c7ccc8 100644 --- a/src/api/app/controllers/statistics/maintenance_statistics_controller.rb +++ b/src/api/app/controllers/statistics/maintenance_statistics_controller.rb @@ -7,7 +7,7 @@ def index if @project.is_a?(String) # FIXME: This could be simplified by redirecting to the remote instead remote_instance, remote_project = Project.find_remote_project(@project) - remote_response = Project::RemoteURL.load(remote_instance.remoteurl, maintenance_statistics_path(project: remote_project)) + remote_response = Project::RemoteURL.load(remote_instance, maintenance_statistics_path(project: remote_project)) if remote_response render xml: remote_response else diff --git a/src/api/app/models/distribution.rb b/src/api/app/models/distribution.rb index 7fb4891b75c..de1e1847826 100644 --- a/src/api/app/models/distribution.rb +++ b/src/api/app/models/distribution.rb @@ -40,13 +40,7 @@ def self.all_including_remotes Project.remote.each do |prj| body = Rails.cache.fetch("remote_distribution_#{prj.id}", expires_in: 1.hour) do - begin - Project::RemoteURL.load(prj.remoteurl, '/distributions.xml') - rescue OpenSSL::SSL::SSLError - # skip, but do not die if remote instance have invalid SSL - Rails.logger.error "Remote instance #{prj.remoteurl} has no valid SSL certificate" - next - end + Project::RemoteURL.load(prj, '/distributions.xml') end next if body.blank? # don't let broken remote instances break us xmlhash = Xmlhash.parse(body) diff --git a/src/api/app/models/project.rb b/src/api/app/models/project.rb index 139ac63b33a..9a96010af95 100644 --- a/src/api/app/models/project.rb +++ b/src/api/app/models/project.rb @@ -166,12 +166,7 @@ def self.remote_image_templates def self.load_from_remote(project, path) Rails.cache.fetch("remote_image_templates_#{project.id}", expires_in: 1.hour) do - begin - Project::RemoteURL.load(project.remoteurl, path) - rescue OpenSSL::SSL::SSLError - Rails.logger.error "Remote instance #{project.remoteurl} has no valid SSL certificate" - nil - end + Project::RemoteURL.load(project, path) end end diff --git a/src/api/app/models/project/remote_url.rb b/src/api/app/models/project/remote_url.rb index 1db13ff3670..0db63f6483c 100644 --- a/src/api/app/models/project/remote_url.rb +++ b/src/api/app/models/project/remote_url.rb @@ -1,39 +1,19 @@ -module Project::RemoteURL - - def load(remote_uri, path) - uri = URI.parse(remote_uri + path) - http = nil - content = nil - proxyuri = ENV['http_proxy'] - proxyuri = CONFIG['http_proxy'] if CONFIG['http_proxy'].present? - noproxy = ENV['no_proxy'] - noproxy = CONFIG['no_proxy'] if CONFIG['no_proxy'].present? - - noproxy_applies = false - if noproxy - np_split = noproxy.split(',') - noproxy_applies = np_split.any? { |np| uri.host.end_with?(np.strip) } +class Project::RemoteURL + def self.load(remote_project, path) + uri = URI.parse(remote_project.remoteurl + path) + # prefer environment variables if set + if ENV['http_proxy'].blank? + ENV['http_proxy'] = Configuration.first.http_proxy end - - if proxyuri && noproxy_applies == false - proxy = URI.parse(proxyuri) - proxy_user, proxy_pass = proxy.userinfo.split(/:/) if proxy.userinfo - http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy_user, proxy_pass).new(uri.host, uri.port) - else - http = Net::HTTP.new(uri.host, uri.port) + if ENV['no_proxy'].blank? + ENV['no_proxy'] = Configuration.first.no_proxy end - http.use_ssl = (uri.scheme == 'https') begin - http.start - response = http.get uri.request_uri - content = response.body if response.is_a?(Net::HTTPSuccess) - rescue SocketError, Errno::EINTR, Errno::EPIPE, EOFError, Net::HTTPBadResponse, IOError, Errno::ENETUNREACH, - Errno::ETIMEDOUT, Errno::ECONNREFUSED, Timeout::Error => err - Rails.logger.debug "#{err} when fetching #{uri}" - http = nil + uri.open.read + rescue OpenURI::HTTPError, SocketError, Errno::EINTR, Errno::EPIPE, EOFError, Net::HTTPBadResponse, IOError, Errno::ENETUNREACH, + Errno::ETIMEDOUT, Errno::ECONNREFUSED, Timeout::Error, OpenSSL::SSL::SSLError => err + Rails.logger.debug "#{err} when fetching #{path} from #{remote_project.remoteurl}" + nil end - http.finish if http && http.started? - content end - end