module Surtr::ACME

Public Class Methods

certificate(keyfile, endpoint, destination, domains) click to toggle source
# File lib/surtr/acme.rb, line 54
def self.certificate (keyfile, endpoint, destination, domains)
  key = OpenSSL::PKey::RSA.new(File.read(keyfile))
  client = Acme::Client.new(private_key: key, endpoint: "https://acme-#{endpoint}.api.letsencrypt.org")
  csr = Acme::Client::CertificateRequest.new(names: domains)
  certificate = client.new_certificate(csr)
  FileUtils.mkpath destination
  File.write File.join(destination, "privkey.pem"), certificate.request.private_key.to_pem
  File.write File.join(destination, "cert.pem"), certificate.to_pem
  File.write File.join(destination, "chain.pem"), certificate.chain_to_pem
  File.write File.join(destination, "fullchain.pem"), certificate.fullchain_to_pem
end
challenge(keyfile, endpoint, domain) click to toggle source
# File lib/surtr/acme.rb, line 19
def self.challenge (keyfile, endpoint, domain)
  key = OpenSSL::PKey::RSA.new(File.read(keyfile))
  client = Acme::Client.new(private_key: key, endpoint: "https://acme-#{endpoint}.api.letsencrypt.org")
  auth = client.authorize(domain: domain)
  case auth.status
  when "pending"
    challenge = auth.dns01
    return [false, [challenge.record_name, domain].join("."), challenge.record_type, challenge.record_content]
  when "valid"
    return true
  else
    fail "#{domain}: unexpected authorization status: #{auth.status}"
  end
end
keygen(keyfile) click to toggle source
# File lib/surtr/acme.rb, line 9
def self.keygen (keyfile)
  File.write keyfile, OpenSSL::PKey::RSA.new(4096).to_pem
end
register(keyfile, endpoint, email) click to toggle source
# File lib/surtr/acme.rb, line 13
def self.register (keyfile, endpoint, email)
  key = OpenSSL::PKey::RSA.new(File.read(keyfile))
  client = Acme::Client.new(private_key: key, endpoint: "https://acme-#{endpoint}.api.letsencrypt.org")
  client.register(contact: "mailto:#{email}").agree_terms
end
verify(keyfile, endpoint, domain) click to toggle source
# File lib/surtr/acme.rb, line 34
def self.verify (keyfile, endpoint, domain)
  key = OpenSSL::PKey::RSA.new(File.read(keyfile))
  client = Acme::Client.new(private_key: key, endpoint: "https://acme-#{endpoint}.api.letsencrypt.org")
  auth = client.authorize(domain: domain)
  case auth.status
  when "pending"
    challenge = auth.dns01
    challenge.request_verification
    while auth.verify_status == "pending"
      sleep 0.1
    end
    return true
  when "valid"
    return true
  else
    fail "#{domain}: unexpected authorization status: #{auth.status}"
  end
end