Skip to content

Commit

Permalink
cdn-proxy - let proxy to be configured when calling CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
iNecas committed Nov 15, 2011
1 parent 7d1da2e commit 7929063
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/config/katello.yml
Expand Up @@ -76,6 +76,8 @@ common:
sync_threads: 4
#sync_KBlimit: 100



# Setup your foreman environment here
foreman:
url: https://foreman.instance.company.com:443
Expand All @@ -84,6 +86,15 @@ common:
oauth_key: cloud_forms
oauth_secret: shhh

# Setup proxy to use when accessing CDN.
# Katello calls CDN to retrieve list of repositories for a product
# Uncomment in case you want this calls to go through your proxy
# cdn_proxy:
# host: localhost
# port: 3128
# user: username
# password: password

#
# Production environment configuration
#
Expand Down
55 changes: 53 additions & 2 deletions src/lib/resources/cdn.rb
Expand Up @@ -31,24 +31,75 @@ def self.parse_version(releasever)

class CdnResource
attr_reader :url
attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_password

def initialize url, options = {}
options.reverse_merge!(:verify_ssl => 9)
options.assert_valid_keys(:ssl_client_key, :ssl_client_cert, :ssl_ca_file, :verify_ssl)
if options[:ssl_client_cert]
options.reverse_merge!(:ssl_ca_file => CdnResource.ca_file)
end
@resource = RestClient::Resource.new url, options
load_proxy_settings

@uri = URI.parse(url)
@net = net_http_class.new(@uri.host, @uri.port)
@net.use_ssl = @uri.is_a?(URI::HTTPS)

@net.cert = options[:ssl_client_cert]
@net.key = options[:ssl_client_key]
@net.ca_file = options[:ssl_ca_file]

if (options[:verify_ssl] == false) || (options[:verify_ssl] == OpenSSL::SSL::VERIFY_NONE)
@net.verify_mode = OpenSSL::SSL::VERIFY_NONE
elsif options[:verify_ssl].is_a? Integer
@net.verify_mode = options[:verify_ssl]
@net.verify_callback = lambda do |preverify_ok, ssl_context|
if (!preverify_ok) || ssl_context.error != 0
err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
raise RestClient::SSLCertificateNotVerified.new(err_msg)
end
true
end
end
end

def get(path, headers={})
@resource[path].get headers
path = File.join(@uri.request_uri,path)
Rails.logger.debug "CDN: Requesting path #{path}"
req = Net::HTTP::Get.new(path)
begin
@net.start do |http|
res = http.request(req, nil) { |http_response| http_response.read_body }
return res.body
end
rescue EOFError
raise RestClient::ServerBrokeConnection
rescue Timeout::Error
raise RestClient::RequestTimeout
end
end

def self.ca_file
"#{Rails.root}/ca/redhat-uep.pem"
end

def net_http_class
if proxy_host
Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_password)
else
Net::HTTP
end
end

def load_proxy_settings
if AppConfig.cdn_proxy
self.proxy_host = AppConfig.cdn_proxy.host
self.proxy_port = AppConfig.cdn_proxy.port
self.proxy_user = AppConfig.cdn_proxy.user
self.proxy_password = AppConfig.cdn_proxy.password
end
end

end

class CdnVarSubstitutor
Expand Down
45 changes: 29 additions & 16 deletions src/spec/lib/cdn_spec.rb
Expand Up @@ -13,28 +13,41 @@
CDN::CdnVarSubstitutor.new(provider_url, connect_options)
end

before(:each) do
@rest_client_mock = RestClient::Resource.new(provider_url, connect_options)
RestClient::Resource.stub(:new).with(provider_url, connect_options).and_return(@rest_client_mock)
@rest_client_mock.stub(:"[]").and_return do |path|
path_mock = mock
path_mock.stub!(:get).and_return do |headers|
case path.count("/")
when 7 then "i386\nx86_64"
when 6 then "6\n61"
else raise "unexpected count of nested paths: #{path}"
end
end
path_mock
end
end

it "should substitute all variables with values in listings" do
stub_cdn_requests(["6","61"],["i386", "x86_64"])
substitutions_with_urls = subject.substitute_vars(path_with_variables)
substitutions_with_urls[{"releasever" => "6", "basearch" => "i386"}].should == "/content/dist/rhel/server/5/6/i386/os"
substitutions_with_urls[{"releasever" => "61", "basearch" => "i386"}].should == "/content/dist/rhel/server/5/61/i386/os"
substitutions_with_urls[{"releasever" => "6", "basearch" => "x86_64"}].should == "/content/dist/rhel/server/5/6/x86_64/os"
substitutions_with_urls[{"releasever" => "61", "basearch" => "x86_64"}].should == "/content/dist/rhel/server/5/61/x86_64/os"
substitutions_with_urls.should have(4).items
end

it "should be able to use proxy" do
AppConfig.cdn_proxy = OpenStruct.new(:host => "localhost", :port => 3128, :user => "test", :password => "pwd")

Net::HTTP.stub("Proxy" => Net::HTTP)
Net::HTTP.should_receive("Proxy").with("localhost", 3128, "test", "pwd")

subject
end

# all requests for listing releasevers and basearchs reeturn the values in
# arguments.
def stub_cdn_requests(releasevers, basearchs)
uri = URI.parse(provider_url)
net_mock = Net::HTTP.new(uri.host, uri.port)
Net::HTTP.stub(:new).with(uri.host, uri.port).and_return(net_mock)
request_mock = mock
request_mock.stub!(:request).and_return do |req,headers|
body = case req.path.count("/")
when 6 then releasevers.join("\n")
when 7 then basearchs.join("\n")
else raise "unexpected count of nested paths: #{req.path}"
end
mock(:body => body)
end
net_mock.stub(:start).and_yield(request_mock)
end
end

0 comments on commit 7929063

Please sign in to comment.