2019-06-06 18:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PagesDomains
|
|
|
|
class ObtainLetsEncryptCertificateService
|
2019-06-26 11:47:21 +00:00
|
|
|
# time for processing validation requests for acme challenges
|
|
|
|
# 5-15 seconds is usually enough
|
|
|
|
CHALLENGE_PROCESSING_DELAY = 1.minute.freeze
|
|
|
|
|
|
|
|
# time LetsEncrypt ACME server needs to generate the certificate
|
|
|
|
# no particular SLA, usually takes 10-15 seconds
|
|
|
|
CERTIFICATE_PROCESSING_DELAY = 1.minute.freeze
|
|
|
|
|
2019-06-06 18:55:31 +00:00
|
|
|
attr_reader :pages_domain
|
|
|
|
|
|
|
|
def initialize(pages_domain)
|
|
|
|
@pages_domain = pages_domain
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
pages_domain.acme_orders.expired.delete_all
|
|
|
|
acme_order = pages_domain.acme_orders.first
|
|
|
|
|
|
|
|
unless acme_order
|
|
|
|
::PagesDomains::CreateAcmeOrderService.new(pages_domain).execute
|
2019-06-26 11:47:21 +00:00
|
|
|
PagesDomainSslRenewalWorker.perform_in(CHALLENGE_PROCESSING_DELAY, pages_domain.id)
|
2019-06-06 18:55:31 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
api_order = ::Gitlab::LetsEncrypt::Client.new.load_order(acme_order.url)
|
|
|
|
|
|
|
|
# https://tools.ietf.org/html/rfc8555#section-7.1.6 - statuses diagram
|
|
|
|
case api_order.status
|
|
|
|
when 'ready'
|
|
|
|
api_order.request_certificate(private_key: acme_order.private_key, domain: pages_domain.domain)
|
2019-06-26 11:47:21 +00:00
|
|
|
PagesDomainSslRenewalWorker.perform_in(CERTIFICATE_PROCESSING_DELAY, pages_domain.id)
|
2019-06-06 18:55:31 +00:00
|
|
|
when 'valid'
|
|
|
|
save_certificate(acme_order.private_key, api_order)
|
|
|
|
acme_order.destroy!
|
|
|
|
# when 'invalid'
|
|
|
|
# TODO: implement error handling
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def save_certificate(private_key, api_order)
|
|
|
|
certificate = api_order.certificate
|
2019-06-21 12:06:12 +00:00
|
|
|
pages_domain.update!(gitlab_provided_key: private_key, gitlab_provided_certificate: certificate)
|
2019-06-06 18:55:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|