PlotDirector/Tools/BrowserAcceptance/install_identity.py

40 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Securely install an identity produced by the approved DevBrowserIdentity helper.
This stores credentials only; it does not create users, bypass MFA or access SQL.
"""
import argparse
import json
import os
from pathlib import Path
import re
import stat
def install(source):
if not str(source).startswith('/tmp/pd-dev-browser-'):
raise ValueError('Use an approved newly provisioned fixture identity')
fd=os.open(source,os.O_RDONLY|os.O_NOFOLLOW)
with os.fdopen(fd) as stream:
st=os.fstat(stream.fileno())
if st.st_uid!=os.getuid() or not stat.S_ISREG(st.st_mode) or stat.S_IMODE(st.st_mode)!=0o600 or st.st_nlink!=1 or st.st_size>16384:
raise ValueError('Private provisioning file required')
identity=json.load(stream)
if identity['Database']!='PlotDirector_Development' or not re.fullmatch(r'pwa-browser-[a-f0-9]{32}@example.invalid',identity['Email']) or not isinstance(identity['UserID'],int) or identity['UserID']<1 or len(identity['Password'])<40:
raise ValueError('Dedicated Development provisioning identity required')
identity.update(Origin='https://dev.plotdirector.com',Worker='application',Purpose='CYJ Development browser acceptance')
directory=Path.home()/'.config/cyj/credentials'
directory.mkdir(mode=0o700,parents=True,exist_ok=True)
if directory.is_symlink() or directory.stat().st_uid!=os.getuid() or stat.S_IMODE(directory.stat().st_mode)!=0o700:
raise ValueError('Private credential directory required')
target=directory/'application-browser.json'
fd=os.open(target,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as stream:
json.dump(identity,stream);stream.flush();os.fsync(stream.fileno())
source.unlink()
print('Dedicated Development browser identity installed privately; provisioning file removed. No credential values printed.')
if __name__=='__main__':
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('source',type=Path);args=parser.parse_args()
try:install(args.source)
except (ValueError,OSError,KeyError,TypeError):raise SystemExit('Identity installation refused; inspect private provisioning and existing identity without printing credentials.')