Publish password-TOTP WebUI and bearer MCP access with verified deployment
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Provision and activate the public password/TOTP API on the development server."""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import shlex
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
ORIGIN = 'https://discovery.ease-technologies.de:8443'
|
||||
TENANT = '11111111-1111-4111-8111-111111111111'
|
||||
|
||||
|
||||
def on_server(payload):
|
||||
import fcntl
|
||||
import time
|
||||
root = Path('/opt/rust-discovery')
|
||||
def run(*args):
|
||||
return subprocess.check_output(list(args),text=True).strip()
|
||||
def inspect_role(role):
|
||||
return json.loads(run('docker','inspect','rust-discovery-server-'+role+'-1'))[0]
|
||||
def write(path, content, mode=0o600, owner=None):
|
||||
import tempfile
|
||||
with tempfile.NamedTemporaryFile(dir=path.parent,delete=False) as stream:
|
||||
temporary=Path(stream.name)
|
||||
try:
|
||||
stream.write(content);stream.flush();os.fsync(stream.fileno())
|
||||
os.fchmod(stream.fileno(),mode)
|
||||
if owner is not None:os.fchown(stream.fileno(),owner,owner)
|
||||
os.replace(temporary,path)
|
||||
finally:temporary.unlink(missing_ok=True)
|
||||
work=root/'devstate/public-access'
|
||||
work.mkdir(mode=0o700,exist_ok=True)
|
||||
with (work/'deployment.lock').open('a') as lock:
|
||||
fcntl.flock(lock,fcntl.LOCK_EX)
|
||||
if payload['action']=='prepare':
|
||||
config=payload['accounts']
|
||||
user,=config['users']
|
||||
if str(uuid.UUID(user['principal'].removeprefix('local:'))) != user['principal'][6:]:
|
||||
raise ValueError('Invalid local principal')
|
||||
folder=root/'devstate/public-auth'
|
||||
folder.mkdir(mode=0o700,exist_ok=True)
|
||||
os.chown(folder,10001,10001);os.chmod(folder,0o700)
|
||||
path=folder/'users.json'
|
||||
encoded=json.dumps(config,separators=(',',':')).encode()
|
||||
if path.exists() and path.read_bytes()!=encoded:
|
||||
raise ValueError('Existing account configuration differs. Review before replacing it.')
|
||||
write(path,encoded,owner=10001)
|
||||
# Bootstrap membership only. No plan, approval or job is created.
|
||||
query=f"""BEGIN;
|
||||
SELECT set_config('app.tenant_id','{TENANT}',true);
|
||||
INSERT INTO memberships(tenant_id,principal,kind,permissions)
|
||||
SELECT '{TENANT}','{user['principal']}','human',array_append(array_remove(permissions,'apt:native_acceptance'),'tokens:manage')
|
||||
FROM memberships WHERE tenant_id='{TENANT}' AND principal='simulated-testprincipal-a'
|
||||
ON CONFLICT(tenant_id,principal) DO NOTHING;
|
||||
COMMIT;"""
|
||||
subprocess.run(['docker','exec','-i','rust-discovery-server-postgres-1','psql','-XqAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'],input=query,text=True,check=True,stdout=subprocess.DEVNULL)
|
||||
print(json.dumps({'accounts_prepared':True,'username':user['username'],'principal':user['principal']}))
|
||||
return
|
||||
if payload['action']=='publish':
|
||||
ca=root/'devstate/api-pki/ca.pem'
|
||||
auth=json.loads(run('curl','--noproxy','*','--silent','--show-error','--fail','--max-time','5','--cacert',str(ca),'https://localhost:18445/auth/config'))
|
||||
if auth.get('dev_login_enabled') or auth.get('oidc_configured') or not auth.get('local_login_enabled'):
|
||||
raise ValueError('Public API authentication is not ready')
|
||||
source=payload['files']
|
||||
nginx=base64.b64decode(source['server-bootstrap-ingress.conf'],validate=True)
|
||||
service=base64.b64decode(source['server-bootstrap-ingress.service'],validate=True)
|
||||
validation=nginx.decode()
|
||||
for name, path in {
|
||||
'chain.pem':'/opt/rmm-bootstrap-download/chain.pem',
|
||||
'key.pem':'/opt/rmm-bootstrap-download/domain.key',
|
||||
'gateway-ca.pem':str(root/'devstate/pki/ca.pem'),
|
||||
'api-ca.pem':str(ca),
|
||||
}.items():
|
||||
validation=validation.replace('/run/credentials/rmm-install-ingress.service/'+name,path)
|
||||
check=work/'nginx-validation.conf'
|
||||
write(check,validation.encode())
|
||||
run('/usr/sbin/nginx','-t','-c',str(check))
|
||||
targets={Path('/etc/rmm-install-ingress.conf'):nginx,
|
||||
Path('/etc/systemd/system/rmm-install-ingress.service'):service}
|
||||
backup={path:path.read_bytes() for path in targets}
|
||||
try:
|
||||
for path,raw in targets.items():write(path,raw,0o644)
|
||||
run('systemctl','daemon-reload')
|
||||
run('systemctl','restart','rmm-install-ingress.service')
|
||||
for attempt in range(20):
|
||||
try:
|
||||
verified=json.loads(run('curl','--noproxy','*','--resolve','discovery.ease-technologies.de:8443:127.0.0.1','--silent','--show-error','--fail','--max-time','5',ORIGIN+'/auth/config'))
|
||||
if verified!=auth:raise ValueError('Public authentication response differs')
|
||||
break
|
||||
except subprocess.CalledProcessError:
|
||||
if attempt==19:raise
|
||||
time.sleep(1)
|
||||
except Exception:
|
||||
for path,raw in backup.items():write(path,raw,0o644)
|
||||
run('systemctl','daemon-reload');run('systemctl','restart','rmm-install-ingress.service')
|
||||
raise
|
||||
print(json.dumps({'public_origin':ORIGIN,'local_password_totp':True,'dev_login':False,'oauth':False}))
|
||||
return
|
||||
active=inspect_role('api')
|
||||
if inspect_role('gateway')['Image'] != active['Image']:
|
||||
raise ValueError('API and gateway images differ')
|
||||
tag='rmm-public-api:'+active['Image'].split(':')[1]
|
||||
run('docker','tag',active['Image'],tag)
|
||||
write(work/'image.env',('RMM_PUBLIC_API_IMAGE='+tag+'\n').encode())
|
||||
compose=['docker','compose','--project-name','rust-discovery-server','--project-directory',str(root/'deploy'),'--env-file',str(root/'devstate/.env'),'--env-file',str(work/'image.env')]
|
||||
files=[Path(x) for x in active['Config']['Labels']['com.docker.compose.project.config_files'].split(',')]
|
||||
for file in files:
|
||||
if not file.is_file() or not file.resolve().is_relative_to(root):raise ValueError('Missing deployment file')
|
||||
compose+=['-f',str(file)]
|
||||
for name,encoded in payload['files'].items():
|
||||
dest=(root/'deploy'/name)
|
||||
if name != 'server.public-access.compose.yaml':raise ValueError('Unexpected deployment overlay')
|
||||
write(dest,base64.b64decode(encoded,validate=True),0o644)
|
||||
if dest not in files:compose+=['-f',str(dest)]
|
||||
os.environ['RMM_PUBLIC_API_IMAGE']=tag
|
||||
cfg=json.loads(run(*compose,'config','--format','json'))['services']
|
||||
public=cfg['public-api']
|
||||
if (public['environment']['RMM_PROFILE']!='production' or public['environment']['RMM_DEV_AUTH']!='false'
|
||||
or public['ports'][0]['host_ip']!='127.0.0.1' or str(public['ports'][0]['published'])!='18445'
|
||||
or any(k.startswith('RMM_DEV_') and k!='RMM_DEV_AUTH' for k in public['environment'])
|
||||
or public['user']!='10001:10001' or not public['read_only'] or cfg['postgres'].get('ports')):
|
||||
raise ValueError('Unexpected public API boundary')
|
||||
run(*compose,'up','-d','--no-deps','gateway','public-api')
|
||||
ca=root/'devstate/api-pki/ca.pem'
|
||||
for attempt in range(30):
|
||||
try:
|
||||
auth=json.loads(run('curl','--noproxy','*','--silent','--show-error','--fail','--max-time','5','--cacert',str(ca),'https://localhost:18445/auth/config'))
|
||||
if auth.get('dev_login_enabled') or auth.get('oidc_configured') or not auth.get('local_login_enabled'):
|
||||
raise ValueError('Unexpected public authentication modes')
|
||||
break
|
||||
except subprocess.CalledProcessError:
|
||||
if attempt==29:raise
|
||||
time.sleep(1)
|
||||
print(json.dumps({'public_api_running':True,'authentication':auth,'image':active['Image'],'loopback_port':18445}))
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action',choices=('prepare','activate','publish'))
|
||||
args=parser.parse_args()
|
||||
os.umask(0o077)
|
||||
payload={'action':args.action}
|
||||
if args.action=='prepare':
|
||||
path=ROOT/'credentials/public-webui-login.json'
|
||||
if path.exists():
|
||||
info=path.lstat()
|
||||
if not path.is_file() or path.is_symlink() or info.st_uid!=os.getuid() or info.st_mode & 0o077:
|
||||
raise ValueError('Unsafe bootstrap credential file')
|
||||
credentials=json.loads(path.read_bytes())
|
||||
else:
|
||||
secret=base64.b32encode(secrets.token_bytes(20)).decode().rstrip('=')
|
||||
password=secrets.token_urlsafe(32);salt=secrets.token_bytes(32)
|
||||
derived=hashlib.scrypt(password.encode(),salt=salt,n=1<<17,r=8,p=1,dklen=64,maxmem=256*1024*1024)
|
||||
account={'username':'kevin','principal':'local:'+str(uuid.uuid4()),'tenant_id':TENANT,'disabled':False,
|
||||
'password_scrypt':{'salt_hex':salt.hex(),'hash_hex':derived.hex()},'totp_secret_base32':secret}
|
||||
credentials={'origin':ORIGIN,'username':'kevin','password':password,'totp_secret_base32':secret,
|
||||
'otpauth_uri':'otpauth://totp/RustDiscovery:kevin?secret='+secret+'&issuer=RustDiscovery&algorithm=SHA1&digits=6&period=30',
|
||||
'accounts':{'schema_version':1,'users':[account]}}
|
||||
with path.open('x') as stream:json.dump(credentials,stream,indent=2)
|
||||
path.chmod(0o600)
|
||||
payload['accounts']=credentials['accounts']
|
||||
else:
|
||||
names=['server.public-access.compose.yaml'] if args.action=='activate' else ['server-bootstrap-ingress.conf','server-bootstrap-ingress.service']
|
||||
payload['files']={name:base64.b64encode((ROOT/'deploy'/name).read_bytes()).decode() for name in names}
|
||||
from dev_stage import ssh_prefix
|
||||
code=Path(__file__).read_text().rsplit("\nif __name__",1)[0]
|
||||
code+='\non_server('+repr(payload)+')\n'
|
||||
subprocess.run([*ssh_prefix('server'),shlex.join(['python3','-I','-'])],input=code,text=True,check=True)
|
||||
if args.action=='prepare':print('Bootstrap credentials stored privately at credentials/public-webui-login.json')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user