chef 15.8.23-universal-mingw32 → 15.9.17-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -2
  3. data/README.md +1 -1
  4. data/Rakefile +9 -0
  5. data/distro/powershell/chef/chef.psm1 +459 -459
  6. data/distro/templates/powershell/chef/chef.psm1.erb +459 -0
  7. data/lib/chef/application.rb +1 -1
  8. data/lib/chef/chef_fs/chef_fs_data_store.rb +1 -1
  9. data/lib/chef/chef_fs/command_line.rb +5 -5
  10. data/lib/chef/cookbook/metadata.rb +5 -4
  11. data/lib/chef/cookbook/synchronizer.rb +1 -0
  12. data/lib/chef/cookbook_loader.rb +1 -1
  13. data/lib/chef/data_collector/config_validation.rb +7 -7
  14. data/lib/chef/deprecated.rb +5 -5
  15. data/lib/chef/dsl/platform_introspection.rb +2 -2
  16. data/lib/chef/dsl/reboot_pending.rb +3 -3
  17. data/lib/chef/http/json_output.rb +1 -1
  18. data/lib/chef/knife.rb +1 -1
  19. data/lib/chef/knife/bootstrap/templates/README.md +2 -2
  20. data/lib/chef/knife/bootstrap/templates/chef-full.erb +9 -9
  21. data/lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb +2 -2
  22. data/lib/chef/knife/core/ui.rb +1 -1
  23. data/lib/chef/knife/core/windows_bootstrap_context.rb +8 -8
  24. data/lib/chef/knife/data_bag_edit.rb +2 -2
  25. data/lib/chef/knife/edit.rb +1 -1
  26. data/lib/chef/knife/environment_compare.rb +1 -1
  27. data/lib/chef/knife/list.rb +1 -1
  28. data/lib/chef/mixin/api_version_request_handling.rb +1 -1
  29. data/lib/chef/provider/group/usermod.rb +2 -5
  30. data/lib/chef/provider/mount/mount.rb +1 -1
  31. data/lib/chef/provider/package/homebrew.rb +1 -2
  32. data/lib/chef/provider/package/rubygems.rb +1 -1
  33. data/lib/chef/provider/package/yum/python_helper.rb +2 -2
  34. data/lib/chef/provider/package/yum/rpm_utils.rb +4 -4
  35. data/lib/chef/provider/package/zypper.rb +2 -1
  36. data/lib/chef/provider/remote_file.rb +2 -2
  37. data/lib/chef/provider/route.rb +4 -4
  38. data/lib/chef/provider/windows_env.rb +3 -3
  39. data/lib/chef/resource.rb +7 -2
  40. data/lib/chef/resource/build_essential.rb +46 -22
  41. data/lib/chef/resource/cron.rb +1 -1
  42. data/lib/chef/resource/cron_d.rb +1 -1
  43. data/lib/chef/resource/execute.rb +1 -1
  44. data/lib/chef/resource/freebsd_package.rb +0 -3
  45. data/lib/chef/resource/homebrew_package.rb +1 -2
  46. data/lib/chef/resource/hostname.rb +2 -2
  47. data/lib/chef/resource/kernel_module.rb +3 -2
  48. data/lib/chef/resource/openbsd_package.rb +0 -3
  49. data/lib/chef/resource/remote_file.rb +1 -1
  50. data/lib/chef/resource/rhsm_errata.rb +9 -2
  51. data/lib/chef/resource/rhsm_errata_level.rb +11 -5
  52. data/lib/chef/resource/rhsm_register.rb +27 -12
  53. data/lib/chef/resource/swap_file.rb +2 -2
  54. data/lib/chef/resource/timezone.rb +2 -2
  55. data/lib/chef/resource/windows_certificate.rb +0 -2
  56. data/lib/chef/resource/windows_share.rb +1 -1
  57. data/lib/chef/shell/ext.rb +1 -1
  58. data/lib/chef/util/diff.rb +3 -3
  59. data/lib/chef/util/selinux.rb +2 -2
  60. data/lib/chef/version.rb +1 -1
  61. data/lib/chef/win32/error.rb +1 -1
  62. data/lib/chef/win32/registry.rb +1 -1
  63. data/spec/data/cookbooks/starter/recipes/default.rb +1 -1
  64. data/spec/data/windows_certificates/base64_test.cer +18 -20
  65. data/spec/data/windows_certificates/othertest.cer +0 -0
  66. data/spec/data/windows_certificates/test.cer +0 -0
  67. data/spec/data/windows_certificates/test.p7b +0 -0
  68. data/spec/data/windows_certificates/test.pem +18 -19
  69. data/spec/data/windows_certificates/test.pfx +0 -0
  70. data/spec/functional/resource/ifconfig_spec.rb +2 -2
  71. data/spec/functional/resource/windows_certificate_spec.rb +9 -9
  72. data/spec/functional/resource/windows_service_spec.rb +1 -2
  73. data/spec/functional/run_lock_spec.rb +7 -1
  74. data/spec/functional/win32/service_manager_spec.rb +1 -1
  75. data/spec/functional/win32/version_info_spec.rb +4 -4
  76. data/spec/integration/client/client_spec.rb +2 -2
  77. data/spec/integration/client/exit_code_spec.rb +1 -0
  78. data/spec/integration/client/ipv6_spec.rb +1 -0
  79. data/spec/integration/knife/chef_fs_data_store_spec.rb +1 -0
  80. data/spec/integration/knife/chef_repo_path_spec.rb +1 -0
  81. data/spec/integration/knife/chef_repository_file_system_spec.rb +1 -0
  82. data/spec/integration/knife/chefignore_spec.rb +1 -0
  83. data/spec/integration/knife/client_bulk_delete_spec.rb +1 -0
  84. data/spec/integration/knife/client_create_spec.rb +1 -0
  85. data/spec/integration/knife/client_delete_spec.rb +1 -0
  86. data/spec/integration/knife/client_key_create_spec.rb +1 -0
  87. data/spec/integration/knife/client_key_delete_spec.rb +1 -0
  88. data/spec/integration/knife/client_key_list_spec.rb +1 -0
  89. data/spec/integration/knife/client_key_show_spec.rb +1 -0
  90. data/spec/integration/knife/client_list_spec.rb +1 -0
  91. data/spec/integration/knife/client_show_spec.rb +1 -0
  92. data/spec/integration/knife/common_options_spec.rb +1 -0
  93. data/spec/integration/knife/config_get_profile_spec.rb +1 -0
  94. data/spec/integration/knife/config_get_spec.rb +1 -0
  95. data/spec/integration/knife/config_list_profiles_spec.rb +1 -0
  96. data/spec/integration/knife/config_use_profile_spec.rb +1 -0
  97. data/spec/integration/knife/cookbook_api_ipv6_spec.rb +1 -0
  98. data/spec/integration/knife/cookbook_bulk_delete_spec.rb +1 -0
  99. data/spec/integration/knife/cookbook_download_spec.rb +1 -0
  100. data/spec/integration/knife/cookbook_list_spec.rb +1 -0
  101. data/spec/integration/knife/cookbook_show_spec.rb +1 -0
  102. data/spec/integration/knife/cookbook_upload_spec.rb +1 -0
  103. data/spec/integration/knife/data_bag_create_spec.rb +1 -0
  104. data/spec/integration/knife/data_bag_delete_spec.rb +1 -0
  105. data/spec/integration/knife/data_bag_edit_spec.rb +1 -0
  106. data/spec/integration/knife/data_bag_from_file_spec.rb +1 -0
  107. data/spec/integration/knife/data_bag_list_spec.rb +1 -0
  108. data/spec/integration/knife/data_bag_show_spec.rb +1 -0
  109. data/spec/integration/knife/delete_spec.rb +1 -0
  110. data/spec/integration/knife/deps_spec.rb +3 -2
  111. data/spec/integration/knife/diff_spec.rb +1 -0
  112. data/spec/integration/knife/download_spec.rb +1 -0
  113. data/spec/integration/knife/environment_compare_spec.rb +1 -0
  114. data/spec/integration/knife/environment_create_spec.rb +1 -0
  115. data/spec/integration/knife/environment_delete_spec.rb +1 -0
  116. data/spec/integration/knife/environment_from_file_spec.rb +1 -0
  117. data/spec/integration/knife/environment_list_spec.rb +1 -0
  118. data/spec/integration/knife/environment_show_spec.rb +1 -0
  119. data/spec/integration/knife/list_spec.rb +1 -0
  120. data/spec/integration/knife/node_bulk_delete_spec.rb +1 -0
  121. data/spec/integration/knife/node_create_spec.rb +1 -0
  122. data/spec/integration/knife/node_delete_spec.rb +1 -0
  123. data/spec/integration/knife/node_environment_set_spec.rb +1 -0
  124. data/spec/integration/knife/node_from_file_spec.rb +1 -0
  125. data/spec/integration/knife/node_list_spec.rb +1 -0
  126. data/spec/integration/knife/node_run_list_add_spec.rb +1 -0
  127. data/spec/integration/knife/node_run_list_remove_spec.rb +1 -0
  128. data/spec/integration/knife/node_run_list_set_spec.rb +1 -0
  129. data/spec/integration/knife/node_show_spec.rb +1 -0
  130. data/spec/integration/knife/raw_spec.rb +1 -0
  131. data/spec/integration/knife/redirection_spec.rb +1 -0
  132. data/spec/integration/knife/role_bulk_delete_spec.rb +1 -0
  133. data/spec/integration/knife/role_create_spec.rb +1 -0
  134. data/spec/integration/knife/role_delete_spec.rb +1 -0
  135. data/spec/integration/knife/role_from_file_spec.rb +1 -0
  136. data/spec/integration/knife/role_list_spec.rb +1 -0
  137. data/spec/integration/knife/role_show_spec.rb +1 -0
  138. data/spec/integration/knife/search_node_spec.rb +1 -0
  139. data/spec/integration/knife/show_spec.rb +1 -0
  140. data/spec/integration/knife/upload_spec.rb +1 -0
  141. data/spec/integration/recipes/accumulator_spec.rb +1 -0
  142. data/spec/integration/recipes/lwrp_inline_resources_spec.rb +1 -0
  143. data/spec/integration/recipes/lwrp_spec.rb +1 -0
  144. data/spec/integration/recipes/notifies_spec.rb +1 -0
  145. data/spec/integration/recipes/notifying_block_spec.rb +1 -0
  146. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -0
  147. data/spec/integration/recipes/resource_action_spec.rb +1 -0
  148. data/spec/integration/recipes/unified_mode_spec.rb +1 -0
  149. data/spec/integration/solo/solo_spec.rb +1 -0
  150. data/spec/spec_helper.rb +7 -10
  151. data/spec/support/chef_helpers.rb +1 -1
  152. data/spec/support/key_helpers.rb +0 -2
  153. data/spec/support/platform_helpers.rb +7 -3
  154. data/spec/support/platforms/win32/spec_service.rb +27 -25
  155. data/spec/support/shared/context/config.rb +0 -3
  156. data/spec/support/shared/integration/integration_helper.rb +0 -1
  157. data/spec/support/shared/unit/execute_resource.rb +0 -2
  158. data/spec/support/shared/unit/provider/file.rb +0 -1
  159. data/spec/support/shared/unit/script_resource.rb +2 -4
  160. data/spec/support/shared/unit/windows_script_resource.rb +0 -2
  161. data/spec/unit/cookbook/gem_installer_spec.rb +1 -1
  162. data/spec/unit/cookbook/metadata_spec.rb +1 -10
  163. data/spec/unit/data_collector_spec.rb +1 -1
  164. data/spec/unit/deprecated_spec.rb +3 -3
  165. data/spec/unit/encrypted_data_bag_item/check_encrypted_spec.rb +1 -1
  166. data/spec/unit/encrypted_data_bag_item_spec.rb +2 -2
  167. data/spec/unit/knife/core/windows_bootstrap_context_spec.rb +3 -3
  168. data/spec/unit/knife_spec.rb +6 -7
  169. data/spec/unit/mixin/shell_out_spec.rb +27 -29
  170. data/spec/unit/provider/file_spec.rb +1 -0
  171. data/spec/unit/resource/build_essential_spec.rb +48 -2
  172. data/spec/unit/resource/data/InstallHistory_with_CLT.plist +92 -0
  173. data/spec/unit/resource/data/InstallHistory_without_CLT.plist +38 -0
  174. data/spec/unit/resource/execute_spec.rb +1 -1
  175. data/spec/unit/resource/rhsm_register_spec.rb +24 -0
  176. data/spec/unit/resource/windows_share_spec.rb +3 -1
  177. data/tasks/rspec.rb +1 -0
  178. metadata +9 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e39ae27c2d7728c9088a216d1c3ce11575a6d2e02a77a2a489fee40fb2d728bf
