From 21ad6c114abf831c769c4b6fcf441b14eb8118dc Mon Sep 17 00:00:00 2001 From: kaspernj Date: Tue, 28 Feb 2017 16:43:49 +0100 Subject: [PATCH 1/2] Try to choose a domain that isn't a wildcard --- lib/tasks/letsencrypt.rake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/tasks/letsencrypt.rake b/lib/tasks/letsencrypt.rake index f1e9ce3..6fb656f 100644 --- a/lib/tasks/letsencrypt.rake +++ b/lib/tasks/letsencrypt.rake @@ -53,8 +53,11 @@ namespace :letsencrypt do print "Testing filename works (to bring up app)..." # Get the domain name from Heroku - hostname = heroku.domain.list(heroku_app).first['hostname'] - + heroku_domains = heroku.domain.list(heroku_app) + heroku_domain = heroku_domains.find { |heroku_domain_i| !heroku_domain_i["hostname"].start_with?("*.") } + raise "Couldn't find domain on Heroku that wasn't a wildcard: #{heroku_domains}" unless heroku_domain + hostname = heroku_domain["hostname"] + # Wait at least a little bit, otherwise the first request will almost always fail. sleep(2) From 37d03f8bb6d9d510aff76cea4efdb525dafca879 Mon Sep 17 00:00:00 2001 From: kaspernj Date: Tue, 28 Feb 2017 16:50:11 +0100 Subject: [PATCH 2/2] Allow for HEROKU_APP_DOMAIN env variable to define hostname --- lib/letsencrypt-rails-heroku/letsencrypt.rb | 7 ++++--- lib/tasks/letsencrypt.rake | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/letsencrypt-rails-heroku/letsencrypt.rb b/lib/letsencrypt-rails-heroku/letsencrypt.rb index b30ffc7..141055e 100644 --- a/lib/letsencrypt-rails-heroku/letsencrypt.rb +++ b/lib/letsencrypt-rails-heroku/letsencrypt.rb @@ -9,20 +9,21 @@ def self.configure end def self.challenge_configured? - configuration.acme_challenge_filename && + configuration.acme_challenge_filename && configuration.acme_challenge_filename.start_with?(".well-known/") && configuration.acme_challenge_file_content end class Configuration - attr_accessor :heroku_token, :heroku_app, :acme_email, :acme_domain, :acme_endpoint - + attr_accessor :heroku_token, :heroku_app, :heroku_app_domain, :acme_email, :acme_domain, :acme_endpoint + # Not settable by user; part of the gem's behaviour. attr_reader :acme_challenge_filename, :acme_challenge_file_content def initialize @heroku_token = ENV["HEROKU_TOKEN"] @heroku_app = ENV["HEROKU_APP"] + @heroku_app_domain = ENV["HEROKU_APP_DOMAIN"] @acme_email = ENV["ACME_EMAIL"] @acme_domain = ENV["ACME_DOMAIN"] @acme_endpoint = ENV["ACME_ENDPOINT"] || 'https://acme-v01.api.letsencrypt.org/' diff --git a/lib/tasks/letsencrypt.rake b/lib/tasks/letsencrypt.rake index 6fb656f..02c47f0 100644 --- a/lib/tasks/letsencrypt.rake +++ b/lib/tasks/letsencrypt.rake @@ -53,10 +53,18 @@ namespace :letsencrypt do print "Testing filename works (to bring up app)..." # Get the domain name from Heroku - heroku_domains = heroku.domain.list(heroku_app) - heroku_domain = heroku_domains.find { |heroku_domain_i| !heroku_domain_i["hostname"].start_with?("*.") } - raise "Couldn't find domain on Heroku that wasn't a wildcard: #{heroku_domains}" unless heroku_domain - hostname = heroku_domain["hostname"] + if Letsencrypt.configuration.heroku_app_domain + puts "Using hostname from HEROKU_APP_DOMAIN environment variable" + hostname = Letsencrypt.configuration.heroku_app_domain + else + puts "Trying to guess hostname from registered app domains" + heroku_domains = heroku.domain.list(heroku_app) + heroku_domain = heroku_domains.find { |heroku_domain_i| !heroku_domain_i["hostname"].start_with?("*.") } + raise "Couldn't find domain on Heroku that wasn't a wildcard: #{heroku_domains}" unless heroku_domain + hostname = heroku_domain["hostname"] + end + + puts "Using hostname: #{hostname}" # Wait at least a little bit, otherwise the first request will almost always fail. sleep(2)