Permalink
Browse files

Improved aws registry lookup

Using URI to parse uris
Using Resolv with Hosts and DNS to resolve aws registry address
  • Loading branch information...
1 parent 4d81cfa commit 7a63a63bb78c698ccef00cfec1961397023aee93 Jeffrey Peckham and Vinicius Fuentes committed Apr 2, 2013
@@ -122,20 +122,31 @@ def lookup_registry(user_data)
end
def bosh_lookup(hostname, nameservers)
- resolver = Resolv::DNS.new(:nameserver => nameservers)
- resolver.getaddress(hostname)
+ resolver = Resolv.new([Resolv::Hosts.new, Resolv::DNS.new(nameserver: nameservers)])
+ resolver.each_address(hostname) do |address|
+ begin
+ return address if IPAddr.new(address).ipv4?
+ rescue ArgumentError
+ end
+ end
+ raise Resolv::ResolvError, "Could not resolve #{hostname}"
end
def extract_registry_hostname(endpoint)
- match = endpoint.match(%r{https*://([^:]+):})
- unless match.size == 2
+ uri = URI.parse(endpoint)
+ hostname = uri.hostname
+
+ if hostname.nil?
raise LoadSettingsError, "Could not extract registry hostname"
end
- match[1]
+
+ hostname
end
def inject_registry_ip(ip, endpoint)
- endpoint.sub(%r{//[^:]+:}, "//#{ip}:")
+ uri = URI.parse(endpoint)
+ uri.hostname = ip
+ uri.to_s
end
def get_openssh_key
@@ -72,6 +72,20 @@ def body
endpoint.should == "http://4.3.2.1:25777"
end
+ it 'should allow registry endpoint with URI containing username and password' do
+ dns_mock = double('DNS Resolver')
+ dns_mock.should_receive(:each_address).with('example.org').and_yield('1.2.3.4')
+
+ Resolv::DNS.should_receive(:new).and_return(dns_mock)
+ hostname = "admin:admin@example.org"
+ nameservers = ["5.4.3.2"]
+
+ data = user_data("http://#{hostname}:25777", nameservers)
+ endpoint = described_class.lookup_registry(data)
+
+ endpoint.should == "http://admin:admin@1.2.3.4:25777"
+ end
+
it "should raise an error when it can't lookup the name" do
Bosh::Agent::Infrastructure::Aws::Registry.should_receive(:bosh_lookup)
.and_raise(Resolv::ResolvError)
@@ -8,6 +8,8 @@ network:
type: manual
vip: <%= vip %>
ip: 10.10.0.6
+ dns:
+ - 10.10.0.2
cloud_properties:
subnet: <%= subnet %>

0 comments on commit 7a63a63

Please sign in to comment.