Skip to content

Commit

Permalink
check for old SMT server API when registration fails (bnc#889503)
Browse files Browse the repository at this point in the history
display error about outdated SMT server

- 3.1.106
  • Loading branch information
lslezak committed Aug 19, 2014
1 parent e585481 commit 1d10af6
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 3 deletions.
7 changes: 7 additions & 0 deletions package/yast2-registration.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Tue Aug 19 14:17:23 UTC 2014 - lslezak@suse.cz

- check for old SMT server API when registration fails, display
error about outdated SMT server (bnc#889503)
- 3.1.106

-------------------------------------------------------------------
Mon Aug 18 13:34:05 UTC 2014 - lslezak@suse.cz

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-registration.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-registration
Version: 3.1.105
Version: 3.1.106
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
28 changes: 28 additions & 0 deletions src/lib/registration/connect_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require "registration/helpers"
require "registration/exceptions"
require "registration/storage"
require "registration/smt_status"
require "registration/ssl_certificate"
require "registration/ssl_certificate_details"
require "registration/url_helpers"
Expand Down Expand Up @@ -107,6 +108,11 @@ def self.catch_registration_errors(message_prefix: "", show_update_hint: false,
e.message << "\n\n\n" + msg
end

report_error(message_prefix + _("Registration failed."), e)
when 404
# add an extra message when an old SMT server if found
check_smt_api(e)

report_error(message_prefix + _("Registration failed."), e)
when 422
# Error popup
Expand Down Expand Up @@ -229,6 +235,28 @@ def self.report_ssl_error(message, cert)
)
end

def self.check_smt_api(e)
url = UrlHelpers.registration_url
if !url.nil?
# test old SMT instance
smt_status = SmtStatus.new(url, insecure: Helpers.insecure_registration)

if smt_status.ncc_api_present?
# display just the hostname in the server URL
display_url = URI(url)
display_url.path = ""
display_url.query = nil
# TRANSLATORS: error message, %s is a server URL,
# e.g. https://smt.example.com
msg = _("An old registration server was detected at\n%s.\n" \
"Make sure the latest product supporting the new registration\n" \
"protocol is installed at the server.") % display_url

e.message << "\n\n" + msg
end
end
end

end

end
3 changes: 2 additions & 1 deletion src/lib/registration/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require "net/http"
require "uri"
require "openssl"
require "registration/exceptions"

module Registration

Expand Down Expand Up @@ -69,7 +70,7 @@ def self.download_file(file_url, insecure: false, redirection_count: 10)
download_file(location, insecure: insecure, redirection_count: redirection_count - 1)
else
log.error "HTTP request failed: Error #{response.code}:#{response.message}: #{response.body}"
raise "Downloading #{file_url} failed: #{response.message}"
raise DownloadError, "Downloading #{file_url} failed: #{response.message}"
end
end

Expand Down
3 changes: 3 additions & 0 deletions src/lib/registration/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ def initialize(msg, service)
@service = service
end
end

class DownloadError < RuntimeError
end
end
46 changes: 46 additions & 0 deletions src/lib/registration/smt_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

require "registration/downloader"
require "uri"

module Registration

class SmtStatus
include Yast::Logger

attr_reader :url, :insecure

def initialize(url, insecure: false)
@url = url.is_a?(URI) ? url : URI(url)
@insecure = insecure
end

# check whether (old) NCC API is present at the server
def ncc_api_present?
download_url = ncc_api_url
log.info "Checking NCC API presence: #{download_url}"

begin
Downloader.download(download_url, insecure: insecure)
log.info "NCC API found"
return true
rescue DownloadError
log.info "Download failed, NCC API probably not present"
return false
end
end

private

def ncc_api_url
# create an URI copy, the URL will be modified
ncc_url = url.dup

# NCC API should provide "/center/regsvc?command=listproducts" query
ncc_url.path = "/center/regsvc"
ncc_url.query = "command=listproducts"

ncc_url
end
end

end
2 changes: 1 addition & 1 deletion test/downloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
expect_any_instance_of(Net::HTTP).to receive(:request).
with(an_instance_of(Net::HTTP::Get)).and_return(index)

expect{Registration::Downloader.download(url)}.to raise_error RuntimeError,
expect{Registration::Downloader.download(url)}.to raise_error Registration::DownloadError,
"Downloading #{url} failed: Not Found"
end

Expand Down
35 changes: 35 additions & 0 deletions test/smt_status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /usr/bin/env rspec

require_relative "spec_helper"
require_relative "yast_stubs"

describe "Registration::SmtStatus" do
before do
stub_yast_require
require "registration/smt_status"
end

let(:url) { "https://example.com" }
subject { Registration::SmtStatus.new(url) }

describe "#ncc_api_present?2" do
let(:expected_url) { URI("https://example.com/center/regsvc?command=listproducts") }

it "returns true when /center/regsvc?command=listproducts returns OK" do
expect(Registration::Downloader).to receive(:download).
with(expected_url, :insecure => false).
and_return(true)

expect(subject.ncc_api_present?).to be_true
end

it "returns false otherwise" do
expect(Registration::Downloader).to receive(:download).
with(expected_url, :insecure => false).
and_raise(Registration::DownloadError)

expect(subject.ncc_api_present?).to be_false
end
end

end

0 comments on commit 1d10af6

Please sign in to comment.