Show warning if forwarding port less than 1024 [closes GH-487]

This commit is contained in:
Mitchell Hashimoto 2011-09-11 23:56:02 -07:00
parent 99646735d0
commit 7219f3d05b
5 changed files with 23 additions and 38 deletions

View File

@ -2,6 +2,8 @@
- Fix regression with remote paths from chef-solo. [GH-431] - Fix regression with remote paths from chef-solo. [GH-431]
- Fix issue where Vagrant crashes if `.vagrant` file becomes invalid. [GH-496] - Fix issue where Vagrant crashes if `.vagrant` file becomes invalid. [GH-496]
- Issue a warning instead of an error for attempting to forward a port
<= 1024. [GH-487]
## 0.8.6 (August 28, 2011) ## 0.8.6 (August 28, 2011)

View File

@ -10,7 +10,7 @@ module Vagrant
@app = app @app = app
@env = env @env = env
threshold_check unless ENV["USER"] == "root" threshold_check
external_collision_check external_collision_check
end end
@ -23,7 +23,10 @@ module Vagrant
# 1024, which causes the forwarded ports to fail. # 1024, which causes the forwarded ports to fail.
def threshold_check def threshold_check
@env.env.config.vm.forwarded_ports.each do |name, options| @env.env.config.vm.forwarded_ports.each do |name, options|
raise Errors::ForwardPortBelowThreshold if options[:hostport] <= 1024 if options[:hostport] <= 1024
@env.ui.warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
return
end
end end
end end

View File

@ -168,11 +168,6 @@ module Vagrant
error_key(:auto_empty, "vagrant.actions.vm.forward_ports") error_key(:auto_empty, "vagrant.actions.vm.forward_ports")
end end
class ForwardPortBelowThreshold < VagrantError
status_code(25)
error_key(:below_threshold_error, "vagrant.actions.vm.forward_ports")
end
class ForwardPortCollision < VagrantError class ForwardPortCollision < VagrantError
status_code(26) status_code(26)
error_key(:collision_error, "vagrant.actions.vm.forward_ports") error_key(:collision_error, "vagrant.actions.vm.forward_ports")

View File

@ -314,11 +314,6 @@ en:
VM: %{vm_name} VM: %{vm_name}
Forwarded port: %{name} (%{guest_port} => %{host_port}) Forwarded port: %{name} (%{guest_port} => %{host_port})
below_threshold_error: |-
The host port of all forwarded ports must be above 1024. VirtualBox
does not allow host ports to be below 1024. (Guest ports below 1024
are fine. For example: SSH on port 22 on the guest can be forwarded
to port 2222, but not 222).
collision_error: |- collision_error: |-
Vagrant cannot forward the specified ports on this VM, since they Vagrant cannot forward the specified ports on this VM, since they
would collide with another VirtualBox virtual machine's forwarded would collide with another VirtualBox virtual machine's forwarded
@ -336,6 +331,12 @@ en:
non_nat: |- non_nat: |-
VirtualBox adapter #%{adapter} not configured as "NAT" VirtualBox adapter #%{adapter} not configured as "NAT"
Skipping port forwarding '%{name}'. Skipping port forwarding '%{name}'.
privileged_ports: |-
You are trying to forward to privileged ports (ports <= 1024). Most
operating systems restrict this to only privileged process (typically
processes running as an administrative user). This is a warning in case
the port forwarding doesn't work. If any problems occur, please try a
port higher than 1024.
halt: halt:
force: Forcing shutdown of VM... force: Forcing shutdown of VM...
host_name: host_name:

View File

@ -9,7 +9,6 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
@vm.stubs(:name).returns("foo") @vm.stubs(:name).returns("foo")
@env["vm"] = @vm @env["vm"] = @vm
@env["vm.modify"] = mock("proc") @env["vm.modify"] = mock("proc")
ENV["USER"] = "not-root"
end end
context "initializing" do context "initializing" do
@ -26,34 +25,19 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
@env.env.config.vm.forwarded_ports.clear @env.env.config.vm.forwarded_ports.clear
end end
context "for non-root users" do should "issue a warning for ports less than 1024" do
should "error if has a port below threshold" do
@env.env.config.vm.forward_port("foo", 22, 222) @env.env.config.vm.forward_port("foo", 22, 222)
assert_raises(Vagrant::Errors::ForwardPortBelowThreshold) { @klass.new(@app, @env) }
@env.ui.expects(:warn).once
@klass.new(@app, @env)
end end
should "not error if ports are fine" do should "not issue a warning for ports greater than 1024" do
@env.env.config.vm.forward_port("foo", 22, 2222) @env.env.config.vm.forward_port("foo", 22, 2222)
assert_nothing_raised { @klass.new(@app, @env) }
end
@env.ui.expects(:warn).never
@klass.new(@app, @env)
end end
context "for a root user" do
setup do
ENV["USER"] = "root"
end
should "not error for any port" do
@env.env.config.vm.forward_port("foo", 22, 222)
assert_nothing_raised { @klass.new(@app, @env) }
@env.env.config.vm.forward_port("foo", 22, 2222)
assert_nothing_raised { @klass.new(@app, @env) }
end
end
end end
context "checking for colliding external ports" do context "checking for colliding external ports" do