From 323db938b5e93ca63c470345896763890478c4d5 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Tue, 30 Jul 2019 15:07:58 -0700 Subject: Blinding applying chefstyle -a. Signed-off-by: Ryan Davis --- Rakefile | 2 +- lib/mixlib/shellout.rb | 8 ++++++-- lib/mixlib/shellout/unix.rb | 8 ++++++-- lib/mixlib/shellout/windows.rb | 9 ++++++++- lib/mixlib/shellout/windows/core_ext.rb | 18 +++++++++++------- spec/mixlib/shellout_spec.rb | 10 ++++++---- spec/spec_helper.rb | 2 +- spec/support/platform_helpers.rb | 1 + 8 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Rakefile b/Rakefile index c2be547..4b83938 100644 --- a/Rakefile +++ b/Rakefile @@ -40,4 +40,4 @@ task :console do IRB.start end -task default: [:spec, :style] +task default: %i{spec style} diff --git a/lib/mixlib/shellout.rb b/lib/mixlib/shellout.rb index e95e7df..9345eea 100644 --- a/lib/mixlib/shellout.rb +++ b/lib/mixlib/shellout.rb @@ -210,15 +210,17 @@ module Mixlib # TODO migrate to shellout/unix.rb def uid return nil unless user - user.kind_of?(Integer) ? user : Etc.getpwnam(user.to_s).uid + + user.is_a?(Integer) ? user : Etc.getpwnam(user.to_s).uid end # The gid that the subprocess will switch to. If the group attribute is # given as a group name, it is converted to a gid by Etc.getgrnam # TODO migrate to shellout/unix.rb def gid - return group.kind_of?(Integer) ? group : Etc.getgrnam(group.to_s).gid if group + return group.is_a?(Integer) ? group : Etc.getgrnam(group.to_s).gid if group return Etc.getpwuid(uid).gid if using_login? + nil end @@ -231,6 +233,7 @@ module Mixlib # results when the command exited with an unexpected status. def format_for_exception return "Command execution failed. STDOUT/STDERR suppressed for sensitive resource" if sensitive + msg = "" msg << "#{@terminate_reason}\n" if @terminate_reason msg << "---- Begin output of #{command} ----\n" @@ -363,6 +366,7 @@ module Mixlib if login && !user raise InvalidCommandOption, "cannot set login without specifying a user" end + super end end diff --git a/lib/mixlib/shellout/unix.rb b/lib/mixlib/shellout/unix.rb index 321d2fd..b8f42e0 100644 --- a/lib/mixlib/shellout/unix.rb +++ b/lib/mixlib/shellout/unix.rb @@ -53,14 +53,16 @@ module Mixlib # to the user's secondary groups def sgids return nil unless using_login? + user_name = Etc.getpwuid(uid).name - all_seconderies.select { |g| g.mem.include?(user_name) }.map { |g| g.gid } + all_seconderies.select { |g| g.mem.include?(user_name) }.map(&:gid) end # The environment variables that are deduced from simulating logon # Only valid if login is used def logon_environment return {} unless using_login? + entry = Etc.getpwuid(uid) # According to `man su`, the set fields are: # $HOME, $SHELL, $USER, $LOGNAME, $PATH, and $IFS @@ -269,6 +271,7 @@ module Mixlib # Keep this unbuffered for now def write_to_child_stdin return unless input + child_stdin << input child_stdin.close # Kick things off end @@ -337,7 +340,7 @@ module Mixlib set_cwd begin - command.kind_of?(Array) ? exec(*command, close_others: true) : exec(command, close_others: true) + command.is_a?(Array) ? exec(*command, close_others: true) : exec(command, close_others: true) raise "forty-two" # Should never get here rescue Exception => e @@ -365,6 +368,7 @@ module Mixlib def reap_errant_child return if attempt_reap + @terminate_reason = "Command exceeded allowed execution time, process terminated" logger.error("Command exceeded allowed execution time, sending TERM") if logger Process.kill(:TERM, child_pgid) diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index db4fe32..26130d3 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -110,6 +110,7 @@ module Mixlib unless GetExitCodeProcess(process.process_handle, exit_code) raise get_last_error end + @status = ThingThatLooksSortOfLikeAProcessStatus.new @status.exitstatus = exit_code.unpack("l").first @@ -170,8 +171,9 @@ module Mixlib def consume_output(open_streams, stdout_read, stderr_read) return false if open_streams.length == 0 + ready = IO.select(open_streams, nil, nil, READ_WAIT_TIME) - return true if ! ready + return true unless ready if ready.first.include?(stdout_read) begin @@ -227,6 +229,7 @@ module Mixlib # @return String def combine_args(*args) return args[0] if args.length == 1 + args.map do |arg| if arg =~ /[ \t\n\v"]/ arg = arg.gsub(/(\\*)"/, '\1\1\"') # interior quotes with N preceeding backslashes need 2N+1 backslashes @@ -321,10 +324,12 @@ module Mixlib return true unless quote when "%" return true if env + env = env_first_char = true next else next unless env + if env_first_char env_first_char = false (env = false) && next if c !~ /[A-Za-z_]/ @@ -370,6 +375,7 @@ module Mixlib def unsafe_process?(name, logger) return false unless system_required_processes.include? name + logger.debug( "A request to kill a critical system process - #{name} - was received and skipped." ) @@ -383,6 +389,7 @@ module Mixlib def kill_process_tree(pid, wmi, logger) wmi.query("select * from Win32_Process where ParentProcessID=#{pid}").each do |instance| next if unsafe_process?(instance.wmi_ole_object.name, logger) + child_pid = instance.wmi_ole_object.processid kill_process_tree(child_pid, wmi, logger) kill_process(instance, logger) diff --git a/lib/mixlib/shellout/windows/core_ext.rb b/lib/mixlib/shellout/windows/core_ext.rb index 73f6611..83a31ab 100644 --- a/lib/mixlib/shellout/windows/core_ext.rb +++ b/lib/mixlib/shellout/windows/core_ext.rb @@ -73,19 +73,19 @@ module Process::Functions [:pointer], :bool attach_pfunc :LoadUserProfileW, - [:handle, :pointer], :bool + %i{handle pointer}, :bool attach_pfunc :UnloadUserProfile, - [:handle, :handle], :bool + %i{handle handle}, :bool ffi_lib :advapi32 attach_pfunc :LogonUserW, - [:buffer_in, :buffer_in, :buffer_in, :ulong, :ulong, :pointer], :bool + %i{buffer_in buffer_in buffer_in ulong ulong pointer}, :bool attach_pfunc :CreateProcessAsUserW, - [:ulong, :buffer_in, :buffer_inout, :pointer, :pointer, :int, - :ulong, :buffer_in, :buffer_in, :pointer, :pointer], :bool + %i{ulong buffer_in buffer_inout pointer pointer int + ulong buffer_in buffer_in pointer pointer}, :bool ffi_lib :user32 @@ -93,7 +93,7 @@ module Process::Functions [], :ulong attach_pfunc :GetUserObjectInformationA, - [:ulong, :uint, :buffer_out, :ulong, :pointer], :bool + %i{ulong uint buffer_out ulong pointer}, :bool end # Override Process.create to check for running in the Service window station and doing @@ -109,7 +109,7 @@ module Process class << self def create(args) - unless args.kind_of?(Hash) + unless args.is_a?(Hash) raise TypeError, "hash keyword arguments expected" end @@ -137,6 +137,7 @@ module Process unless valid_keys.include?(key) raise ArgumentError, "invalid key '#{key}'" end + hash[key] = val end @@ -149,6 +150,7 @@ module Process unless valid_si_keys.include?(key) raise ArgumentError, "invalid startup_info key '#{key}'" end + si_hash[key] = val end end @@ -367,6 +369,7 @@ module Process unless GetProfileType(ptr) raise SystemCallError.new("GetProfileType", FFI.errno) end + ptr.read_uint end @@ -374,6 +377,7 @@ module Process unless LoadUserProfileW(token, profile_ptr) raise SystemCallError.new("LoadUserProfileW", FFI.errno) end + true end diff --git a/spec/mixlib/shellout_spec.rb b/spec/mixlib/shellout_spec.rb index ceb80d9..ed5e30a 100644 --- a/spec/mixlib/shellout_spec.rb +++ b/spec/mixlib/shellout_spec.rb @@ -410,7 +410,8 @@ describe Mixlib::ShellOut do let(:options) do { cwd: cwd, user: user, login: true, domain: domain, password: password, group: group, umask: umask, timeout: timeout, environment: environment, returns: valid_exit_codes, - live_stream: stream, input: input } end + live_stream: stream, input: input } + end let(:cwd) { "/tmp" } let(:user) { "toor" } @@ -635,7 +636,7 @@ describe Mixlib::ShellOut do # to match how whoami returns the information it "should run as current user" do - expect(running_user).to eql("#{ENV['USERDOMAIN'].downcase}\\#{ENV['USERNAME'].downcase}") + expect(running_user).to eql("#{ENV["USERDOMAIN"].downcase}\\#{ENV["USERNAME"].downcase}") end end @@ -653,7 +654,7 @@ describe Mixlib::ShellOut do let(:options) { { user: user, password: password } } it "should run as specified user" do - expect(running_user).to eql("#{ENV['COMPUTERNAME'].downcase}\\#{user}") + expect(running_user).to eql("#{ENV["COMPUTERNAME"].downcase}\\#{user}") end context "when :elevated => true" do @@ -1514,7 +1515,8 @@ describe Mixlib::ShellOut do %q{STDERR: msg_in_stderr}, "---- End output of #{cmd} ----", "Ran #{cmd} returned 0", - ] end + ] + end it "should format exception messages" do exception_output.each_with_index do |output_line, i| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 913ee7c..1f3c818 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,7 +8,7 @@ require "timeout" # Load everything from spec/support # Do not change the gsub. -Dir["spec/support/**/*.rb"].map { |f| f.gsub(%r{.rb$}, "") }.each { |f| require f } +Dir["spec/support/**/*.rb"].map { |f| f.gsub(/.rb$/, "") }.each { |f| require f } RSpec.configure do |config| config.mock_with :rspec diff --git a/spec/support/platform_helpers.rb b/spec/support/platform_helpers.rb index a03a0e0..735cc8f 100644 --- a/spec/support/platform_helpers.rb +++ b/spec/support/platform_helpers.rb @@ -16,5 +16,6 @@ end def root? return false if windows? + Process.euid == 0 end -- cgit v1.2.1