]> git.netwichtig.de Git - user/henk/code/ruby/macir.git/commitdiff
change account key file handling to be more robust and shorter
authorHendrik Jäger <gitcommit@henk.geekmail.org>
Thu, 1 Feb 2024 13:10:28 +0000 (14:10 +0100)
committerHendrik Jäger <gitcommit@henk.geekmail.org>
Thu, 1 Feb 2024 13:10:28 +0000 (14:10 +0100)
macir.rb

index 9523a0fdaf14c2be91aecf4ab65e833ae60da069..83f1917a39b107e8f9244718ad72f2c79198b39f 100644 (file)
--- a/macir.rb
+++ b/macir.rb
@@ -16,9 +16,8 @@ def read_config( path = 'config.yaml' )
   rescue Psych::SyntaxError
     $stderr.puts "Parsing configfile failed: " + $!.to_s
     raise
-  rescue Errno::ENOENT => ex
+  rescue Errno::ENOENT
     $stderr.puts "IO failed: " + $!.to_s
-    p ex
   end
   return config
 end
@@ -26,17 +25,19 @@ end
 def read_account_key( path = 'pkey.pem' )
   p "Reading account key from #{path}"
   if File.readable?( path )
-    p "Key exists, trying to parse"
-    pkey_file = File.new( path )
-    privatekey_string = pkey_file.read
+    p "File #{path} is readable, trying to parse"
+    privatekey_string = File.read( path )
     private_key = OpenSSL::PKey::EC.new( privatekey_string )
   else
-    p "Key does not exist, trying to create"
-    pkey_file = File.new( path, 'w' )
-    private_key = OpenSSL::PKey::EC.generate( "prime256v1" )
-    pkey_pem = private_key.private_to_pem
-    pkey_file.write( pkey_pem )
+    if File.exists?( path )
+      raise( "The file #{path} exists but is not readable. Make it readable or specify different path" )
+    else
+      p "File #{path} does not exist, trying to create"
+      private_key = OpenSSL::PKey::EC.generate( "prime256v1" )
+      File.write( path, private_key.private_to_pem )
+    end
   end
+  return private_key
 end
 
 def deploy_dns01_challenge_token( domain, token, nameserver, config )