From 072bb26a3078caa4d465d518db6e3a1c170f0c21 Mon Sep 17 00:00:00 2001 From: Max Lincoln Date: Tue, 22 Jul 2014 19:58:27 -0400 Subject: [PATCH] Change @ssl to @transport --- plugins/communicators/winrm/config.rb | 11 +++++---- plugins/communicators/winrm/errors.rb | 4 ++++ plugins/communicators/winrm/shell.rb | 11 +++++++-- templates/locales/comm_winrm.yml | 2 ++ .../plugins/communicators/winrm/shell_test.rb | 23 +++++++++++++++---- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/plugins/communicators/winrm/config.rb b/plugins/communicators/winrm/config.rb index 7ac28404e..0e7db6f63 100644 --- a/plugins/communicators/winrm/config.rb +++ b/plugins/communicators/winrm/config.rb @@ -8,7 +8,7 @@ module VagrantPlugins attr_accessor :guest_port attr_accessor :max_tries attr_accessor :timeout - attr_accessor :ssl + attr_accessor :transport def initialize @username = UNSET_VALUE @@ -18,19 +18,20 @@ module VagrantPlugins @guest_port = UNSET_VALUE @max_tries = UNSET_VALUE @timeout = UNSET_VALUE - @ssl = UNSET_VALUE + @transport = UNSET_VALUE @ssl_peer_verification = UNSET_VALUE end def finalize! @username = "vagrant" if @username == UNSET_VALUE @password = "vagrant" if @password == UNSET_VALUE + @transport = :ssl if @transport == UNSET_VALUE @host = nil if @host == UNSET_VALUE - @port = (@ssl ? 5986 : 5985) if @port == UNSET_VALUE - @guest_port = (@ssl ? 5986 : 5985) if @guest_port == UNSET_VALUE + is_ssl = @transport == :ssl + @port = (is_ssl ? 5986 : 5985) if @port == UNSET_VALUE + @guest_port = (is_ssl ? 5986 : 5985) if @guest_port == UNSET_VALUE @max_tries = 20 if @max_tries == UNSET_VALUE @timeout = 60 if @timeout == UNSET_VALUE - @ssl = false if @ssl == UNSET_VALUE @ssl_peer_verification = true if @ssl_peer_verification == UNSET_VALUE end diff --git a/plugins/communicators/winrm/errors.rb b/plugins/communicators/winrm/errors.rb index 552b8a300..90c3c6dda 100644 --- a/plugins/communicators/winrm/errors.rb +++ b/plugins/communicators/winrm/errors.rb @@ -29,6 +29,10 @@ module VagrantPlugins class WinRMFileTransferError < WinRMError error_key(:winrm_file_transfer_error) end + + class WinRMInvalidTransport < WinRMError + error_key(:invalid_transport) + end end end end diff --git a/plugins/communicators/winrm/shell.rb b/plugins/communicators/winrm/shell.rb index 7b3f98ae5..cd34b01fe 100644 --- a/plugins/communicators/winrm/shell.rb +++ b/plugins/communicators/winrm/shell.rb @@ -115,7 +115,7 @@ module VagrantPlugins @logger.info(" - Username: #{@config.username}") @logger.info(" - Transport: #{@config.transport}") - client = ::WinRM::WinRMWebService.new(endpoint, transport.to_sym, endpoint_options) + client = ::WinRM::WinRMWebService.new(endpoint, @config.transport.to_sym, endpoint_options) client.set_timeout(@config.timeout) client.toggle_nori_type_casting(:off) #we don't want coersion of types client @@ -126,7 +126,14 @@ module VagrantPlugins end def endpoint - "http#{@ssl ? 's' : ''}://#{@host}:#{@port}/wsman" + case @config.transport + when :ssl + "https://#{@host}:#{@port}/wsman" + when :plaintext + "http://#{@host}:#{@port}/wsman" + else + raise Errors::WinRMInvalidTransport, transport: @config.transport + end end def endpoint_options diff --git a/templates/locales/comm_winrm.yml b/templates/locales/comm_winrm.yml index 9de47a9cf..8800fbbb0 100644 --- a/templates/locales/comm_winrm.yml +++ b/templates/locales/comm_winrm.yml @@ -29,6 +29,8 @@ en: Message: %{message} invalid_shell: |- %{shell} is not a supported type of Windows shell. + invalid_transport: |- + %{transport} is not a supported WinRM transport. winrm_not_ready: |- The box is not able to report an address for WinRM to connect to yet. WinRM cannot access this Vagrant environment. Please wait for the diff --git a/test/unit/plugins/communicators/winrm/shell_test.rb b/test/unit/plugins/communicators/winrm/shell_test.rb index 5b694b62d..18d2dd021 100644 --- a/test/unit/plugins/communicators/winrm/shell_test.rb +++ b/test/unit/plugins/communicators/winrm/shell_test.rb @@ -7,6 +7,7 @@ describe VagrantPlugins::CommunicatorWinRM::WinRMShell do include_context "unit" let(:session) { double("winrm_session") } + let(:port) { config.transport == :ssl ? 5986 : 5985 } let(:config) { VagrantPlugins::CommunicatorWinRM::Config.new.tap do |c| c.username = 'username' @@ -16,7 +17,7 @@ describe VagrantPlugins::CommunicatorWinRM::WinRMShell do } subject do - described_class.new('localhost', 5985, config).tap do |comm| + described_class.new('localhost', port, config).tap do |comm| allow(comm).to receive(:new_session).and_return(session) end end @@ -50,15 +51,29 @@ describe VagrantPlugins::CommunicatorWinRM::WinRMShell do end describe ".endpoint" do - it "should create winrm endpoint address" do - expect(subject.send(:endpoint)).to eq("http://localhost:5985/wsman") + context 'when transport is :ssl' do + it "should create winrm endpoint address using https" do + expect(subject.send(:endpoint)).to eq("https://localhost:5986/wsman") + end + end + + context "when transport is :plaintext" do + let(:config) { + VagrantPlugins::CommunicatorWinRM::Config.new.tap do |c| + c.transport = :plaintext + c.finalize! + end + } + it "should create winrm endpoint address using http" do + expect(subject.send(:endpoint)).to eq("http://localhost:5985/wsman") + end end end describe ".endpoint_options" do it "should create endpoint options" do expect(subject.send(:endpoint_options)).to eq( - { user: "username", pass: "password", host: "localhost", port: 5985, + { user: "username", pass: "password", host: "localhost", port: 5986, basic_auth_only: true, no_ssl_peer_verification: false }) end end