Skip to content

Commit

Permalink
Simplifify loading from remote url
Browse files Browse the repository at this point in the history
ruby's OpenURI supports http_proxy and no_proxy (since 2.5),
so no need to carry our own implementation of it.

What's left is simplication of exception handling, but
I'm not so sure which exceptions are actually left to
be thrown
  • Loading branch information
coolo committed Sep 2, 2018
1 parent 6250a9a commit d4ef3ac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 47 deletions.
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions src/api/app/models/distribution.rb
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/api/app/models/project.rb
Expand Up @@ -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

Expand Down
46 changes: 13 additions & 33 deletions 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

0 comments on commit d4ef3ac

Please sign in to comment.