From d91d70dd378a4a91c740b03b0852432ef128b24a Mon Sep 17 00:00:00 2001 From: mh Date: Fri, 4 Nov 2016 18:52:39 +0100 Subject: store key & hostname --- lib/puppet/parser/functions/generate_onion_key.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/puppet/parser/functions/generate_onion_key.rb b/lib/puppet/parser/functions/generate_onion_key.rb index 2964268..9ee5351 100644 --- a/lib/puppet/parser/functions/generate_onion_key.rb +++ b/lib/puppet/parser/functions/generate_onion_key.rb @@ -7,12 +7,13 @@ Requires a location to load and store the private key, as well an identifier, wh Example: - res = generate_onion_key('/tmp','my_secrect_key') + res = generate_onion_key('/tmp','my_secret_key') notice "Onion Address: \${res[0]" notice "Priavte Key: \${res[1]" -If /tmp/my_secrect_key.key exists, it will be loaded and the onion address will be generated from it. +It will also store the onion address under /tmp/my_secret_key.hostname. +If /tmp/my_secret_key.key exists, but not the hostname file. Then the function will be loaded and the onion address will be generated from it. EOS ) do |args| @@ -24,17 +25,24 @@ If /tmp/my_secrect_key.key exists, it will be loaded and the onion address will raise(Puppet::ParseError, "generate_onion_key(): requires location (#{location}) to be a directory") unless File.directory?(location) path = File.join(location,identifier) - private_key = if File.exists?(path) - pk = OpenSSL::PKey::RSA.new(File.read(path)) - raise(Puppet::ParseError, "generate_onion_key(): key in path #{path} must have a length of 1024bit") unless (pk.n.num_bytes * 8) == 1024 + private_key = if File.exists?(kf="#{path}.key") + pk = OpenSSL::PKey::RSA.new(File.read(kf)) + raise(Puppet::ParseError, "generate_onion_key(): key in path #{kf} must have a length of 1024bit") unless (pk.n.num_bytes * 8) == 1024 pk else # 1024 is hardcoded by tor pk = OpenSSL::PKey::RSA.generate(1024) - File.open(path,'w'){|f| f << pk.to_s } + File.open(kf,'w'){|f| f << pk.to_s } pk end + onion_address = if File.exists?(hf="#{path}.hostname") + File.read(hf) + else + oa = function_onion_address([private_key]) + File.open(hf,'w'){|f| f << oa.to_s } + oa + end - [ function_onion_address([private_key]), private_key.to_s ] + [ onion_address, private_key.to_s ] end end -- cgit v1.2.3