class Acmesmith::Storages::Filesystem

Attributes

path[R]

Public Class Methods

new(path:) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 10
def initialize(path:)
  @path = Pathname(path)
end

Public Instance Methods

get_account_key() click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 16
def get_account_key
  raise NotExist.new("Account key doesn't exist") unless account_key_path.exist?
  AccountKey.new account_key_path.read
end
get_certificate(common_name, version: 'current') click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 39
def get_certificate(common_name, version: 'current')
  raise NotExist.new("Certificate for #{common_name.inspect} of #{version} version doesn't exist") unless certificate_base_path(common_name, version).exist?
  certificate = certificate_path(common_name, version).read
  chain = chain_path(common_name, version).read
  private_key = private_key_path(common_name, version).read
  Certificate.new(certificate, chain, private_key)
end
get_current_certificate_version(common_name) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 55
def get_current_certificate_version(common_name)
  path.join('certs', common_name, 'current').readlink
end
list_certificate_versions(common_name) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 51
def list_certificate_versions(common_name)
  Dir[path.join('certs', common_name, '*').to_s].map { |_| File.basename(_) }.reject { |_| _ == 'current' }
end
list_certificates() click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 47
def list_certificates
  Dir[path.join('certs', '*').to_s].map { |_| File.basename(_) }
end
put_account_key(key, passphrase = nil) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 21
def put_account_key(key, passphrase = nil)
  raise AlreadyExist if account_key_path.exist?
  File.write account_key_path.to_s, key.export(passphrase), 0, perm: 0600
end
put_certificate(cert, passphrase = nil, update_current: true) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 26
def put_certificate(cert, passphrase = nil, update_current: true)
  h = cert.export(passphrase)
  certificate_base_path(cert.common_name, cert.version).mkpath
  File.write certificate_path(cert.common_name, cert.version), "#{h[:certificate].rstrip}\n"
  File.write chain_path(cert.common_name, cert.version), "#{h[:chain].rstrip}\n"
  File.write fullchain_path(cert.common_name, cert.version), "#{h[:fullchain].rstrip}\n"
  File.write private_key_path(cert.common_name, cert.version), "#{h[:private_key].rstrip}\n", 0, perm: 0600
  if update_current
    File.symlink(cert.version, certificate_base_path(cert.common_name, 'current.new'))
    File.rename(certificate_base_path(cert.common_name, 'current.new'), certificate_base_path(cert.common_name, 'current'))
  end
end

Private Instance Methods

account_key_path() click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 61
def account_key_path
  path.join('account.pem')
end
certificate_base_path(cn, ver) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 65
def certificate_base_path(cn, ver)
  path.join('certs', cn, ver)
end
certificate_path(cn, ver) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 69
def certificate_path(cn, ver)
  certificate_base_path(cn, ver).join('cert.pem')
end
chain_path(cn, ver) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 77
def chain_path(cn, ver)
  certificate_base_path(cn, ver).join('chain.pem')
end
fullchain_path(cn, ver) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 81
def fullchain_path(cn, ver)
  certificate_base_path(cn, ver).join('fullchain.pem')
end
private_key_path(cn, ver) click to toggle source
# File lib/acmesmith/storages/filesystem.rb, line 73
def private_key_path(cn, ver)
  certificate_base_path(cn, ver).join('key.pem')
end