Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove naive middleware check for a griddler path #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions griddler-ses.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "sinatra", "~> 1.4"
spec.add_development_dependency "rspec", "~> 3.0"
end
2 changes: 1 addition & 1 deletion lib/griddler/ses/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.normalize_params(params)
def normalize_params
# use sns_endpoint to parse and validate the sns message
sns_msg = SnsEndpoint::AWS::SNS::Message.new sns_json
raise "Invalid SNS message" unless sns_msg.authentic? && sns_msg.topic_arn.end_with?('griddler')
raise "Invalid SNS message" unless sns_msg.authentic?

case sns_msg.type
when :SubscriptionConfirmation
Expand Down
10 changes: 1 addition & 9 deletions lib/griddler/ses/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ def call(env)
# a bug on the AWS side doesn't set the content type to application/json type properly,
# so we have to intercept and do this in order for Griddler's controller to correctly
# parse the parameters (see https://forums.aws.amazon.com/thread.jspa?messageID=418160)
if is_griddler_request?(env) && is_aws_sns_request?(env)
if is_aws_sns_request?(env)
env['CONTENT_TYPE'] = 'application/json; charset=UTF-8'
end

@app.call(env)
end

private
def griddler_path
@griddler_path ||= Rails.application.routes.url_helpers.url_for(controller: 'griddler/emails', action: 'create', only_path: true)
end

def is_griddler_request?(request)
request['REQUEST_PATH'] == griddler_path
end

def is_aws_sns_request?(request)
request['HTTP_X_AMZ_SNS_MESSAGE_TYPE'].present?
end
Expand Down
9 changes: 8 additions & 1 deletion lib/griddler/ses/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ module Griddler
module Ses
class Railtie < Rails::Railtie
initializer "griddler_ses.configure_rails_initialization" do |app|
Rails.application.middleware.insert_before ActionDispatch::ParamsParser, Griddler::Ses::Middleware
if ::Rails::VERSION::MAJOR >= 5
# Rails 5 no longer instantiates ActionDispatch::ParamsParser
# https://github.com/rails/rails/commit/a1ced8b52ce60d0634e65aa36cb89f015f9f543d
Rails.application.middleware.use Middleware
Rails.application.middleware.use Griddler::Ses::Middleware
else
Rails.application.middleware.insert_before ActionDispatch::ParamsParser, Griddler::Ses::Middleware
end
end
end
end
Expand Down