Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ipv4_only option to limit connecting to guest IPv4 addresses only #12686

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions plugins/providers/hyperv/action/wait_for_ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ def initialize(app, env)

def call(env)
timeout = env[:machine].provider_config.ip_address_timeout
ipv4_only = env[:machine].provider_config.ipv4_only

env[:ui].output("Waiting for the machine to report its IP address...")
env[:ui].detail("Timeout: #{timeout} seconds")
if ipv4_only
env[:ui].detail("Waiting for IPv4 address only")
end

guest_ip = nil
Timeout.timeout(timeout) do
Expand All @@ -29,8 +33,8 @@ def call(env)

if guest_ip
begin
IPAddr.new(guest_ip)
break
ip = IPAddr.new(guest_ip)
break unless ipv4_only && !ip.ipv4?()
rescue IPAddr::InvalidAddressError
# Ignore, continue looking.
@logger.warn("Invalid IP address returned: #{guest_ip}")
Expand Down
7 changes: 7 additions & 0 deletions plugins/providers/hyperv/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Config < Vagrant.plugin("2", :config)
attr_accessor :vm_integration_services
# @return [Boolean] Enable Enhanced session mode
attr_accessor :enable_enhanced_session_mode
# @return [Boolean] Enable IPv4 only for connection to guest
attr_accessor :ipv4_only

def initialize
@ip_address_timeout = UNSET_VALUE
Expand All @@ -68,6 +70,7 @@ def initialize
@enable_checkpoints = UNSET_VALUE
@vm_integration_services = {}
@enable_enhanced_session_mode = UNSET_VALUE
@ipv4_only = UNSET_VALUE
end

def finalize!
Expand Down Expand Up @@ -107,6 +110,10 @@ def finalize!
@enable_checkpoints ||= @enable_automatic_checkpoints

@enable_enhanced_session_mode = false if @enable_enhanced_session_mode == UNSET_VALUE

if @ipv4_only == UNSET_VALUE
@ipv4_only = Vagrant::Util::Platform.wsl? ? true : false
end
end

def validate(machine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
let(:provider){ double("provider", driver: driver) }
let(:driver){ double("driver") }
let(:machine){ double("machine", provider: provider, provider_config: provider_config) }
let(:provider_config){ double("provider_config", ip_address_timeout: ip_address_timeout) }
let(:provider_config){ double("provider_config", ip_address_timeout: ip_address_timeout, ipv4_only: ipv4_only) }
let(:ip_address_timeout){ 1 }
let(:ipv4_only){ false }

let(:subject){ described_class.new(app, env) }

before do
allow(driver).to receive(:read_guest_ip).and_return("ip" => "127.0.0.1")
allow(app).to receive(:call)
allow(Vagrant::Util::Platform).to receive(:wsl?).and_return(false)
end

it "should call the app on success" do
Expand All @@ -35,4 +37,30 @@
expect(subject).to receive(:sleep)
subject.call(env)
end

it "should call the app on success with IPv6 address" do
expect(driver).to receive(:read_guest_ip).and_return("ip" => "FE80:0000:0000:0000:0202:B3FF:FE1E:8329")
subject.call(env)
end

context "IPv4_only" do
let(:ipv4_only){ true }

it "should call the app on success" do
expect(app).to receive(:call)
subject.call(env)
end

it "should set a timeout for waiting for IPv4 address" do
expect(Timeout).to receive(:timeout).with(ip_address_timeout)
subject.call(env)
end

it "should retry until it receives a valid IPv4 address" do
expect(driver).to receive(:read_guest_ip).and_return("ip" => "FE80:0000:0000:0000:0202:B3FF:FE1E:8329")
expect(driver).to receive(:read_guest_ip).and_return("ip" => "127.0.0.1")
expect(subject).to receive(:sleep)
subject.call(env)
end
end
end
27 changes: 26 additions & 1 deletion test/unit/plugins/providers/hyperv/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@
end
end


describe "#enable_enhanced_session_mode" do
it "is false by default" do
subject.finalize!
Expand All @@ -255,4 +254,30 @@
expect(subject.enable_enhanced_session_mode).to eq(true)
end
end

describe "#ipv4_only" do
context "within the WSL" do
before{ allow(Vagrant::Util::Platform).to receive(:wsl?).and_return(true) }

it "is true by default when in WSL" do
subject.finalize!
expect(subject.ipv4_only).to eq(true)
end
end

context "not within the WSL" do
before{ allow(Vagrant::Util::Platform).to receive(:wsl?).and_return(false) }

it "is false by default" do
subject.finalize!
expect(subject.ipv4_only).to eq(false)
end
end

it "can be set" do
subject.ipv4_only = true
subject.finalize!
expect(subject.ipv4_only).to eq(true)
end
end
end
1 change: 1 addition & 0 deletions website/content/docs/providers/hyperv/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ you may set. A complete reference is shown below:
- `enable_automatic_checkpoints` (boolean) Enable automatic checkpoints of the VM. Default: false
- `enable_enhanced_session_mode` (boolean) - Enable enhanced session transport type for the VM. Default: false
- `ip_address_timeout` (integer) - Number of seconds to wait for the VM to report an IP address. Default: 120.
- `ipv4_only` (boolean) - Only connect to a guests IPv4 address ignoring an IPv6 address. Default: false unless running in WSL, in which case it will default to true.
- `linked_clone` (boolean) - Use differencing disk instead of cloning entire VHD. Default: false
- `mac` (string) - MAC address for the guest network interface
- `maxmemory` (integer) - Maximum number of megabytes allowed to be allocated for the VM. When set Dynamic Memory Allocation will be enabled.
Expand Down