"""Reset only the independent FreeScan administrator, preserving all source systems.""" import argparse import getpass import json from pathlib import Path import sys ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / '.tools/python')) from bootstrap_db import META, OWNER, validate_target def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--password', help='omit to enter the new password interactively') args = parser.parse_args() password = args.password if args.password is not None else getpass.getpass('New FreeScan admin password: ') if not 8 <= len(password) <= 16: raise ValueError('Administrator password must have 8-16 characters') runtime_path = ROOT / '.local/runtime.json' runtime = json.loads(runtime_path.read_text(encoding='utf-8')) mysql = runtime['mysql'] validate_target(mysql) import bcrypt import pymysql connection = pymysql.connect(host=mysql['host'], port=mysql.get('port', 3306), user=mysql['user'], password=mysql['password'], database=mysql['database'], charset='utf8mb4') try: with connection.cursor() as cursor: cursor.execute(f'SELECT value FROM `{META}` WHERE name=%s', ('owner',)) if cursor.fetchone() != (OWNER,): raise RuntimeError('Independent database ownership check failed') cursor.execute("SELECT id FROM system_users WHERE id=1 AND username='admin' AND tenant_id=1 AND deleted=0 FOR UPDATE") if cursor.fetchone() != (1,): raise RuntimeError('Independent admin identity check failed') cursor.execute("UPDATE system_users SET password=%s WHERE id=1 AND username='admin' AND tenant_id=1 AND deleted=0", (bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode(),)) connection.commit() except BaseException: connection.rollback() raise finally: connection.close() runtime['adminPassword'] = password runtime_path.write_text(json.dumps(runtime, ensure_ascii=False, indent=2), encoding='utf-8') (ROOT / '.local/initial-admin.txt').write_text('tenant=AM FreeScan\nusername=admin\npassword=' + password + '\n', encoding='utf-8') print('Independent FreeScan admin password updated; source systems were not accessed') if __name__ == '__main__': main()