module Keyth

Keyhandling module with various functions for keeping keys out of configuration files.

Public Class Methods

add_key(key, value) click to toggle source

Adds a key to the store Params:

key

name of the key (without the keyth: prefix)

value

the key value

# File lib/keyth.rb, line 31
def self.add_key(key, value)
  load_keyfile unless @key_list
  application, key_name = key.split('/')
  @key_list[application] = {} unless @key_list.key?(application)
  @key_list[application][key_name] = value
  save_keyfile
end
apply_to(obj)
Alias for: fetch_keys
delete_key(key) click to toggle source

Removes a key from the store Params:

key

name of the key (without the keyth: prefix)

# File lib/keyth.rb, line 42
def self.delete_key(key)
  load_keyfile unless @key_list
  application, key_name = key.split('/')
  @key_list[application].delete(key_name)
  @key_list.delete(application) if @key_list[application].empty?
  save_keyfile
end
fetch_keys(obj) click to toggle source

Fixes a string, array-alike, or hash-alike by automatically retrieving keys for any value prefixed with “keyth:” Params:

obj

the object to fix

# File lib/keyth.rb, line 76
def self.fetch_keys(obj)
  load_keyfile unless @key_list
  case
  when obj.respond_to?(:keys)
    obj.each do |k, v|
      obj[k] = fetch_keys(v)
    end
  when obj.respond_to?(:each)
    obj.each_with_index do |v, i|
      obj[i] = fetch_keys(v)
    end
  when obj.is_a?(String)
    obj = obj.gsub(/^keyth\:(.*)/) { get_key_safe(Regexp.last_match[1]) || "Missing Key: [#{obj}]" }
  end
  obj
end
Also aliased as: apply_to
get_key(key) click to toggle source

Retrieves a key from the store, raising errors if the key is missing Params:

key

name of the key (without the keyth: prefix)

# File lib/keyth.rb, line 12
def self.get_key(key)
  load_keyfile unless @key_list
  application, key_name = key.split('/')
  fail "Application not found: #{application}!" unless @key_list[application]
  fail "Key not found: #{key}!" unless @key_list[application][key_name]
  @key_list[application][key_name]
end
get_key_safe(key) click to toggle source

Retrieves a key from the store, returning nil if the key is missing Params:

key

name of the key (without the keyth: prefix)

# File lib/keyth.rb, line 23
def self.get_key_safe(key)
  get_key(key) rescue nil
end
keys(application = nil) click to toggle source

Gets a list of keys in the store Params:

application

if not nil, only return keys where the part of the key before the slash matches.

# File lib/keyth.rb, line 53
def self.keys(application = nil)
  load_keyfile unless @key_list
  keys = {}
  @key_list.each do |k1, v|
    v.each do |k2, v2|
      keys[k1 + '/' + k2] = v2 if k1 == application || application.nil?
    end
  end
  keys
end
load_keyfile() click to toggle source

Load the keyfile. By default, the keystore is loaded if necessary by the using functions, so it is unnecessary to call this directly.

# File lib/keyth.rb, line 105
def self.load_keyfile
  if File.file?(keyfile_location)
    @key_list = YAML.pre_keyth_load(File.open(keyfile_location))
  else
    @key_list = {}
  end
end
load_yaml(file) click to toggle source

Reads a YAML file, automatically retrieving keys for any value prefixed with “keyth:” Params:

file

file object containing YAML to read

# File lib/keyth.rb, line 67
def self.load_yaml(file)
  load_keyfile unless @key_list
  keys = YAML.pre_keyth_load(file)
  fetch_keys(keys)
end
namespace(namespace) click to toggle source
# File lib/keyth.rb, line 97
def self.namespace(namespace)
  @namespace = namespace
  @keylist = nil
  @namespace
end
save_keyfile() click to toggle source

Save the keyfile. By default, the keystore is saved when changes are made to it, so it is unnecessary to call this directly.

# File lib/keyth.rb, line 115
def self.save_keyfile
  load_keyfile unless @key_list
  File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml }
end

Private Class Methods

keyfile_location() click to toggle source
# File lib/keyth.rb, line 122
def self.keyfile_location
  @namespace = 'default' unless @namespace
  ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml')
end