Class: Bolt::Shell::Bash

Inherits:
Bolt::Shell show all
Defined in:
lib/bolt/shell/bash.rb,
lib/bolt/shell/bash/tmpdir.rb

Defined Under Namespace

Classes: Tmpdir

Constant Summary collapse

CHUNK_SIZE =
4096

Instance Attribute Summary

Attributes inherited from Bolt::Shell

#conn, #logger, #target

Instance Method Summary collapse

Methods inherited from Bolt::Shell

#default_input_method, #envify_params, #select_implementation, #select_interpreter, #unwrap_sensitive_args

Constructor Details

#initialize(target, conn) ⇒ Bash

Returns a new instance of Bash.



11
12
13
14
15
16
17
18
# File 'lib/bolt/shell/bash.rb', line 11

def initialize(target, conn)
  super

  @run_as = nil

  @sudo_id = SecureRandom.uuid
  @sudo_password = @target.options['sudo-password'] || @target.password
end

Instance Method Details

#check_sudo(out, inp, stdin) ⇒ Object

See if there’s a sudo prompt in the output If not, return the output



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/bolt/shell/bash.rb', line 196

def check_sudo(out, inp, stdin)
  buffer = out.readpartial(CHUNK_SIZE)
  # Split on newlines, including the newline
  lines = buffer.split(/(?<=\n)/)
  # handle_sudo will return the line if it is not a sudo prompt or error
  lines.map! { |line| handle_sudo(inp, line, stdin) }
  lines.join
# If stream has reached EOF, no password prompt is expected
# return an empty string
rescue EOFError
  ''
end

#download(source, destination, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bolt/shell/bash.rb', line 55

def download(source, destination, options = {})
  running_as(options[:run_as]) do
    # Target OS may be either Unix or Windows. Without knowing the target OS before-hand
    # we can't assume whether the path separator is '/' or '\'. Assume we're connecting
    # to a target with Unix and then check if the path exists after downloading.
    download = File.join(destination, Bolt::Util.unix_basename(source))

    conn.download_file(source, destination, download)

    # If the download path doesn't exist, then the file was likely downloaded from Windows
    # using a source path with backslashes (e.g. 'C:\Users\Administrator\foo'). The file
    # should be saved to the expected location, so update the download path assuming a
    # Windows basename so the result shows the correct local path.
    unless File.exist?(download)
      download = File.join(destination, Bolt::Util.windows_basename(source))
    end

    Bolt::Result.for_download(target, source, destination, download)
  end
end

#execute(command, sudoable: false, **options) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/bolt/shell/bash.rb', line 324

