Browse Source

update the key create to call it via the web interface to create new keys

Sven Czarnian 3 years ago
parent
commit
6aa2009cce
1 changed files with 60 additions and 5 deletions
  1. 60 5
      aman/tools/KeyPairCreator.py

+ 60 - 5
aman/tools/KeyPairCreator.py

@@ -1,7 +1,9 @@
 #!/usr/bin/env python
 
 import argparse
+from datetime import datetime
 import os
+import sys
 from typing import Tuple
 import zmq.auth
 
@@ -10,14 +12,12 @@ import zmq.auth
 # @return The public and private key tuple
 def KeyPairCreator(directory: str, server: bool) -> Tuple[str, str]:
     if not server:
-        print('Creating a new pair for a client...')
         target = 'client'
     else:
-        print('Creating a new pair for the server...')
         target = 'server'
 
     public, private = zmq.auth.create_certificates(directory, target)
-    return (public, private)
+    return public, private
 
 def str2bool(value):
     if isinstance(value, bool):
@@ -29,16 +29,71 @@ def str2bool(value):
     else:
         raise argparse.ArgumentTypeError('Boolean value expected')
 
+def findIdentificationKey(path, publicKey : bool):
+    if True == publicKey:
+        identifier = 'public-key = '
+    else:
+        identifier = 'secret-key = '
+
+    with open(path) as file:
+        key = ''
+
+        for line in file:
+            if identifier in line:
+                elements = line.split('=')
+                for idx in range(1, len(elements)):
+                    if 0 == len(key):
+                        key = elements[idx][2:-1]
+                        key = key + elements[idx][-1]
+                    else:
+                        key = key + '=' + elements[idx]
+
+                return key[0:-2]
+
+    return None
+
 if __name__ == '__main__':
     # create the commandline parser
     parser = argparse.ArgumentParser(description='Create a new key-value pair')
-    parser.add_argument('directory', help='Directory where to store the key pair')
+    parser.add_argument('--directory', type=str, help='Directory where to store the key pair')
+    parser.add_argument('--publickey', nargs='?', type=str, default=os.getcwd(), help='Full path to the public key of the server')
     parser.add_argument('--server', default=False, action='store_true', help="Creates server key pair")
     args = parser.parse_args()
 
+    # validate the arguments
+    if False == args.server and not os.path.exists(args.publickey):
+        sys.stderr.write('The public key of the server cannot be found')
+        sys.exit(-1)
+
     # create the directory if it does not exist
     if not os.path.exists(args.directory):
         os.makedirs(args.directory)
 
     # create the keys
-    KeyPairCreator(args.directory, args.server)
+    _, private = KeyPairCreator(args.directory, args.server)
+
+    if False == args.server:
+        publicServer  = findIdentificationKey(args.publickey, True)
+        publicClient  = findIdentificationKey(private, True)
+        privateClient = findIdentificationKey(private, False)
+
+        if None == publicServer:
+            sys.stderr.write('The public key of the server cannot be found in the defined file')
+            sys.exit(-1)
+        if None == publicClient:
+            sys.stderr.write('Unable to extract the created public key')
+            sys.exit(-1)
+        if None == privateClient:
+            sys.stderr.write('Unable to extract the created private key')
+            sys.exit(-1)
+
+        # rename keys
+        timestamp = str(datetime.now(tz=None))
+        timestamp = timestamp.replace(' ', '_')
+        timestamp = timestamp.replace(':', '-')
+        os.rename(os.path.join(args.directory, 'client.key'), os.path.join(args.directory, timestamp + '.key'))
+        os.rename(os.path.join(args.directory, 'client.key_secret'), os.path.join(args.directory, timestamp + '.key_secret'))
+
+        print(publicServer)
+        print(publicClient)
+        print(privateClient)