Class: Tipi::ACME::SQLiteCertificateStore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SQLiteCertificateStore

Returns a new instance of SQLiteCertificateStore.



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/tipi/acme.rb', line 269

def initialize(path)
  require 'extralite'

  @db = Extralite::Database.new(path)
  @db.query("
    create table if not exists certificates (
      name primary key not null,
      private_key not null,
      certificate not null,
      expired_stamp not null
    );"
  )
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



267
268
269
# File 'lib/tipi/acme.rb', line 267

def db
  @db
end

Instance Method Details

#get(name) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/tipi/acme.rb', line 292

def get(name)
  remove_expired_certificates

  entry = @db.query_single_row("
    select name, private_key, certificate, expired_stamp
      from certificates
     where name = ?
  ", name)
  return nil unless entry
  entry[:expired_stamp] = Time.at(entry[:expired_stamp])
  entry[:private_key] = OpenSSL::PKey::RSA.new(entry[:private_key])
  entry
rescue Extralite::Error => e
  p error_in_get: e
  raise e
end

#remove_expired_certificatesObject



309
310
311
312
313
314
315
316
317
# File 'lib/tipi/acme.rb', line 309

def remove_expired_certificates
  @db.query("
    delete from certificates
    where expired_stamp < ?
  ", Time.now.to_i)
rescue Extralite::Error => e
  p error_in_remove_expired_certificates: e
  raise e
end

#set(name, private_key:, certificate:, expired_stamp:) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/tipi/acme.rb', line 283

def set(name, private_key:, certificate:, expired_stamp:)
  @db.query("
    insert into certificates values (?, ?, ?, ?)
  ", name, private_key.to_s, certificate, expired_stamp.to_i)
rescue Extralite::Error => e
  p error_in_set: e
  raise e
end