Skip to content

Commit

Permalink
cmd: Extract methods to allow import from external
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
MaciejKucia committed Mar 10, 2019
1 parent f637d13 commit 89e91b6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
73 changes: 40 additions & 33 deletions kolla_ansible/cmd/genpwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -91,40 +129,9 @@ def main():
# length of password
length = 40

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)
])
genpwd(passwords_file, length, uuid_keys, ssh_keys, blank_keys,
fernet_keys, hmac_md5_keys)

with open(passwords_file, 'w') as f:
f.write(yaml.safe_dump(passwords, default_flow_style=False))

if __name__ == '__main__':
main()
23 changes: 13 additions & 10 deletions kolla_ansible/cmd/mergepwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@
import yaml


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:
def mergepwd(old, new, final):
with open(old, "r") as old_file:
old_passwords = yaml.safe_load(old_file)

with open(args.new, "r") as new_file:
with open(new, "r") as new_file:
new_passwords = yaml.safe_load(new_file)

new_passwords.update(old_passwords)

with open(args.final, "w") as destination:
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()
mergepwd(args.old, args.new, args.final)


if __name__ == '__main__':
main()

0 comments on commit 89e91b6

Please sign in to comment.