#!/usr/bin/env python3 """Generate synthetic accounts in a COPY of Architecture B's protected JSON config.""" import argparse, json, os, re, secrets, subprocess from pathlib import Path p=argparse.ArgumentParser() p.add_argument('--configuration',required=True,help='Installed B appsettings.json; never modified') p.add_argument('--output',required=True) p.add_argument('--count',type=int,default=100);p.add_argument('--dotnet',default='dotnet') p.add_argument('--hash-tool',default=str(Path(__file__).parent/'hash-tool/PasswordHash.dll')) a=p.parse_args() if not 1<=a.count<=10000: p.error('count must be 1..10000') config=json.loads(Path(a.configuration).read_text()) # The native installer emits colon-delimited keys; hand-written examples use nested JSON. flat_indices=[int(m.group(1)) for key in config if (m:=re.fullmatch(r'Authentication:Accounts:(\d+):AccountId',key,re.I))] existing=config.get('Authentication',{}).get('Accounts') if flat_indices and existing is not None: p.error('Mixed flat/nested account configuration is ambiguous') flat=bool(flat_indices) if flat: existing=[{'AccountId':v} for k,v in config.items() if re.fullmatch(r'Authentication:Accounts:\d+:AccountId',k,re.I)] if not isinstance(existing,list) or not existing: p.error('No installer-created Authentication:Accounts found') if any(x.get('AccountId','').startswith('energy-patient-') for x in existing): p.error('Energy accounts already exist; reuse their credentials rather than overwrite') next_index=max(flat_indices)+1 if flat else len(existing) out=Path(a.output);out.mkdir(mode=0o700,parents=True,exist_ok=False) accounts=[] for i in range(a.count): account={'AccountId':f'energy-patient-{i+1:04d}','Role':'Patient','Password':secrets.token_hex(24)} result=subprocess.run([a.dotnet,a.hash_tool],input=account['Password']+'\n',text=True,capture_output=True) if result.returncode: raise SystemExit('Password hash tool failed; no configuration installed') hashed=result.stdout.strip() if not hashed or '\n' in hashed: raise SystemExit('Password hash tool returned unexpected output') entry={'AccountId':account['AccountId'],'Role':account['Role'],'PasswordHash':hashed} if flat: for key,value in entry.items(): config[f'Authentication:Accounts:{next_index+i}:{key}']=value else: existing.append(entry) accounts.append(account) for name,value in [('appsettings.json',config),('accounts.json',accounts)]: path=out/name fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600) with os.fdopen(fd,'w') as f: json.dump(value,f,indent=2); f.write('\n') print(f'Generated {a.count} synthetic accounts. Original configuration was not changed.') print('Review and install',out/'appsettings.json','with root:healthapp-b ownership and mode 640, then restart healthapp-b.') print('Keep accounts.json private. No database or application configuration was installed automatically.')