Class: Bolt::Transport::Sudoable

Inherits:
Base
  • Object
show all
Defined in:
lib/bolt/transport/sudoable.rb,
lib/bolt/transport/sudoable/tmpdir.rb,
lib/bolt/transport/sudoable/connection.rb

Direct Known Subclasses

Local, SSH

Defined Under Namespace

Classes: Connection, Tmpdir

Constant Summary

Constants inherited from Base

Base::ENVIRONMENT_METHODS, Base::STDIN_METHODS

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#assert_batch_size_one, #batch_command, #batch_connected?, #batch_script, #batch_task, #batch_upload, #batches, #connected?, #default_input_method, #envify_params, #initialize, #provided_features, #select_implementation, #select_interpreter, #unwrap_sensitive_args, #with_events

Constructor Details

This class inherits a constructor from Bolt::Transport::Base

Class Method Details

.sudo_promptObject



9
10
11
# File 'lib/bolt/transport/sudoable.rb', line 9

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

Instance Method Details

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bolt/transport/sudoable.rb', line 131

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

#run_command(target, command, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bolt/transport/sudoable.rb', line 13

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

#run_script(target, script, arguments, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bolt/transport/sudoable.rb', line 46

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

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

#run_task(target, task, arguments, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
# File 'lib/bolt/transport/sudoable.rb', line 66

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

  with_connection(target) do |conn|
    conn.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.debug("Running '#{executable}' with #{arguments.to_json}#{interpreter_debug}")
      # unpack any Sensitive data
      arguments = unwrap_sensitive_args(arguments)

      conn.with_tempdir 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.copy_file(file['path'], File.join(dir.to_s, file['name']))
          end
        end

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

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

        remote_task_path = conn.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 = conn.write_executable(dir, wrapper, 'wrapper.sh')
        end

        dir.chown(conn.run_as)

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

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bolt/transport/sudoable.rb', line 26

def upload(target, source, destination, options = {})
  with_connection(target) do |conn|
    conn.running_as(options[:run_as]) do
      conn.with_tempdir do |dir|
        basename = File.basename(destination)
        tmpfile = File.join(dir.to_s, basename)
        conn.copy_file(source, tmpfile)
        # pass over file ownership if we're using run-as to be a different user
        dir.chown(conn.run_as)
        result = conn.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
end