Class: Puma::Acme::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/acme/manager.rb

Overview

Manager tracks and performs the ACME workflow steps for a certificate.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, directory:, tos_agreed:, eab:, contact: nil) ⇒ Manager

Returns a new instance of Manager.



9
10
11
12
13
14
15
# File 'lib/puma/acme/manager.rb', line 9

def initialize(store:, directory:, tos_agreed:, eab:, contact: nil)
  @store = store
  @contact = contact
  @directory = directory
  @tos_agreed = [true, directory].include?(tos_agreed)
  @eab = eab
end

Instance Attribute Details

#contactObject (readonly)

Returns the value of attribute contact.



7
8
9
# File 'lib/puma/acme/manager.rb', line 7

def contact
  @contact
end

#directoryObject (readonly)

Returns the value of attribute directory.



7
8
9
# File 'lib/puma/acme/manager.rb', line 7

def directory
  @directory
end

#eabObject (readonly)

Returns the value of attribute eab.



7
8
9
# File 'lib/puma/acme/manager.rb', line 7

def eab
  @eab
end

#tos_agreedObject (readonly)

Returns the value of attribute tos_agreed.



7
8
9
# File 'lib/puma/acme/manager.rb', line 7

def tos_agreed
  @tos_agreed
end

Instance Method Details

#accountObject



17
18
19
# File 'lib/puma/acme/manager.rb', line 17

def 
  @store.read(Account.key(directory: directory, contact: contact, eab: eab))
end

#account!Object



29
30
31
# File 'lib/puma/acme/manager.rb', line 29

def account!
  @store.fetch(Account.key(directory: directory, contact: contact, eab: eab)) {  }
end

#answer(type:, token:) ⇒ Object



25
26
27
# File 'lib/puma/acme/manager.rb', line 25

def answer(type:, token:)
  @store.read(Answer.key(type: type, token: token))
end

#cert(algorithm:, identifiers:) ⇒ Object



21
22
23
# File 'lib/puma/acme/manager.rb', line 21

def cert(algorithm:, identifiers:)
  @store.read(Cert.key(algorithm: algorithm, identifiers: identifiers))
end

#cert!(algorithm:, identifiers:) ⇒ Object



33
34
35
# File 'lib/puma/acme/manager.rb', line 33

def cert!(algorithm:, identifiers:)
  @store.fetch(Cert.key(algorithm: algorithm, identifiers: identifiers)) { Cert.new(algorithm: algorithm, identifiers: identifiers) }
end

#download!(cert) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/puma/acme/manager.rb', line 80

def download!(cert)
  stale_check!(cert)

  acme_order = client.order(url: cert.order.url)

  cert.cert_pem = acme_order.certificate

  @store.write(cert.key, cert) && cert
end

#finalize!(cert) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puma/acme/manager.rb', line 62

def finalize!(cert)
  stale_check!(cert)

  names = cert.identifiers.map(&:value)
  common_name = names.first
  private_key = new_key(cert.algorithm)

  csr = ::Acme::Client::CertificateRequest.new(common_name: common_name, names: names, private_key: private_key)

  acme_order = client.order(url: cert.order.url)
  return unless acme_order.finalize(csr: csr)

  cert.order = Order.from(acme_order)
  cert.key_pem = private_key.to_pem

  @store.write(cert.key, cert) && cert
end

#order!(cert) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puma/acme/manager.rb', line 37

def order!(cert)
  stale_check!(cert)

  identifiers = cert.identifiers.map(&:value)
  acme_order = client.new_order(**cert.to_h.slice(:not_before, :not_after).merge(identifiers: identifiers))
  cert.order = Order.from(acme_order)

  # TODO: maybe move this to caller
  cert.order.authorizations.each do |authz|
    authz.challenges.each do |challenge|
      next unless challenge.type == CHALLENGE_TYPE

      validate!(challenge)
    end
  end

  @store.write(cert.key, cert) && cert.order
end

#reload!(cert) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/puma/acme/manager.rb', line 90

def reload!(cert)
  stale_check!(cert)

  acme_order = client.order(url: cert.order.url)
  cert.order = Order.from(acme_order)

  @store.write(cert.key, cert) && cert
end

#validate!(challenge) ⇒ Object



56
57
58
59
60
# File 'lib/puma/acme/manager.rb', line 56

def validate!(challenge)
  @store.write(challenge.answer.key, challenge.answer)

  client.request_challenge_validation(url: challenge.url)
end