Merge "cmd: Extract methods to allow import from external"

This commit is contained in:
Zuul 2019-03-21 09:03:57 +00:00 committed by Gerrit Code Review
commit 6d54496314
2 changed files with 54 additions and 44 deletions

View File

@ -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()

View File

@ -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__':