From fd948f85522363acb6d3ee5c3f1a745bc5da6217 Mon Sep 17 00:00:00 2001 From: guerremdq Date: Thu, 20 Mar 2014 17:01:45 -0300 Subject: [PATCH 1/5] Add sudo for redir --- lib/vagrant-lxc/action/clear_forwarded_ports.rb | 2 +- lib/vagrant-lxc/action/forward_ports.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-lxc/action/clear_forwarded_ports.rb b/lib/vagrant-lxc/action/clear_forwarded_ports.rb index 48dbe5b..52d4450 100644 --- a/lib/vagrant-lxc/action/clear_forwarded_ports.rb +++ b/lib/vagrant-lxc/action/clear_forwarded_ports.rb @@ -15,7 +15,7 @@ module Vagrant redir_pids.each do |pid| next unless is_redir_pid?(pid) @logger.debug "Killing pid #{pid}" - system "pkill -TERM -P #{pid}" + system "sudo pkill -TERM -P #{pid}" end @logger.info "Removing redir pids files" diff --git a/lib/vagrant-lxc/action/forward_ports.rb b/lib/vagrant-lxc/action/forward_ports.rb index 5bc4efe..accc70c 100644 --- a/lib/vagrant-lxc/action/forward_ports.rb +++ b/lib/vagrant-lxc/action/forward_ports.rb @@ -79,7 +79,7 @@ module Vagrant params = %W( --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} ) params.unshift "--laddr=#{host_ip}" if host_ip params << '--syslog' if ENV['REDIR_LOG'] - redir_cmd = "redir #{params.join(' ')} 2>/dev/null" + redir_cmd = "sudo redir #{params.join(' ')} 2>/dev/null" @logger.debug "Forwarding port with `#{redir_cmd}`" spawn redir_cmd From 697d8bde08c08a9f90d54cab402f10bf577a5664 Mon Sep 17 00:00:00 2001 From: guerremdq Date: Sun, 23 Mar 2014 13:39:42 -0300 Subject: [PATCH 2/5] only use sudo with redir when port number is lower than 1024 --- lib/vagrant-lxc/action/forward_ports.rb | 7 +- .../unit/action/clear_forwarded_ports_spec.rb | 4 +- spec/unit/action/sudo_forward_ports_spec.rb | 77 +++++++++++++++++++ 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 spec/unit/action/sudo_forward_ports_spec.rb diff --git a/lib/vagrant-lxc/action/forward_ports.rb b/lib/vagrant-lxc/action/forward_ports.rb index accc70c..afad40c 100644 --- a/lib/vagrant-lxc/action/forward_ports.rb +++ b/lib/vagrant-lxc/action/forward_ports.rb @@ -79,8 +79,11 @@ module Vagrant params = %W( --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} ) params.unshift "--laddr=#{host_ip}" if host_ip params << '--syslog' if ENV['REDIR_LOG'] - redir_cmd = "sudo redir #{params.join(' ')} 2>/dev/null" - + if host_port < 1024 + redir_cmd = "sudo redir #{params.join(' ')} 2>/dev/null" + else + redir_cmd = "redir #{params.join(' ')} 2>/dev/null" + end @logger.debug "Forwarding port with `#{redir_cmd}`" spawn redir_cmd end diff --git a/spec/unit/action/clear_forwarded_ports_spec.rb b/spec/unit/action/clear_forwarded_ports_spec.rb index 7f98e23..182ac3c 100644 --- a/spec/unit/action/clear_forwarded_ports_spec.rb +++ b/spec/unit/action/clear_forwarded_ports_spec.rb @@ -29,7 +29,7 @@ describe Vagrant::LXC::Action::ClearForwardedPorts do context 'with a valid redir pid' do it 'kills known processes' do - expect(subject).to have_received(:system).with("pkill -TERM -P #{pid}") + expect(subject).to have_received(:system).with("sudo pkill -TERM -P #{pid}") end end @@ -37,7 +37,7 @@ describe Vagrant::LXC::Action::ClearForwardedPorts do let(:pid_cmd) { 'sudo ls' } it 'does not kill the process' do - expect(subject).not_to have_received(:system).with("pkill -TERM -P #{pid}") + expect(subject).not_to have_received(:system).with("sudo pkill -TERM -P #{pid}") end end end diff --git a/spec/unit/action/sudo_forward_ports_spec.rb b/spec/unit/action/sudo_forward_ports_spec.rb new file mode 100644 index 0000000..e888bcb --- /dev/null +++ b/spec/unit/action/sudo_forward_ports_spec.rb @@ -0,0 +1,77 @@ +require 'unit_helper' + +require 'tmpdir' +require 'vagrant-lxc/provider' +require 'vagrant-lxc/action/forward_ports' + +describe Vagrant::LXC::Action::ForwardPorts do + let(:app) { double(:app, call: true) } + let(:env) { {machine: machine, ui: double(info: true)} } + let(:machine) { double(:machine) } + let!(:data_dir) { Pathname.new(Dir.mktmpdir) } + let(:provider) { double(Vagrant::LXC::Provider, ssh_info: {host: container_ip}) } + let(:host_ip) { '127.0.0.1' } + let(:host_port) { 80 } + let(:guest_port) { 80 } + let(:container_ip) { '10.0.1.234' } + let(:pid) { 'a-pid' } + let(:forward_conf) { {guest: guest_port, host: host_port, host_ip: host_ip} } + let(:networks) { [[:other_config, {}], [:forwarded_port, forward_conf]] } + + subject { described_class.new(app, env) } + + before do + machine.stub_chain(:config, :vm, :networks).and_return(networks) + machine.stub(provider: provider, data_dir: data_dir) + + subject.stub(exec: true) + subject.stub(spawn: pid) + end + + after { FileUtils.rm_rf data_dir.to_s } + + it 'forwards ports using redir' do + subject.stub(system: true) + subject.call(env) + expect(subject).to have_received(:spawn).with( + "sudo redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" + ) + end + + it 'skips --laddr parameter if host_ip is nil' do + forward_conf.delete(:host_ip) + subject.stub(system: true) + subject.call(env) + expect(subject).to have_received(:spawn).with( + "sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" + ) + end + + it 'skips --laddr parameter if host_ip is a blank string' do + forward_conf[:host_ip] = ' ' + subject.stub(system: true) + subject.call(env) + expect(subject).to have_received(:spawn).with( + "sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" + ) + end + + it "stores redir pids on machine's data dir" do + subject.stub(system: true) + subject.call(env) + pid_file = data_dir.join('pids', "redir_#{host_port}.pid").read + expect(pid_file).to eq(pid) + end + + it 'allows disabling a previously forwarded port' do + forward_conf[:disabled] = true + subject.stub(system: true) + subject.call(env) + expect(subject).not_to have_received(:spawn) + end + + it 'raises RedirNotInstalled error if `redir` is not installed' do + subject.stub(system: false) + expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RedirNotInstalled) + end +end From 36b38b76649d8015a627ea558f2626764ee9a243 Mon Sep 17 00:00:00 2001 From: Facundo Guerrero Date: Thu, 27 Mar 2014 15:25:12 -0300 Subject: [PATCH 3/5] add expect for warn message --- spec/unit/action/sudo_forward_ports_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/unit/action/sudo_forward_ports_spec.rb b/spec/unit/action/sudo_forward_ports_spec.rb index e888bcb..b81e559 100644 --- a/spec/unit/action/sudo_forward_ports_spec.rb +++ b/spec/unit/action/sudo_forward_ports_spec.rb @@ -33,6 +33,7 @@ describe Vagrant::LXC::Action::ForwardPorts do it 'forwards ports using redir' do subject.stub(system: true) subject.call(env) + expect(env).to have_received(:warn) expect(subject).to have_received(:spawn).with( "sudo redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) @@ -42,6 +43,7 @@ describe Vagrant::LXC::Action::ForwardPorts do forward_conf.delete(:host_ip) subject.stub(system: true) subject.call(env) + expect(env).to have_received(:warn) expect(subject).to have_received(:spawn).with( "sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) @@ -51,6 +53,7 @@ describe Vagrant::LXC::Action::ForwardPorts do forward_conf[:host_ip] = ' ' subject.stub(system: true) subject.call(env) + expect(env).to have_received(:warn) expect(subject).to have_received(:spawn).with( "sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) From d8d371984bc0de661e7b80f53cc02a4df2a1dcb3 Mon Sep 17 00:00:00 2001 From: idontdomath Date: Sat, 29 Mar 2014 20:21:14 -0300 Subject: [PATCH 4/5] enabled warning on the used environment for forward ports spec. removed warn checks on test were the condition didn't apply. --- spec/unit/action/sudo_forward_ports_spec.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spec/unit/action/sudo_forward_ports_spec.rb b/spec/unit/action/sudo_forward_ports_spec.rb index b81e559..77602ae 100644 --- a/spec/unit/action/sudo_forward_ports_spec.rb +++ b/spec/unit/action/sudo_forward_ports_spec.rb @@ -6,7 +6,7 @@ require 'vagrant-lxc/action/forward_ports' describe Vagrant::LXC::Action::ForwardPorts do let(:app) { double(:app, call: true) } - let(:env) { {machine: machine, ui: double(info: true)} } + let(:env) { {machine: machine, ui: double(info: true, warn: true)} } let(:machine) { double(:machine) } let!(:data_dir) { Pathname.new(Dir.mktmpdir) } let(:provider) { double(Vagrant::LXC::Provider, ssh_info: {host: container_ip}) } @@ -33,7 +33,6 @@ describe Vagrant::LXC::Action::ForwardPorts do it 'forwards ports using redir' do subject.stub(system: true) subject.call(env) - expect(env).to have_received(:warn) expect(subject).to have_received(:spawn).with( "sudo redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) @@ -43,7 +42,6 @@ describe Vagrant::LXC::Action::ForwardPorts do forward_conf.delete(:host_ip) subject.stub(system: true) subject.call(env) - expect(env).to have_received(:warn) expect(subject).to have_received(:spawn).with( "sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) @@ -53,7 +51,6 @@ describe Vagrant::LXC::Action::ForwardPorts do forward_conf[:host_ip] = ' ' subject.stub(system: true) subject.call(env) - expect(env).to have_received(:warn) expect(subject).to have_received(:spawn).with( "sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) From 0b1fffc6e3113dbec2103a3318aa32f6255e5f95 Mon Sep 17 00:00:00 2001 From: guerremdq Date: Tue, 29 Apr 2014 00:02:16 -0300 Subject: [PATCH 5/5] clean redir pid with sudo only if port is < than 1024 --- lib/vagrant-lxc/action/clear_forwarded_ports.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/vagrant-lxc/action/clear_forwarded_ports.rb b/lib/vagrant-lxc/action/clear_forwarded_ports.rb index 52d4450..bf6d1b3 100644 --- a/lib/vagrant-lxc/action/clear_forwarded_ports.rb +++ b/lib/vagrant-lxc/action/clear_forwarded_ports.rb @@ -13,9 +13,13 @@ module Vagrant if redir_pids.any? env[:ui].info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting") redir_pids.each do |pid| - next unless is_redir_pid?(pid) - @logger.debug "Killing pid #{pid}" - system "sudo pkill -TERM -P #{pid}" + next unless is_redir_pid?(pid[0]) + @logger.debug "Killing pid #{pid[0]}" + if pid[1] + system "sudo pkill -TERM -P #{pid[0]}" + else + system "pkill -TERM -P #{pid[0]}" + end end @logger.info "Removing redir pids files" @@ -31,7 +35,9 @@ module Vagrant def redir_pids @redir_pids = Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].map do |file| - File.read(file).strip.chomp + port_number = file.split(/[^\d]/).join + privileged_port = true if Integer(port_number) > 1024 + a = [ File.read(file).strip.chomp , privileged_port ] end end