class Hiera::Backend::Trocla_backend

Public Class Methods

new() click to toggle source
# File lib/hiera/backend/trocla_backend.rb, line 8
def initialize
  Hiera.debug('Hiera Trocla backend starting')
  begin
    require 'trocla'
  rescue
    require 'rubygems'
    require 'trocla'
  end

  @trocla_conf = Config[:trocla] && Config[:trocla][:config]
  @trocla = ::Trocla.new(@trocla_conf)
end

Public Instance Methods

get_password_from_hierarchy(trocla_key, format, scope, order_override) click to toggle source

Try to retrieve a password from a hierarchy

# File lib/hiera/backend/trocla_backend.rb, line 47
def get_password_from_hierarchy(trocla_key, format, scope, order_override)
  answer = nil
  Backend.datasources(scope, order_override) do |source|
    key = hierarchical_key(source, trocla_key)
    answer = @trocla.get_password(key, format)
    break unless answer.nil?
  end
  return answer
end
global_options(format, scope, order_override) click to toggle source

returns global options for password generation

# File lib/hiera/backend/trocla_backend.rb, line 75
def global_options(format, scope, order_override)
  g_options = Backend.lookup('trocla_options', {}, scope, order_override, :hash)
  g_options.merge(g_options[format] || {})
end
hierarchical_key(source, trocla_key) click to toggle source
# File lib/hiera/backend/trocla_backend.rb, line 70
def hierarchical_key(source, trocla_key)
  "hiera/#{source}/#{trocla_key}"
end
key_options(trocla_key, format, scope, order_override) click to toggle source

returns per key options for password generation

# File lib/hiera/backend/trocla_backend.rb, line 81
def key_options(trocla_key, format, scope, order_override)
  k_options = Backend.lookup('trocla_options::' + trocla_key, {}, scope, order_override, :hash)
  k_options.merge(k_options[format] || {})
end
lookup(key, scope, order_override, resolution_type) click to toggle source
# File lib/hiera/backend/trocla_backend.rb, line 21
def lookup(key, scope, order_override, resolution_type)
  # return immediately if this is no trocla lookup
  return nil unless key[/^trocla_lookup::/] or key[/^trocla_hierarchy::/]

  method, format, trocla_key = key.split('::', 3)

  case method
  when 'trocla_lookup'
    trocla_lookup(trocla_key, format, scope, order_override)
  when 'trocla_hierarchy'
    trocla_hierarchy(trocla_key, format, scope, order_override)
  end
end
options(trocla_key, format, scope, order_override) click to toggle source

retrieve options hash and merge the format specific settings into the defaults

# File lib/hiera/backend/trocla_backend.rb, line 87
def options(trocla_key, format, scope, order_override)
  g_options = global_options(format, scope, order_override)
  k_options = key_options(trocla_key, format, scope, order_override)
  g_options.merge(k_options)
end
set_password_in_hierarchy(trocla_key, format, scope, order_override) click to toggle source

Set the password in the hierarchy at the top level or whatever level is specified in the options hash with 'order_override'

# File lib/hiera/backend/trocla_backend.rb, line 59
def set_password_in_hierarchy(trocla_key, format, scope, order_override)
  opts = options(trocla_key, format, scope, order_override)
  answer = nil
  Backend.datasources(scope, opts['order_override']) do |source|
    key = hierarchical_key(source, trocla_key)
    answer = @trocla.password(key, format, opts)
    break unless answer.nil?
  end
  return answer
end
trocla_hierarchy(trocla_key, format, scope, order_override) click to toggle source
# File lib/hiera/backend/trocla_backend.rb, line 41
def trocla_hierarchy(trocla_key, format, scope, order_override)
  get_password_from_hierarchy(trocla_key, format, scope, order_override) ||
    set_password_in_hierarchy(trocla_key, format, scope, order_override)
end
trocla_lookup(trocla_key, format, scope, order_override) click to toggle source

This is a simple lookup which will return a password for the key

# File lib/hiera/backend/trocla_backend.rb, line 36
def trocla_lookup(trocla_key, format, scope, order_override)
  opts = options(trocla_key, format, scope, order_override)
  @trocla.password(trocla_key, format, opts)
end