From 89e91b69bde7e6f1242f6f167e77e14d7c30521a Mon Sep 17 00:00:00 2001 From: Maciej Kucia Date: Sat, 9 Feb 2019 22:27:50 +0100 Subject: [PATCH] cmd: Extract methods to allow import from external When methods for passwords generation and merge are extracted then external apps and scripts can use those methods without resolving to subprocess execution or injecting sys.argv. Change-Id: I99aff7852180534129fa36859075306eea776ba9 Signed-off-by: Maciej Kucia --- kolla_ansible/cmd/genpwd.py | 73 +++++++++++++++++++---------------- kolla_ansible/cmd/mergepwd.py | 25 ++++++------ 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/kolla_ansible/cmd/genpwd.py b/kolla_ansible/cmd/genpwd.py index 366964df7b..c4573e999d 100755 --- a/kolla_ansible/cmd/genpwd.py +++ b/kolla_ansible/cmd/genpwd.py @@ -54,6 +54,44 @@ def generate_RSA(bits=4096): return private_key, public_key +def genpwd(passwords_file, length, uuid_keys, ssh_keys, blank_keys, + fernet_keys, hmac_md5_keys): + with open(passwords_file, 'r') as f: + passwords = yaml.safe_load(f.read()) + + for k, v in passwords.items(): + if (k in ssh_keys and + (v is None + or v.get('public_key') is None + and v.get('private_key') is None)): + private_key, public_key = generate_RSA() + passwords[k] = { + 'private_key': private_key, + 'public_key': public_key + } + continue + if v is None: + if k in blank_keys and v is None: + continue + if k in uuid_keys: + passwords[k] = uuidutils.generate_uuid() + elif k in hmac_md5_keys: + passwords[k] = (hmac.new( + uuidutils.generate_uuid().encode(), ''.encode(), md5) + .hexdigest()) + elif k in fernet_keys: + passwords[k] = fernet.Fernet.generate_key() + else: + passwords[k] = ''.join([ + random.SystemRandom().choice( + string.ascii_letters + string.digits) + for n in range(length) + ]) + + with open(passwords_file, 'w') as f: + f.write(yaml.safe_dump(passwords, default_flow_style=False)) + + def main(): parser = argparse.ArgumentParser() parser.add_argument( @@ -91,40 +129,9 @@ def main(): # length of password length = 40 - with open(passwords_file, 'r') as f: - passwords = yaml.safe_load(f.read()) + genpwd(passwords_file, length, uuid_keys, ssh_keys, blank_keys, + fernet_keys, hmac_md5_keys) - for k, v in passwords.items(): - if (k in ssh_keys and - (v is None - or v.get('public_key') is None - and v.get('private_key') is None)): - private_key, public_key = generate_RSA() - passwords[k] = { - 'private_key': private_key, - 'public_key': public_key - } - continue - if v is None: - if k in blank_keys and v is None: - continue - if k in uuid_keys: - passwords[k] = uuidutils.generate_uuid() - elif k in hmac_md5_keys: - passwords[k] = (hmac.new( - uuidutils.generate_uuid().encode(), ''.encode(), md5) - .hexdigest()) - elif k in fernet_keys: - passwords[k] = fernet.Fernet.generate_key() - else: - passwords[k] = ''.join([ - random.SystemRandom().choice( - string.ascii_letters + string.digits) - for n in range(length) - ]) - - with open(passwords_file, 'w') as f: - f.write(yaml.safe_dump(passwords, default_flow_style=False)) if __name__ == '__main__': main() diff --git a/kolla_ansible/cmd/mergepwd.py b/kolla_ansible/cmd/mergepwd.py index ea3d128772..4c697629a0 100755 --- a/kolla_ansible/cmd/mergepwd.py +++ b/kolla_ansible/cmd/mergepwd.py @@ -16,23 +16,26 @@ import argparse import yaml +def mergepwd(old, new, final): + with open(old, "r") as old_file: + old_passwords = yaml.safe_load(old_file) + + with open(new, "r") as new_file: + new_passwords = yaml.safe_load(new_file) + + new_passwords.update(old_passwords) + + with open(final, "w") as destination: + yaml.safe_dump(new_passwords, destination, default_flow_style=False) + + def main(): parser = argparse.ArgumentParser() parser.add_argument("--old", help="old password file", required=True) parser.add_argument("--new", help="new password file", required=True) parser.add_argument("--final", help="merged password file", required=True) args = parser.parse_args() - - with open(args.old, "r") as old_file: - old_passwords = yaml.safe_load(old_file) - - with open(args.new, "r") as new_file: - new_passwords = yaml.safe_load(new_file) - - new_passwords.update(old_passwords) - - with open(args.final, "w") as destination: - yaml.safe_dump(new_passwords, destination, default_flow_style=False) + mergepwd(args.old, args.new, args.final) if __name__ == '__main__':