Filter any machine_ip when preparing the NFS settings

When preparing the NFS settings on VirtualBox the guest IP addresses
are pulled from VirtualBox directly and any static addresses are
pulled as well. This can lead to aquiring a host IP and machine IP
but results in a failure of NFS mount because the IPs are not on
the same network. This filters the machine IP result to validate
it is within the host adapter IP range.
This commit is contained in:
Chris Roberts 2017-07-19 17:02:01 -07:00
parent d14b230b6c
commit 4bc8cc7563
2 changed files with 47 additions and 3 deletions

View File

@ -1,3 +1,4 @@
require "ipaddr"
require "vagrant/action/builtin/mixin_synced_folders"
module VagrantPlugins
@ -59,6 +60,23 @@ module VagrantPlugins
end
end
if host_ip && !machine_ip.empty?
interface = @machine.provider.driver.read_host_only_interfaces.detect do |iface|
iface[:ip] == host_ip
end
host_ipaddr = IPAddr.new("#{host_ip}/#{interface.fetch(:netmask, "0.0.0.0")}")
case machine_ip
when String
machine_ip = nil if !host_ipaddr.include?(machine_ip)
when Array
machine_ip.delete_if do |m_ip|
!host_ipaddr.include?(m_ip)
end
machine_ip = nil if machine_ip.empty?
end
end
raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip
env[:nfs_host_ip] = host_ip

View File

@ -47,6 +47,10 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do
end
context "with an nfs synced folder" do
let(:host_only_interfaces) {
[{name: "vmnet2", ip: "1.2.3.4"}]
}
before do
# We can't be on Windows, because NFS gets disabled on Windows
Vagrant::Util::Platform.stub(windows?: false)
@ -58,9 +62,7 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do
driver.stub(read_network_interfaces: {
2 => {type: :hostonly, hostonly: "vmnet2"},
})
driver.stub(read_host_only_interfaces: [
{name: "vmnet2", ip: "1.2.3.4"},
])
driver.stub(read_host_only_interfaces: host_only_interfaces)
allow(driver).to receive(:read_guest_ip).with(1).and_return("2.3.4.5")
# override sleep to 0 so test does not take seconds
@ -68,6 +70,30 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do
allow(subject).to receive(:retry_options).and_return(retry_options.merge(sleep: 0))
end
context "with host interface netmask defined" do
context "with machine IP included within host interface range" do
let(:host_only_interfaces) {
[{name: "vmnet2", ip: "2.3.4.1", netmask: "255.255.255.0"}]
}
it "sets nfs_host_ip and nfs_machine_ip properly" do
subject.call(env)
expect(env[:nfs_host_ip]).to eq("2.3.4.1")
expect(env[:nfs_machine_ip]).to eq("2.3.4.5")
end
end
context "with machine IP included within host interface range" do
let(:host_only_interfaces) {
[{name: "vmnet2", ip: "1.2.3.4", netmask: "255.255.255.0"}]
}
it "raises an error when the machine IP is not within host interface range" do
expect{ subject.call(env) }.to raise_error(Vagrant::Errors::NFSNoHostonlyNetwork)
end
end
end
it "sets nfs_host_ip and nfs_machine_ip properly" do
subject.call(env)