Skip to content

Commit

Permalink
Allocate new response objects for every request
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Aug 2, 2024
1 parent f08e08b commit 2f954d9
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/active_elastic_job/rack/sqs_message_consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ module Rack
# environment, which verifies the digest, have to use the *same*
# +secrets.secret_key_base+ setting.
class SqsMessageConsumer
OK_RESPONSE = [ '200'.freeze, { 'Content-Type'.freeze => 'text/plain'.freeze }, [ 'OK'.freeze ] ]
FORBIDDEN_RESPONSE = [
'403'.freeze,
{ 'Content-Type'.freeze => 'text/plain'.freeze },
[ 'Request forbidden!'.freeze ]
]

# 172.17.0.x is the default for Docker
# 172.18.0.x is the default for the bridge network of Docker Compose
DOCKER_HOST_IP = /172.1(7|8).0.\d+/.freeze
Expand All @@ -43,19 +36,19 @@ def call(env) #:nodoc:
request = ActionDispatch::Request.new env
if enabled? && (aws_sqsd?(request) || sqsd?(request))
unless request.local? || sent_from_docker_host?(request)
return FORBIDDEN_RESPONSE
return forbidden_response
end

if periodic_task?(request)
execute_periodic_task(request)
return OK_RESPONSE
return ok_response
elsif originates_from_gem?(request)
begin
execute_job(request)
rescue ActiveElasticJob::MessageVerifier::InvalidDigest => e
return FORBIDDEN_RESPONSE
return forbidden_response
end
return OK_RESPONSE
return ok_response
end
end
@app.call(env)
Expand All @@ -74,6 +67,14 @@ def enabled?
Rails.application.config.active_elastic_job.process_jobs
end

def ok_response
[ '200', { 'Content-Type' => 'text/plain' }, [ 'OK' ] ]
end

def forbidden_response
[ '403', { 'Content-Type' => 'text/plain' }, [ 'Request forbidden!' ] ]
end

def verify!(request)
digest = request.headers['HTTP_X_AWS_SQSD_ATTR_MESSAGE_DIGEST'.freeze]
message = request.body_stream.read
Expand Down

0 comments on commit 2f954d9

Please sign in to comment.