From 18d229ca829536c115e351489f88891376232620 Mon Sep 17 00:00:00 2001 From: Jeremy Roberts Date: Sun, 30 Aug 2015 13:09:25 -0400 Subject: [PATCH 1/3] Added execution_time_limit for WinRM. Adds a configurable value for WinRm and the elevated permission shell ExecutionTimeLimit. Please see mitchellh/vagrant#5506 Ex: config.winrm.execution_time_limit = "P1D" --- plugins/communicators/winrm/communicator.rb | 3 ++- plugins/communicators/winrm/config.rb | 4 ++++ plugins/communicators/winrm/scripts/elevated_shell.ps1.erb | 5 +++-- plugins/communicators/winrm/shell.rb | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/communicators/winrm/communicator.rb b/plugins/communicators/winrm/communicator.rb index fdb3fe615..38fda78f9 100644 --- a/plugins/communicators/winrm/communicator.rb +++ b/plugins/communicators/winrm/communicator.rb @@ -210,7 +210,8 @@ module VagrantPlugins "powershell -executionpolicy bypass -file \"#{guest_script_path}\" " + "-username \"#{shell.username}\" -password \"#{shell.password}\" " + - "-encoded_command \"#{wrapped_encoded_command}\"" + "-encoded_command \"#{wrapped_encoded_command}\" " + + "-execution_time_limit \"#{shell.execution_time_limit}\"" end # Handles the raw WinRM shell result and converts it to a diff --git a/plugins/communicators/winrm/config.rb b/plugins/communicators/winrm/config.rb index 79901d503..3387ca142 100644 --- a/plugins/communicators/winrm/config.rb +++ b/plugins/communicators/winrm/config.rb @@ -11,6 +11,7 @@ module VagrantPlugins attr_accessor :timeout attr_accessor :transport attr_accessor :ssl_peer_verification + attr_accessor :execution_time_limit def initialize @username = UNSET_VALUE @@ -23,6 +24,7 @@ module VagrantPlugins @timeout = UNSET_VALUE @transport = UNSET_VALUE @ssl_peer_verification = UNSET_VALUE + @execution_time_limit = UNSET_VALUE end def finalize! @@ -37,6 +39,7 @@ module VagrantPlugins @retry_delay = 2 if @retry_delay == UNSET_VALUE @timeout = 1800 if @timeout == UNSET_VALUE @ssl_peer_verification = true if @ssl_peer_verification == UNSET_VALUE + @execution_time_limit = "PT2H" if @execution_time_limit == UNSET_VALUE end def validate(machine) @@ -49,6 +52,7 @@ module VagrantPlugins errors << "winrm.max_tries cannot be nil." if @max_tries.nil? errors << "winrm.retry_delay cannot be nil." if @max_tries.nil? errors << "winrm.timeout cannot be nil." if @timeout.nil? + errors << "winrm.execution_time_limit cannot be nil." if @execution_time_limit.nil? unless @ssl_peer_verification == true || @ssl_peer_verification == false errors << "winrm.ssl_peer_verification must be a boolean." end diff --git a/plugins/communicators/winrm/scripts/elevated_shell.ps1.erb b/plugins/communicators/winrm/scripts/elevated_shell.ps1.erb index 17767e436..66bc94043 100644 --- a/plugins/communicators/winrm/scripts/elevated_shell.ps1.erb +++ b/plugins/communicators/winrm/scripts/elevated_shell.ps1.erb @@ -1,4 +1,4 @@ -param([String]$username, [String]$password, [String]$encoded_command) +param([String]$username, [String]$password, [String]$encoded_command, [String]$execution_time_limit) $task_name = "WinRM_Elevated_Shell" $out_file = "$env:SystemRoot\Temp\WinRM_Elevated_Shell.log" @@ -33,7 +33,7 @@ $task_xml = @' false false false - PT2H + {execution_time_limit} 4 @@ -49,6 +49,7 @@ $arguments = "/c powershell.exe -EncodedCommand $encoded_command > $out_file $task_xml = $task_xml.Replace("{arguments}", $arguments) $task_xml = $task_xml.Replace("{username}", $username) +$task_xml = $task_xml.Replace("{execution_time_limit}", $execution_time_limit) $schedule = New-Object -ComObject "Schedule.Service" $schedule.Connect() diff --git a/plugins/communicators/winrm/shell.rb b/plugins/communicators/winrm/shell.rb index 9b0f4e302..967565046 100644 --- a/plugins/communicators/winrm/shell.rb +++ b/plugins/communicators/winrm/shell.rb @@ -37,6 +37,7 @@ module VagrantPlugins attr_reader :port attr_reader :username attr_reader :password + attr_reader :execution_time_limit attr_reader :config def initialize(host, port, config) @@ -47,6 +48,7 @@ module VagrantPlugins @port = port @username = config.username @password = config.password + @execution_time_limit = config.execution_time_limit @config = config end From 44154c92a90e32b5b5189ece0d12f086198bdd77 Mon Sep 17 00:00:00 2001 From: Jeremy Roberts Date: Sun, 30 Aug 2015 21:08:39 -0400 Subject: [PATCH 2/3] Fixing WinRM communicator unit test. --- test/unit/plugins/communicators/winrm/communicator_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/plugins/communicators/winrm/communicator_test.rb b/test/unit/plugins/communicators/winrm/communicator_test.rb index ceea0985d..42588a511 100644 --- a/test/unit/plugins/communicators/winrm/communicator_test.rb +++ b/test/unit/plugins/communicators/winrm/communicator_test.rb @@ -20,6 +20,7 @@ describe VagrantPlugins::CommunicatorWinRM::Communicator do before do allow(shell).to receive(:username).and_return('vagrant') allow(shell).to receive(:password).and_return('password') + allow(shell).to receive(:execution_time_limit).and_return('PT2H') end describe ".ready?" do @@ -56,7 +57,7 @@ describe VagrantPlugins::CommunicatorWinRM::Communicator do expect(shell).to receive(:upload).with(kind_of(String), "c:/tmp/vagrant-elevated-shell.ps1") expect(shell).to receive(:powershell) do |cmd| expect(cmd).to eq("powershell -executionpolicy bypass -file \"c:/tmp/vagrant-elevated-shell.ps1\" " + - "-username \"vagrant\" -password \"password\" -encoded_command \"ZABpAHIAOwAgAGUAeABpAHQAIAAkAEwAQQBTAFQARQBYAEkAVABDAE8ARABFAA==\"") + "-username \"vagrant\" -password \"password\" -encoded_command \"ZABpAHIAOwAgAGUAeABpAHQAIAAkAEwAQQBTAFQARQBYAEkAVABDAE8ARABFAA==\" -execution_time_limit \"PT2H\"") end.and_return({ exitcode: 0 }) expect(subject.execute("dir", { elevated: true })).to eq(0) end From c844a9c4fd587c78b8ada9c2ac521712aa99f0f2 Mon Sep 17 00:00:00 2001 From: Jeremy Roberts Date: Mon, 31 Aug 2015 11:27:08 -0400 Subject: [PATCH 3/3] Adding WinRM execution_time_limit to log. --- plugins/communicators/winrm/communicator.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/communicators/winrm/communicator.rb b/plugins/communicators/winrm/communicator.rb index 38fda78f9..6a11d985c 100644 --- a/plugins/communicators/winrm/communicator.rb +++ b/plugins/communicators/winrm/communicator.rb @@ -38,6 +38,7 @@ module VagrantPlugins # Got it! Let the user know what we're connecting to. @machine.ui.detail("WinRM address: #{shell.host}:#{shell.port}") @machine.ui.detail("WinRM username: #{shell.username}") + @machine.ui.detail("WinRM execution_time_limit: #{shell.execution_time_limit}") @machine.ui.detail("WinRM transport: #{shell.config.transport}") last_message = nil