Last active
February 10, 2022 06:46
-
-
Save cynici/6c425bcac436fd52f6b33a64a4ea80be to your computer and use it in GitHub Desktop.
DEVOPS-6911 Convert ipsec.secrets into Chef data bag per-user JSON files
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
#!/usr/bin/env python3 | |
description = '''Convert vpn-{uat,prod}.mrdcourier.com:/etc/ipsec.secrets | |
into Chef data bag items, one user per item. | |
''' | |
import json | |
import os | |
import re | |
import sys | |
import logging | |
import argparse | |
from typing import List, NamedTuple, Dict | |
import pprint | |
def load_file(filepath: str, prefix: str, result: Dict) -> None: | |
'''Read input file and load into result''' | |
grep_jira = re.compile(r'^#\s*(DEVOPS-\d+).*') | |
grep_credential = re.compile(r'@(\S+)\s+:\s+EAP\s+"([^"]+)"') | |
grep_comment = re.compile(r"^\s*#") | |
with open(filepath) as fh: | |
lines = fh.readlines() | |
#print(len(lines)) | |
for i, line in enumerate(lines): | |
if cred := grep_credential.search(line): | |
user, password = cred.groups() | |
disabled = grep_comment.search(line) is not None | |
if jira := grep_jira.search(lines[i-1]): | |
jira = jira.group(1) | |
else: | |
jira = None | |
if user not in result: | |
result[user] = {} | |
if prefix in result[user]: | |
print(f'Duplicate {prefix=} {user=} {i=} {line=}', file=sys.stderr) | |
result[user][prefix] = dict(disabled=disabled, password=password) | |
if jira: | |
result[user][prefix]['jira'] = jira | |
def merge_files_to_dict(args) -> None: | |
users = {} | |
load_file(args.uat_file, 'log_uat', users) | |
load_file(args.prod_file, 'log_prod', users) | |
#pprint.pprint(users, depth=3) | |
#print(json.dumps(users)) | |
for user, d in users.items(): | |
json_file = os.path.join(args.databag_dir, f'{user}.json') | |
with open(json_file, 'w') as fh: | |
databag = dict(id=user) | |
if 'log_uat' in d: | |
databag['log_uat'] = d['log_uat'] | |
if 'log_prod' in d: | |
databag['log_prod'] = d['log_prod'] | |
fh.write(json.dumps(databag, indent=2, sort_keys=True)+'\n') | |
def file_path(string: str) -> str: | |
if os.path.isfile(string): | |
return string | |
else: | |
raise argparse.ArgumentTypeError(f'{string} is not an existing file') | |
def dir_path(string: str) -> str: | |
if os.path.isdir(string): | |
return string | |
else: | |
raise argparse.ArgumentTypeError(f'{string} is not an existing directory') | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description=description, | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
) | |
parser.add_argument('uat_file', type=file_path, | |
metavar='uat_file', help='UAT ipsec.secrets') | |
parser.add_argument('prod_file', type=file_path, | |
metavar='prod_file', help='production ipsec.secrets') | |
parser.add_argument('databag_dir', type=dir_path, | |
metavar='databag_dir', help='Directory to save output user data bags') | |
args = parser.parse_args() | |
merge_files_to_dict(args) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output JSON file
I have decided to capture commented user accounts as well as JIRA ticket references useful for auditing. So, the cookbook logic has to take into account disabled.