chef 16.7.61 → 16.8.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -2
  3. data/README.md +1 -1
  4. data/chef.gemspec +2 -1
  5. data/lib/chef/application/base.rb +1 -1
  6. data/lib/chef/client.rb +3 -0
  7. data/lib/chef/compliance/default_attributes.rb +89 -0
  8. data/lib/chef/compliance/fetcher/automate.rb +69 -0
  9. data/lib/chef/compliance/fetcher/chef_server.rb +134 -0
  10. data/lib/chef/compliance/reporter/automate.rb +202 -0
  11. data/lib/chef/compliance/reporter/chef_server_automate.rb +92 -0
  12. data/lib/chef/compliance/reporter/compliance_enforcer.rb +20 -0
  13. data/lib/chef/compliance/reporter/json_file.rb +19 -0
  14. data/lib/chef/compliance/runner.rb +250 -0
  15. data/lib/chef/cookbook_manifest.rb +1 -0
  16. data/lib/chef/encrypted_data_bag_item/assertions.rb +1 -1
  17. data/lib/chef/exceptions.rb +4 -0
  18. data/lib/chef/http/ssl_policies.rb +6 -0
  19. data/lib/chef/knife/bootstrap/train_connector.rb +1 -1
  20. data/lib/chef/knife/core/ui.rb +4 -1
  21. data/lib/chef/knife/ssh.rb +1 -1
  22. data/lib/chef/mixin/powershell_exec.rb +3 -1
  23. data/lib/chef/platform/query_helpers.rb +4 -4
  24. data/lib/chef/powershell.rb +2 -0
  25. data/lib/chef/provider/dsc_resource.rb +12 -24
  26. data/lib/chef/provider/dsc_script.rb +16 -20
  27. data/lib/chef/provider/git.rb +5 -5
  28. data/lib/chef/resource/chef_client_config.rb +1 -1
  29. data/lib/chef/resource/dsc_script.rb +8 -1
  30. data/lib/chef/resource/hostname.rb +3 -3
  31. data/lib/chef/resource/template.rb +2 -2
  32. data/lib/chef/resource/windows_certificate.rb +7 -1
  33. data/lib/chef/resource_collection/resource_set.rb +1 -1
  34. data/lib/chef/util/dsc/configuration_generator.rb +52 -11
  35. data/lib/chef/util/dsc/lcm_output_parser.rb +3 -4
  36. data/lib/chef/util/dsc/local_configuration_manager.rb +17 -14
  37. data/lib/chef/util/dsc/resource_store.rb +5 -11
  38. data/lib/chef/version.rb +1 -1
  39. data/lib/chef/win32/api/file.rb +4 -0
  40. data/spec/functional/resource/dsc_script_spec.rb +3 -6
  41. data/spec/integration/client/client_spec.rb +2 -1
  42. data/spec/integration/compliance/compliance_spec.rb +81 -0
  43. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -0
  44. data/spec/spec_helper.rb +1 -1
  45. data/spec/unit/client_spec.rb +1 -0
  46. data/spec/unit/compliance/fetcher/automate_spec.rb +134 -0
  47. data/spec/unit/compliance/fetcher/chef_server_spec.rb +93 -0
  48. data/spec/unit/compliance/reporter/automate_spec.rb +427 -0
  49. data/spec/unit/compliance/reporter/chef_server_automate_spec.rb +177 -0
  50. data/spec/unit/compliance/reporter/compliance_enforcer_spec.rb +48 -0
  51. data/spec/unit/compliance/runner_spec.rb +113 -0
  52. data/spec/unit/http/ssl_policies_spec.rb +11 -0
  53. data/spec/unit/knife/core/node_editor_spec.rb +1 -1
  54. data/spec/unit/mixin/powershell_exec_spec.rb +1 -1
  55. data/spec/unit/platform/query_helpers_spec.rb +11 -12
  56. data/spec/unit/provider/dsc_resource_spec.rb +10 -27
  57. data/spec/unit/provider/dsc_script_spec.rb +1 -1
  58. data/spec/unit/provider/mount/windows_spec.rb +1 -0
  59. data/spec/unit/provider/systemd_unit_spec.rb +1 -1
  60. data/spec/unit/resource/windows_certificate_spec.rb +12 -0
  61. data/spec/unit/util/dsc/configuration_generator_spec.rb +79 -0
  62. data/spec/unit/util/dsc/local_configuration_manager_spec.rb +27 -35
  63. metadata +37 -12
  64. data/lib/chef/util/powershell/cmdlet.rb +0 -169
  65. data/lib/chef/util/powershell/cmdlet_result.rb +0 -61
  66. data/spec/functional/util/powershell/cmdlet_spec.rb +0 -111
  67. data/spec/unit/util/powershell/cmdlet_spec.rb +0 -106