4
- data.tar.gz: 8dcf877fc5b474c19a1ed509b9f281beee9a78d89aca9baae5c57811e19355ed
3
+ metadata.gz: 957a12918f6ab4455d5c3d03efa63317614d9c840463920bc003c07bd4e04a32
4
+ data.tar.gz: 255e03a1b503b91ee88f014add1dfba039302820ba1a8ea7f0e57a4b7646185b
5
5
  SHA512:
6
- metadata.gz: 000e1c6d1f52160ce6bffa144e2f2ae94aabbe7d72aa189222ee7c5e7be2e445bad02765d919eaa7eca55ff957782b11b24844952cdcf74e0ad1538ac561c80f
7
- data.tar.gz: 7721fae6454f09db2fdcc5e8c1de0cb86a1041e3b33789e81bcf4dfd54bb20fdbd4988d3974114a070ca01eaefcb993fdf08f20bb741230662efb6ab086d55e9
6
+ metadata.gz: 11e3cb0b31ba96aa8736042df77e1b21e177592264d6e0781932cb3ac92698f94655bb605a6a32ab3259af39ee73493a98509fc10aaad044a7ec97201b7d3383
7
+ data.tar.gz: b476bb85e0651ad949210900d5d0dc5da61b1445035e1cb09e1f825c70bf8c1ae285f349bacf6d090f777eed44d70bdd11577a8d97e1359535a5ab7639fdc3af
data/Gemfile CHANGED
@@ -46,7 +46,8 @@ end
46
46
 
47
47
  # Everything except AIX
48
48
  group(:ruby_prof) do
49
- gem "ruby-prof"
49
+ # ruby-prof 1.3.0 does not compile on our centos6 builders/kitchen testers
50
+ gem "ruby-prof", "< 1.3.0"
50
51
  end
51
52
 
52
53
  # Everything except AIX and Windows
@@ -58,7 +59,7 @@ group(:development, :test) do
58
59
  # we pin rake as a copy of rake is installed from the ruby source
59
60
  # if you bump the ruby version you should confirm we don't end up with
60
61
  # two rake gems installed again
61
- gem "rake", "<= 12.3.2"
62
+ gem "rake", "<= 12.3.3"
62
63
 
