Skip to content

Commit

Permalink
Use the fixed syntax for IPv6 safe URI building via a backport.
Browse files Browse the repository at this point in the history
The backport in ManageIQ#2172 enables us to use the fixed syntax now.
  • Loading branch information
jrafanie committed Mar 24, 2015
1 parent cbff38e commit af55b05
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 32 deletions.
15 changes: 3 additions & 12 deletions lib/VMwareWebService/MiqVimClientBase.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

$:.push(File.dirname(__FILE__))
require 'pathname'
require Pathname.new(__dir__).join("../util/extensions/miq-uri")

require 'sync'
require 'VimService'
Expand Down Expand Up @@ -36,17 +37,7 @@ def initialize(server, username, password)
end

def sdk_uri
require 'uri'
uri = URI::HTTPS.build(:path => "/sdk")

# IPv6 addresses need to be wrapped in [] in URI's
# See: https://www.ietf.org/rfc/rfc2732.txt
# URI::Generic#hostname= will automatically wrap an ipv6 address in []
# uri.hostname = "::1"
# uri.to_s => "http://[::1]/foo"
# Feature request to URI::Generic.build: https://github.com/ruby/ruby/pull/765
uri.hostname = @server
uri
URI::HTTPS.build(:host => server, :path => "/sdk")
end

def self.receiveTimeout=(val)
Expand Down
9 changes: 1 addition & 8 deletions vmdb/app/models/ems_kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ def self.raw_connect(hostname, port, api_version)
end

def self.raw_api_endpoint(hostname, port)
require 'uri'

uri = URI::HTTP.build(:path => "/api", :port => port.to_i)

# URI::Generic#hostname= was added in ruby 1.9.3 and will automatically
# wrap an ipv6 address in []
uri.hostname = hostname
uri
URI::HTTP.build(:host => hostname, :port => port.to_i, :path => "/api")
end

def api_endpoint
Expand Down
8 changes: 1 addition & 7 deletions vmdb/app/models/ems_microsoft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ def self.raw_connect(username, password, auth_url)
WinRM::WinRMWebService.new(auth_url, :ssl, :user => username, :pass => password, :disable_sspi => true)
end

# Utilize URI::Generic#hostname to add support for IPv6 literals
# TODO: simplify this once https://github.com/ruby/ruby/pull/765 lands in our ruby
def self.auth_url(hostname, port = nil)
port ||= 5985
require 'uri'
uri = URI::HTTP.build(:port => port, :path => "/wsman")
uri.hostname = hostname
uri.to_s
URI::HTTP.build(:host => hostname, :port => port || 5985, :path => "/wsman").to_s
end

def connect(options = {})
Expand Down
6 changes: 1 addition & 5 deletions vmdb/app/models/mixins/web_server_worker_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ def self.all_ports_in_use
self.server_scope.all.collect { |w| w.port unless w.is_stopped? && !MiqProcess.is_worker?(w.pid)}.compact
end

# Utilize URI::Generic#hostname to add support for IPv6 literals
# TODO: simplify this once https://github.com/ruby/ruby/pull/765 lands in our ruby
def self.build_uri(port)
uri = URI::HTTP.build(:port => port)
uri.hostname = binding_address
uri.to_s
URI::HTTP.build(:host => binding_address, :port => port).to_s
end

def self.sync_workers
Expand Down
7 changes: 7 additions & 0 deletions vmdb/spec/models/ems_kubernetes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "spec_helper"

describe EmsKubernetes do
it ".raw_api_endpoint (ipv6)" do
expect(described_class.raw_api_endpoint("::1", 123).to_s).to eq "http://[::1]:123/api"
end
end
12 changes: 12 additions & 0 deletions vmdb/spec/models/mixins/web_server_worker_mixin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

describe WebServerWorkerMixin do
it "build_uri (ipv6)" do
class TestClass
include WebServerWorkerMixin
end

TestClass.stub(:binding_address => "::1")
expect(TestClass.build_uri(123)).to eq "http://[::1]:123"
end
end

0 comments on commit af55b05

Please sign in to comment.