Class: SecureDBFields::Keyring

Inherits:
Object
  • Object
show all
Defined in:
lib/secure_db_fields/keys.rb

Constant Summary collapse

DEFAULT_PATH =
"/etc/secure_db_fields/keys.env"
APP_RELATIVE_PATH =
"config/secure_db_fields/keys.env"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = self.class.default_path) ⇒ Keyring

Returns a new instance of Keyring.



25
26
27
28
# File 'lib/secure_db_fields/keys.rb', line 25

def initialize(path = self.class.default_path)
  @path = path
  @values = parse_file(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/secure_db_fields/keys.rb', line 8

def path
  @path
end

Class Method Details

.default_pathObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/secure_db_fields/keys.rb', line 10

def self.default_path
  env = ENV["SECURE_DB_FIELDS_KEY_FILE"]
  return env if env && !env.empty?

  if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
    rails_path = Rails.root.join(APP_RELATIVE_PATH).to_s
    return rails_path if File.exist?(rails_path)
  end

  app_path = File.expand_path(APP_RELATIVE_PATH, Dir.pwd)
  return app_path if File.exist?(app_path)

  DEFAULT_PATH
end

Instance Method Details

#active_key_idObject



45
46
47
48
# File 'lib/secure_db_fields/keys.rb', line 45

def active_key_id
  value = @values["SDF_ACTIVE_KEY_ID"]
  value ? Integer(value) : 1
end

#blind_index_key(domain = nil) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/secure_db_fields/keys.rb', line 36

def blind_index_key(domain = nil)
  if domain && !domain.to_s.empty?
    name = "SDF_BIDX_#{normalize_domain(domain)}_KEY_HEX"
    fetch_hex_key(name) || raise(KeyError, "missing blind-index key #{name}")
  else
    fetch_hex_key("SDF_BIDX_KEY_HEX") || raise(KeyError, "missing SDF_BIDX_KEY_HEX")
  end
end

#encryption_key(key_id = 1) ⇒ Object



30
31
32
33
34
# File 'lib/secure_db_fields/keys.rb', line 30

def encryption_key(key_id = 1)
  fetch_hex_key("SDF_ENC_KEY_#{Integer(key_id)}_HEX") ||
    (Integer(key_id) == 1 ? fetch_hex_key("SDF_ENC_KEY_HEX") : nil) ||
    raise(KeyError, "missing encryption key for key_id=#{key_id}")
end