|
| 1 | +""" |
| 2 | +Create the initial admin user and organization for ExaFS. |
| 3 | +
|
| 4 | +Run this once after 'python db-init.py' to set up the first administrator |
| 5 | +and their organization. Without at least one admin user, the application |
| 6 | +cannot be managed through the web interface. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python create-admin.py |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +from os import environ |
| 14 | + |
| 15 | +from flowapp import create_app, db |
| 16 | +from flowapp.models import Organization, Role, User |
| 17 | + |
| 18 | +import config |
| 19 | + |
| 20 | + |
| 21 | +def prompt(label, required=True, default=None): |
| 22 | + """Prompt for input, optionally with a default value.""" |
| 23 | + if default: |
| 24 | + display = f"{label} [{default}]: " |
| 25 | + else: |
| 26 | + display = f"{label}: " |
| 27 | + |
| 28 | + while True: |
| 29 | + value = input(display).strip() |
| 30 | + if not value and default: |
| 31 | + return default |
| 32 | + if value: |
| 33 | + return value |
| 34 | + if not required: |
| 35 | + return "" |
| 36 | + print(f" {label} is required.") |
| 37 | + |
| 38 | + |
| 39 | +def create_admin(): |
| 40 | + exafs_env = environ.get("EXAFS_ENV", "Production").lower() |
| 41 | + if exafs_env in ("devel", "development"): |
| 42 | + app = create_app(config.DevelopmentConfig) |
| 43 | + else: |
| 44 | + app = create_app(config.ProductionConfig) |
| 45 | + |
| 46 | + db.init_app(app) |
| 47 | + |
| 48 | + with app.app_context(): |
| 49 | + # Verify migrations have been run |
| 50 | + admin_role = Role.query.filter_by(name="admin").first() |
| 51 | + if not admin_role: |
| 52 | + print("Error: roles not found in database.") |
| 53 | + print("Please run 'python db-init.py' first.") |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + print() |
| 57 | + print("ExaFS initial admin setup") |
| 58 | + print("=" * 40) |
| 59 | + |
| 60 | + # --- User --- |
| 61 | + print() |
| 62 | + print("Admin user") |
| 63 | + print("-" * 20) |
| 64 | + print("UUID is the unique identifier used for authentication.") |
| 65 | + print("For SSO (Shibboleth), this is typically the eppn attribute.") |
| 66 | + print("For local auth, use any unique string (e.g. email address).") |
| 67 | + print() |
| 68 | + |
| 69 | + while True: |
| 70 | + uuid = prompt("UUID (e.g. user@example.edu)") |
| 71 | + existing = User.query.filter_by(uuid=uuid).first() |
| 72 | + if existing: |
| 73 | + print(f" A user with UUID '{uuid}' already exists.") |
| 74 | + overwrite = input(" Update this user's roles and org? (yes/no): ").strip().lower() |
| 75 | + if overwrite == "yes": |
| 76 | + user = existing |
| 77 | + break |
| 78 | + continue |
| 79 | + user = None |
| 80 | + break |
| 81 | + |
| 82 | + name = prompt("Full name", required=False) |
| 83 | + email = prompt("Email", default=uuid if "@" in uuid else None) |
| 84 | + phone = prompt("Phone", required=False) |
| 85 | + |
| 86 | + # --- Organization --- |
| 87 | + print() |
| 88 | + print("Organization") |
| 89 | + print("-" * 20) |
| 90 | + print("Address ranges (arange) are whitespace-separated CIDR prefixes.") |
| 91 | + print("Example: 192.0.2.0/24 2001:db8::/32") |
| 92 | + print() |
| 93 | + |
| 94 | + orgs = Organization.query.all() |
| 95 | + if orgs: |
| 96 | + print("Existing organizations:") |
| 97 | + for org in orgs: |
| 98 | + print(f" [{org.id}] {org.name}") |
| 99 | + print() |
| 100 | + choice = input("Use existing organization ID, or press Enter to create new: ").strip() |
| 101 | + if choice.isdigit(): |
| 102 | + org = Organization.query.get(int(choice)) |
| 103 | + if not org: |
| 104 | + print(f" Organization {choice} not found, creating new.") |
| 105 | + org = None |
| 106 | + else: |
| 107 | + org = None |
| 108 | + else: |
| 109 | + org = None |
| 110 | + |
| 111 | + if org is None: |
| 112 | + org_name = prompt("Organization name") |
| 113 | + org_arange = prompt("Address ranges (CIDR, space-separated)") |
| 114 | + org = Organization(name=org_name, arange=org_arange) |
| 115 | + db.session.add(org) |
| 116 | + db.session.flush() # get org.id before commit |
| 117 | + print(f" Created organization: {org.name}") |
| 118 | + |
| 119 | + # --- Confirm --- |
| 120 | + print() |
| 121 | + print("Summary") |
| 122 | + print("=" * 40) |
| 123 | + print(f" UUID: {uuid}") |
| 124 | + print(f" Name: {name or '(not set)'}") |
| 125 | + print(f" Email: {email or '(not set)'}") |
| 126 | + print(f" Phone: {phone or '(not set)'}") |
| 127 | + print(f" Role: admin") |
| 128 | + print(f" Organization: {org.name}") |
| 129 | + print() |
| 130 | + |
| 131 | + confirm = input("Create admin user? (yes/no): ").strip().lower() |
| 132 | + if confirm != "yes": |
| 133 | + print("Aborted.") |
| 134 | + db.session.rollback() |
| 135 | + sys.exit(0) |
| 136 | + |
| 137 | + # --- Create or update user --- |
| 138 | + if user is None: |
| 139 | + user = User(uuid=uuid, name=name or None, email=email or None, phone=phone or None) |
| 140 | + db.session.add(user) |
| 141 | + else: |
| 142 | + if name: |
| 143 | + user.name = name |
| 144 | + if email: |
| 145 | + user.email = email |
| 146 | + if phone: |
| 147 | + user.phone = phone |
| 148 | + |
| 149 | + # Assign admin role (avoid duplicates) |
| 150 | + if not user.role.filter_by(name="admin").first(): |
| 151 | + user.role.append(admin_role) |
| 152 | + |
| 153 | + # Assign organization (avoid duplicates) |
| 154 | + if not user.organization.filter_by(id=org.id).first(): |
| 155 | + user.organization.append(org) |
| 156 | + |
| 157 | + db.session.commit() |
| 158 | + |
| 159 | + print() |
| 160 | + print(f"Admin user '{uuid}' created successfully.") |
| 161 | + print(f"Organization: {org.name}") |
| 162 | + print() |
| 163 | + print("You can now log in and manage ExaFS through the web interface.") |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + create_admin() |
0 commit comments