Sha256: 821106b64839cab438639752bcb9aa5f1d504775e954caedb6d3caaa632b2fe3

Contents?: true

Size: 1.68 KB

Versions: 23

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Isomorfeus
  module Puppetmaster
    class Server
      class ExecutorMiddleware
        @@request_key = nil

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

        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
                  result = TOPLEVEL_BINDING.eval('self').instance_eval(request_hash['code']) if request_hash['code']
                  response = Rack::Response.new(Oj.dump({ 'result' => result }),
                                                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
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
isomorfeus-puppetmaster-0.6.9 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.8 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.7 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.6 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.5 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.4 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.3 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.2 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.1 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.6.0 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.7 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.6 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.5 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.4 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.3 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.2 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.1 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.5.0 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.4.1 lib/isomorfeus/puppetmaster/server/executor_middleware.rb
isomorfeus-puppetmaster-0.4.0 lib/isomorfeus/puppetmaster/server/executor_middleware.rb