From 6d228becf977aa573c6d33558b65d84e72cba3ca Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Thu, 19 Mar 2020 17:16:21 -0400 Subject: [PATCH] Raise an error if host IP is not found --- .../builtin/handle_forwarded_port_collisions.rb | 6 +++++- lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 4 ++++ .../handle_forwarded_port_collisions_test.rb | 17 +++++++++++------ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index 186636c05..ffa9bad67 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -271,7 +271,11 @@ module Vagrant end else # Do a regular check - is_port_open?(host_ip, host_port) + if test_host_ip == "0.0.0.0" || ipv4_addresses.include?(test_host_ip) + is_port_open?(test_host_ip, host_port) + else + raise Errors::ForwardPortHostIPNotFound + end end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index efc0e2d65..38cce7967 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -400,6 +400,10 @@ module Vagrant error_key(:auto_empty, "vagrant.actions.vm.forward_ports") end + class ForwardPortHostIPNotFound < VagrantError + error_key(:host_ip_not_found, "vagrant.actions.vm.forward_ports") + end + class ForwardPortCollision < VagrantError error_key(:collision_error, "vagrant.actions.vm.forward_ports") end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 743be4761..75d649c34 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2321,6 +2321,10 @@ en: forwarding: Forwarding ports... forwarding_entry: |- %{guest_port} (guest) => %{host_port} (host) (adapter %{adapter}) + host_ip_not_found: + You are trying to forward a host IP that does not exist. Please set `host_ip` + to the address of an existing IPv4 network interface, or remove the option + from your port forward configuration. non_nat: |- VirtualBox adapter #%{adapter} not configured as "NAT". Skipping port forwards on this adapter. diff --git a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb index 8a5eca4c4..c429cf3e5 100644 --- a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb +++ b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb @@ -177,9 +177,11 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do describe "#port_check" do let(:host_ip){ "127.0.0.1" } let(:host_port){ 8080 } + let(:test_ips) { [ "127.0.0.1", "192.168.1.7" ] } before do instance.instance_variable_set(:@machine, machine) + allow(instance).to receive(:ipv4_addresses).and_return(test_ips) end it "should check if the port is open" do @@ -187,13 +189,8 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do instance.send(:port_check, host_ip, host_port) end - context "when host_ip is 0.0.0.0" do + context "when host ip is 0.0.0.0" do let(:host_ip) { "0.0.0.0" } - let(:test_ips) { [ "127.0.0.1", "192.168.1.7" ] } - - before do - allow(instance).to receive(:ipv4_addresses).and_return(test_ips) - end context "on windows" do let(:guest) { :windows } @@ -221,5 +218,13 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do end end end + + context "when host ip does not exist" do + let(:host_ip) { "127.0.0.2" } + + it "should raise an error" do + expect{ instance.send(:port_check, host_ip, host_port) }.to raise_error(Vagrant::Errors::ForwardPortHostIPNotFound) + end + end end end