Publish password-TOTP WebUI and bearer MCP access with verified deployment

This commit is contained in:
2026-11-17 20:29:07 +01:00
parent 9fa0fb81ff
commit 5a93bc146f
18 changed files with 441 additions and 17 deletions
+13 -5
View File
@@ -91,6 +91,8 @@ def activate(payload):
with (work/'deployment.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX)
active = inspect('api')
queried_public = subprocess.run(['docker','inspect','rust-discovery-server-public-api-1'], capture_output=True, text=True)
public_api = json.loads(queried_public.stdout)[0] if queried_public.returncode == 0 else None
dist = next(Path(m['Source']) for m in active['Mounts'] if m['Destination'] == '/web')
if dist != root / 'web/dist' or active['Config']['Labels']['com.docker.compose.project'] != 'rust-discovery-server':
raise RuntimeError('Unexpected development WebUI deployment')
@@ -111,13 +113,18 @@ def activate(payload):
raise RuntimeError('Server build digest mismatch')
if inspect('gateway')['Image'] != active['Image']:
raise RuntimeError('API and Gateway have different base images')
labels = active['Config']['Labels']
labels = (public_api or active)['Config']['Labels']
if labels['com.docker.compose.project'] != 'rust-discovery-server':
raise RuntimeError('Unexpected compose project')
files = [Path(x) for x in labels['com.docker.compose.project.config_files'].split(',')]
base_files = [f for f in files if not (f.parent.parent == work and f.name == 'image.compose.yaml')]
compose = ['docker','compose','--project-name','rust-discovery-server','--project-directory',str(root/'deploy'),
'--env-file',str(root/'devstate/.env')]
public_env = root / 'devstate/public-access/image.env'
if public_api is not None:
if not public_env.is_file():
raise ValueError('Missing public API image configuration')
compose.extend(['--env-file', str(public_env)])
for path in base_files:
if not path.is_file() or not path.resolve().is_relative_to(root):
raise ValueError('Missing deployment overlay')
@@ -139,7 +146,8 @@ def activate(payload):
if subprocess.run(['docker','image','inspect',tag],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode:
run('docker','build','--network=none','--pull=false','-t',tag,str(context))
overlay=role_dir/'image.compose.yaml'
write_atomic(overlay,('services:\n'+''.join(f' {r}:\n image: {tag}\n pull_policy: never\n' for r in ('migrate','api','gateway'))).encode())
deployed_roles = ['api', 'gateway'] + (['public-api'] if public_api is not None else [])
write_atomic(overlay,('services:\n'+''.join(f' {r}:\n image: {tag}\n pull_policy: never\n' for r in ['migrate', *deployed_roles])).encode())
for path, content in payload['overlays'].items():
file = (root / 'deploy') / path
if not re.fullmatch(r'server\.[\w.-]+\.compose\.yaml', path) or file.is_symlink():
@@ -149,7 +157,7 @@ def activate(payload):
compose.extend(['-f',str(file)])
compose.extend(['-f',str(overlay)])
config=json.loads(run(*compose,'config','--format','json'))['services']
for role,port in [('api','18443'),('gateway','18444')]:
for role,port in [('api','18443'),('gateway','18444')] + ([('public-api','18445')] if public_api is not None else []):
svc=config[role]
if (svc['ports'][0]['host_ip']!='127.0.0.1' or str(svc['ports'][0]['published'])!=port or len(svc['ports'])!=1
or svc['user']!='10001:10001' or not svc['read_only']):
@@ -161,8 +169,8 @@ def activate(payload):
recorded = dict(line.split('|') for line in run(*sql,"SELECT version,encode(checksum,'hex') FROM _sqlx_migrations WHERE success").splitlines())
if recorded != payload['migrations']:
raise RuntimeError('Applied migrations differ from the build revision')
run(*compose,'up','-d','--no-deps','--no-build','--pull','never','api','gateway')
for role,host,port,ca in [('api','localhost','18443','api-pki'),('gateway','discovery.ease-technologies.de','18444','pki')]:
run(*compose,'up','-d','--no-deps','--no-build','--pull','never',*deployed_roles)
for role,host,port,ca in [('api','localhost','18443','api-pki'),('gateway','discovery.ease-technologies.de','18444','pki')] + ([('public-api','localhost','18445','api-pki')] if public_api is not None else []):
for _ in range(25):
res=subprocess.run(['curl','--noproxy','*','--resolve',f'{host}:{port}:127.0.0.1','--cacert',str(root/f'devstate/{ca}/ca.pem'),
'--max-time','2','--silent','--fail',f'https://{host}:{port}/healthz'],capture_output=True)
+179
View File
@@ -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()