def execute(command, sudoable: false, **options)
  run_as = options[:run_as] || self.run_as
  escalate = sudoable && run_as && conn.user != run_as
  use_sudo = escalate && @target.options['run-as-command'].nil?

  # Depending on the transport, whether we're using sudo and whether
  # there are environment variables to set, we may need to stitch
  # together multiple commands into a single sh invocation
  commands = [inject_interpreter(options[:interpreter], command)]

  if options[:environment]
    env_decl = options[:environment].map do |env, val|
      "#{env}=#{Shellwords.shellescape(val)}"
    end.join(' ')
  end

  if escalate
    sudo_str = if use_sudo
                 sudo_exec = target.options['sudo-executable'] || "sudo"
                 sudo_flags = [sudo_exec, "-S", "-H", "-u", run_as, "-p", sudo_prompt]
                 Shellwords.shelljoin(sudo_flags)
               else
                 Shellwords.shelljoin(@target.options['run-as-command'] + [run_as])
               end
    commands.unshift('cd') if conn.reset_cwd?
    commands.unshift(sudo_success(@sudo_id)) if options[:stdin] && !options[:wrapper]
  end

  command_str = if sudo_str || env_decl
                  "sh -c #{Shellwords.shellescape(commands.join('; '))}"
                else
                  commands.last
                end

  command_str = [sudo_str, env_decl, command_str].compact.join(' ')

  @logger.trace { "Executing `#{command_str}`" }

  in_buffer = if !use_sudo && options[:stdin]
                String.new(options[:stdin], encoding: 'binary')
              else
                String.new(encoding: 'binary')
              end
  # Chunks of this size will be read in one iteration
  index = 0
  timeout = 0.1
  result_output = Bolt::Node::Output.new

  inp, out, err, t = conn.execute(command_str)
  read_streams = { out => String.new,
                   err => String.new }
  write_stream = in_buffer.empty? ? [] : [inp]

  # See if there's a sudo prompt
  if use_sudo
    ready_read = select([err], nil, nil, timeout * 5)
    read_streams[err] << check_sudo(err, inp, options[:stdin]) if ready_read
  end

  # True while the process is running or waiting for IO input
  while t.alive?
    # See if we can read from out or err, or write to in
    ready_read, ready_write, = select(read_streams.keys, write_stream, nil, timeout)

    # Read from out and err
    ready_read&.each do |stream|
      # Check for sudo prompt
      read_streams[stream] << if use_sudo
                                check_sudo(stream, inp, options[:stdin])
                              else
                                stream.readpartial(CHUNK_SIZE)
                              end
    rescue EOFError
    end

    # select will either return an empty array if there are no
    # writable streams or nil if no IO object is available before the
    # timeout is reached.
    writable = if ready_write.respond_to?(:empty?)
                 !ready_write.empty?
               else
                 !ready_write.nil?
               end

    begin
      if writable && index < in_buffer.length
        to_print = in_buffer[index..-1]
        # On Windows, select marks the input stream as writable even if
        # it's full. We need to check whether we received wait_writable
        # and treat that as not having written anything.
        written = inp.write_nonblock(to_print, exception: false)
        index += written unless written == :wait_writable

        if index >= in_buffer.length && !write_stream.empty?
          inp.close
          write_stream = []
        end
      end
    # If a task has stdin as an input_method but doesn't actually read
    # from stdin, the task may return and close the input stream before
    # we finish writing
    rescue Errno::EPIPE
      write_stream = []
    end
  end
  # Read any remaining data in the pipe. Do not wait for
  # EOF in case the pipe is inherited by a child process.
  read_streams.each do |stream, _|
    loop { read_streams[stream] << stream.read_nonblock(CHUNK_SIZE) }
  rescue Errno::EAGAIN, EOFError
  end
  result_output.stdout << read_streams[out]
  result_output.stderr << read_streams[err]
  result_output.exit_code = t.value.respond_to?(:exitstatus) ? t.value.exitstatus : t.value

  case result_output.exit_code
  when 0
    @logger.trace { "Command `#{command_str}` returned successfully" }
  when 126
    msg = "\n\nThis may be caused by the default tmpdir being mounted "\
      "using 'noexec'. See http://pup.pt/task-failure for details and workarounds."
    result_output.stderr << msg
    @logger.trace { "Command #{command_str} failed with exit code #{result_output.exit_code}" }
  else
    @logger.trace { "Command #{command_str} failed with exit code #{result_output.exit_code}" }
  end
  result_output
rescue StandardError
  # Ensure we close stdin and kill the child process
  inp&.close
  t&.terminate if t&.alive?
  @logger.trace { "Command aborted" }
  raise
end

#handle_sudo(stdin, err, sudo_stdin) ⇒ Object

If prompted for sudo password, send password to stdin and return an empty string. Otherwise, check for sudo errors and raise Bolt error. If sudo_id is detected, that means the task needs to have stdin written. If error is not sudo-related, return the stderr string to be added to node output



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bolt/shell/bash.rb', line 165

def handle_sudo(stdin, err, sudo_stdin)
  if err.include?(sudo_prompt)
    # A wild sudo prompt has appeared!
    if @sudo_password
      stdin.write("#{@sudo_password}\n")
      ''
    else
      raise Bolt::Node::EscalateError.new(
        "Sudo password for user #{conn.user} was not provided for #{target}",
        'NO_PASSWORD'
      )
    end
  elsif err =~ /^#{@sudo_id}/
    if sudo_stdin
      begin
        stdin.write("#{sudo_stdin}\n")
        stdin.close
      # If a task has stdin as an input_method but doesn't actually read
      # from stdin, the task may return and close the input stream before
      # we finish writing
      rescue Errno::EPIPE
      end
    end
    ''
  else
    handle_sudo_errors(err)
  end
end

#handle_sudo_errors(err) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bolt/shell/bash.rb', line 209

