Skip to content

Commit

Permalink
move HTTP download code to registration/downloader
Browse files Browse the repository at this point in the history
and share it (used in EulaDownloader and SslCertificate classes)
  • Loading branch information
lslezak committed Aug 5, 2014
1 parent 75de9ee commit 4fffa22
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
65 changes: 65 additions & 0 deletions src/lib/registration/downloader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# encoding: utf-8

# ------------------------------------------------------------------------------
# Copyright (c) 2014 Novell, Inc. All Rights Reserved.
#
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail, you may find
# current contact information at www.novell.com.
# ------------------------------------------------------------------------------
#
#

require "yast"

require "net/http"
require "uri"
require "openssl"

module Registration

# module for downloading files, include it in the classes which need
# HTTP/HTTPS download support
module Downloader
include Yast::Logger

private

def download_file(file_url, insecure: false)
file_url = URI(file_url) unless file_url.is_a?(URI)
http = Net::HTTP.new(file_url.host, file_url.port)

# switch to HTTPS connection if needed
if file_url.is_a? URI::HTTPS
http.use_ssl = true
http.verify_mode = insecure ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
log.warn("Warning: SSL certificate verification disabled") if insecure
else
log.warn("Warning: Using insecure \"#{file_url.scheme}\" transfer protocol")
end

# TODO: handle redirection?
request = Net::HTTP::Get.new(file_url.request_uri)
response = http.request(request)

if response.is_a?(Net::HTTPSuccess)
return response.body
else
log.error "HTTP request failed: Error #{response.code}:#{response.message}: #{response.body}"
raise "Downloading #{file_url} failed: #{response.message}"
end
end

end
end
33 changes: 4 additions & 29 deletions src/lib/registration/eula_downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

require "yast"

require "net/http"
require "uri"
require "openssl"
require "registration/downloader"

module Registration

Expand All @@ -34,6 +32,8 @@ class EulaDownloader
attr_reader :base_url, :target_dir, :insecure

include Yast::Logger
# adds download_file() method
include Downloader

# name of the directory index file with list of available files
INDEX_FILE = "directory.yast"
Expand All @@ -54,7 +54,7 @@ def download
license_file_url.path = File.join(license_file_url.path, license)

log.info "Downloading license from #{license_file_url}..."
license_text = download_file(license_file_url)
license_text = download_file(license_file_url, insecure: insecure)
log.info "Downloaded license: #{license_text[0..32].inspect}... (#{license_text.bytesize} bytes)"

license_file_name = File.join(target_dir, license)
Expand All @@ -66,31 +66,6 @@ def download

private

def download_file(file_url)
file_url = URI(file_url) unless file_url.is_a?(URI)
http = Net::HTTP.new(file_url.host, file_url.port)

# switch to HTTPS connection if needed
if file_url.is_a? URI::HTTPS
http.use_ssl = true
http.verify_mode = insecure ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
log.warn("Warning: SSL certificate verification disabled") if insecure
else
log.warn("Warning: Using insecure \"#{file_url.scheme}\" transfer protocol")
end

# TODO: handle redirection?
request = Net::HTTP::Get.new(file_url.request_uri)
response = http.request(request)

if response.is_a?(Net::HTTPSuccess)
return response.body
else
log.error "HTTP request failed: Error #{response.code}:#{response.message}: #{response.body}"
raise "Downloading #{file_url} failed: #{response.message}"
end
end

# returns list of available files in a remote location
def available_licenses
# download the index file (directory.yast)
Expand Down
8 changes: 8 additions & 0 deletions src/lib/registration/ssl_certificate.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

require "openssl"
require "suse/connect"
require "registration/downloader"

module Registration

# class handling SSL certificate
class SslCertificate
# adds download_file() method
extend Downloader

attr_reader :x509_cert

Expand All @@ -23,6 +26,11 @@ def self.load(data)
SslCertificate.new(cert)
end

def self.download(url, insecure: false)
result = download_file(url, insecure: insecure)
load(result)
end

def sha1_fingerprint
::SUSE::Connect::YaST.cert_sha1_fingerprint(x509_cert)
end
Expand Down

0 comments on commit 4fffa22

Please sign in to comment.