From 31ff5323007cae89f88d53dd804739b564f49015 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 9 Feb 2016 00:58:24 -0600 Subject: [PATCH 1/7] Add Heroku generated app.json --- app.json | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 app.json diff --git a/app.json b/app.json new file mode 100644 index 00000000..00a73dc1 --- /dev/null +++ b/app.json @@ -0,0 +1,127 @@ +{ + "name": "huboard-web", + "scripts": { + }, + "env": { + "AWS_ACCESS_KEY_ID": { + "required": true + }, + "AWS_S3_BUCKET": { + "required": true + }, + "AWS_SECRET_ACCESS_KEY": { + "required": true + }, + "BUILDPACK_URL": { + "required": true + }, + "CLOUDFRONT_URL": { + "required": true + }, + "COUCH_DATABASE": { + "required": true + }, + "COUCH_URL": { + "required": true + }, + "ELASTICSEARCH_URL": { + "required": true + }, + "GITHUB_API_ENDPOINT": { + "required": true + }, + "GITHUB_AUTH_STRATEGY": { + "required": true + }, + "GITHUB_CLIENT_ID": { + "required": true + }, + "GITHUB_SECRET": { + "required": true + }, + "GITHUB_WEB_ENDPOINT": { + "required": true + }, + "GITHUB_WEBHOOK_ENDPOINT": { + "required": true + }, + "HEROKU_APP_ID": { + "required": true + }, + "HEROKU_APP_NAME": { + "required": true + }, + "HEROKU_RELEASE_VERSION": { + "required": true + }, + "HEROKU_SLUG_COMMIT": { + "required": true + }, + "HEROKU_SLUG_DESCRIPTION": { + "required": true + }, + "HUBOARD_ENV": { + "required": true + }, + "LANG": { + "required": true + }, + "MAX_THREADS": { + "required": true + }, + "RACK_ENV": { + "required": true + }, + "RAILS_ENV": { + "required": true + }, + "RAILS_SERVE_STATIC_FILES": { + "required": true + }, + "RAYGUN_APIKEY": { + "required": true + }, + "REDIS_URL": { + "required": true + }, + "SECRET_KEY": { + "required": true + }, + "SECRET_KEY_BASE": { + "required": true + }, + "SEGEMENTIO_KEY": { + "required": true + }, + "SEGMENTIO_KEY": { + "required": true + }, + "SESSION_SECRET": { + "required": true + }, + "SOCKET_BACKEND": { + "required": true + }, + "STRIPE_API": { + "required": true + }, + "STRIPE_PUBLISHABLE_API": { + "required": true + }, + "STRIPE_SECRET_KEY": { + "required": true + }, + "WARDEN_GITHUB_VERIFIER_SECRET": { + "required": true + } + }, + "addons": [ + "rediscloud", + "memcachier" + ], + "buildpacks": [ + { + "url": "https://github.com/heroku/heroku-buildpack-multi" + } + ] +} From d05bacb1a982df778f1bd8c659a266a421f429cc Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Tue, 9 Feb 2016 11:57:39 -0600 Subject: [PATCH 2/7] Add HEROKU_PARENT_APP_NAME --- app.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app.json b/app.json index 00a73dc1..45928450 100644 --- a/app.json +++ b/app.json @@ -51,6 +51,9 @@ "HEROKU_APP_NAME": { "required": true }, + "HEROKU_PARENT_APP_NAME": { + "required": true + }, "HEROKU_RELEASE_VERSION": { "required": true }, From 54362b85c332cbe7958562f14b5a5dc2fad57d22 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Thu, 25 Feb 2016 22:43:37 -0500 Subject: [PATCH 3/7] Override Warden URL normalizer --- config/initializers/warden.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config/initializers/warden.rb b/config/initializers/warden.rb index 5c83027a..f8eb50d1 100644 --- a/config/initializers/warden.rb +++ b/config/initializers/warden.rb @@ -21,3 +21,16 @@ Warden::Manager.serialize_from_session { |key| Warden::GitHub::Verifier.load(key) } Warden::Manager.serialize_into_session { |user| Warden::GitHub::Verifier.dump(user) } +module Warden + module GitHub + class Config + alias_method :base_normalized_uri, :normalized_uri + + def normalized_uri(uri_or_path) + uri = base_normalized_uri(uri_or_path) + puts "Warden Override: #{uri}" + uri + end + end + end +end From 9cf03c19444e08ae8048926cb6688c9f8747d354 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Fri, 26 Feb 2016 00:39:27 -0500 Subject: [PATCH 4/7] Adjust Warden redirect_uri for Heroku review apps Heroku sets APP_NAME and PARENT_APP_NAME environment variables. The latter needs to replace the former in the OAuth redirect hostname, and the former needs to be appended as a query parameter to use on return. Env: HEROKU_APP_NAME = hb-pr-123 HEROKU_PARENT_APP_NAME = hb Input: https://hb-pr-123.herokuapp.com/ Output: https://hb.herokuapp.com/?APP_NAME=hb-pr-123 --- config/initializers/warden.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config/initializers/warden.rb b/config/initializers/warden.rb index f8eb50d1..e3b5f59b 100644 --- a/config/initializers/warden.rb +++ b/config/initializers/warden.rb @@ -21,6 +21,8 @@ Warden::Manager.serialize_from_session { |key| Warden::GitHub::Verifier.load(key) } Warden::Manager.serialize_into_session { |user| Warden::GitHub::Verifier.dump(user) } +require 'uri' + module Warden module GitHub class Config @@ -28,6 +30,15 @@ class Config def normalized_uri(uri_or_path) uri = base_normalized_uri(uri_or_path) + + app_name = ENV["HEROKU_APP_NAME"] + parent_app_name = ENV["HEROKU_PARENT_APP_NAME"] + puts "Warden Override: #{app_name} => #{parent_app_name}" + if parent_app_name && parent_app_name != app_name + uri.host.sub! app_name, parent_app_name + uri.query = URI.encode_www_form("APP_NAME" => app_name) + end + puts "Warden Override: #{uri}" uri end From 5b401c027977562f28269bf920ca8fd8afb960e9 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 27 Feb 2016 12:42:16 -0500 Subject: [PATCH 5/7] Add AppRedirect middleware to redirect to APP_NAME --- config/initializers/warden.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/config/initializers/warden.rb b/config/initializers/warden.rb index e3b5f59b..eacc9969 100644 --- a/config/initializers/warden.rb +++ b/config/initializers/warden.rb @@ -45,3 +45,34 @@ def normalized_uri(uri_or_path) end end end + +class Huboard + class Warden + class AppRedirect + def initialize(app, params={}) + @app = app + @params = params + end + + def call(env) + uri = Addressable::URI.parse(env['REQUEST_URI']) + + app_name = uri.query_values['APP_NAME'] if uri.query_values + parent_app_name = ENV['HEROKU_APP_NAME'] + puts "AppRedirect: #{parent_app_name} => #{app_name}" + + if app_name && parent_app_name + uri.query_values = uri.query_values(Array).reject { |kvp| kvp[0] == 'APP_NAME' } unless !uri.query_values + + app_redirect = "#{env['rack.url_scheme']}://#{env['HTTP_HOST'].sub(parent_app_name, app_name)}#{uri}" + puts "AppRedirect to #{app_redirect}" + return [302, {"Location" => app_redirect}, self] + end + + @app.call env + end + end + end +end + +Rails.application.middleware.insert_before Warden::Manager, Huboard::Warden::AppRedirect From a46cffdd2da07561d293eb229126ca80524f27cf Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Thu, 3 Mar 2016 11:26:04 -0600 Subject: [PATCH 6/7] Switch puts to Rails.logger.info --- config/initializers/warden.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/initializers/warden.rb b/config/initializers/warden.rb index eacc9969..2e043118 100644 --- a/config/initializers/warden.rb +++ b/config/initializers/warden.rb @@ -33,13 +33,13 @@ def normalized_uri(uri_or_path) app_name = ENV["HEROKU_APP_NAME"] parent_app_name = ENV["HEROKU_PARENT_APP_NAME"] - puts "Warden Override: #{app_name} => #{parent_app_name}" + Rails.logger.info "Warden Override: #{app_name} => #{parent_app_name}" if parent_app_name && parent_app_name != app_name uri.host.sub! app_name, parent_app_name uri.query = URI.encode_www_form("APP_NAME" => app_name) end - puts "Warden Override: #{uri}" + Rails.logger.info "Warden Override: #{uri}" uri end end @@ -59,13 +59,13 @@ def call(env) app_name = uri.query_values['APP_NAME'] if uri.query_values parent_app_name = ENV['HEROKU_APP_NAME'] - puts "AppRedirect: #{parent_app_name} => #{app_name}" + Rails.logger.info "AppRedirect: #{parent_app_name} => #{app_name}" if app_name && parent_app_name uri.query_values = uri.query_values(Array).reject { |kvp| kvp[0] == 'APP_NAME' } unless !uri.query_values app_redirect = "#{env['rack.url_scheme']}://#{env['HTTP_HOST'].sub(parent_app_name, app_name)}#{uri}" - puts "AppRedirect to #{app_redirect}" + Rails.logger.info "AppRedirect to #{app_redirect}" return [302, {"Location" => app_redirect}, self] end From ae3fd45042da800044d101fe8335a99eff2aaa5b Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Thu, 3 Mar 2016 11:55:02 -0600 Subject: [PATCH 7/7] Suppress AppRedirect log if app_name is missing --- config/initializers/warden.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/warden.rb b/config/initializers/warden.rb index 2e043118..2db9a758 100644 --- a/config/initializers/warden.rb +++ b/config/initializers/warden.rb @@ -59,7 +59,7 @@ def call(env) app_name = uri.query_values['APP_NAME'] if uri.query_values parent_app_name = ENV['HEROKU_APP_NAME'] - Rails.logger.info "AppRedirect: #{parent_app_name} => #{app_name}" + Rails.logger.info "AppRedirect: #{parent_app_name} => #{app_name}" if app_name if app_name && parent_app_name uri.query_values = uri.query_values(Array).reject { |kvp| kvp[0] == 'APP_NAME' } unless !uri.query_values