Skip to content

Commit

Permalink
Merge pull request #17222 from robbmanes/properly_validate_proxy_urls
Browse files Browse the repository at this point in the history
Resolve string handling for "https://" or "http://" in update_rhsm_conf
(cherry picked from commit b44f6b1)

https://bugzilla.redhat.com/show_bug.cgi?id=1572621
  • Loading branch information
carbonin authored and simaishi committed Apr 27, 2018
1 parent be9b69f commit f25f810
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
5 changes: 3 additions & 2 deletions app/models/registration_system.rb
Expand Up @@ -83,8 +83,9 @@ def self.update_rhsm_conf(options = {})

return unless option_values[:proxy_address]

write_rhsm_config(:proxy_hostname => option_values[:proxy_address].split(':')[0],
:proxy_port => option_values[:proxy_address].split(':')[1],
proxy_uri = URI.parse(option_values[:proxy_address].include?("://") ? option_values[:proxy_address] : "http://#{option_values[:proxy_address]}")
write_rhsm_config(:proxy_hostname => proxy_uri.host,
:proxy_port => proxy_uri.port,
:proxy_user => option_values[:proxy_username],
:proxy_password => option_values[:proxy_password])
end
Expand Down
35 changes: 21 additions & 14 deletions spec/models/registration_system_spec.rb
Expand Up @@ -181,10 +181,10 @@
ssl_verify_depth = 3
# an http proxy server to use
proxy_hostname = 192.0.2.0
proxy_hostname = ProxyHostnameValue
# port for http proxy server
proxy_port = myport
proxy_port = 0
# user name for authenticating to an http proxy, if needed
proxy_user = my_dummy_username
Expand All @@ -195,14 +195,9 @@
EOT
end

let(:rhsm_conf) { Tempfile.new(@spec_name.downcase) }
let(:rhsm_conf) { Tempfile.new }

before do
@proxy_args = {:registration_http_proxy_server => "192.0.2.0:myport",
:registration_http_proxy_username => "my_dummy_username",
:registration_http_proxy_password => "my_dummy_password"}

@spec_name = File.basename(__FILE__).split(".rb").first.freeze
stub_const("RegistrationSystem::RHSM_CONFIG_FILE", rhsm_conf.path)
rhsm_conf.write(original_rhsm_conf)
rhsm_conf.close
Expand All @@ -213,10 +208,22 @@
FileUtils.rm_f("#{rhsm_conf.path}.miq_orig")
end

it "will save then update the original config file" do
RegistrationSystem.update_rhsm_conf(@proxy_args)
expect(File.read("#{rhsm_conf.path}.miq_orig")).to eq(original_rhsm_conf)
expect(File.read(rhsm_conf)).to eq(updated_rhsm_conf)
context "will save then update the original config file" do
["", "http://", "https://"].each do |prefix|
["proxy.example.com", "192.0.2.0", "[2001:db8::]"].each do |address|
params = {
:registration_http_proxy_server => "#{prefix}#{address}:0",
:registration_http_proxy_username => "my_dummy_username",
:registration_http_proxy_password => "my_dummy_password"
}

it "with #{params[:registration_http_proxy_server]}" do
RegistrationSystem.update_rhsm_conf(params)
expect(File.read("#{rhsm_conf.path}.miq_orig")).to eq(original_rhsm_conf)
expect(File.read(rhsm_conf)).to eq(updated_rhsm_conf.sub(/ProxyHostnameValue/, address))
end
end
end
end

it "with no proxy server will not update the rhsm config file" do
Expand All @@ -227,10 +234,10 @@
it "with no options will use database valuses" do
MiqDatabase.seed
MiqDatabase.first.update_attributes(
:registration_http_proxy_server => "192.0.2.0:myport"
:registration_http_proxy_server => "192.0.2.0:0"
)
RegistrationSystem.update_rhsm_conf
expect(File.read(rhsm_conf)).to include("proxy_hostname = 192.0.2.0", "proxy_port = myport")
expect(File.read(rhsm_conf)).to include("proxy_hostname = 192.0.2.0", "proxy_port = 0")
end
end
end

0 comments on commit f25f810

Please sign in to comment.