"""Executed inside the UI container of ci/smoke_image.sh; no external networking.""" import http.cookiejar import json import os from pathlib import Path import sys import time import urllib.error import urllib.request from urllib.parse import urlsplit origin = os.environ['UI_ORIGIN'] authority = urlsplit(origin).netloc opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())) csrf = '' def request(path, data=None): headers = {'Host': authority} if data is not None: headers.update({'Origin': origin, 'Content-Type': 'application/json', 'X-Homegate-CSRF': csrf}) payload = None if data is None else json.dumps(data).encode() with opener.open(urllib.request.Request('http://127.0.0.1:8081' + path, payload, headers), timeout=2) as r: body = r.read() return json.loads(body) if 'application/json' in r.headers.get('Content-Type', '') else body assert os.getuid() == 1000 assert not Path('/run/secrets/npm_password').exists() assert not Path('/run/secrets/pihole_password').exists() assert not Path('/data').exists() assert Path('/app/VERSION').read_text().strip() for attempt in range(30): try: session = request('/api/session') break except urllib.error.URLError: if attempt == 29: raise time.sleep(1) assert session['authenticated'] is False try: request('/api/state') except urllib.error.HTTPError as error: assert error.code == 401 else: raise AssertionError('Unauthenticated state is exposed') csrf = request('/api/login', {'password': Path('/run/secrets/ui_password').read_text()})['csrf'] state = request('/api/state') if sys.argv[-1] == 'write': assert state['config']['services'] == [] state['config']['services'] = [{'hostname': 'smoke.home', 'display_name': 'Сохранённый сервис', 'upstream': 'http://192.168.42.137:8000', 'ssh': {'alias': 'smoke', 'user': 'admin'}}] state = request('/api/config', {'config': state['config'], 'revision': state['revision']}) assert state['config']['proxy_ip'] == '192.168.42.11' assert state['config']['domain_zones'] == ['home'] assert state['config']['services'][0]['display_name'] == 'Сохранённый сервис' assert b'Host smoke smoke.home' in request('/api/ssh-config') assert len(list(Path('/config/backups').glob('*.yaml'))) >= 1 print('UI: non-root login, secret isolation, saved configuration and SSH export verified')