Class: Encryptbot::Cert
- Inherits:
-
Object
- Object
- Encryptbot::Cert
- Defined in:
- lib/encryptbot/cert.rb
Instance Attribute Summary collapse
-
#account_email ⇒ Object
readonly
Returns the value of attribute account_email.
-
#domains ⇒ Object
readonly
Returns the value of attribute domains.
-
#test_mode ⇒ Object
readonly
Returns the value of attribute test_mode.
Instance Method Summary collapse
-
#add ⇒ Object
Add certificate.
-
#initialize ⇒ Cert
constructor
A new instance of Cert.
-
#ready_for_challenge(domain, dns_challenge) ⇒ Object
Check if TXT value has been set correctly.
Constructor Details
#initialize ⇒ Cert
12 13 14 15 16 |
# File 'lib/encryptbot/cert.rb', line 12 def initialize @domains = Encryptbot.configuration.domains @account_email = Encryptbot.configuration.acme_email @test_mode = Encryptbot.configuration.test_mode end |
Instance Attribute Details
#account_email ⇒ Object (readonly)
Returns the value of attribute account_email.
10 11 12 |
# File 'lib/encryptbot/cert.rb', line 10 def account_email @account_email end |
#domains ⇒ Object (readonly)
Returns the value of attribute domains.
10 11 12 |
# File 'lib/encryptbot/cert.rb', line 10 def domains @domains end |
#test_mode ⇒ Object (readonly)
Returns the value of attribute test_mode.
10 11 12 |
# File 'lib/encryptbot/cert.rb', line 10 def test_mode @test_mode end |
Instance Method Details
#add ⇒ Object
Add certificate
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/encryptbot/cert.rb', line 19 def add unless Encryptbot.configuration.valid? raise Encryptbot::Error::SetupError, "Encryptbot is configured incorrectly. Check all required variables have been set." end # setup ACME client private_key = OpenSSL::PKey::RSA.new(4096) client = Acme::Client.new( private_key: private_key, directory: @test_mode ? "https://acme-staging-v02.api.letsencrypt.org/directory" : "https://acme-v02.api.letsencrypt.org/directory" ) account = client.new_account( contact: "mailto:#{@account_email}", terms_of_service_agreed: true ) # create order order = client.new_order(identifiers: @domains) puts "Start Authorization" # authorization of domains = [] order..each do || dns_challenge = .dns domain = .domain puts "Start Authorization of #{domain}" dns_entry = { name: dns_challenge.record_name, type: dns_challenge.record_type, content: dns_challenge.record_content } Encryptbot::Services::Route53.new(domain, dns_entry).add_challenge # check if the DNS service has updated sleep(8) attempts = 3 while !ready_for_challenge(domain, dns_challenge) && attempts > 0 sleep(8) attempts -= 1 end # request verifification dns_challenge.request_validation # check if dns challange was accepted while dns_challenge.status == "pending" sleep(2) dns_challenge.reload end puts "Completed authorization of #{domain}. Status: #{dns_challenge.status}" if dns_challenge.status == 'invalid' << domain end end # end auth loop if .any? raise Encryptbot::Error::DomainAuthorizationFailedError, "Domains failed to authorize: #{failed_domain_authorizations.join(', ')}." end if order.status == "invalid" raise Encryptbot::Error::InvalidOrderError, "Certificate order was invalid. DNS Challenge failed." end # Generate certificate puts "Generate Certificate" csr = Acme::Client::CertificateRequest.new(names: @domains) order.finalize(csr: csr) sleep(1) while order.status == "processing" # add certificate to heroku puts "Adding Certificate to heroku" certificate = order.certificate private_key = csr.private_key.to_pem Encryptbot::Heroku.new.add_certificate(order.certificate, private_key) puts "Completed" end |
#ready_for_challenge(domain, dns_challenge) ⇒ Object
Check if TXT value has been set correctly
99 100 101 102 103 104 105 106 107 |
# File 'lib/encryptbot/cert.rb', line 99 def ready_for_challenge(domain, dns_challenge) record = "#{dns_challenge.record_name}.#{domain}" challenge_value = dns_challenge.record_content txt_value = Resolv::DNS.open do |dns| records = dns.getresources(record, Resolv::DNS::Resource::IN::TXT); records.empty? ? nil : records.map(&:data).join(" ") end txt_value == challenge_value end |