Created
June 14, 2019 14:18
-
-
Save aikchar/7840df23276e6e516a1a113f2f337391 to your computer and use it in GitHub Desktop.
Candidate interview script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
--- | |
jeremy: jeremy-authorized-key | |
marie: maries-authorized-key | |
''' | |
''' | |
--- | |
users: | |
jeremy: | |
- group1 | |
- group2 | |
marie: | |
- group2 | |
host_groups: | |
group1: | |
- host1.example.com | |
- host2.example.com | |
group2: | |
- host2.example.com | |
- host3.example.com | |
''' | |
import yaml | |
def run(authorized_keys_file, user_groups_file): | |
with open(authorized_keys_file) as fd: | |
authorized_keys = yaml.safeload(fd) | |
with open(user_groups_file) as fd: | |
user_groups = yaml.safeload(fd) | |
host_keys = dict() | |
for user, groups in user_groups["users"].items(): | |
try: | |
user_key = authorized_keys[user] | |
except KeyError: | |
print("Error message") | |
continue | |
for group in groups: | |
try: | |
hosts = user_groups["host_groups"][group] | |
except KeyError: | |
# TODO | |
raise | |
else: | |
for host in hosts: | |
try: | |
h = host_keys[host] | |
except KeyError: | |
host_keys[host] = [{"user": user, "key": user_key}] | |
else: | |
for item in h: | |
if user == item["user"]: | |
break | |
else: | |
h.append({"user": user, "key": user_key}) | |
host_keys[host] = h | |
# h.append(user_key) | |
# host_keys[host] = h | |
# host1.example.com: [{jeremy: jeremy-authorized-key}, ...] | |
# host2.example.com: [{jeremy: jeremy-authorized-key}, {jeremy: jeremy-authorized-key}, ...] | |
# host_keys looks like | |
# host1.example.com: [jeremy-authorized-key] | |
# host2.example.com: [jeremy-authorized-key, maries-authorized-key] | |
# host3.example.com: [jeremy-authorized-key, maries-authorized-key] | |
if __name__ == "__main__": | |
import argparse | |
# TODO | |
run(authorized_keys_file=args.authorized_keys, user_groups_file=args.user_groups) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment