Class: CertificateDepot::Store

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/certificate_depot/store.rb

Overview

Manages a directory with certificates. It's mainly used by the depot to generate a unique serial for its certificates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Store

Creates a new Store instance. The path should be a directory containing certificates in PEM format.


9
10
11
12
13
# File 'lib/certificate_depot/store.rb', line 9

def initialize(path)
  @path         = path
  @certificates = []
  load
end

Instance Attribute Details

#pathObject

Returns the value of attribute path


5
6
7
# File 'lib/certificate_depot/store.rb', line 5

def path
  @path
end

Instance Method Details

#<<(certificate) ⇒ Object

Append a certificate to the store.


27
28
29
# File 'lib/certificate_depot/store.rb', line 27

def <<(certificate)
  @certificates << certificate
end

#each(&block) ⇒ Object


51
52
53
# File 'lib/certificate_depot/store.rb', line 51

def each(&block)
  @certificates.each(&block)
end

#loadObject

Reads all certificates from disk.


42
43
44
45
46
47
# File 'lib/certificate_depot/store.rb', line 42

def load
  (Dir.entries(@path) - %w(. .. ca.crt)).each do |entry|
    certificate_path = File.join(@path, entry)
    self << CertificateDepot::Certificate.from_file(certificate_path)
  end
end

#next_serial_numberObject

Returns an unused serial which can be used to generate a new certificate for the store.


22
23
24
# File 'lib/certificate_depot/store.rb', line 22

def next_serial_number
  size + 1
end

#sizeObject

Returns the number of certificates in the store.


16
17
18
# File 'lib/certificate_depot/store.rb', line 16

def size
  @certificates.size
end

#syncObject

Writes all unsaved certificates to disk.


32
33
34
35
36
37
38
39
# File 'lib/certificate_depot/store.rb', line 32

def sync
  @certificates.each do |certificate|
    certificate_path = File.join(@path, "#{certificate.serial_number}.crt")
    unless File.exist?(certificate_path)
      certificate.write_to(certificate_path)
    end
  end
end