Class: LECLI::CertificateBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lecli/certificate_builder.rb

Overview

Helper class to generate certs and access the default options

Constant Summary collapse

YAML_FILENAME =
'lecli.yml'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ CertificateBuilder

Returns a new instance of CertificateBuilder.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lecli/certificate_builder.rb', line 13

def initialize
  @challenges = []
  @production = false

  # Pass a block to edit the new object for prod/staging or other options
  yield self if block_given?

  prod_url = 'https://acme-v02.api.letsencrypt.org/directory'
  staging_url = 'https://acme-staging-v02.api.letsencrypt.org/directory'
  @endpoint = @production ? prod_url : staging_url
end

Instance Attribute Details

#productionObject

Returns the value of attribute production.



9
10
11
# File 'lib/lecli/certificate_builder.rb', line 9

def production
  @production
end

Class Method Details

.load_options(config_file:) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/lecli/certificate_builder.rb', line 49

def self.load_options(config_file:)
  opts = LECLI::CertificateBuilder.runtime_defaults
  opts.merge!(YAML.load_file(config_file)) if File.file?(config_file)
  required_options = LECLI::CertificateBuilder.required_options

  # Should return nil if all required options are not present
  opts if (opts.keys & required_options).count == required_options.count
end

.persist_defaults_file(override:) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/lecli/certificate_builder.rb', line 58

def self.persist_defaults_file(override:)
  opts = LECLI::CertificateBuilder.sample_options
  if !File.file?(YAML_FILENAME) || override
    File.write(YAML_FILENAME, opts.to_yaml)
    puts YAML_FILENAME
  else
    puts "#{YAML_FILENAME} already exists. Try `lecli help yaml`"
  end
end

.required_optionsObject



25
26
27
# File 'lib/lecli/certificate_builder.rb', line 25

def self.required_options
  ['domains', 'common_name', 'account_email']
end

.runtime_defaultsObject



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

def self.runtime_defaults
  {
    'request_key' => 'request.pem',
    'certificate_key' => 'certificate.pem',
    'challenges_relative_path' => 'challenges'
  }
end

.sample_optionsObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lecli/certificate_builder.rb', line 29

def self.sample_options
  {
    'domains' => ['example.com', 'test.net'],
    'common_name' => 'example.com',
    'account_email' => '[email protected]',
    'request_key' => 'request.pem',
    'certificate_key' => 'certificate.pem',
    'challenges_relative_path' => 'challenges',
    'success_callback_script' => 'deploy.sh'
  }
end

Instance Method Details

#generate_certs(options) ⇒ Object



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
# File 'lib/lecli/certificate_builder.rb', line 68

def generate_certs(options)
  success = true

  begin
    request_challenges(options: options)
    sleep(3) # We are unaware of challenge hosting, better give extra time

    request_challenge_validation
    request_key = finalize_order(
      domains: options['domains'],
      title: options['common_name']
    )

    write_certificate(
      cert: @order.certificate, relative_path: options['certificate_key']
    )
    write_certificate(
      cert: request_key, relative_path: options['request_key']
    )
  rescue Acme::Client::Error::RateLimited => e
    puts e.message
    success = false
  end

  success
end