diff --git a/Gemfile b/Gemfile index ac310c2..5f2ca4d 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ source "https://rubygems.org" # Specify your gem's dependencies in win32-certstore.gemspec gemspec -gem "mixlib-shellout", "< 3.2.3" +# gem "mixlib-shellout", "< 3.2.3" if Gem.ruby_version.to_s.start_with?("2.5") # 16.7.23 required ruby 2.6+ diff --git a/lib/win32/certstore/mixin/shell_exec.rb b/lib/win32/certstore/mixin/shell_exec.rb deleted file mode 100644 index 8021924..0000000 --- a/lib/win32/certstore/mixin/shell_exec.rb +++ /dev/null @@ -1,105 +0,0 @@ -# -# Author:: Daniel DeLeo () -# Copyright:: Copyright (c) 2017 Chef Software, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require "mixlib/shellout" unless defined?(Mixlib::ShellOut) - -module Win32 - class Certstore - module Mixin - module ShellExec - def shell_out_command(*command_args) - cmd = Mixlib::ShellOut.new(*command_args) - cmd.live_stream - cmd.run_command - if cmd.error! - raise Mixlib::ShellOut::ShellCommandFailed, cmd.error! - end - - cmd - end - - # Run a command under powershell with the same API as shell_out. The - # options hash is extended to take an "architecture" flag which - # can be set to :i386 or :x86_64 to force the windows architecture. - # - # @param script [String] script to run - # @param options [Hash] options hash - # @return [Mixlib::Shellout] mixlib-shellout object - def powershell_exec(*command_args) - script = command_args.first - options = command_args.last.is_a?(Hash) ? command_args.last : nil - - run_command_with_os_architecture(script, options) - end - - # Run a command under powershell with the same API as shell_out! - # (raises exceptions on errors) - # - # @param script [String] script to run - # @param options [Hash] options hash - # @return [Mixlib::Shellout] mixlib-shellout object - def powershell_exec!(*command_args) - cmd = powershell_exec(*command_args) - cmd.error! - cmd - end - - private - - # Helper function to run shell_out and wrap it with the correct - # flags to possibly disable WOW64 redirection (which we often need - # because chef-client runs as a 32-bit app on 64-bit windows). - # - # @param script [String] script to run - # @param options [Hash] options hash - # @return [Mixlib::Shellout] mixlib-shellout object - def run_command_with_os_architecture(script, options) - options ||= {} - options = options.dup - - shell_out_command( - build_powershell_command(script), - options - ) - end - - # Helper to build a powershell command around the script to run. - # - # @param script [String] script to run - # @return [String] powershell command to execute - def build_powershell_command(script) - flags = [ - # Hides the copyright banner at startup. - "-NoLogo", - # Does not present an interactive prompt to the user. - "-NonInteractive", - # Does not load the Windows PowerShell profile. - "-NoProfile", - # always set the ExecutionPolicy flag - # see http://technet.microsoft.com/en-us/library/ee176961.aspx - "-ExecutionPolicy Unrestricted", - # Powershell will hang if STDIN is redirected - # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected - "-InputFormat None", - ] - - "powershell.exe #{flags.join(" ")} -Command \"#{script.gsub('"', '\"')}\"" - end - end - end - end -end diff --git a/spec/win32/unit/store/shell_exec_spec.rb b/spec/win32/unit/store/shell_exec_spec.rb deleted file mode 100644 index 28e3289..0000000 --- a/spec/win32/unit/store/shell_exec_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require "spec_helper" -require "win32/certstore/mixin/shell_exec" - -describe Win32::Certstore::Mixin::ShellExec do - let(:string_class) { Class.new { include Win32::Certstore::Mixin::ShellExec } } - subject(:string_obj) { string_class.new } - - context "when testing individual methods" do - describe "#shell_out_Command" do - it "executes shellout command" do - cmd = "echo '#{rand(1000)}'" - expect(string_obj).to receive(:shell_out_Command).with(cmd).and_return(true) - string_obj.shell_out_Command(cmd) - end - - it "raises Mixlib::ShellOut::ShellCommandFailed error if invalid command is passed" do - cmd = "powershell.exe -Command -in 'abc'" - expect { string_obj.shell_out_command(cmd) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed) - end - end - end -end diff --git a/win32-certstore.gemspec b/win32-certstore.gemspec index 3ac2eb8..d974022 100644 --- a/win32-certstore.gemspec +++ b/win32-certstore.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler" spec.add_development_dependency "rspec", "~> 3.0" - spec.add_dependency "mixlib-shellout" + # spec.add_dependency "mixlib-shellout" spec.add_dependency "ffi" spec.add_runtime_dependency "chef-powershell" spec.metadata["yard.run"] = "yri"