Class: Isomorfeus::Puppetmaster::Server::ExecutorMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/puppetmaster/server/executor_middleware.rb

Constant Summary collapse

@@request_key =
nil

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ExecutorMiddleware

Returns a new instance of ExecutorMiddleware.



9
10
11
12
# File 'lib/isomorfeus/puppetmaster/server/executor_middleware.rb', line 9

def initialize(app)
  raise '@@request_key not set!' unless @@request_key
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/isomorfeus/puppetmaster/server/executor_middleware.rb', line 14

def call(env)
  if env['PATH_INFO'] == '/__executor__' && env['REQUEST_METHOD'] == 'POST'
    request = Rack::Request.new(env)
    response = nil
    unless request.body.nil?
      request_hash = Oj.load(request.body.read, mode: :strict)
      if request_hash['key'] != @@request_key
        response = Rack::Response.new(Oj.dump({ 'error' => 'wrong key given, execution denied' }),
                                     401,
                                     'Content-Type' => 'application/json')
      else
        begin
          if Isomorfeus.respond_to?(:init_store)
            Isomorfeus.init_store
            Isomorfeus.store.clear! if Isomorfeus.store.respond_to?(:clear!)
          end
          result = TOPLEVEL_BINDING.eval(request_hash['code'], request_hash['file'], request_hash['line']) if request_hash['code']
          result = result.value if result.is_a?(Promise)
          response = Rack::Response.new(Oj.dump({ 'result' => result }, circular: true),
                                        200,
                                        'Content-Type' => 'application/json')
        rescue Exception => e
          response = Rack::Response.new(Oj.dump({ 'error' => "#{e.class}: #{e.message}", 'backtrace' => e.backtrace&.join("\n") }),
                                        200,
                                        'Content-Type' => 'application/json')
        end
      end
    end
    response.finish
  else
    @app.call(env)
  end
end