demskeq8 | ef65699 | 2024-03-20 08:50:14 +0100 | [diff] [blame] | 1 | import csv |
| 2 | import json |
| 3 | from argparse import ArgumentParser |
| 4 | |
| 5 | from jinja2 import Template |
| 6 | |
| 7 | |
| 8 | class UserCreator: |
| 9 | template_str = """ |
| 10 | { |
| 11 | "users": [ |
| 12 | {% for user in users %} |
| 13 | { |
| 14 | "firstName": "{{ user.firstName }}", |
| 15 | "lastName": "{{ user.lastName }}", |
| 16 | "email": "{{ user.email }}", |
| 17 | "enabled": "{{ user.enabled }}", |
| 18 | "username": "{{ user.username }}", |
| 19 | "credentials": [ |
| 20 | { |
| 21 | "type": "password", |
| 22 | "value": "{{ user.password }}", |
| 23 | {% if force_pwd_change is false %} |
| 24 | "temporary": "{{ user.force_pwd_change }}" |
| 25 | {% else %} |
| 26 | "temporary": "true" |
| 27 | {% endif %} |
| 28 | } |
| 29 | ], |
| 30 | "requiredActions": [ |
| 31 | "UPDATE_PASSWORD" |
| 32 | ] |
| 33 | }{% if not loop.last %},{% endif %} |
| 34 | {% endfor %} |
| 35 | ], |
| 36 | "grants": [ |
| 37 | {% for user in users %} |
| 38 | { |
| 39 | "username": "{{ user.username }}", |
| 40 | "role": "{{ user.role }}" |
| 41 | }{% if not loop.last %},{% endif %} |
| 42 | {% endfor %} |
| 43 | ] |
| 44 | } |
| 45 | """ |
| 46 | |
| 47 | def __init__(self, csv_file: str): |
| 48 | self.csv_file_path: str = csv_file |
| 49 | |
| 50 | def get_users_from_csv(self) -> list[dict]: |
| 51 | """ |
| 52 | Get the users from the CSV file |
| 53 | @return: list of users |
| 54 | """ |
| 55 | users: list[dict] = [] |
| 56 | with open(self.csv_file_path, "r") as file: |
| 57 | dict_reader: csv.DictReader = csv.DictReader(file) |
| 58 | for row in dict_reader: |
| 59 | users.append(row) |
| 60 | return users |
| 61 | |
| 62 | def create_users_json(self, users: list[dict], output_json_path: str, force_pwd_change: bool ) -> None: |
| 63 | """ |
| 64 | Create the users JSON from the users list. Uses Jinja2 template to create the JSON. |
| 65 | @param users: list of users to create the JSON |
| 66 | @param output_json_path: path to the output JSON file |
| 67 | @return: JSON string |
| 68 | """ |
| 69 | if force_pwd_change is True: |
| 70 | print("Enforce password change for all users!") |
| 71 | template = Template(self.template_str) |
| 72 | users_json: str = template.render(users=users, force_pwd_change=force_pwd_change) |
| 73 | with open(output_json_path, 'w') as f: |
| 74 | json.dump(json.loads(users_json), f, indent=4) |
| 75 | print(f"Users JSON file created at {output_json_path}") |
| 76 | |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | parser = ArgumentParser(description="Create users JSON file from CSV file") |
| 80 | parser.add_argument("csv_file_path", type=str, help="Path to the CSV file containing the users data") |
| 81 | parser.add_argument("--output", "-o", type=str, required=False, default="authentication.json", |
| 82 | help="Path to the output JSON file e.g. authentication.json") |
| 83 | parser.add_argument("--force-pwd-change","-f", action='store_true', help="Enforce password change for all users, overwrites value in user data" ) |
| 84 | |
| 85 | args = parser.parse_args() |
| 86 | user_creator = UserCreator(args.csv_file_path) |
| 87 | user_list = user_creator.get_users_from_csv() |
| 88 | user_creator.create_users_json(user_list, args.output, args.force_pwd_change) |