@@ -49,23 +49,22 @@ describe Chef::Util::DSC::LocalConfigurationManager do
49
49
  EOH
50
50
  end
51
51
 
52
- let(:lcm_status) do
53
- double("LCM cmdlet status", stderr: lcm_standard_error, return_value: lcm_standard_output, succeeded?: lcm_cmdlet_success)
52
+ let(:powershell) do
53
+ double("Chef::PowerShell", errors: lcm_errors, error?: !lcm_errors.empty?, result: lcm_result)
54
54
  end
55
55
 
56
56
  describe "test_configuration method invocation" do
57
57
  context "when interacting with the LCM using a PowerShell cmdlet" do
58
58
  before(:each) do
59
- allow(lcm).to receive(:run_configuration_cmdlet).and_return(lcm_status)
59
+ allow(lcm).to receive(:run_configuration_cmdlet).and_return(powershell)
60
60
  allow(lcm).to receive(:ps_version_gte_5?).and_return(false)
61
61
  end
62
62
  context "that returns successfully" do
63
- let(:lcm_standard_output) { normal_lcm_output }
64
- let(:lcm_standard_error) { nil }
65
- let(:lcm_cmdlet_success) { true }
63
+ let(:lcm_result) { normal_lcm_output }
64
+ let(:lcm_errors) { [] }
66
65
 
67
66
  it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do
68
- test_configuration_result = lcm.test_configuration("config", {})
67
+ test_configuration_result = lcm.test_configuration("config")
69
68
  expect(test_configuration_result.class).to be(Array)
70
69
  expect(test_configuration_result.length).to be > 0
71
70
  expect(Chef::Log).not_to receive(:warn)
@@ -73,13 +72,12 @@ describe Chef::Util::DSC::LocalConfigurationManager do
73
72
  end
74
73
 
75
74
  context "when running on PowerShell version 5" do
76
- let(:lcm_standard_output) { normal_lcm_output }
77
- let(:lcm_standard_error) { nil }
78
- let(:lcm_cmdlet_success) { true }
75
+ let(:lcm_result) { normal_lcm_output }
76
+ let(:lcm_errors) { [] }
79
77
 
80
78
  it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do
81
79
  allow(lcm).to receive(:ps_version_gte_5?).and_return(true)
82
- test_configuration_result = lcm.test_configuration("config", {})
80
+ test_configuration_result = lcm.test_configuration("config")
83
81
  expect(test_configuration_result.class).to be(Array)
84
82
  expect(test_configuration_result.length).to be > 0
85
83
  expect(Chef::Log).not_to receive(:warn)
@@ -87,13 +85,12 @@ describe Chef::Util::DSC::LocalConfigurationManager do
87
85
  end
88
86
 
89
87
  context "when running on PowerShell version less than 5" do
90
- let(:lcm_standard_output) { normal_lcm_output }
91
- let(:lcm_standard_error) { nil }
92
- let(:lcm_cmdlet_success) { true }
88
+ let(:lcm_result) { normal_lcm_output }
89
+ let(:lcm_errors) { [] }
93
90
 
94
91
  it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do
95
92
  allow(lcm).to receive(:ps_version_gte_5?).and_return(false)
96
- test_configuration_result = lcm.test_configuration("config", {})
93
+ test_configuration_result = lcm.test_configuration("config")
97
94
  expect(test_configuration_result.class).to be(Array)
98
95
  expect(test_configuration_result.length).to be > 0
