summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/functions/trocla_get.rb20
-rw-r--r--lib/puppet/parser/functions/trocla_set.rb10
-rw-r--r--lib/puppet/util/trocla_helper.rb4
3 files changed, 22 insertions, 12 deletions
diff --git a/lib/puppet/parser/functions/trocla_get.rb b/lib/puppet/parser/functions/trocla_get.rb
index 01c4e01..fb5cd5a 100644
--- a/lib/puppet/parser/functions/trocla_get.rb
+++ b/lib/puppet/parser/functions/trocla_get.rb
@@ -4,7 +4,7 @@ module Puppet::Parser::Functions
Usage:
- $password_user1 = trocla_get(key,[format='plain'])
+ $password_user1 = trocla_get(key,[format='plain'[,raise_error=true]])
Means:
@@ -16,13 +16,23 @@ Get the plain text password for the key 'user1'
Get the mysql style sha1 hashed password.
-It will raise a parse error if the password haven't yet been stored in trocla.
+By default puppet will raise a parse error if the password haven't yet been
+stored in trocla. This can be turned off by setting false as a third argument:
+
+ $password_user3 = trocla_get('user2','mysql',false)
+
+the return value will be undef if the key & format pair is not found.
"
) do |*args|
+ if args[0].is_a?(Array)
+ args = args[0]
+ end
require File.dirname(__FILE__) + '/../../util/trocla_helper'
- if (answer=Puppet::Util::TroclaHelper.trocla(:get_password,false,*args)).nil?
- raise(Puppet::ParseError, "No password for key,format #{args.flatten.inspect} found!")
+ args[1] ||= 'plain'
+ raise_error = args[2].nil? ? true : args[2]
+ if (answer=Puppet::Util::TroclaHelper.trocla(:get_password,false,[args[0],args[1]])).nil? && raise_error
+ raise(Puppet::ParseError, "No password for key,format #{args[0..1].flatten.inspect} found!")
end
- answer
+ answer.nil? ? :undef : answer
end
end
diff --git a/lib/puppet/parser/functions/trocla_set.rb b/lib/puppet/parser/functions/trocla_set.rb
index c3c9b49..06da5ae 100644
--- a/lib/puppet/parser/functions/trocla_set.rb
+++ b/lib/puppet/parser/functions/trocla_set.rb
@@ -38,21 +38,21 @@ trocla, for example via cli.
if args[0].is_a?(Array)
args = args[0]
end
-
+
key = args[0]
value = args[1]
raise(Puppet::ParseError, "You need to pass at least key & value as an argument!") if key.nil? || value.nil?
-
+
format = args[2] || 'plain'
return_format = args[3] || format
options = args[4] || {}
-
+
configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml")
-
+
raise(Puppet::ParseError, "Trocla config file #{configfile} not readable") unless File.exist?(configfile)
require 'trocla'
-
+
result = (trocla=Trocla.new(configfile)).set_password(key,format,value)
if format != return_format && (result = trocla.get_password(key,return_format)).nil?
raise(Puppet::ParseError, "Plaintext password is not present, but required to return password in format #{return_format}") if (return_format == 'plain') || trocla.get_password(key,'plain').nil?
diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb
index 64ef859..94670ae 100644
--- a/lib/puppet/util/trocla_helper.rb
+++ b/lib/puppet/util/trocla_helper.rb
@@ -16,7 +16,7 @@ module Puppet::Util::TroclaHelper
key = args[0] || raise(Puppet::ParseError, "You need to pass at least a key as an argument!")
format = args[1] || 'plain'
options = args[2] || {}
-
+
if options.is_a?(String)
require 'yaml'
options = YAML.load(options)
@@ -27,7 +27,7 @@ module Puppet::Util::TroclaHelper
raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile)
require 'trocla'
-
+
has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format)
end
module_function :trocla