Module: Model::AcmeOrder

Extended by:
ActiveSupport::Concern
Included in:
Com::AcmeOrder
Defined in:
app/models/com/model/acme_order.rb

Instance Method Summary collapse

Instance Method Details

#certObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/com/model/acme_order.rb', line 119

def cert
  r = order.certificate
rescue Acme::Client::Error::BadNonce
  retry
else
  file = Tempfile.new
  file.binmode
  file.write r
  file.rewind
  self.issued_at = Time.current
  self.status = order.status
  self.cert_key.attach io: file, filename: "#{identifiers_string}.pem"
  r
end

#common_nameObject



93
94
95
# File 'app/models/com/model/acme_order.rb', line 93

def common_name
  identifiers.first
end

#csrObject



97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/com/model/acme_order.rb', line 97

def csr
  return @csr if defined? @csr
  @csr = Acme::Client::CertificateRequest.new(names: identifiers, subject: { common_name: common_name })
  Tempfile.open do |file|
    file.binmode
    file.write @csr.private_key.to_pem
    file.rewind
    self.private_pem.attach io: file, filename: "#{identifiers_string}.key"
  end
  @csr
end

#finalizeObject

status: valid



110
111
112
113
114
115
116
117
# File 'app/models/com/model/acme_order.rb', line 110

def finalize
  order.finalize(csr: csr)
rescue Acme::Client::Error::BadNonce
  retry
ensure
  self.status = order.status
  self.save
end

#get_cert(tries = 2) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/com/model/acme_order.rb', line 67

def get_cert(tries = 2)
  case order.status
  when 'invalid'
    order(true)
  when 'pending'
    acme_identifiers.map(&:authorization)
    acme_identifiers.map(&:auto_verify).all?(true) && order.reload
  when 'ready'
    finalize
  when 'valid'
    cert # order.certificate_url.present?
  end
rescue
  retry unless (tries -= 1).zero?
end

#identifiersObject



83
84
85
# File 'app/models/com/model/acme_order.rb', line 83

def identifiers
  acme_identifiers.pluck(:identifier).sort
end

#identifiers_stringObject



87
88
89
90
91
# File 'app/models/com/model/acme_order.rb', line 87

def identifiers_string
  r = identifiers.first
  r = r.delete_prefix '*.'
  r.gsub('.', '_')
end

#order(renewal = false) ⇒ Object

status: pending



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/com/model/acme_order.rb', line 35

def order(renewal = false)
  if defined?(@order) && !renewal
    return @order
  end

  if !renewal && url
    begin
      @order = .client.order(url: url)
    rescue Acme::Client::Error::BadNonce
      retry
    rescue Acme::Client::Error::NotFound => e
      @order = renew_order
    end
  else
    @order = renew_order
  end

  @order
end

#renew_before_expiredObject



134
135
136
# File 'app/models/com/model/acme_order.rb', line 134

def renew_before_expired
  AcmeJob.set(wait_until: issued_at + 2.months).perform_later(self)
end

#renew_orderObject

x



56
57
58
59
60
61
62
63
64
65
# File 'app/models/com/model/acme_order.rb', line 56

def renew_order
  r = .client.new_order(identifiers: identifiers)
rescue Acme::Client::Error::BadNonce
  retry
else
  self.orderid = r.to_h[:url].split('/')[-1]
  self.assign_attributes r.to_h.slice(:status, :url)
  self.save
  r
end