99
96
  expect(Chef::Log).not_to receive(:warn)
@@ -104,10 +101,9 @@ describe Chef::Util::DSC::LocalConfigurationManager do
104
101
  let(:common_command_prefix) { "$ProgressPreference = 'SilentlyContinue';" }
105
102
  let(:ps4_base_command) { "#{common_command_prefix} Start-DscConfiguration -path tmp -wait -erroraction 'stop' -force" }
106
103
  let(:lcm_command_ps4) { ps4_base_command + " -whatif; if (! $?) { exit 1 }" }
107
- let(:lcm_command_ps5) { "#{common_command_prefix} Test-DscConfiguration -path tmp | format-list" }
108
- let(:lcm_standard_output) { normal_lcm_output }
109
- let(:lcm_standard_error) { nil }
110
- let(:lcm_cmdlet_success) { true }
104
+ let(:lcm_command_ps5) { "#{common_command_prefix} Test-DscConfiguration -path tmp | format-list | Out-String" }
105
+ let(:lcm_result) { normal_lcm_output }
106
+ let(:lcm_errors) { [] }
111
107
 
112
108
  it "successfully returns command when apply_configuration true" do
113
109
  expect(lcm.send(:lcm_command, true)).to eq(ps4_base_command)
@@ -125,9 +121,8 @@ describe Chef::Util::DSC::LocalConfigurationManager do
125
121
  end
126
122
 
127
123
  context "that fails due to missing what-if switch in DSC resource cmdlet implementation" do
128
- let(:lcm_standard_output) { "" }
129
- let(:lcm_standard_error) { no_whatif_lcm_output }
130
- let(:lcm_cmdlet_success) { false }
124
+ let(:lcm_result) { "" }
125
+ let(:lcm_errors) { [no_whatif_lcm_output] }
131
126
 
132
127
  it "returns true when passed to #whatif_not_supported?" do
133
128
  expect(lcm.send(:whatif_not_supported?, no_whatif_lcm_output)).to be_truthy
@@ -137,40 +132,38 @@ describe Chef::Util::DSC::LocalConfigurationManager do
137
132
  expect(Chef::Log).to receive(:warn).at_least(:once)
138
133
  expect(lcm).to receive(:whatif_not_supported?).and_call_original
139
134
  test_configuration_result = nil
140
- expect { test_configuration_result = lcm.test_configuration("config", {}) }.not_to raise_error
135
+ expect { test_configuration_result = lcm.test_configuration("config") }.not_to raise_error
141
136
  expect(test_configuration_result.class).to be(Array)
142
137
  end
143
138
  end
144
139
 
145
140
  context "that fails due to a DSC resource not being imported before StartDSCConfiguration -whatif is executed" do
146
- let(:lcm_standard_output) { "" }
147
- let(:lcm_standard_error) { dsc_resource_import_failure_output }
148
- let(:lcm_cmdlet_success) { false }
141
+ let(:lcm_result) { "" }
142
+ let(:lcm_errors) { [dsc_resource_import_failure_output] }
149
143
 
150
144
  it "logs a warning if the message is formatted as expected when a resource import failure occurs" do
151
145
  expect(Chef::Log).to receive(:warn).at_least(:once)
152
146
  expect(lcm).to receive(:dsc_module_import_failure?).and_call_original
153
147
  test_configuration_result = nil
154
- expect { test_configuration_result = lcm.test_configuration("config", {}) }.not_to raise_error
148
+ expect { test_configuration_result = lcm.test_configuration("config") }.not_to raise_error
155
149
  end
156
150
 
157
151
  it "returns a (possibly empty) array of ResourceInfo instances" do
158
152
  expect(Chef::Log).to receive(:warn).at_least(:once)
159
153
  test_configuration_result = nil
160
- expect { test_configuration_result = lcm.test_configuration("config", {}) }.not_to raise_error
154
+ expect { test_configuration_result = lcm.test_configuration("config") }.not_to raise_error
161
155
  expect(test_configuration_result.class).to be(Array)
162
156
  end
163
157
  end
164
158
 
165
159
  context "that fails due to an unknown PowerShell cmdlet error" do
