Sha256: e590be697331bcd5f1e6524453a58f022e8b9cab83e327a913e51a19f9041064

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true
require 'proxes/services/logger'
require 'rack-proxy'
require 'proxes/request'
require 'proxes/policies/request_policy'
require 'proxes/helpers/pundit'
require 'proxes/helpers/authentication'
require 'proxes/services/logger'

module ProxES
  class Security
    attr_reader :env, :logger

    include ProxES::Helpers::Authentication
    include ProxES::Helpers::Pundit

    def initialize(app, logger = nil)
      @app = app
      @logger = logger || ProxES::Services::Logger.instance
    end

    def error(message, code = 500)
      [code, { 'Content-Type' => 'application/json' }, ['{"error":"' + message + '}']]
    end

    def call(env)
      @env = env

      request = ProxES::Request.from_env(env)

      logger.debug '==========================BEFORE================================================'
      logger.debug '= ' + "Request: #{request.fullpath}".ljust(76) + ' ='
      logger.debug '= ' + "Endpoint: #{request.endpoint}".ljust(76) + ' ='
      logger.debug '================================================================================'

      begin
        check_basic
        authorize request
      rescue StandardError => e
        logger.debug "Access denied by security layer: #{e.message}"
        return error 'Forbidden', 403
      end
      request.index = policy_scope(request) if request.indices?

      logger.debug '==========================AFTER================================================='
      logger.debug '= ' + "Request: #{request.fullpath}".ljust(76) + ' ='
      logger.debug '= ' + "Endpoint: #{request.endpoint}".ljust(76) + ' ='
      logger.debug '================================================================================'

      begin
        @app.call request.env
      rescue Errno::EHOSTUNREACH
        error 'Could not reach Elasticsearch at ' + ENV['ELASTICSEARCH_URL']
      rescue Errno::ECONNREFUSED
        error 'Elasticsearch not listening at ' + ENV['ELASTICSEARCH_URL']
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proxes-0.2.0 lib/proxes/security.rb
proxes-0.1.0 lib/proxes/security.rb