module LetsEncrypt

Constants

ENDPOINT

Production mode API Endpoint

ENDPOINT_STAGING

Staging mode API Endpoint, the rate limit is higher but got invalid certificate for testing

VERSION

Public Class Methods

certificate_model() click to toggle source
# File lib/letsencrypt.rb, line 90
def certificate_model
  @certificate_model ||= config.certificate_model.constantize
end
client() click to toggle source

Create the ACME Client to Let's Encrypt

# File lib/letsencrypt.rb, line 23
def client
  @client ||= ::Acme::Client.new(
    private_key: private_key,
    directory: directory
  )
end
config(&block) click to toggle source

Config how to Let's Encrypt works for Rails

LetsEncrypt.config do |config|
  # Always use production mode to connect Let's Encrypt API server
  config.use_staging = false
 end
# File lib/letsencrypt.rb, line 79
def config(&block)
  @config ||= Configuration.new
  instance_exec(@config, &block) if block_given?
  @config
end
directory() click to toggle source

Get current using Let's Encrypt endpoint

# File lib/letsencrypt.rb, line 41
def directory
  @endpoint ||= config.use_staging? ? ENDPOINT_STAGING : ENDPOINT
end
generate_private_key() click to toggle source
# File lib/letsencrypt.rb, line 62
def generate_private_key
  key = OpenSSL::PKey::RSA.new(4096)
  File.open(private_key_path, 'w') { |f| f.write(key.to_s) }
  logger.info "Created new private key for Let's Encrypt"
  key
end
load_private_key() click to toggle source
# File lib/letsencrypt.rb, line 34
def load_private_key
  return ENV['LETSENCRYPT_PRIVATE_KEY'] if config.use_env_key
  return File.open(private_key_path) if File.exist?(private_key_path)
  generate_private_key
end
logger() click to toggle source
# File lib/letsencrypt.rb, line 69
def logger
  @logger ||= LoggerProxy.new(Rails.logger, tags: ['LetsEncrypt'])
end
private_key() click to toggle source
# File lib/letsencrypt.rb, line 30
def private_key
  @private_key ||= OpenSSL::PKey::RSA.new(load_private_key)
end
private_key_path() click to toggle source
# File lib/letsencrypt.rb, line 58
def private_key_path
  config.private_key_path || Rails.root.join('config', 'letsencrypt.key')
end
register(email) click to toggle source

Register a Let's Encrypt account

This is required a private key to do this, and Let's Encrypt will use this private key to connect with domain and assign the owner who can renew and revoked.

# File lib/letsencrypt.rb, line 51
def register(email)
  account = client.new_account(contact: "mailto:#{email}", terms_of_service_agreed: true)
  logger.info "Successfully registered private key with address #{email}"
  account.kid # TODO: Save KID
  true
end
table_name_prefix() click to toggle source

@api private

# File lib/letsencrypt.rb, line 86
def table_name_prefix
  'letsencrypt_'
end