class Sqreen::RemoteCommand

Execute and sanitize remote commands

Constants

KNOWN_COMMANDS

Attributes

uuid[R]

Public Class Methods

coalesce_reloads(commands, res_list) click to toggle source

will need changes if we ever distinguish forced/soft reloads ('force' parameter in the command)

# File lib/sqreen/remote_command.rb, line 75
def self.coalesce_reloads(commands, res_list)
  new_commands = []
  saw_rules_reload = false
  commands.reverse_each do |cmd_json|
    name = cmd_json['name']
    unless name == 'rules_reload'
      new_commands.unshift cmd_json
      next
    end

    if saw_rules_reload
      res_list[cmd_json['uuid']] =
        { :status => false, :reason => "redundant rules_reload ignored" }
    else
      saw_rules_reload = true
      new_commands.unshift cmd_json
      next
    end
  end

  new_commands
end
new(json_desc) click to toggle source
# File lib/sqreen/remote_command.rb, line 31
def initialize(json_desc)
  @name = json_desc['name'].to_sym
  @params = json_desc.fetch('params', [])
  @uuid = json_desc['uuid']
end
process_list(runner, commands, context_infos = {}) click to toggle source
# File lib/sqreen/remote_command.rb, line 52
def self.process_list(runner, commands, context_infos = {})
  res_list = {}

  return res_list unless commands

  unless commands.is_a? Array
    Sqreen.log.debug { format('Wrong commands type %s', commands.class) }
    Sqreen.log.debug { commands.inspect }
    return res_list
  end
  commands = coalesce_reloads(commands, res_list)
  commands.each do |cmd_json|
    Sqreen.log.debug { cmd_json }
    cmd = RemoteCommand.new(cmd_json)
    Sqreen.log.debug { cmd.inspect }
    uuid = cmd.uuid
    res_list[uuid] = cmd.process(runner, context_infos)
  end
  res_list
end

Public Instance Methods

process(runner, context_infos = {}) click to toggle source
# File lib/sqreen/remote_command.rb, line 37
def process(runner, context_infos = {})
  failing = validate_command(runner)
  return failing if failing
  Sqreen.log.debug { format('processing command %s', @name) }
  begin
    output = runner.send(KNOWN_COMMANDS[@name], *@params, context_infos)
  rescue => e
    Sqreen.log.warn { "Command failed with #{e}" }
    Sqreen.log.debug { e.backtrace.map { |x| "  #{x}" }.join("\n") }
    Sqreen::RemoteException.record(e)
    return { :status => false, :reason => "error: #{e.inspect}" }
  end
  format_output(output)
end
to_h() click to toggle source
# File lib/sqreen/remote_command.rb, line 98
def to_h
  {
    :name => @name,
  }
end

Protected Instance Methods

format_output(output) click to toggle source
# File lib/sqreen/remote_command.rb, line 118
def format_output(output)
  case output
  when NilClass
    { :status => false, :reason => 'nil returned' }
  when TrueClass
    { :status => true }
  when FailureOutput
    { :status => false, :output => output.wrapped_output }
  else
    { :status => true, :output => output }
  end
end
validate_command(runner) click to toggle source
# File lib/sqreen/remote_command.rb, line 106
def validate_command(runner)
  unless KNOWN_COMMANDS.include?(@name)
    msg = format("unknown command name '%s'", @name)
    Sqreen.log.debug { msg }
    return { :status => false, :reason => msg }
  end
  return nil if runner.respond_to?(KNOWN_COMMANDS[@name])
  msg = format("not implemented '%s'", @name)
  Sqreen.log.debug { msg }
  { :status => false, :reason => msg }
end