Module: DevCert::Util

Defined in:
lib/devcert/util.rb

Class Method Summary collapse

Class Method Details

.export(path, entity) ⇒ Object



40
41
42
43
44
# File 'lib/devcert/util.rb', line 40

def self.export(path, entity)
  open path, 'w' do |io|
    io.write entity.to_pem
  end
end

.generate_serialObject



60
61
62
63
64
65
# File 'lib/devcert/util.rb', line 60

def self.generate_serial
  machine_bytes = ['foo'].pack('p').size
  machine_bits = machine_bytes * 8
  machine_max_signed = 2**(machine_bits - 1) - 1
  ::SecureRandom.random_number machine_max_signed
end

.get_defaultsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/devcert/util.rb', line 7

def self.get_defaults
  path = ::File.absolute_path 'defaults.yaml', ::Dir.pwd
  data = \
    if ::File.exist? path
      ::YAML.load(::File.open(path)).fetch('devcert', {})
    else
      {}
    end

  {
    organization: data.fetch('organization', 'Acme Ltd.'),
    country: data.fetch('country', 'US'),
    state_name: data.fetch('state_name', 'California'),
    locality: data.fetch('locality', 'San Francisco')
  }
end

.load_bundle(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/devcert/util.rb', line 46

def self.load_bundle(path)
  full_path = ::File.absolute_path path, __dir__
  if ::File.exist? full_path
    data = ::YAML.load ::File.open full_path
    {
      common_name: data[:common_name],
      private_key: ::OpenSSL::PKey::RSA.new(data[:private_key]),
      certificate: ::OpenSSL::X509::Certificate.new(data[:certificate])
    }
  else
    raise "No bundle at #{full_path} exists!"
  end
end

.normalize_name(name) ⇒ Object



24
25
26
# File 'lib/devcert/util.rb', line 24

def self.normalize_name(name)
  name.gsub(/[ .-]/, '_')
end

.save_bundle(path, common_name, key, cert) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/devcert/util.rb', line 28

def self.save_bundle(path, common_name, key, cert)
  bundle = {
    common_name: common_name,
    private_key: key.to_der,
    certificate: cert.to_der
  }

  open path, 'w' do |io|
    io.write bundle.to_yaml
  end
end