def handle_sudo_errors(err)
  case err
  when /^#{conn.user} is not in the sudoers file\./
    @logger.trace { err }
    raise Bolt::Node::EscalateError.new(
      "User #{conn.user} does not have sudo permission on #{target}",
      'SUDO_DENIED'
    )
  when /^Sorry, try again\./
    @logger.trace { err }
    raise Bolt::Node::EscalateError.new(
      "Sudo password for user #{conn.user} not recognized on #{target}",
      'BAD_PASSWORD'
    )
  else
    # No need to raise an error - just return the string
    err
  end
end

#inject_interpreter(interpreter, command) ⇒ Object

Returns string with the interpreter conditionally prepended



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/bolt/shell/bash.rb', line 312

def inject_interpreter(interpreter, command)
  if interpreter
    if command.is_a?(Array)
      command.unshift(interpreter)
    else
      command = [interpreter, command]
    end
  end

  command.is_a?(String) ? command : Shellwords.shelljoin(command)
end

#make_executable(path) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/bolt/shell/bash.rb', line 262

def make_executable(path)
  result = execute(['chmod', 'u+x', path])
  if result.exit_code != 0
    message = "Could not make file '#{path}' executable: #{result.stderr.string}"
    raise Bolt::Node::FileError.new(message, 'CHMOD_ERROR')
  end
end

#make_tmpdirObject



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/bolt/shell/bash.rb', line 270

def make_tmpdir
  tmpdir = @target.options.fetch('tmpdir', '/tmp')
  script_dir = @target.options.fetch('script-dir', SecureRandom.uuid)
  tmppath = File.join(tmpdir, script_dir)
  command = ['mkdir', '-m', 700, tmppath]

  result = execute(command)
  if result.exit_code != 0
    raise Bolt::Node::FileError.new("Could not make tmpdir: #{result.stderr.string}", 'TMPDIR_ERROR')
  end
  path = tmppath || result.stdout.string.chomp
  Bolt::Shell::Bash::Tmpdir.new(self, path)
end

#make_wrapper_stringio(task_path, stdin, interpreter = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/bolt/shell/bash.rb', line 229

def make_wrapper_stringio(task_path, stdin, interpreter = nil)
  if interpreter
    StringIO.new(<<~SCRIPT)
      #!/bin/sh
      '#{interpreter}' '#{task_path}' <<'EOF'
      #{stdin}
      EOF
    SCRIPT
  else
    StringIO.new(<<~SCRIPT)
      #!/bin/sh
      '#{task_path}' <<'EOF'
      #{stdin}
      EOF
    SCRIPT
  end
end

#provided_featuresObject



20
21
22
# File 'lib/bolt/shell/bash.rb', line 20

def provided_features
  ['shell']
end

#run_asObject

This method allows the @run_as variable to be used as a per-operation override for the user to run as. When @run_as is unset, the user specified on the target will be used.



250
251
252
# File 'lib/bolt/shell/bash.rb', line 250

def run_as
  @run_as || target.options['run-as']
end

#run_command(command, options = {}, position = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bolt/shell/bash.rb', line 24

def run_command(command, options = {}, position = [])
  running_as(options[:run_as]) do
    output = execute(command, environment: options[:env_vars], sudoable: true)
    Bolt::Result.for_command(target,
                             output.stdout.string,
                             output.stderr.string,
                             output.exit_code,
                             'command',
                             command,
                             position)
  end
end

#run_script(script, arguments, options = {}, position = []) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bolt/shell/bash.rb', line 76

def run_script(script, arguments, options = {}, position = [])
  # unpack any Sensitive data
  arguments = unwrap_sensitive_args(arguments)

  running_as(options[:run_as]) do
    with_tmpdir do |dir|
      path = write_executable(dir.to_s, script)
      dir.chown(run_as)
      output = execute([path, *arguments], environment: options[:env_vars], sudoable: true)
      Bolt::Result.for_command(target,
                               output.stdout.string,
                               output.stderr.string,
                               output.exit_code,
                               'script',
                               script,
                               position)
    end
  end
end

#run_task(task, arguments, options = {}, position = []) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bolt/shell/bash.rb', line 96