63
64
  gem "rspec-core", "~> 3.5"
64
65
  gem "rspec-mocks", "~> 3.5"
data/README.md CHANGED
@@ -22,7 +22,7 @@ For Chef Infra usage, please refer to our [Learn Chef Rally](https://learn.chef.
22
22
 
23
23
  Other useful resources for Chef Infra users:
24
24
 
25
- - Documentation: <https://docs.chef.io>
25
+ - Documentation: <https://docs.chef.io/>
26
26
  - Source: <https://github.com/chef/chef/tree/master>
27
27
  - Tickets/Issues: <https://github.com/chef/chef/issues>
28
28
  - Slack: [Chef Community Slack](https://community-slack.chef.io/)
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ begin
22
22
  require_relative "tasks/dependencies"
23
23
  require_relative "tasks/announce"
24
24
  require_relative "tasks/docs"
25
+ require_relative "lib/chef/dist"
25
26
  rescue LoadError => e
26
27
  puts "Skipping missing rake dep: #{e}"
27
28
  end
@@ -35,6 +36,14 @@ task :super_install do
35
36
  Dir.chdir(path)
36
37
  sh("rake install")
37
38
  end
39
+
40
+ # Templating the powershell extensions so we can inject distro constants
41
+ template_file = ::File.join(::File.dirname(__FILE__), "distro", "templates", "powershell", "chef", "chef.psm1.erb")
42
+ psm1_path = ::File.join(::File.dirname(__FILE__), "distro", "powershell", "chef")
43
+ FileUtils.mkdir_p psm1_path
44
+ template = ERB.new(IO.read(template_file))
45
+ chef_psm1 = template.result
46
+ File.open(::File.join(psm1_path, "chef.psm1"), "w") { |f| f.write(chef_psm1) }
38
47
  end
39
48
 
40
49
  task install: :super_install
@@ -1,459 +1,459 @@
1
- 
2
- function Load-Win32Bindings {
3
- Add-Type -TypeDefinition @"
4
- using System;
5
- using System.Diagnostics;
6
- using System.Runtime.InteropServices;
7
-
8
- namespace Chef
9
- {
10
-
11
- [StructLayout(LayoutKind.Sequential)]
12
- public struct PROCESS_INFORMATION
13
- {
14
- public IntPtr hProcess;
15
- public IntPtr hThread;
16
- public uint dwProcessId;
17
- public uint dwThreadId;
18
- }
19
-
20
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
21
- public struct STARTUPINFO
22
- {
23
- public uint cb;
24
- public string lpReserved;
25
- public string lpDesktop;
26
- public string lpTitle;
27
- public uint dwX;
28
- public uint dwY;
29
- public uint dwXSize;
30
- public uint dwYSize;
31
- public uint dwXCountChars;
32
- public uint dwYCountChars;
33
- public uint dwFillAttribute;
34
- public STARTF dwFlags;
35
- public ShowWindow wShowWindow;
36
- public short cbReserved2;
37
- public IntPtr lpReserved2;
38
- public IntPtr hStdInput;
39
- public IntPtr hStdOutput;
40
- public IntPtr hStdError;
41
- }
42
-
43
- [StructLayout(LayoutKind.Sequential)]
44
- public struct SECURITY_ATTRIBUTES
45
- {
46
- public int length;
47
- public IntPtr lpSecurityDescriptor;
48
- public bool bInheritHandle;
49
- }
50
-
51
- [Flags]
52
- public enum CreationFlags : int
53
- {
54
- NONE = 0,
55
- DEBUG_PROCESS = 0x00000001,
56
- DEBUG_ONLY_THIS_PROCESS = 0x00000002,
57
- CREATE_SUSPENDED = 0x00000004,
58
- DETACHED_PROCESS = 0x00000008,
59
- CREATE_NEW_CONSOLE = 0x00000010,
60
- CREATE_NEW_PROCESS_GROUP = 0x00000200,
61
- CREATE_UNICODE_ENVIRONMENT = 0x00000400,
62
- CREATE_SEPARATE_WOW_VDM = 0x00000800,
63
- CREATE_SHARED_WOW_VDM = 0x00001000,
64
- CREATE_PROTECTED_PROCESS = 0x00040000,
65
- EXTENDED_STARTUPINFO_PRESENT = 0x00080000,
66
- CREATE_BREAKAWAY_FROM_JOB = 0x01000000,
67
- CREATE_PRESERVE_CODE_AUTHZ_LEVEL = 0x02000000,
68
- CREATE_DEFAULT_ERROR_MODE = 0x04000000,
69
- CREATE_NO_WINDOW = 0x08000000,
70
- }
71
-
72
- [Flags]
73
- public enum STARTF : uint
74
- {
75
- STARTF_USESHOWWINDOW = 0x00000001,
76
- STARTF_USESIZE = 0x00000002,
77
- STARTF_USEPOSITION = 0x00000004,
78
- STARTF_USECOUNTCHARS = 0x00000008,
79
- STARTF_USEFILLATTRIBUTE = 0x00000010,
80
- STARTF_RUNFULLSCREEN = 0x00000020, // ignored for non-x86 platforms
81
- STARTF_FORCEONFEEDBACK = 0x00000040,
82
- STARTF_FORCEOFFFEEDBACK = 0x00000080,
83
- STARTF_USESTDHANDLES = 0x00000100,
84
- }
85
-
86
- public enum ShowWindow : short
87
- {
88
- SW_HIDE = 0,
89
- SW_SHOWNORMAL = 1,
90
- SW_NORMAL = 1,
91
- SW_SHOWMINIMIZED = 2,
92
- SW_SHOWMAXIMIZED = 3,
93
- SW_MAXIMIZE = 3,
94
- SW_SHOWNOACTIVATE = 4,
95
- SW_SHOW = 5,
96
- SW_MINIMIZE = 6,
97
- SW_SHOWMINNOACTIVE = 7,
98
- SW_SHOWNA = 8,
99
- SW_RESTORE = 9,
100
- SW_SHOWDEFAULT = 10,
101
- SW_FORCEMINIMIZE = 11,
102
- SW_MAX = 11
103
- }
104
-
105
- public enum StandardHandle : int
106
- {
107
- Input = -10,
108
- Output = -11,
109
- Error = -12
110
- }
111
-
112
- public enum HandleFlags : int
113
- {
114
- HANDLE_FLAG_INHERIT = 0x00000001,
115
- HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002
116
- }
117
-
118
- public static class Kernel32
119
- {
120
- [DllImport("kernel32.dll", SetLastError=true)]
121
- [return: MarshalAs(UnmanagedType.Bool)]
122
- public static extern bool CreateProcess(
123
- string lpApplicationName,
124
- string lpCommandLine,
125
- ref SECURITY_ATTRIBUTES lpProcessAttributes,
126
- ref SECURITY_ATTRIBUTES lpThreadAttributes,
127
- [MarshalAs(UnmanagedType.Bool)] bool bInheritHandles,
128
- CreationFlags dwCreationFlags,
129
- IntPtr lpEnvironment,
130
- string lpCurrentDirectory,
131
- ref STARTUPINFO lpStartupInfo,
132
- out PROCESS_INFORMATION lpProcessInformation);
133
-
134
- [DllImport("kernel32.dll", SetLastError=true)]
135
- public static extern IntPtr GetStdHandle(
136
- StandardHandle nStdHandle);
137
-
138
- [DllImport("kernel32.dll")]
139
- public static extern bool SetHandleInformation(
140
- IntPtr hObject,
141
- int dwMask,
142
- uint dwFlags);
143
-
144
- [DllImport("kernel32", SetLastError=true)]
145
- [return: MarshalAs(UnmanagedType.Bool)]
146
- public static extern bool CloseHandle(
147
- IntPtr hObject);
148
-
149
- [DllImport("kernel32", SetLastError=true)]
150
- [return: MarshalAs(UnmanagedType.Bool)]
151
- public static extern bool GetExitCodeProcess(
152
- IntPtr hProcess,
153
- out int lpExitCode);
154
-
155
- [DllImport("kernel32.dll", SetLastError = true)]
156
- public static extern bool CreatePipe(
157
- out IntPtr phReadPipe,
158
- out IntPtr phWritePipe,
159
- IntPtr lpPipeAttributes,
160
- uint nSize);
161
-
162
- [DllImport("kernel32.dll", SetLastError = true)]
163
- public static extern bool ReadFile(
164
- IntPtr hFile,
165
- [Out] byte[] lpBuffer,
166
- uint nNumberOfBytesToRead,
167
- ref int lpNumberOfBytesRead,
168
- IntPtr lpOverlapped);
169
-
170
- [DllImport("kernel32.dll", SetLastError = true)]
171
- public static extern bool PeekNamedPipe(
172
- IntPtr handle,
173
- byte[] buffer,
174
- uint nBufferSize,
175
- ref uint bytesRead,
176
- ref uint bytesAvail,
177
- ref uint BytesLeftThisMessage);
178
-
179
- public const int STILL_ACTIVE = 259;
180
- }
181
- }
182
- "@
183
- }
184
-
185
- function Run-ExecutableAndWait($AppPath, $ArgumentString) {
186
- # Use the Win32 API to create a new process and wait for it to terminate.
187
- $null = Load-Win32Bindings
188
-
189
- $si = New-Object Chef.STARTUPINFO
190
- $pi = New-Object Chef.PROCESS_INFORMATION
191
-
192
- $pSec = New-Object Chef.SECURITY_ATTRIBUTES
193
- $pSec.Length = [System.Runtime.InteropServices.Marshal]::SizeOf($pSec)
194
- $pSec.bInheritHandle = $true
195
- $tSec = New-Object Chef.SECURITY_ATTRIBUTES
196
- $tSec.Length = [System.Runtime.InteropServices.Marshal]::SizeOf($tSec)
197
- $tSec.bInheritHandle = $true
198
-
199
- # Create pipe for process stdout
200
- $ptr = [System.Runtime.InteropServices.Marshal]::AllocHGlobal([System.Runtime.InteropServices.Marshal]::SizeOf($si))
201
- [System.Runtime.InteropServices.Marshal]::StructureToPtr($pSec, $ptr, $true)
202
- $hReadOut = [IntPtr]::Zero
203
- $hWriteOut = [IntPtr]::Zero
204
- $success = [Chef.Kernel32]::CreatePipe([ref] $hReadOut, [ref] $hWriteOut, $ptr, 0)
205
- if (-Not $success) {
206
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
207
- throw "Unable to create output pipe. Error code $reason."
208
- }
209
- $success = [Chef.Kernel32]::SetHandleInformation($hReadOut, [Chef.HandleFlags]::HANDLE_FLAG_INHERIT, 0)
210
- if (-Not $success) {
211
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
212
- throw "Unable to set output pipe handle information. Error code $reason."
213
- }
214
-
215
- $si.cb = [System.Runtime.InteropServices.Marshal]::SizeOf($si)
216
- $si.wShowWindow = [Chef.ShowWindow]::SW_SHOW
217
- $si.dwFlags = [Chef.STARTF]::STARTF_USESTDHANDLES
218
- $si.hStdOutput = $hWriteOut
219
- $si.hStdError = $hWriteOut
220
- $si.hStdInput = [Chef.Kernel32]::GetStdHandle([Chef.StandardHandle]::Input)
221
-
222
- $success = [Chef.Kernel32]::CreateProcess(
223
- $AppPath,
224
- $ArgumentString,
225
- [ref] $pSec,
226
- [ref] $tSec,
227
- $true,
228
- [Chef.CreationFlags]::NONE,
229
- [IntPtr]::Zero,
230
- $pwd,
231
- [ref] $si,
232
- [ref] $pi
233
- )
234
- if (-Not $success) {
235
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
236
- throw "Unable to create process [$ArgumentString]. Error code $reason."
237
- }
238
-
239
- $buffer = New-Object byte[] 1024
240
-
241
- # Initialize reference variables
242
- $bytesRead = 0
243
- $bytesAvailable = 0
244
- $bytesLeftThisMsg = 0
245
- $global:LASTEXITCODE = [Chef.Kernel32]::STILL_ACTIVE
246
-
247
- $isActive = $true
248
- while ($isActive) {
249
- $success = [Chef.Kernel32]::GetExitCodeProcess($pi.hProcess, [ref] $global:LASTEXITCODE)
250
- if (-Not $success) {
251
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
252
- throw "Process exit code unavailable. Error code $reason."
253
- }
254
-
255
- $success = [Chef.Kernel32]::PeekNamedPipe(
256
- $hReadOut,
257
- $null,
258
- $buffer.Length,
259
- [ref] $bytesRead,
260
- [ref] $bytesAvailable,
261
- [ref] $bytesLeftThisMsg
262
- )
263
- if (-Not $success) {
264
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
265
- throw "Output pipe unavailable for peeking. Error code $reason."
266
- }
267
-
268
- if ($bytesRead -gt 0) {
269
- while ([Chef.Kernel32]::ReadFile($hReadOut, $buffer, $buffer.Length, [ref] $bytesRead, 0)) {
270
- $output = [Text.Encoding]::UTF8.GetString($buffer, 0, $bytesRead)
271
- if ($output) {
272
- $output
273
- }
274
- if ($bytesRead -lt $buffer.Length) {
275
- # Partial buffer indicating the end of stream, break out of ReadFile loop
276
- # ReadFile will block until:
277
- # The number of bytes requested is read.
278
- # A write operation completes on the write end of the pipe.
279
- # An asynchronous handle is being used and the read is occurring asynchronously.
280
- # An error occurs.
281
- break
282
- }
283
- }
284
- } else {
285
- # For some reason, you can't read from the read-end of the read-pipe before the write end has started
286
- # to write. Otherwise the process just blocks forever and never returns from the read. So we peek
287
- # at the pipe until there is something. But don't peek too eagerly. This is stupid stupid stupid.
288
- # There must be a way to do this without having to peek at a pipe first but I have not found it.
289
- #
290
- # Note to the future intrepid soul who wants to fix this:
291
- # 0) This is related to unreasonable CPU usage by the wrapper PS script on a 1 VCPU VM (either Hyper-V
292
- # or VirtualBox) running a consumer Windows SKU (Windows 10 for example...). Test it there.
293
- # 1) Maybe this entire script is unnecessary and the bugs mentioned below have been fixed or don't need
294
- # to be supported.
295
- # 2) The server and consumer windows schedulers have different defaults. I had a hard time reproducing
296
- # any issue on a win 2008 on win 2012 server default setup. See the "foreground application scheduler
297
- # priority" setting to see if it's relevant.
298
- # 3) This entire endeavor is silly anyway - why are we reimplementing process forking all over? Maybe try
299
- # to get the folks above to accept patches instead of extending this crazy script.
300
- Start-Sleep -s 1
301
- # Start-Sleep -m 100
302
- }
303
-
304
- if ($global:LASTEXITCODE -ne [Chef.Kernel32]::STILL_ACTIVE) {
305
- $isActive = $false
306
- }
307
- }
308
-
309
- # Cleanup handles
310
- $success = [Chef.Kernel32]::CloseHandle($pi.hProcess)
311
- if (-Not $success) {
312
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
313
- throw "Unable to release process handle. Error code $reason."
314
- }
315
- $success = [Chef.Kernel32]::CloseHandle($pi.hThread)
316
- if (-Not $success) {
317
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
318
- throw "Unable to release thread handle. Error code $reason."
319
- }
320
- $success = [Chef.Kernel32]::CloseHandle($hWriteOut)
321
- if (-Not $success) {
322
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
323
- throw "Unable to release output write handle. Error code $reason."
324
- }
325
- $success = [Chef.Kernel32]::CloseHandle($hReadOut)
326
- if (-Not $success) {
327
- $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
328
- throw "Unable to release output read handle. Error code $reason."
329
- }
330
- [System.Runtime.InteropServices.Marshal]::FreeHGlobal($ptr)
331
- }
332
-
333
- function Get-ScriptDirectory {
334
- if (!$PSScriptRoot) {
335
- $Invocation = (Get-Variable MyInvocation -Scope 1).Value
336
- $PSScriptRoot = Split-Path $Invocation.MyCommand.Path
337
- }
338
- $PSScriptRoot
339
- }
340
-
341
- function Run-RubyCommand($command, $argList) {
342
- # This method exists to take the given list of arguments and get it past ruby's command-line
343
- # interpreter unscathed and untampered. See https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L1582
344
- # for a list of transformations that ruby attempts to perform with your command-line arguments
345
- # before passing it onto a script. The most important task is to defeat the globbing
346
- # and wild-card expansion that ruby performs. Note that ruby does not use MSVCRT's argc/argv
347
- # and deliberately reparses the raw command-line instead.
348
- #
349
- # To stop ruby from interpreting command-line arguments as globs, they need to be enclosed in '
350
- # Ruby doesn't allow any escape characters inside '. This unfortunately prevents us from sending
351
- # any strings which themselves contain '. Ruby does allow multi-fragment arguments though.
352
- # "foo bar"'baz qux'123"foo" is interpreted as 1 argument because there are no un-escaped
353
- # whitespace there. The argument would be interpreted as the string "foo barbaz qux123foo".
354
- # This lets us escape ' characters by exiting the ' quoted string, injecting a "'" fragment and
355
- # then resuming the ' quoted string again.
356
- #
357
- # In the process of defeating ruby, one must also defeat the helpfulness of powershell.
358
- # When arguments come into this method, the standard PS rules for interpreting cmdlet arguments
359
- # apply. When using & (call operator) and providing an array of arguments, powershell (verified
360
- # on PS 4.0 on Windows Server 2012R2) will not evaluate them but (contrary to documentation),
361
- # it will still marginally interpret them. The behaviour of PS 5.0 seems to be different but
362
- # ignore that for now. If any of the provided arguments has a space in it, powershell checks
363
- # the first and last character to ensure that they are " characters (and that's all it checks).
364
- # If they are not, it will blindly surround that argument with " characters. It won't do this
365
- # operation if no space is present, even if other special characters are present. If it notices
366
- # leading and trailing " characters, it won't actually check to see if there are other "
367
- # characters in the string. Since PS 5.0 changes this behavior, we could consider using the --%
368
- # "stop screwing up my arguments" operator, which is available since PS 3.0. When encountered
369
- # --% indicates that the rest of line is to be sent literally... except if the parser encounters
370
- # %FOO% cmd style environment variables. Because reasons. And there is no way to escape the
371
- # % character in *any* waym shape or form.
372
- # https://connect.microsoft.com/PowerShell/feedback/details/376207/executing-commands-which-require-quotes-and-variables-is-practically-impossible
373
- #
374
- # In case you think that you're either reading this incorrectly or that I'm full of shit, here
375
- # are some examples. These use EchoArgs.exe from the PowerShell Community Extensions package.
376
- # I have not included the argument parsing output from EchoArgs.exe to prevent confusing you with
377
- # more details about MSVCRT's parsing algorithm.
378
- #
379
- # $x = "foo '' bar `"baz`""
380
- # & EchoArgs @($x, $x)
381
- # Command line:
382
- # "C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" "foo '' bar "baz"" "foo '' bar "baz""
383
- #
384
- # $x = "abc'123'nospace`"lulz`"!!!"
385
- # & EchoArgs @($x, $x)
386
- # Command line:
387
- # "C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" abc'123'nospace"lulz"!!! abc'123'nospace"lulz"!!!
388
- #
389
- # $x = "`"`"Look ma! Tonnes of spaces! 'foo' 'bar'`"`""
390
- # & EchoArgs @($x, $x)
391
- # Command line:
392
- # "C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" ""Look ma! Tonnes of spaces! 'foo' 'bar'"" ""Look ma! Tonnes of spaces! 'foo' 'bar'""
393
- #
394
- # Given all this, we can now device a strategy to work around all these immensely helpful, well
395
- # documented and useful tools by looking at each incoming argument, escaping any ' characters
396
- # with a '"'"' sequence, surrounding each argument with ' & joining them with a space separating
397
- # them.
398
- # There is another bug (https://bugs.ruby-lang.org/issues/11142) that causes ruby to mangle any
399
- # "" two-character double quote sequence but since we always emit our strings inside ' except for
400
- # ' characters, this should be ok. Just remember that an argument '' should get translated to
401
- # ''"'"''"'"'' on the command line. If those intervening empty ''s are not present, the presence
402
- # of "" will cause ruby to mangle that argument.
403
- $transformedList = $argList | foreach { "'" + ( $_ -replace "'","'`"'`"'" ) + "'" }
404
- $fortifiedArgString = $transformedList -join ' '
405
-
406
- # Use the correct embedded ruby path. We'll be deployed at a path that looks like
407
- # [C:\opscode or some other prefix]\chef\modules\chef
408
- $ruby = Join-Path (Get-ScriptDirectory) "..\..\embedded\bin\ruby.exe"
409
- $commandPath = Join-Path (Get-ScriptDirectory) "..\..\bin\$command"
410
-
411
- Run-ExecutableAndWait $ruby """$ruby"" '$commandPath' $fortifiedArgString"
412
- }
413
-
414
-
415
- function chef-apply {
416
- Run-RubyCommand 'chef-apply' $args
417
- }
418
-
419
- function chef-client {
420
- Run-RubyCommand 'chef-client' $args
421
- }
422
-
423
- function chef-service-manager {
424
- Run-RubyCommand 'chef-service-manager' $args
425
- }
426
-
427
- function chef-shell {
428
- Run-RubyCommand 'chef-shell' $args
429
- }
430
-
431
- function chef-solo {
432
- Run-RubyCommand 'chef-solo' $args
433
- }
434
-
435
- function chef-windows-service {
436
- Run-RubyCommand 'chef-windows-service' $args
437
- }
438
-
439
- function knife {
440
- Run-RubyCommand 'knife' $args
441
- }
442
-
443
- Export-ModuleMember -function chef-apply
444
- Export-ModuleMember -function chef-client
445
- Export-ModuleMember -function chef-service-manager
446
- Export-ModuleMember -function chef-shell
447
- Export-ModuleMember -function chef-solo
448
- Export-ModuleMember -function chef-windows-service
449
- Export-ModuleMember -function knife
450
-
451
- # To debug this module, uncomment the line below
452
- # Export-ModuleMember -function Run-RubyCommand
453
-
454
- # Then run the following to reload the module. Use puts_argv as a helpful debug executable.
455
- # Remove-Module chef
456
- # Import-Module chef
457
- # "puts ARGV" | Out-File C:\opscode\chef\bin\puts_args -Encoding ASCII
458
- # Copy-Item C:\opscode\chef\bin\ohai.bat C:\opscode\chef\bin\puts_args.bat
459
- # Run-RubyCommand puts_args 'Here' "are" some '"very interesting"' 'arguments[to]' "`"try out`""
1
+ 
2
+ function Load-Win32Bindings {
3
+ Add-Type -TypeDefinition @"
4
+ using System;
5
+ using System.Diagnostics;
6
+ using System.Runtime.InteropServices;
7
+
8
+ namespace Chef
9
+ {
10
+
11
+ [StructLayout(LayoutKind.Sequential)]
12
+ public struct PROCESS_INFORMATION
13
+ {
14
+ public IntPtr hProcess;
15
+ public IntPtr hThread;
16
+ public uint dwProcessId;
17
+ public uint dwThreadId;
18
+ }
19
+
20
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
21
+ public struct STARTUPINFO
22
+ {
23
+ public uint cb;
24
+ public string lpReserved;
25
+ public string lpDesktop;
26
+ public string lpTitle;
27
+ public uint dwX;
28
+ public uint dwY;
29
+ public uint dwXSize;
30
+ public uint dwYSize;
31
+ public uint dwXCountChars;
32
+ public uint dwYCountChars;
33
+ public uint dwFillAttribute;
34
+ public STARTF dwFlags;
35
+ public ShowWindow wShowWindow;
36
+ public short cbReserved2;
37
+ public IntPtr lpReserved2;
38
+ public IntPtr hStdInput;
39
+ public IntPtr hStdOutput;
40
+ public IntPtr hStdError;
41
+ }
42
+
43
+ [StructLayout(LayoutKind.Sequential)]
44
+ public struct SECURITY_ATTRIBUTES
45
+ {
46
+ public int length;
47
+ public IntPtr lpSecurityDescriptor;
48
+ public bool bInheritHandle;
49
+ }
50
+
51
+ [Flags]
52
+ public enum CreationFlags : int
53
+ {
54
+ NONE = 0,
55
+ DEBUG_PROCESS = 0x00000001,
56
+ DEBUG_ONLY_THIS_PROCESS = 0x00000002,
57
+ CREATE_SUSPENDED = 0x00000004,
58
+ DETACHED_PROCESS = 0x00000008,
59
+ CREATE_NEW_CONSOLE = 0x00000010,
60
+ CREATE_NEW_PROCESS_GROUP = 0x00000200,
61
+ CREATE_UNICODE_ENVIRONMENT = 0x00000400,
62
+ CREATE_SEPARATE_WOW_VDM = 0x00000800,
63
+ CREATE_SHARED_WOW_VDM = 0x00001000,
64
+ CREATE_PROTECTED_PROCESS = 0x00040000,
65
+ EXTENDED_STARTUPINFO_PRESENT = 0x00080000,
66
+ CREATE_BREAKAWAY_FROM_JOB = 0x01000000,
67
+ CREATE_PRESERVE_CODE_AUTHZ_LEVEL = 0x02000000,
68
+ CREATE_DEFAULT_ERROR_MODE = 0x04000000,
69
+ CREATE_NO_WINDOW = 0x08000000,
70
+ }
71
+
72
+ [Flags]
73
+ public enum STARTF : uint
74
+ {
75
+ STARTF_USESHOWWINDOW = 0x00000001,
76
+ STARTF_USESIZE = 0x00000002,
77
+ STARTF_USEPOSITION = 0x00000004,
78
+ STARTF_USECOUNTCHARS = 0x00000008,
79
+ STARTF_USEFILLATTRIBUTE = 0x00000010,
80
+ STARTF_RUNFULLSCREEN = 0x00000020, // ignored for non-x86 platforms
81
+ STARTF_FORCEONFEEDBACK = 0x00000040,
82
+ STARTF_FORCEOFFFEEDBACK = 0x00000080,
83
+ STARTF_USESTDHANDLES = 0x00000100,
84
+ }
85
+
86
+ public enum ShowWindow : short
87
+ {
88
+ SW_HIDE = 0,
89
+ SW_SHOWNORMAL = 1,
90
+ SW_NORMAL = 1,
91
+ SW_SHOWMINIMIZED = 2,
92
+ SW_SHOWMAXIMIZED = 3,
93
+ SW_MAXIMIZE = 3,
94
+ SW_SHOWNOACTIVATE = 4,
95
+ SW_SHOW = 5,
96
+ SW_MINIMIZE = 6,
97
+ SW_SHOWMINNOACTIVE = 7,
98
+ SW_SHOWNA = 8,
99
+ SW_RESTORE = 9,
100
+ SW_SHOWDEFAULT = 10,
101
+ SW_FORCEMINIMIZE = 11,
102
+ SW_MAX = 11
103
+ }
104
+
105
+ public enum StandardHandle : int
106
+ {
107
+ Input = -10,
108
+ Output = -11,
109
+ Error = -12
110
+ }
111
+
112
+ public enum HandleFlags : int
113
+ {
114
+ HANDLE_FLAG_INHERIT = 0x00000001,
115
+ HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002
116
+ }
117
+
118
+ public static class Kernel32
119
+ {
120
+ [DllImport("kernel32.dll", SetLastError=true)]
121
+ [return: MarshalAs(UnmanagedType.Bool)]
122
+ public static extern bool CreateProcess(
123
+ string lpApplicationName,
124
+ string lpCommandLine,
125
+ ref SECURITY_ATTRIBUTES lpProcessAttributes,
126
+ ref SECURITY_ATTRIBUTES lpThreadAttributes,
127
+ [MarshalAs(UnmanagedType.Bool)] bool bInheritHandles,
128
+ CreationFlags dwCreationFlags,
129
+ IntPtr lpEnvironment,
130
+ string lpCurrentDirectory,
131
+ ref STARTUPINFO lpStartupInfo,
132
+ out PROCESS_INFORMATION lpProcessInformation);
133
+
134
+ [DllImport("kernel32.dll", SetLastError=true)]
135
+ public static extern IntPtr GetStdHandle(
136
+ StandardHandle nStdHandle);
137
+
138
+ [DllImport("kernel32.dll")]
139
+ public static extern bool SetHandleInformation(
140
+ IntPtr hObject,
141
+ int dwMask,
142
+ uint dwFlags);
143
+
144
+ [DllImport("kernel32", SetLastError=true)]
145
+ [return: MarshalAs(UnmanagedType.Bool)]
146
+ public static extern bool CloseHandle(
147
+ IntPtr hObject);
148
+
149
+ [DllImport("kernel32", SetLastError=true)]
150
+ [return: MarshalAs(UnmanagedType.Bool)]
151
+ public static extern bool GetExitCodeProcess(
152
+ IntPtr hProcess,
153
+ out int lpExitCode);
154
+
155
+ [DllImport("kernel32.dll", SetLastError = true)]
156
+ public static extern bool CreatePipe(
157
+ out IntPtr phReadPipe,
158
+ out IntPtr phWritePipe,
159
+ IntPtr lpPipeAttributes,
160
+ uint nSize);
161
+
162
+ [DllImport("kernel32.dll", SetLastError = true)]
163
+ public static extern bool ReadFile(
164
+ IntPtr hFile,
165
+ [Out] byte[] lpBuffer,
166
+ uint nNumberOfBytesToRead,
167
+ ref int lpNumberOfBytesRead,
168
+ IntPtr lpOverlapped);
169
+
170
+ [DllImport("kernel32.dll", SetLastError = true)]
171
+ public static extern bool PeekNamedPipe(
172
+ IntPtr handle,
173
+ byte[] buffer,
174
+ uint nBufferSize,
175
+ ref uint bytesRead,
176
+ ref uint bytesAvail,
177
+ ref uint BytesLeftThisMessage);
178
+
179
+ public const int STILL_ACTIVE = 259;
180
+ }
181
+ }
182
+ "@
183
+ }
184
+
185
+ function Run-ExecutableAndWait($AppPath, $ArgumentString) {
186
+ # Use the Win32 API to create a new process and wait for it to terminate.
187
+ $null = Load-Win32Bindings
188
+
189
+ $si = New-Object Chef.STARTUPINFO
190
+ $pi = New-Object Chef.PROCESS_INFORMATION
191
+
192
+ $pSec = New-Object Chef.SECURITY_ATTRIBUTES
193
+ $pSec.Length = [System.Runtime.InteropServices.Marshal]::SizeOf($pSec)
194
+ $pSec.bInheritHandle = $true
195
+ $tSec = New-Object Chef.SECURITY_ATTRIBUTES
196
+ $tSec.Length = [System.Runtime.InteropServices.Marshal]::SizeOf($tSec)
197
+ $tSec.bInheritHandle = $true
198
+
199
+ # Create pipe for process stdout
200
+ $ptr = [System.Runtime.InteropServices.Marshal]::AllocHGlobal([System.Runtime.InteropServices.Marshal]::SizeOf($si))
201
+ [System.Runtime.InteropServices.Marshal]::StructureToPtr($pSec, $ptr, $true)
202
+ $hReadOut = [IntPtr]::Zero
203
+ $hWriteOut = [IntPtr]::Zero
204
+ $success = [Chef.Kernel32]::CreatePipe([ref] $hReadOut, [ref] $hWriteOut, $ptr, 0)
205
+ if (-Not $success) {
206
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
207
+ throw "Unable to create output pipe. Error code $reason."
208
+ }
209
+ $success = [Chef.Kernel32]::SetHandleInformation($hReadOut, [Chef.HandleFlags]::HANDLE_FLAG_INHERIT, 0)
210
+ if (-Not $success) {
211
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
212
+ throw "Unable to set output pipe handle information. Error code $reason."
213
+ }
214
+
215
+ $si.cb = [System.Runtime.InteropServices.Marshal]::SizeOf($si)
216
+ $si.wShowWindow = [Chef.ShowWindow]::SW_SHOW
217
+ $si.dwFlags = [Chef.STARTF]::STARTF_USESTDHANDLES
218
+ $si.hStdOutput = $hWriteOut
219
+ $si.hStdError = $hWriteOut
220
+ $si.hStdInput = [Chef.Kernel32]::GetStdHandle([Chef.StandardHandle]::Input)
221
+
222
+ $success = [Chef.Kernel32]::CreateProcess(
223
+ $AppPath,
224
+ $ArgumentString,
225
+ [ref] $pSec,
226
+ [ref] $tSec,
227
+ $true,
228
+ [Chef.CreationFlags]::NONE,
229
+ [IntPtr]::Zero,
230
+ $pwd,
231
+ [ref] $si,
232
+ [ref] $pi
233
+ )
234
+ if (-Not $success) {
235
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
236
+ throw "Unable to create process [$ArgumentString]. Error code $reason."
237
+ }
238
+
239
+ $buffer = New-Object byte[] 1024
240
+
241
+ # Initialize reference variables
242
+ $bytesRead = 0
243
+ $bytesAvailable = 0
244
+ $bytesLeftThisMsg = 0
245
+ $global:LASTEXITCODE = [Chef.Kernel32]::STILL_ACTIVE
246
+
247
+ $isActive = $true
248
+ while ($isActive) {
249
+ $success = [Chef.Kernel32]::GetExitCodeProcess($pi.hProcess, [ref] $global:LASTEXITCODE)
250
+ if (-Not $success) {
251
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
252
+ throw "Process exit code unavailable. Error code $reason."
253
+ }
254
+
255
+ $success = [Chef.Kernel32]::PeekNamedPipe(
256
+ $hReadOut,
257
+ $null,
258
+ $buffer.Length,
259
+ [ref] $bytesRead,
260
+ [ref] $bytesAvailable,
261
+ [ref] $bytesLeftThisMsg
262
+ )
263
+ if (-Not $success) {
264
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
265
+ throw "Output pipe unavailable for peeking. Error code $reason."
266
+ }
267
+
268
+ if ($bytesRead -gt 0) {
269
+ while ([Chef.Kernel32]::ReadFile($hReadOut, $buffer, $buffer.Length, [ref] $bytesRead, 0)) {
270
+ $output = [Text.Encoding]::UTF8.GetString($buffer, 0, $bytesRead)
271
+ if ($output) {
272
+ $output
273
+ }
274
+ if ($bytesRead -lt $buffer.Length) {
275
+ # Partial buffer indicating the end of stream, break out of ReadFile loop
276
+ # ReadFile will block until:
277
+ # The number of bytes requested is read.
278
+ # A write operation completes on the write end of the pipe.
279
+ # An asynchronous handle is being used and the read is occurring asynchronously.
280
+ # An error occurs.
281
+ break
282
+ }
283
+ }
284
+ } else {
285
+ # For some reason, you can't read from the read-end of the read-pipe before the write end has started
286
+ # to write. Otherwise the process just blocks forever and never returns from the read. So we peek
287
+ # at the pipe until there is something. But don't peek too eagerly. This is stupid stupid stupid.
288
+ # There must be a way to do this without having to peek at a pipe first but I have not found it.
289
+ #
290
+ # Note to the future intrepid soul who wants to fix this:
291
+ # 0) This is related to unreasonable CPU usage by the wrapper PS script on a 1 VCPU VM (either Hyper-V
292
+ # or VirtualBox) running a consumer Windows SKU (Windows 10 for example...). Test it there.
293
+ # 1) Maybe this entire script is unnecessary and the bugs mentioned below have been fixed or don't need
294
+ # to be supported.
295
+ # 2) The server and consumer windows schedulers have different defaults. I had a hard time reproducing
296
+ # any issue on a win 2008 on win 2012 server default setup. See the "foreground application scheduler
297
+ # priority" setting to see if it's relevant.
298
+ # 3) This entire endeavor is silly anyway - why are we reimplementing process forking all over? Maybe try
299
+ # to get the folks above to accept patches instead of extending this crazy script.
300
+ Start-Sleep -s 1
301
+ # Start-Sleep -m 100
302
+ }
303
+
304
+ if ($global:LASTEXITCODE -ne [Chef.Kernel32]::STILL_ACTIVE) {
305
+ $isActive = $false
306
+ }
307
+ }
308
+
309
+ # Cleanup handles
310
+ $success = [Chef.Kernel32]::CloseHandle($pi.hProcess)
311
+ if (-Not $success) {
312
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
313
+ throw "Unable to release process handle. Error code $reason."
314
+ }
315
+ $success = [Chef.Kernel32]::CloseHandle($pi.hThread)
316
+ if (-Not $success) {
317
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
318
+ throw "Unable to release thread handle. Error code $reason."
319
+ }
320
+ $success = [Chef.Kernel32]::CloseHandle($hWriteOut)
321
+ if (-Not $success) {
322
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
323
+ throw "Unable to release output write handle. Error code $reason."
324
+ }
325
+ $success = [Chef.Kernel32]::CloseHandle($hReadOut)
326
+ if (-Not $success) {
327
+ $reason = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
328
+ throw "Unable to release output read handle. Error code $reason."
329
+ }
330
+ [System.Runtime.InteropServices.Marshal]::FreeHGlobal($ptr)
331
+ }
332
+
333
+ function Get-ScriptDirectory {
334
+ if (!$PSScriptRoot) {
335
+ $Invocation = (Get-Variable MyInvocation -Scope 1).Value
336
+ $PSScriptRoot = Split-Path $Invocation.MyCommand.Path
337
+ }
338
+ $PSScriptRoot
339
+ }
340
+
341
+ function Run-RubyCommand($command, $argList) {
342
+ # This method exists to take the given list of arguments and get it past ruby's command-line
343
+ # interpreter unscathed and untampered. See https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L1582
344
+ # for a list of transformations that ruby attempts to perform with your command-line arguments
345
+ # before passing it onto a script. The most important task is to defeat the globbing
346
+ # and wild-card expansion that ruby performs. Note that ruby does not use MSVCRT's argc/argv
347
+ # and deliberately reparses the raw command-line instead.
348
+ #
349
+ # To stop ruby from interpreting command-line arguments as globs, they need to be enclosed in '
350
+ # Ruby doesn't allow any escape characters inside '. This unfortunately prevents us from sending
351
+ # any strings which themselves contain '. Ruby does allow multi-fragment arguments though.
352
+ # "foo bar"'baz qux'123"foo" is interpreted as 1 argument because there are no un-escaped
353
+ # whitespace there. The argument would be interpreted as the string "foo barbaz qux123foo".
354
+ # This lets us escape ' characters by exiting the ' quoted string, injecting a "'" fragment and
355
+ # then resuming the ' quoted string again.
356
+ #
357
+ # In the process of defeating ruby, one must also defeat the helpfulness of powershell.
358
+ # When arguments come into this method, the standard PS rules for interpreting cmdlet arguments
359
+ # apply. When using & (call operator) and providing an array of arguments, powershell (verified
360
+ # on PS 4.0 on Windows Server 2012R2) will not evaluate them but (contrary to documentation),
361
+ # it will still marginally interpret them. The behaviour of PS 5.0 seems to be different but
362
+ # ignore that for now. If any of the provided arguments has a space in it, powershell checks
363
+ # the first and last character to ensure that they are " characters (and that's all it checks).
364
+ # If they are not, it will blindly surround that argument with " characters. It won't do this
365
+ # operation if no space is present, even if other special characters are present. If it notices
366
+ # leading and trailing " characters, it won't actually check to see if there are other "
367
+ # characters in the string. Since PS 5.0 changes this behavior, we could consider using the --%
368
+ # "stop screwing up my arguments" operator, which is available since PS 3.0. When encountered
369
+ # --% indicates that the rest of line is to be sent literally... except if the parser encounters
370
+ # %FOO% cmd style environment variables. Because reasons. And there is no way to escape the
371
+ # % character in *any* waym shape or form.
372
+ # https://connect.microsoft.com/PowerShell/feedback/details/376207/executing-commands-which-require-quotes-and-variables-is-practically-impossible
373
+ #
374
+ # In case you think that you're either reading this incorrectly or that I'm full of shit, here
375
+ # are some examples. These use EchoArgs.exe from the PowerShell Community Extensions package.
376
+ # I have not included the argument parsing output from EchoArgs.exe to prevent confusing you with
377
+ # more details about MSVCRT's parsing algorithm.
378
+ #
379
+ # $x = "foo '' bar `"baz`""
380
+ # & EchoArgs @($x, $x)
381
+ # Command line:
382
+ # "C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" "foo '' bar "baz"" "foo '' bar "baz""
383
+ #
384
+ # $x = "abc'123'nospace`"lulz`"!!!"
385
+ # & EchoArgs @($x, $x)
386
+ # Command line:
387
+ # "C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" abc'123'nospace"lulz"!!! abc'123'nospace"lulz"!!!
388
+ #
389
+ # $x = "`"`"Look ma! Tonnes of spaces! 'foo' 'bar'`"`""
390
+ # & EchoArgs @($x, $x)
391
+ # Command line:
392
+ # "C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" ""Look ma! Tonnes of spaces! 'foo' 'bar'"" ""Look ma! Tonnes of spaces! 'foo' 'bar'""
393
+ #
394
+ # Given all this, we can now device a strategy to work around all these immensely helpful, well
395
+ # documented and useful tools by looking at each incoming argument, escaping any ' characters
396
+ # with a '"'"' sequence, surrounding each argument with ' & joining them with a space separating
397
+ # them.
398
+ # There is another bug (https://bugs.ruby-lang.org/issues/11142) that causes ruby to mangle any
399
+ # "" two-character double quote sequence but since we always emit our strings inside ' except for
400
+ # ' characters, this should be ok. Just remember that an argument '' should get translated to
401
+ # ''"'"''"'"'' on the command line. If those intervening empty ''s are not present, the presence
402
+ # of "" will cause ruby to mangle that argument.
403
+ $transformedList = $argList | foreach { "'" + ( $_ -replace "'","'`"'`"'" ) + "'" }
404
+ $fortifiedArgString = $transformedList -join ' '
405
+
406
+ # Use the correct embedded ruby path. We'll be deployed at a path that looks like
407
+ # [C:\opscode or some other prefix]\chef\modules\chef
408
+ $ruby = Join-Path (Get-ScriptDirectory) "..\..\embedded\bin\ruby.exe"
409
+ $commandPath = Join-Path (Get-ScriptDirectory) "..\..\bin\$command"
410
+
411
+ Run-ExecutableAndWait $ruby """$ruby"" '$commandPath' $fortifiedArgString"
412
+ }
413
+
414
+
415
+ function chef-apply {
416
+ Run-RubyCommand 'chef-apply' $args
417
+ }
418
+
419
+ function chef-client {
420
+ Run-RubyCommand 'chef-client' $args
421
+ }
422
+
423
+ function chef-service-manager {
424
+ Run-RubyCommand 'chef-service-manager' $args
425
+ }
426
+
427
+ function chef-shell {
428
+ Run-RubyCommand 'chef-shell' $args
429
+ }
430
+
431
+ function chef-solo {
432
+ Run-RubyCommand 'chef-solo' $args
433
+ }
434
+
435
+ function chef-windows-service {
436
+ Run-RubyCommand 'chef-windows-service' $args
437
+ }
438
+
439
+ function knife {
440
+ Run-RubyCommand 'knife' $args
441
+ }
442
+
443
+ Export-ModuleMember -function chef-apply
444
+ Export-ModuleMember -function chef-client
445
+ Export-ModuleMember -function chef-service-manager
446
+ Export-ModuleMember -function chef-shell
447
+ Export-ModuleMember -function chef-solo
448
+ Export-ModuleMember -function chef-windows-service
449
+ Export-ModuleMember -function knife
450
+
451
+ # To debug this module, uncomment the line below
452
+ # Export-ModuleMember -function Run-RubyCommand
453
+
454
+ # Then run the following to reload the module. Use puts_argv as a helpful debug executable.
455
+ # Remove-Module chef
456
+ # Import-Module chef
457
+ # "puts ARGV" | Out-File C:\opscode\chef\bin\puts_args -Encoding ASCII
458
+ # Copy-Item C:\opscode\chef\bin\ohai.bat C:\opscode\chef\bin\puts_args.bat
459
+ # Run-RubyCommand puts_args 'Here' "are" some '"very interesting"' 'arguments[to]' "`"try out`""