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

This commit is contained in:
Sven Czarnian
2021-12-02 23:06:32 +01:00
parent 22216b6627
commit 6aa2009cce

View File

@@ -1,7 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
from datetime import datetime
import os import os
import sys
from typing import Tuple from typing import Tuple
import zmq.auth import zmq.auth
@@ -10,14 +12,12 @@ import zmq.auth
# @return The public and private key tuple # @return The public and private key tuple
def KeyPairCreator(directory: str, server: bool) -> Tuple[str, str]: def KeyPairCreator(directory: str, server: bool) -> Tuple[str, str]:
if not server: if not server:
print('Creating a new pair for a client...')
target = 'client' target = 'client'
else: else:
print('Creating a new pair for the server...')
target = 'server' target = 'server'
public, private = zmq.auth.create_certificates(directory, target) public, private = zmq.auth.create_certificates(directory, target)
return (public, private) return public, private
def str2bool(value): def str2bool(value):
if isinstance(value, bool): if isinstance(value, bool):
@@ -29,16 +29,71 @@ def str2bool(value):
else: else:
raise argparse.ArgumentTypeError('Boolean value expected') 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__': if __name__ == '__main__':
# create the commandline parser # create the commandline parser
parser = argparse.ArgumentParser(description='Create a new key-value pair') 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") parser.add_argument('--server', default=False, action='store_true', help="Creates server key pair")
args = parser.parse_args() 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 # create the directory if it does not exist
if not os.path.exists(args.directory): if not os.path.exists(args.directory):
os.makedirs(args.directory) os.makedirs(args.directory)
# create the keys # 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)