Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit 9d95fdf

Browse files
authored
ADD: Ability to set cloudflare configs in API calls (#29)
* ADD: Ability to override environment variables for CF Currently, each instance of the application can only use one CF account by setting details in the environment variables. Now you can define a cloudflare_email and cloudflare_api_key params and they will override the environment variables on each call. * DEL: console logging that shouldn't be there * UPDATE: Remove all references to the environment variables
1 parent 9330c4f commit 9d95fdf

9 files changed

+44
-34
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ POST /certificate_request
6161
"auth_token": "CHOSEN AUTH TOKEN",
6262
"domains": ["www.substrakt.com", "substrakt.com"],
6363
"zone": "CLOUDFLARE DOMAIN ZONE",
64-
"heroku_app_name": "NAME OF HEROKU APP"
64+
"heroku_app_name": "NAME OF HEROKU APP",
65+
"cloudflare_api_key": "API KEY OF CLOUDFLARE ACCOUNT",
66+
"cloudflare_email": "CLOUDFLARE EMAIL ADDRESS"
6567
}
6668
```
6769

app.json

-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
"website": "https://substrakt.com/",
99
"repository": "https://github.com/substrakt/letsencrypt-heroku",
1010
"env": {
11-
"CLOUDFLARE_API_KEY": {
12-
"description": "Your CloudFlare API Key."
13-
},
14-
"CLOUDFLARE_EMAIL": {
15-
"description": "The email address for your CloudFlare account."
16-
},
1711
"HEROKU_OAUTH_KEY": {
1812
"description": "A valid OAuth key to access your Heroku appications."
1913
},

app.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
status 200
2424
token = SecureRandom.hex
2525
$redis.setex("status_#{token}", 3600, "queued")
26-
CloudflareChallengeWorker.perform_async(@request_payload["zone"], @request_payload["domains"], token, @request_payload["heroku_app_name"], false)
26+
CloudflareChallengeWorker.perform_async(@request_payload["zone"], @request_payload["domains"], token, @request_payload["heroku_app_name"], false, { email: @request_payload['cloudflare_email'], api_key: @request_payload['cloudflare_api_key'] })
2727
{ status: 'queued', uuid: token, url: "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}/certificate_request/#{token}?auth_token=#{ENV['AUTH_TOKEN']}" }.to_json
2828
else
2929
status 422
@@ -56,9 +56,9 @@ def authenticate!
5656
end
5757

5858
def perform_preflight_check
59-
check = PreflightCheck.new(heroku_token: ENV['HEROKU_OAUTH_KEY'], cloudflare_token: ENV['CLOUDFLARE_API_KEY'], cloudflare_email: ENV['CLOUDFLARE_EMAIL'])
59+
check = PreflightCheck.new(heroku_token: ENV['HEROKU_OAUTH_KEY'])
6060

61-
if check.check_cloudflare == false || check.check_heroku == false
62-
halt 422, "Could not connect to Heroku or Cloudflare."
61+
if check.check_heroku == false
62+
halt 422, "Could not connect to Heroku."
6363
end
6464
end

lib/cloudflare_challenge.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33

44
class CloudflareChallenge
55

6-
class NoCloudflareAPIKey < StandardError; end;
7-
class NoCloudflareEmail < StandardError; end;
8-
9-
attr_reader :zone, :domains, :client, :challenges, :token
6+
attr_reader :zone, :domains, :client, :challenges, :token, :email, :api_key
107

118
def initialize(options = {})
12-
raise NoCloudflareAPIKey if ENV['CLOUDFLARE_API_KEY'].blank?
13-
raise NoCloudflareEmail if ENV['CLOUDFLARE_EMAIL'].blank?
9+
@email = options[:email]
10+
@api_key = options[:api_key]
1411

1512
@zone = options[:zone]
1613
@domains = options[:domains]
@@ -22,7 +19,7 @@ def initialize(options = {})
2219
end
2320

2421
def create_challenge_records
25-
cf = CloudFlare::connection(ENV['CLOUDFLARE_API_KEY'], ENV['CLOUDFLARE_EMAIL'])
22+
cf = CloudFlare::connection(@api_key, @email)
2623
@challenges.each do |challenge|
2724
cf.rec_new(@zone, 'TXT', "_acme-challenge.#{challenge.domain}", challenge.dns01.record_content, 1)
2825
end

test/certificate_generator_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
class CertificateGeneratorTest < MiniTest::Test
44

55
def setup
6-
ENV['CLOUDFLARE_API_KEY'] = '547348956734789576'
7-
ENV['CLOUDFLARE_EMAIL'] = '[email protected]'
86
ENV['CONTACT_EMAIL'] = '[email protected]'
97
end
108

@@ -16,6 +14,8 @@ def test_generate_certificate
1614
VCR.use_cassette('new-certificate-debug') do
1715
a = CertificateGenerator.new(challenge: CloudflareChallenge.new(zone: 'substrakt.com',
1816
domains: ['www.substrakt.com', 'substrakt.com'],
17+
api_key: 'fsdfdsf',
18+
1919
client: AcmeClientRegistration.new(debug: true).client))
2020
assert_equal Acme::Client::Certificate, a.certificate.class
2121
end

test/cloudflare_challenge_test.rb

+23-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
class CloudflareChallengeTest < MiniTest::Test
44

55
def setup
6-
ENV['CLOUDFLARE_API_KEY'] = '547348956734789576'
7-
ENV['CLOUDFLARE_EMAIL'] = '[email protected]'
8-
ENV['CONTACT_EMAIL'] = '[email protected]'
6+
ENV['CONTACT_EMAIL'] = '[email protected]'
97
end
108

119
def teardown
@@ -16,29 +14,33 @@ def test_create_an_instance
1614
VCR.use_cassette('acme-new-authz') do
1715
a = CloudflareChallenge.new(zone: 'substrakt.com',
1816
domains: ['www.substrakt.com', 'substrakt.com'],
17+
api_key: 'fsdfdsf',
18+
1919
client: AcmeClientRegistration.new(debug: true).client)
2020
assert_equal CloudflareChallenge, a.class
2121
end
2222
end
2323

24-
def test_raise_an_exception_if_CLOUDFLARE_API_KEY_is_missing
24+
def test_create_an_instance_with_custom_auth
25+
ENV['CLOUDFLARE_EMAIL'] = nil
2526
ENV['CLOUDFLARE_API_KEY'] = nil
26-
assert_raises CloudflareChallenge::NoCloudflareAPIKey do
27-
CloudflareChallenge.new
28-
end
29-
end
30-
31-
def test_raise_an_exception_if_CLOUDFLARE_EMAIL_is_missing
32-
ENV['CLOUDFLARE_EMAIL'] = nil
33-
assert_raises CloudflareChallenge::NoCloudflareEmail do
34-
CloudflareChallenge.new
27+
VCR.use_cassette('acme-new-authz') do
28+
a = CloudflareChallenge.new(zone: 'substrakt.com',
29+
domains: ['www.substrakt.com', 'substrakt.com'],
30+
api_key: 'fdhsufgdjshfgsd',
31+
32+
client: AcmeClientRegistration.new(debug: true).client)
33+
assert_equal '[email protected]', a.email
34+
assert_equal 'fdhsufgdjshfgsd', a.api_key
3535
end
3636
end
3737

3838
def test_set_zone
3939
VCR.use_cassette('acme-new-authz') do
4040
a = CloudflareChallenge.new(zone: 'substrakt.com',
4141
domains: ['www.substrakt.com', 'substrakt.com'],
42+
api_key: 'fsdfdsf',
43+
4244
client: AcmeClientRegistration.new(debug: true).client)
4345
assert_equal 'substrakt.com', a.zone
4446
end
@@ -48,6 +50,8 @@ def test_set_domains
4850
VCR.use_cassette('acme-new-authz') do
4951
a = CloudflareChallenge.new(zone: 'substrakt.com',
5052
domains: ['www.substrakt.com', 'substrakt.com'],
53+
api_key: 'fsdfdsf',
54+
5155
client: AcmeClientRegistration.new(debug: true).client)
5256
assert_equal ['www.substrakt.com', 'substrakt.com'], a.domains
5357
end
@@ -57,6 +61,8 @@ def test_add_challenge_records_to_cloudflare
5761
VCR.use_cassette('acme-new-authz') do
5862
a = CloudflareChallenge.new(zone: 'substrakt.com',
5963
domains: ['www.substrakt.com', 'substrakt.com'],
64+
api_key: 'fsdfdsf',
65+
6066
client: AcmeClientRegistration.new(debug: true).client)
6167
assert_equal ['www.substrakt.com', 'substrakt.com'], a.create_challenge_records
6268
end
@@ -66,6 +72,8 @@ def test_get_list_of_challenges
6672
VCR.use_cassette('acme-new-authz') do
6773
a = CloudflareChallenge.new(zone: 'substrakt.com',
6874
domains: ['www.substrakt.com', 'substrakt.com'],
75+
api_key: 'fsdfdsf',
76+
6977
client: AcmeClientRegistration.new(debug: true).client)
7078
assert_equal Challenge, a.challenges.first.class
7179
end
@@ -75,6 +83,8 @@ def test_verification
7583
VCR.use_cassette('acme-challenge-debug') do
7684
a = CloudflareChallenge.new(zone: 'substrakt.com',
7785
domains: ['max123.substrakt.com', 'max345.substrakt.com'],
86+
api_key: 'fsdfdsf',
87+
7888
client: AcmeClientRegistration.new(debug: true).client)
7989
assert_equal true, a.verify
8090
end

test/logger_test.rb

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def test_log_with_generator
2020
VCR.use_cassette('new-certificate-debug') do
2121
a = CertificateGenerator.new(challenge: CloudflareChallenge.new(zone: 'substrakt.com',
2222
domains: ['www.substrakt.com', 'substrakt.com'],
23+
api_key: 'fsdfdsf',
24+
2325
client: AcmeClientRegistration.new(debug: true).client))
2426
assert_equal "[Zone: substrakt.com - Domains: www.substrakt.com, substrakt.com] ----> This is a test message", Logger.log('This is a test message', generator: a)
2527
end
@@ -29,6 +31,8 @@ def test_a_log_with_generator_should_also_write_to_redis
2931
VCR.use_cassette('new-certificate-debug') do
3032
a = CertificateGenerator.new(challenge: CloudflareChallenge.new(zone: 'substrakt.com',
3133
token: 'testingtesting',
34+
api_key: 'fsdfdsf',
35+
3236
domains: ['www.substrakt.com', 'substrakt.com'],
3337
client: AcmeClientRegistration.new(debug: true).client))
3438
Logger.log('Test message', generator: a)

test/test_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ENV['RACK_ENV'] = 'test'
2+
ENV['ENVIRONMENT'] = 'test'
23
require 'minitest/autorun'
34
require 'rack/test'
45
require 'vcr'

workers/cloudflare_challenge_worker.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ class CloudflareChallengeWorker
1010

1111
sidekiq_options :retry => false
1212

13-
def perform(zone, domains, token, app_name, debug = true)
13+
def perform(zone, domains, token, app_name, debug = true, cloudflare = {})
1414
$redis = Redis.new(url: ENV['REDIS_URL'])
1515
$redis.setex("status_#{token}", 3600, "started")
1616
Logger.log("Starting challenge creation on zone: #{zone}, with domains: #{domains}.")
1717
Logger.log("Debug is #{debug ? 'ON' : 'OFF'}")
1818
a = CloudflareChallenge.new(zone: zone,
1919
domains: domains,
2020
token: token,
21+
email: cloudflare["email"],
22+
api_key: cloudflare["api_key"],
2123
client: AcmeClientRegistration.new(debug: debug).client)
2224

2325
begin

0 commit comments

Comments
 (0)