Skip to content

Commit a250d39

Browse files
Merge pull request #249 from glennsarti/MODULES-8359-remove-xplat-winpowershell
(MODULES-8359) Remove non-Windows support for powershell provider
2 parents 6001b1f + 68ab3dd commit a250d39

File tree

3 files changed

+64
-186
lines changed

3 files changed

+64
-186
lines changed

lib/puppet/provider/exec/powershell.rb

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
require File.join(File.dirname(__FILE__), '../../../puppet_x/puppetlabs/powershell/powershell_manager')
44

55
Puppet::Type.type(:exec).provide :powershell, :parent => Puppet::Provider::Exec do
6+
confine :operatingsystem => :windows
7+
68
commands :powershell =>
79
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
810
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
911
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
1012
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
11-
elsif !Puppet::Util::Platform.windows?
12-
'pwsh'
1313
else
1414
'powershell.exe'
1515
end
@@ -68,33 +68,21 @@ def ps_manager
6868
end
6969

7070
def run(command, check = false)
71-
if Puppet::Util::Platform.windows?
72-
if !PuppetX::PowerShell::PowerShellManager.supported?
73-
self.class.upgrade_message
74-
write_script(command) do |native_path|
75-
# Ideally, we could keep a handle open on the temp file in this
76-
# process (to prevent TOCTOU attacks), and execute powershell
77-
# with -File <path>. But powershell complains that it can't open
78-
# the file for exclusive access. If we close the handle, then an
79-
# attacker could modify the file before we invoke powershell. So
80-
# we redirect powershell's stdin to read from the file. Current
81-
# versions of Windows use per-user temp directories with strong
82-
# permissions, but I'd rather not make (poor) assumptions.
83-
return super("cmd.exe /c \"\"#{native_path(command(:powershell))}\" #{legacy_args} -Command - < \"#{native_path}\"\"", check)
84-
end
85-
else
86-
return ps_manager.execute_resource(command, resource)
87-
end
88-
else
71+
if !PuppetX::PowerShell::PowerShellManager.supported?
72+
self.class.upgrade_message
8973
write_script(command) do |native_path|
9074
# Ideally, we could keep a handle open on the temp file in this
9175
# process (to prevent TOCTOU attacks), and execute powershell
9276
# with -File <path>. But powershell complains that it can't open
9377
# the file for exclusive access. If we close the handle, then an
9478
# attacker could modify the file before we invoke powershell. So
95-
# we redirect powershell's stdin to read from the file.
96-
return super("sh -c \"#{native_path(command(:powershell))} #{posix_args} -Command - < #{native_path}\"", check)
79+
# we redirect powershell's stdin to read from the file. Current
80+
# versions of Windows use per-user temp directories with strong
81+
# permissions, but I'd rather not make (poor) assumptions.
82+
return super("cmd.exe /c \"\"#{native_path(command(:powershell))}\" #{legacy_args} -Command - < \"#{native_path}\"\"", check)
9783
end
84+
else
85+
return ps_manager.execute_resource(command, resource)
9886
end
9987
end
10088

@@ -126,10 +114,4 @@ def native_path(path)
126114
def legacy_args
127115
'-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass'
128116
end
129-
130-
def posix_args
131-
# Note - using -ExecutionPolicy causes PowerShell to abort
132-
# https://github.com/PowerShell/PowerShell/issues/2742
133-
'-NoProfile -NonInteractive -NoLogo'
134-
end
135117
end

0 commit comments

Comments
 (0)