Class: LetsencryptStandalone::Certificate

Inherits:
Base
  • Object
show all
Defined in:
lib/letsencrypt_standalone/certificate.rb

Constant Summary collapse

@@default_names =
{
  certificate: 'cert.pem',
  chain:       'chain.pem',
  fullchain:   'fullchain.pem'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#endpoint_url, #logger, logger, #output_dir, #path, #ssl_subdir

Constructor Details

#initialize(domain:, client:) ⇒ Certificate

Returns a new instance of Certificate.



17
18
19
20
21
22
23
# File 'lib/letsencrypt_standalone/certificate.rb', line 17

def initialize(domain:, client:)
  @certificate  = domain.certificates[:certificate] unless domain.certificates.nil?
	  @files        = domain.files || @@default_names
  @domain       = domain.host
  @client       = client
  @private_key  = domain.private_key
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



15
16
17
# File 'lib/letsencrypt_standalone/certificate.rb', line 15

def files
  @files
end

Instance Method Details

#needs_refresh?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/letsencrypt_standalone/certificate.rb', line 31

def needs_refresh?
  if @certificate.not_after > Time.now + 2*24*3600
    logger.info("It doesnt need to refresh cert for domain: #{@domain}")
    false
  else
    logger.info("It needs to refresh cert for domain: #{@domain}")
    true
  end
end

#obtain_newObject



25
26
27
28
29
# File 'lib/letsencrypt_standalone/certificate.rb', line 25

def obtain_new
  csr = Acme::Client::CertificateRequest.new(names: Array(@domain), private_key: @private_key)
  @certificate = @client.new_certificate(csr) # => #<Acme::Client::Certificate ....>
  return self
end

#saveObject



41
42
43
44
45
46
47
# File 'lib/letsencrypt_standalone/certificate.rb', line 41

def save
  # Save the certificate and the private key to files
  FileUtils.mkdir_p(File.join(output_dir, @domain))
  File.write(File.join(output_dir, @domain, @files[:certificate]), @certificate.to_pem)
  File.write(File.join(output_dir, @domain, @files[:chain]), @certificate.chain_to_pem)
  File.write(File.join(output_dir, @domain, @files[:fullchain]), @certificate.fullchain_to_pem)
end