def run_task(task, arguments, options = {}, position = [])
  implementation = select_implementation(target, task)
  executable = implementation['path']
  input_method = implementation['input_method']
  extra_files = implementation['files']

  running_as(options[:run_as]) do
    stdin, output = nil
    execute_options = {}
    execute_options[:interpreter] = select_interpreter(executable, target.options['interpreters'])
    interpreter_debug = if execute_options[:interpreter]
                          " using '#{execute_options[:interpreter]}' interpreter"
                        end
    # log the arguments with sensitive data redacted, do NOT log unwrapped_arguments
    logger.trace("Running '#{executable}' with #{arguments.to_json}#{interpreter_debug}")
    # unpack any Sensitive data
    arguments = unwrap_sensitive_args(arguments)

    with_tmpdir do |dir|
      if extra_files.empty?
        task_dir = dir
      else
        # TODO: optimize upload of directories
        arguments['_installdir'] = dir.to_s
        task_dir = File.join(dir.to_s, task.tasks_dir)
        dir.mkdirs([task.tasks_dir] + extra_files.map { |file| File.dirname(file['name']) })
        extra_files.each do |file|
          conn.upload_file(file['path'], File.join(dir.to_s, file['name']))
        end
      end

      if Bolt::Task::STDIN_METHODS.include?(input_method)
        stdin = JSON.dump(arguments)
      end

      if Bolt::Task::ENVIRONMENT_METHODS.include?(input_method)
        execute_options[:environment] = envify_params(arguments)
      end

      remote_task_path = write_executable(task_dir, executable)

      # Avoid the horrors of passing data on stdin via a tty on multiple platforms
      # by writing a wrapper script that directs stdin to the task.
      if stdin && target.options['tty']
        wrapper = make_wrapper_stringio(remote_task_path, stdin, execute_options[:interpreter])
        execute_options.delete(:interpreter)
        execute_options[:wrapper] = true
        remote_task_path = write_executable(dir, wrapper, 'wrapper.sh')
      end

      dir.chown(run_as)

      execute_options[:stdin] = stdin
      execute_options[:sudoable] = true if run_as
      output = execute(remote_task_path, **execute_options)
    end
    Bolt::Result.for_task(target, output.stdout.string,
                          output.stderr.string,
                          output.exit_code,
                          task.name,
                          position)
  end
end

#running_as(user) ⇒ Object

Run as the specified user for the duration of the block.



255
256
257
258
259
260
# File 'lib/bolt/shell/bash.rb', line 255

def running_as(user)
  @run_as = user
  yield
ensure
  @run_as = nil
end

#sudo_promptObject



459
460
461
# File 'lib/bolt/shell/bash.rb', line 459

def sudo_prompt
  '[sudo] Bolt needs to run as another user, password: '
end

#sudo_success(sudo_id) ⇒ Object



307
308
309
# File 'lib/bolt/shell/bash.rb', line 307

def sudo_success(sudo_id)
  "echo #{sudo_id} 1>&2"
end

#upload(source, destination, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bolt/shell/bash.rb', line 37

def upload(source, destination, options = {})
  running_as(options[:run_as]) do
    with_tmpdir do |dir|
      basename = File.basename(source)
      tmpfile = File.join(dir.to_s, basename)
      conn.upload_file(source, tmpfile)
      # pass over file ownership if we're using run-as to be a different user
      dir.chown(run_as)
      result = execute(['mv', '-f', tmpfile, destination], sudoable: true)
      if result.exit_code != 0
        message = "Could not move temporary file '#{tmpfile}' to #{destination}: #{result.stderr.string}"
        raise Bolt::Node::FileError.new(message, 'MV_ERROR')
      end
    end
    Bolt::Result.for_upload(target, source, destination)
  end
end

#with_tmpdirObject

A helper to create and delete a tmpdir on the remote system. Yields the directory name.



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/bolt/shell/bash.rb', line 294

def with_tmpdir
  dir = make_tmpdir
  yield dir
ensure
  if dir
    if target.options['cleanup']
      dir.delete
    else
      Bolt::Logger.warn("skip_cleanup", "Skipping cleanup of tmpdir #{dir}")
    end
  end
end

#write_executable(dir, file, filename = nil) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/bolt/shell/bash.rb', line 284

def write_executable(dir, file, filename = nil)
  filename ||= File.basename(file)
  remote_path = File.join(dir.to_s, filename)
  conn.upload_file(file, remote_path)
  make_executable(remote_path)
  remote_path
end