166
- let(:lcm_standard_output) { "some output" }
167
- let(:lcm_standard_error) { "Abort, Retry, Fail?" }
168
- let(:lcm_cmdlet_success) { false }
160
+ let(:lcm_result) { "some output" }
161
+ let(:lcm_errors) { ["Abort, Retry, Fail?"] }
169
162
 
170
163
  it "logs a warning" do
171
164
  expect(Chef::Log).to receive(:warn).at_least(:once)
172
165
  expect(lcm).to receive(:dsc_module_import_failure?).and_call_original
173
- expect { lcm.test_configuration("config", {}) }.not_to raise_error
166
+ expect { lcm.test_configuration("config") }.not_to raise_error
174
167
  end
175
168
  end
176
169
  end
@@ -188,12 +181,11 @@ describe Chef::Util::DSC::LocalConfigurationManager do
188
181
  end
189
182
  end
190
183
 
191
- describe "#run_configuration_cmdlet" do
184
+ describe "#run_configuration_cmdlet", :windows_powershell_dsc_only do
192
185
  context "when invalid dsc script is given" do
193
186
  it "raises exception" do
194
187
  configuration_document = "invalid-config"
195
- shellout_flags = { cwd: nil, environment: nil, timeout: nil }
196
- expect { lcm.send(:run_configuration_cmdlet, configuration_document, true, shellout_flags) }.to raise_error(Chef::Exceptions::PowershellCmdletException)
188
+ expect { lcm.send(:run_configuration_cmdlet, configuration_document, true) }.to raise_error(Chef::PowerShell::CommandFailed)
197
189
  end
198
190
  end
199
191
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef
3
3
  version: !ruby/object:Gem::Version
4
- version: 16.7.61
4
+ version: 16.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Jacob
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef-config
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 16.7.61
19
+ version: 16.8.9
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 16.7.61
26
+ version: 16.8.9
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: chef-utils
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 16.7.61
33
+ version: 16.8.9
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 16.7.61
40
+ version: 16.8.9
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: train-core
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +206,20 @@ dependencies:
206
206
  - - "~>"
207
207
  - !ruby/object:Gem::Version
208
208
  version: '16.0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: inspec-core
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '4.23'
216
+ type: :runtime
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '4.23'
209
223
  - !ruby/object:Gem::Dependency
210
224
  name: ffi
211
225
  requirement: !ruby/object:Gem::Requirement
@@ -240,7 +254,7 @@ dependencies:
240
254
  requirements:
241
255
  - - ">="
242
256
  - !ruby/object:Gem::Version
243
- version: '4.2'
257
+ version: '5.1'
244
258
  - - "<"
245
259
  - !ruby/object:Gem::Version
246
260
  version: '7'
@@ -250,7 +264,7 @@ dependencies:
250
264
  requirements:
251
265
  - - ">="
252
266
  - !ruby/object:Gem::Version
253
- version: '4.2'
267
+ version: '5.1'
254
268
  - - "<"
255
269
  - !ruby/object:Gem::Version
256
270
  version: '7'
@@ -717,6 +731,14 @@ files:
717
731
  - lib/chef/chef_fs/parallelizer/parallel_enumerable.rb
718
732
  - lib/chef/chef_fs/path_utils.rb
719
733
  - lib/chef/client.rb
734
+ - lib/chef/compliance/default_attributes.rb
735
+ - lib/chef/compliance/fetcher/automate.rb
736
+ - lib/chef/compliance/fetcher/chef_server.rb
737
+ - lib/chef/compliance/reporter/automate.rb
738
+ - lib/chef/compliance/reporter/chef_server_automate.rb
739
+ - lib/chef/compliance/reporter/compliance_enforcer.rb
740
+ - lib/chef/compliance/reporter/json_file.rb
741
+ - lib/chef/compliance/runner.rb
720
742
  - lib/chef/config.rb
721
743
  - lib/chef/config_fetcher.rb
722
744
  - lib/chef/constants.rb
@@ -1441,8 +1463,6 @@ files:
1441
1463
  - lib/chef/util/editor.rb
1442
1464
  - lib/chef/util/file_edit.rb
