Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Beetrack/rack-es-logger into dev…
Browse files Browse the repository at this point in the history
…elop
  • Loading branch information
kalelc committed Dec 7, 2020
2 parents a745cf8 + 63f94af commit 833b09f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 39 deletions.
19 changes: 9 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
PATH
remote: .
specs:
es_logger (1.3.2)
es_logger (1.5.0)
connection_pool (~> 2.2.2, >= 2.2.2)
elasticsearch (~> 7.4)
elasticsearch-persistence (~> 7.0.0, >= 7.0.0)
jwt (~> 2.2.2)
rack (~> 2.0)
sidekiq (>= 5.1.3, <= 5.3.0)
sidekiq (>= 5.1.3, <= 6.1.2)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -54,6 +55,7 @@ GEM
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
json (2.3.0)
jwt (2.2.2)
method_source (0.9.2)
minitest (5.14.2)
multi_json (1.14.1)
Expand All @@ -65,11 +67,9 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (2.2.3)
rack-protection (2.1.0)
rack
rainbow (3.0.0)
rake (12.3.3)
redis (4.1.4)
redis (4.2.5)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
Expand All @@ -91,11 +91,10 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
ruby-progressbar (1.10.1)
sidekiq (5.2.9)
connection_pool (~> 2.2, >= 2.2.2)
sidekiq (6.1.1)
connection_pool (>= 2.2.2)
rack (~> 2.0)
rack-protection (>= 1.5.0)
redis (>= 3.3.5, < 4.2)
redis (>= 4.2.0)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
Expand All @@ -105,7 +104,7 @@ GEM
tzinfo (1.2.8)
thread_safe (~> 0.1)
unicode-display_width (1.6.1)
zeitwerk (2.4.1)
zeitwerk (2.4.2)

PLATFORMS
ruby
Expand Down
7 changes: 4 additions & 3 deletions es_logger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ Gem::Specification.new do |spec|
'public gem pushes.'
end

spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(Regexp.new('^(test|spec|features)/')) }
end
spec.require_paths = ['lib']

spec.add_runtime_dependency 'connection_pool', '~> 2.2.2', '>= 2.2.2'
spec.add_runtime_dependency 'elasticsearch', '~> 7.4'
spec.add_runtime_dependency 'elasticsearch-persistence', '~> 7.0.0', '>= 7.0.0'
spec.add_runtime_dependency 'jwt', '~>2.2.2'
spec.add_runtime_dependency 'rack', '~> 2.0'
spec.add_runtime_dependency 'sidekiq', '>= 5.1.3', '<= 5.3.0'
spec.add_runtime_dependency 'sidekiq', '>= 5.1.3', '<= 6.1.2'
spec.add_development_dependency 'elasticsearch-extensions', '~> 0.0.31'
spec.add_development_dependency 'pry', '~> 0.12.2'
spec.add_development_dependency 'rspec', '~> 3.8', '<= 3.8'
Expand Down
12 changes: 7 additions & 5 deletions lib/es_logger/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def self.configure
end

class Configuration
attr_accessor :async_handler
attr_accessor :include_pattern
attr_accessor :elasticsearch
attr_accessor :elasticsearch_index_name
attr_accessor :elasticsearch_pool_connection
attr_accessor :jwt,
:async_handler,
:include_pattern,
:elasticsearch,
:elasticsearch_index_name,
:elasticsearch_pool_connection

def initialize
@jwt = nil
@async_handler = nil
@include_pattern = nil
@elasticsearch = {}
Expand Down
67 changes: 48 additions & 19 deletions lib/es_logger/response.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
# frozen_string_literal: true

require 'jwt'
require 'es_logger/configuration'

module EsLogger
class Response
def self.call(env)
request = ::Rack::Request.new(env)
is_json = request&.media_type&.downcase == 'application/json'
body_stream = is_json ? request.body.read : nil
request.body.rewind if body_stream
JWT_REGEX = Regexp.new('^(Bearer\s)?(?<token>[a-zA-Z0-9\-_.]+)$')

def self.call(env)
payload = {
remote_address: env['REMOTE_ADDR'],
request_method: env['REQUEST_METHOD'],
path: env['PATH_INFO'],
query_string_params: ::Rack::Utils.parse_nested_query(env['QUERY_STRING']),
params: is_json && body_stream.length.positive? ? JSON.parse(body_stream) : nil
params: request_params(env),
headers: http_headers(env).to_json,
authorization: decode_jwt_token(env),
query_string_params: query_string(env)
}

controller = env['action_controller.instance']
return payload unless (controller = env['action_controller.instance'])

payload.merge!(
'controller_name' => controller.controller_name,
'controller_action' => controller.action_name,
'content_type' => controller.content_type || :none
)
end

def self.query_string(env)
::Rack::Utils.parse_nested_query(env['QUERY_STRING'])
end

def self.request_params(env)
request = ::Rack::Request.new(env)
return unless request&.media_type

is_json = request.media_type.downcase == 'application/json'
return unless is_json

request.body.rewind if (body_stream = request.body.read)
return unless body_stream.length.positive?

JSON.parse(body_stream)
end

def self.http_headers(env)
env.select { |k, _| k.to_s.start_with? 'HTTP_' }
end

def self.decode_jwt_token(env)
jwt = EsLogger.configuration.jwt
return if jwt.nil?

if controller
payload.merge!(
'controller_name' => controller.controller_name,
'controller_action' => controller.action_name,
'content_type' => controller.content_type || :none
)
end
jwt_key = "HTTP_#{jwt.upcase}"
jwt_value = env[jwt_key].to_s.match(JWT_REGEX)[:token]
return if jwt_value.nil?

payload
JWT.decode(jwt_value, nil, false).first
rescue JWT::DecodeError
nil
end
end
end
2 changes: 1 addition & 1 deletion lib/es_logger/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module EsLogger
VERSION = '1.3.3'
VERSION = '1.5.0'
end
3 changes: 3 additions & 0 deletions lib/generators/es_logger/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# Examples:
# ^\/api\/external\/\w+ -> /api/external/v1, /api/external/v1/routes
# config.include_pattern = [/^\/api\/external\/\w+/]
#
# this option allows decode JWT header to index in ES
# config.jwt = 'Authorization'

# params configuration to connect with Elasticsearch
config.elasticsearch = {
Expand Down
4 changes: 3 additions & 1 deletion spec/rack_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe EsLogger::Rack, elasticsearch: true do
Expand All @@ -6,7 +8,7 @@
let(:request) { Rack::MockRequest.new(es_logger) }
let!(:configure) do
EsLogger.configure do |c|
c.include_pattern = [/^\/api\/external\/\w+/]
c.include_pattern = [Regexp.new('^/api/external/\w+')]
c.elasticsearch = {
user: ENV['ELASTICSEARCH_USER'] || 'elastic',
password: ENV['ELASTICSEARCH_PASSWORD'] || '',
Expand Down

0 comments on commit 833b09f

Please sign in to comment.