-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
download and confirm add-on licenses from the registration server
- Loading branch information
Showing
7 changed files
with
314 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# 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" | ||
|
||
module Registration | ||
|
||
# class for downloading addon EULAs from the registration server | ||
class EulaDownloader | ||
attr_accessor :base_url, :target_dir, :insecure | ||
|
||
include Yast::Logger | ||
|
||
# name of the directory index file with list of available files | ||
INDEX_FILE = "directory.yast" | ||
|
||
def initialize(base_url, target_dir, insecure: false) | ||
@base_url = base_url | ||
@target_dir = target_dir | ||
@insecure = insecure | ||
end | ||
|
||
# start the download | ||
def download | ||
licenses = download_license_index | ||
|
||
# download the files listed in the index | ||
licenses.each do |license| | ||
license_file_url = URI(base_url) | ||
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) | ||
log.info "Downloaded license size: #{license_text.size}" | ||
|
||
license_file_name = File.join(target_dir, license) | ||
|
||
log.info "Saving the license to file: #{license_file_name}" | ||
File.write(license_file_name, license_text) | ||
end | ||
end | ||
|
||
private | ||
|
||
def download_file(file_url) | ||
url = file_url.is_a?(URI) ? file_url : URI(file_url) | ||
http = Net::HTTP.new(url.host, url.port) | ||
|
||
# switch to HTTPS connection if needed | ||
if 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 \"#{url.scheme}\" transfer protocol") | ||
end | ||
|
||
# TODO: handle redirection? | ||
request = Net::HTTP::Get.new(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 "Download failed" | ||
end | ||
end | ||
|
||
# returns list of available files in a remote location | ||
def download_license_index | ||
# download the index file (directory.yast) | ||
index_url = URI(base_url) | ||
|
||
# add the index file to the URL path | ||
index_url.path = File.join(index_url.path, INDEX_FILE) | ||
|
||
# download the index | ||
log.info "Downloading license index from #{index_url}..." | ||
licenses = download_file(index_url).split | ||
|
||
# the index file itself might be also present in the list, just remove it | ||
licenses.reject!{|license| license == INDEX_FILE} | ||
log.info "Downloaded license index: #{licenses}" | ||
|
||
licenses | ||
end | ||
|
||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
require "yast" | ||
require "registration/eula_downloader" | ||
|
||
module Registration | ||
module UI | ||
|
||
class AddonEulaDialog | ||
include Yast::Logger | ||
include Yast::I18n | ||
include Yast::UIShortcuts | ||
|
||
attr_accessor :addons | ||
|
||
Yast.import "Popup" | ||
Yast.import "ProductLicense" | ||
Yast.import "Report" | ||
Yast.import "Wizard" | ||
|
||
# create a new dialog for accepting importing a SSL certificate and run it | ||
def self.run(selected_addons) | ||
dialog = AddonEulaDialog.new(selected_addons) | ||
dialog.run | ||
end | ||
|
||
# @param selected_addons | ||
def initialize(selected_addons) | ||
textdomain "registration" | ||
@addons = selected_addons | ||
end | ||
|
||
# display the EULA for each dialog and wait for a button click | ||
# @return [Symbol] user input (:import, :cancel) | ||
def run | ||
Yast::Wizard.SetContents( | ||
# dialog title | ||
_("License Agreement"), | ||
Label(_("Downloading Licenses...")), | ||
"", | ||
false, | ||
false | ||
) | ||
|
||
all_accepted = addons.all? do |addon| | ||
if addon.eula_url | ||
log.info "Addon '#{addon.short_name}' has an EULA at #{addon.eula_url}" | ||
accept_eula(addon) | ||
else | ||
# no EULA specified => accepted | ||
true | ||
end | ||
end | ||
|
||
# go back if any EULA has not been accepted, let the user deselect the | ||
# not accepted extension | ||
all_accepted ? :next : :back | ||
end | ||
|
||
private | ||
|
||
def accept_eula(addon) | ||
Dir.mktmpdir("extension-eula-") do |tmpdir| | ||
begin | ||
Yast::Popup.Feedback( | ||
_("Downloading License Agreement..."), | ||
addon.short_name | ||
) do | ||
# download the license (with translations) | ||
loader = EulaDownloader.new(addon.eula_url, tmpdir, | ||
insecure: Helpers.insecure_registration) | ||
|
||
loader.download | ||
end | ||
rescue Exception => e | ||
log.error "Download failed: #{e.message}: #{e.backtrace}" | ||
# %s is an extension name, e.g. "SUSE Linux Enterprise Software Development Kit" | ||
Yast::Report.Error(_("Downloading the license for\n%s\nfailed.") % addon.short_name) | ||
return false | ||
end | ||
|
||
Yast::ProductLicense.AskLicensesAgreementWithHeading( | ||
[tmpdir], | ||
Yast::ProductLicense.license_patterns, | ||
# do not continue if not accepted | ||
"abort", | ||
# enable [Back] | ||
true, | ||
# base product | ||
false, | ||
# require agreement | ||
true, | ||
# dialog title | ||
_("Extension and Module License Agreement"), | ||
# %s is an extension name, e.g. "SUSE Linux Enterprise Software Development Kit" | ||
_("%s License Agreement") % addon.short_name | ||
) == :accepted | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#! /usr/bin/env rspec | ||
|
||
require_relative "spec_helper" | ||
require_relative "yast_stubs" | ||
require 'tmpdir' | ||
|
||
describe "Registration::EulaDownloader" do | ||
|
||
before do | ||
stub_yast_require | ||
require "registration/eula_downloader" | ||
end | ||
|
||
describe ".download" do | ||
it "downloads the license with translations" do | ||
en_eula = "English EULA" | ||
de_eula = "Deutsch EULA" | ||
|
||
index = Net::HTTPSuccess.new("1.1", 200, "OK") | ||
index.should_receive(:body).and_return("directory.yast\nlicense.txt\nlicense.de.txt") | ||
|
||
license = Net::HTTPSuccess.new("1.1", 200, "OK") | ||
license.should_receive(:body).and_return(en_eula) | ||
|
||
license_de = Net::HTTPSuccess.new("1.1", 200, "OK") | ||
license_de.should_receive(:body).and_return(de_eula) | ||
|
||
# mock the responses for respective URL paths | ||
Net::HTTP.any_instance.stub(:request) do |request| | ||
case request.path | ||
when "/eula/directory.yast" | ||
index | ||
when "/eula/license.txt" | ||
license | ||
when "/eula/license.de.txt" | ||
license_de | ||
end | ||
end | ||
|
||
Dir.mktmpdir do |tmpdir| | ||
loader = Registration::EulaDownloader.new("https://example.com/eula", tmpdir) | ||
|
||
expect{loader.download}.not_to raise_error | ||
|
||
# the index file is not saved | ||
expect(Dir.entries(tmpdir)).to match_array([".", "..", "license.txt", "license.de.txt"]) | ||
# check the license content | ||
expect(File.read(File.join(tmpdir, "license.de.txt"))).to eq(de_eula) | ||
expect(File.read(File.join(tmpdir, "license.de.txt"))).to eq(de_eula) | ||
end | ||
end | ||
|
||
it "it raises an exception when download fails" do | ||
index = Net::HTTPNotFound.new("1.1", 404, "Not Found") | ||
index.should_receive(:body).and_return("") | ||
|
||
Net::HTTP.any_instance.should_receive(:request). | ||
with(an_instance_of(Net::HTTP::Get)).and_return(index) | ||
|
||
Dir.mktmpdir do |tmpdir| | ||
loader = Registration::EulaDownloader.new("https://example.com/eula", tmpdir) | ||
|
||
expect{loader.download}.to raise_error RuntimeError, "Download failed" | ||
|
||
# nothing saved | ||
expect(Dir.entries(tmpdir)).to match_array([".", ".."]) | ||
end | ||
end | ||
end | ||
|
||
end |