1443
1465
  - lib/chef/util/path_helper.rb
1444
- - lib/chef/util/powershell/cmdlet.rb
1445
- - lib/chef/util/powershell/cmdlet_result.rb
1446
1466
  - lib/chef/util/powershell/ps_credential.rb
1447
1467
  - lib/chef/util/selinux.rb
1448
1468
  - lib/chef/util/threaded_job_queue.rb
@@ -1989,7 +2009,6 @@ files:
1989
2009
  - spec/functional/shell_spec.rb
1990
2010
  - spec/functional/tiny_server_spec.rb
1991
2011
  - spec/functional/util/path_helper_spec.rb
1992
- - spec/functional/util/powershell/cmdlet_spec.rb
1993
2012
  - spec/functional/version_spec.rb
1994
2013
  - spec/functional/win32/crypto_spec.rb
1995
2014
  - spec/functional/win32/registry_spec.rb
@@ -2001,6 +2020,7 @@ files:
2001
2020
  - spec/integration/client/client_spec.rb
2002
2021
  - spec/integration/client/exit_code_spec.rb
2003
2022
  - spec/integration/client/ipv6_spec.rb
2023
+ - spec/integration/compliance/compliance_spec.rb
2004
2024
  - spec/integration/knife/chef_fs_data_store_spec.rb
2005
2025
  - spec/integration/knife/chef_repo_path_spec.rb
2006
2026
  - spec/integration/knife/chef_repository_file_system_spec.rb
@@ -2165,6 +2185,12 @@ files:
2165
2185
  - spec/unit/chef_fs/path_util_spec.rb
2166
2186
  - spec/unit/chef_spec.rb
2167
2187
  - spec/unit/client_spec.rb
2188
+ - spec/unit/compliance/fetcher/automate_spec.rb
2189
+ - spec/unit/compliance/fetcher/chef_server_spec.rb
2190
+ - spec/unit/compliance/reporter/automate_spec.rb
2191
+ - spec/unit/compliance/reporter/chef_server_automate_spec.rb
2192
+ - spec/unit/compliance/reporter/compliance_enforcer_spec.rb
2193
+ - spec/unit/compliance/runner_spec.rb
2168
2194
  - spec/unit/config_fetcher_spec.rb
2169
2195
  - spec/unit/config_spec.rb
2170
2196
  - spec/unit/cookbook/chefignore_spec.rb
@@ -2700,7 +2726,6 @@ files:
2700
2726
  - spec/unit/util/dsc/resource_store.rb
2701
2727
  - spec/unit/util/editor_spec.rb
2702
2728
  - spec/unit/util/file_edit_spec.rb
2703
- - spec/unit/util/powershell/cmdlet_spec.rb
2704
2729
  - spec/unit/util/powershell/ps_credential_spec.rb
2705
2730
  - spec/unit/util/selinux_spec.rb
2706
2731
  - spec/unit/util/threaded_job_queue_spec.rb
