Class: Encryptbot::Cert

Inherits:
Object
  • Object
show all
Defined in:
lib/encryptbot/cert.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCert

Returns a new instance of Cert.



13
14
15
16
17
18
# File 'lib/encryptbot/cert.rb', line 13

def initialize
  @domain_list = Encryptbot.configuration.domains
  @domain_names = @domain_list.map{|d| d[:domain] }
  @account_email = Encryptbot.configuration.acme_email
  @test_mode = Encryptbot.configuration.test_mode
end

Instance Attribute Details

#account_emailObject (readonly)

Returns the value of attribute account_email.



11
12
13
# File 'lib/encryptbot/cert.rb', line 11

def 
  @account_email
end

#domain_listObject (readonly)

Returns the value of attribute domain_list.



11
12
13
# File 'lib/encryptbot/cert.rb', line 11

def domain_list
  @domain_list
end

#domain_namesObject (readonly)

Returns the value of attribute domain_names.



11
12
13
# File 'lib/encryptbot/cert.rb', line 11

def domain_names
  @domain_names
end

#test_modeObject (readonly)

Returns the value of attribute test_mode.



11
12
13
# File 'lib/encryptbot/cert.rb', line 11

def test_mode
  @test_mode
end

Instance Method Details

#addObject

Add certificate



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
# File 'lib/encryptbot/cert.rb', line 21

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"
  )
   = client.(
    contact: "mailto:#{@account_email}",
    terms_of_service_agreed: true
  )

  # create order
  order = client.new_order(identifiers: @domain_names)

  # authorization of domains
  order.authorizations.each do |authorization|
    dns_challenge = authorization.dns
    domain = authorization.domain
    dns_entry = {
      name: dns_challenge.record_name,
      type: dns_challenge.record_type,
      content: dns_challenge.record_content
    }
    case @domain_list.detect{|t| t[:domain].gsub("*.", "") == domain }[:service]
    when "route53"
      Encryptbot::Services::Route53.new(domain, dns_entry).add_challenge
    when "cloudflare"
      Encryptbot::Services::Cloudflare.new(domain, dns_entry).add_challenge
    when "dyn"
      Encryptbot::Services::Dyn.new(domain, dns_entry).add_challenge
    else
      raise Encryptbot::Error::UnknownServiceError, "#{domain} service unknown"
    end
    # 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

  end # end auth loop

  if order.status == "invalid"
    raise Encryptbot::Error::InvalidOrderError, "Certificate order was invalid. DNS Challenge failed."
  end

  # Generate certificate
  csr = Acme::Client::CertificateRequest.new(names: @domain_names)
  order.finalize(csr: csr)
  sleep(1) while order.status == "processing"

  # add certificate to heroku
  certificate = order.certificate
  private_key = csr.private_key.to_pem
  Encryptbot::Heroku.new.add_certificate(order.certificate, private_key)
end

#ready_for_challenge(domain, dns_challenge) ⇒ Object

Check if TXT value has been set correctly



95
96
97
98
99
100
101
102
103
# File 'lib/encryptbot/cert.rb', line 95

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