Skip to content

Commit b16c32a

Browse files
sakasusu0919spesnova717
authored andcommitted
Fixes #38973 - SSH API of BMC now resolves FQDN to IP
Previously, Smart Proxy's ip() method for SSH API of BMC directly returned the :host parameter to Foreman. Now, if :host is an FQDN, it is resolved to an IP address before being returned. If :host is already an IP, it's returned directly. Co-Authored-By: Yusuke Hirota <hirota.yusuke@fujitsu.com>
1 parent cbfe296 commit b16c32a

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

modules/bmc/ssh.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'ipaddr'
2+
require 'resolv'
3+
14
module Proxy
25
module BMC
36
class SSH < Base
@@ -62,7 +65,15 @@ def powercycle
6265
end
6366

6467
def ip
65-
host
68+
IPAddr.new(host).to_s
69+
rescue IPAddr::InvalidAddressError
70+
begin
71+
logger.debug("Host '#{host}' is not an IP address, attempting to resolve as FQDN.")
72+
Resolv.getaddress(host)
73+
rescue Resolv::ResolvError => e
74+
logger.warn("Failed to resolve FQDN '#{host}': #{e.class} - #{e.message}")
75+
''
76+
end
6677
end
6778

6879
# the following are dummy implementations

test/bmc/bmc_api_ssh_test.rb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'json'
33
require 'bmc/bmc_api'
44
require 'bmc/ssh'
5+
require 'bmc/bmc_plugin'
56

67
ENV['RACK_ENV'] = 'test'
78

@@ -47,10 +48,35 @@ def test_powercycle
4748
assert_equal 200, last_response.status
4849
end
4950

50-
def test_lan_ip
51-
Proxy::BMC::SSH.any_instance.expects(:ip).returns('')
52-
get "/#{@host}/lan/ip", @args
51+
def test_lan_ip_when_host_is_ip
52+
host = "192.168.1.1"
53+
get "/#{host}/lan/ip", @args
5354
assert_equal 200, last_response.status
55+
assert_equal host, JSON.parse(last_response.body)["result"]
56+
end
57+
58+
def test_lan_ip_when_host_is_ipv6
59+
host = '2001:db8::1'
60+
get "/#{host}/lan/ip", @args
61+
assert_equal 200, last_response.status
62+
assert_equal host, JSON.parse(last_response.body)['result']
63+
end
64+
65+
def test_lan_ip_when_host_is_resolvable_fqdn
66+
host = "resolvable.example.com"
67+
resolved_ip = "192.168.1.2"
68+
Resolv.expects(:getaddress).with(host).returns(resolved_ip)
69+
get "/#{host}/lan/ip", @args
70+
assert_equal 200, last_response.status
71+
assert_equal resolved_ip, JSON.parse(last_response.body)["result"]
72+
end
73+
74+
def test_lan_ip_when_host_is_unresolvable_fqdn
75+
host = "unresolvable.example.com"
76+
Resolv.expects(:getaddress).with(host).raises(Resolv::ResolvError.new("no address for #{host}"))
77+
get "/#{host}/lan/ip", @args
78+
assert_equal 200, last_response.status
79+
assert_equal "", JSON.parse(last_response.body)["result"]
5480
end
5581

5682
def test_lan_mac

0 commit comments

Comments
 (0)