@@ -1,169 +0,0 @@
1
- #
2
- # Author:: Adam Edwards (<adamed@chef.io>)
3
- #
4
- # Copyright:: Copyright (c) Chef Software Inc.
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- module Mixlib
20
- autoload :ShellOut, "mixlib/shellout"
21
- end
22
- require_relative "../../mixin/windows_architecture_helper"
23
- require_relative "cmdlet_result"
24
-
25
- class Chef
26
- class Util
27
- class Powershell
28
- class Cmdlet
29
- def initialize(node, cmdlet, output_format = nil, output_format_options = {})
30
- @output_format = output_format
31
- @node = node
32
-
33
- case output_format
34
- when nil, :text
35
- @json_format = false
36
- when :json, :object
37
- @json_format = true
38
- else
39
- raise ArgumentError, "Invalid output format #{output_format} specified"
40
- end
41
-
42
- @cmdlet = cmdlet
43
- @output_format_options = output_format_options
44
- end
45
-
46
- attr_reader :output_format
47
-
48
- def run(switches = {}, execution_options = {}, *arguments)
49
- streams = { json: CmdletStream.new("json"),
50
- verbose: CmdletStream.new("verbose"),
51
- }
52
-
53
- arguments_string = arguments.join(" ")
54
-
55
- switches_string = command_switches_string(switches)
56
-
57
- json_depth = 5
58
-
59
- if @json_format && @output_format_options.key?(:depth)
60
- json_depth = @output_format_options[:depth]
61
- end
62
-
63
- json_command = if @json_format
64
- " | convertto-json -compress -depth #{json_depth} > #{streams[:json].path}"
65
- else
66
- ""
67
- end
68
- redirections = "4> '#{streams[:verbose].path}'"
69
- command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive "\
70
- "-command \"trap [Exception] {write-error -exception "\
71
- "($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} "\
72
- "#{arguments_string} #{redirections}"\
73
- "#{json_command}\";if ( ! $? ) { exit 1 }"
74
-
75
- augmented_options = { returns: [0], live_stream: false }.merge(execution_options)
76
- command = Mixlib::ShellOut.new(command_string, augmented_options)
77
-
78
- status = nil
79
-
80
- with_os_architecture(@node) do
81
- status = command.run_command
82
- end
83
-
84
- CmdletResult.new(status, streams, @output_format)
85
- end
86
-
87
- def run!(switches = {}, execution_options = {}, *arguments)
88
- result = run(switches, execution_options, arguments)
89
-
90
- unless result.succeeded?
91
- raise Chef::Exceptions::PowershellCmdletException, "PowerShell Cmdlet failed: #{result.stderr}"
92
- end
93
-
94
- result
95
- end
96
-
97
- protected
98
-
99
- include Chef::Mixin::WindowsArchitectureHelper
100
-
101
- def validate_switch_name!(switch_parameter_name)
102
- if !!(switch_parameter_name =~ /\A[A-Za-z]+[_a-zA-Z0-9]*\Z/) == false
103
- raise ArgumentError, "`#{switch_parameter_name}` is not a valid PowerShell cmdlet switch parameter name"
104
- end
105
- end
106
-
107
- def escape_parameter_value(parameter_value)
108
- parameter_value.gsub(/(`|'|"|#)/, '`\1')
109
- end
110
-
111
- def escape_string_parameter_value(parameter_value)
112
- "'#{escape_parameter_value(parameter_value)}'"
113
- end
114
-
115
- def command_switches_string(switches)
116
- command_switches = switches.map do |switch_name, switch_value|
117
- if switch_name.class != Symbol
118
- raise ArgumentError, "Invalid type `#{switch_name} `for PowerShell switch '#{switch_name}'. The switch must be specified as a Symbol'"
119
- end
120
-
121
- validate_switch_name!(switch_name)
122
-
123
- switch_argument = ""
124
- switch_present = true
125
-
126
- case switch_value
127
- when Numeric, Float
128
- switch_argument = switch_value.to_s
129
- when FalseClass
130
- switch_present = false
131
- when TrueClass
132
- when String
133
- switch_argument = escape_string_parameter_value(switch_value)
134
- else
135
- raise ArgumentError, "Invalid argument type `#{switch_value.class}` specified for PowerShell switch `:#{switch_name}`. Arguments to PowerShell must be of type `String`, `Numeric`, `Float`, `FalseClass`, or `TrueClass`"
136
- end
137
-
138
- switch_present ? ["-#{switch_name.to_s.downcase}", switch_argument].join(" ").strip : ""
139
- end
140
-
141
- command_switches.join(" ")
142
- end
143
-
144
- class CmdletStream
145
- def initialize(name)
146
- @filename = Dir::Tmpname.create(name) {}
147
- ObjectSpace.define_finalizer(self, self.class.destroy(@filename))
148
- end
149
-
150
- def path
151
- @filename
152
- end
153
-
154
- def read
155
- if File.exist? @filename
156
- File.open(@filename, "rb:bom|UTF-16LE") do |f|
157
- f.read.encode("UTF-8")
158
- end
159
- end
160
- end
161
-
162
- def self.destroy(name)
163
- proc { File.delete(name) if File.exist? name }
164
- end
165
- end
166
- end
167
- end
168
- end
169
- end