30 lines
2.0 KiB
Python
30 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate additional synthetic accounts in a COPY of gateway configuration."""
|
|
import argparse, json, os, re, secrets, subprocess
|
|
from pathlib import Path
|
|
p=argparse.ArgumentParser()
|
|
p.add_argument('--gateway-env',required=True);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')
|
|
text=Path(a.gateway_env).read_text()
|
|
if 'energy-patient-' in text: p.error('Energy accounts already exist; reuse their credentials rather than overwrite')
|
|
indexes=[int(x) for x in re.findall(r'^Authentication__Accounts__(\d+)__AccountId=',text,re.M)]
|
|
if not indexes: p.error('No installer-created authentication accounts found')
|
|
out=Path(a.output);out.mkdir(mode=0o700,parents=True,exist_ok=False)
|
|
accounts=[]; lines=[]
|
|
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,check=True)
|
|
hashed=result.stdout.strip()
|
|
if not hashed or '\n' in hashed: raise RuntimeError('Password hash tool returned unexpected output')
|
|
index=max(indexes)+1+i
|
|
for key,value in [('AccountId',account['AccountId']),('Role','Patient'),('PasswordHash',hashed)]:
|
|
lines.append(f'Authentication__Accounts__{index}__{key}='+json.dumps(value))
|
|
accounts.append(account)
|
|
for name,value in [('gateway.env',text.rstrip()+'\n'+'\n'.join(lines)+'\n'),('accounts.json',json.dumps(accounts,indent=2))]:
|
|
path=out/name;path.write_text(value);os.chmod(path,0o600)
|
|
print(f'Generated {a.count} synthetic accounts. Original gateway configuration was not changed.')
|
|
print('Review and install',out/'gateway.env','then restart the gateway. Keep accounts.json private.')
|