class HammerCLI::CACertManager

Attributes

ca_store_path[R]

Public Class Methods

new(ca_store_path) click to toggle source
# File lib/hammer_cli/ca_cert_manager.rb, line 7
def initialize(ca_store_path)
  @ca_store_path = File.expand_path(ca_store_path)
end

Public Instance Methods

cert_exist?(uri) click to toggle source
# File lib/hammer_cli/ca_cert_manager.rb, line 22
def cert_exist?(uri)
  File.exist?(cert_file_name(uri))
end
cert_file_name(uri) click to toggle source
# File lib/hammer_cli/ca_cert_manager.rb, line 18
def cert_file_name(uri)
  File.join(ca_store_path, "#{uri.host}_#{uri.port}.pem")
end
store_ca_cert(raw_cert, cert_file) click to toggle source
# File lib/hammer_cli/ca_cert_manager.rb, line 11
def store_ca_cert(raw_cert, cert_file)
  raise HammerCLI::NoCACertificate.new unless is_ca_cert?(raw_cert)
  ensure_ca_store_exist
  File.write(cert_file, raw_cert)
  cert_file
end

Protected Instance Methods

ensure_ca_store_exist() click to toggle source
# File lib/hammer_cli/ca_cert_manager.rb, line 28
def ensure_ca_store_exist
  FileUtils.mkpath(ca_store_path) unless File.directory?(ca_store_path)
end
is_ca_cert?(cert) click to toggle source
# File lib/hammer_cli/ca_cert_manager.rb, line 32
def is_ca_cert?(cert)
  cert = OpenSSL::X509::Certificate.new(cert) if cert.is_a? String
  cert.extensions.any? do |ex|
    (ex.oid == 'basicConstraints' && ex.value.upcase == 'CA:TRUE') ||
        (ex.oid == 'keyUsage' && ex.value =~ /Cert(ificate )?Sign/i)
  end
end