Skip to content

Commit

Permalink
Make secret key base configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tawan committed Nov 27, 2016
1 parent 92ab641 commit 6a2ef64
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/active_elastic_job/rack/sqs_message_consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,21 @@ def enabled?
end

def verify!(request)
secret_key_base = Rails.application.secrets[:secret_key_base]
@verifier ||= ActiveElasticJob::MessageVerifier.new(secret_key_base)
digest = request.headers['HTTP_X_AWS_SQSD_ATTR_MESSAGE_DIGEST'.freeze]
message = request.body_stream.read
request.body_stream.rewind
@verifier.verify!(message, digest)
end

def secret_key_base
config.secret_key_base
end

def config
Rails.application.config.active_elastic_job
end

def aws_sqsd?(request)
# Does not match against a Regexp
# in order to avoid performance penalties.
Expand Down
4 changes: 4 additions & 0 deletions lib/active_elastic_job/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Railtie < Rails::Railtie
config.active_elastic_job.aws_credentials = Aws::InstanceProfileCredentials.new

initializer "active_elastic_job.insert_middleware" do |app|
if app.config.active_elastic_job.secret_key_base.blank?
app.config.active_elastic_job.secret_key_base = app.secrets[:secret_key_base]
end

if app.config.active_elastic_job.process_jobs == true
if app.config.force_ssl
app.config.middleware.insert_before(ActionDispatch::SSL,ActiveElasticJob::Rack::SqsMessageConsumer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def verify_md5_digests!(response, messsage_body, message_attributes)
end

def secret_key_base
@secret_key_base ||= Rails.application.secrets[:secret_key_base]
config.secret_key_base
end
end
end
Expand Down
5 changes: 1 addition & 4 deletions spec/active_elastic_job/rack/sqs_message_consumer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
}

before do
allow(Rails).to receive(:application) { rails_app }
allow(rails_app).to receive(:secrets) {
{ secret_key_base: secret_key_base }
}
allow(sqs_message_consumer).to receive(:secret_key_base) { secret_key_base }
allow(sqs_message_consumer).to receive(:enabled?) { true }
end

Expand Down

0 comments on commit 6a2ef64

Please sign in to comment.