Initial Homegate 0.4.0 with web UI, Compose and Gitea Actions
This commit is contained in:
53
ci/check_compose.py
Normal file
53
ci/check_compose.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Validate both deployment models without a running Docker daemon or real credentials."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import yaml
|
||||
import homegate as h
|
||||
|
||||
|
||||
def main():
|
||||
env = dict(os.environ, HOMEGATE_IMAGE='registry.example/homegate/homegate:0.4.0',
|
||||
HOMEGATE_LAN_IP='192.168.0.10', HOMEGATE_PROXY_IP='192.168.0.10',
|
||||
HOMEGATE_ALLOWED_NETWORKS='[192.168.0.0/24]', HOMEGATE_ZONES='[home, home.arpa]',
|
||||
HOMEGATE_DNS_MODE='manual', UI_ORIGIN='http://192.168.0.10:8098',
|
||||
NPM_URL='http://192.168.0.10:81', NPM_IDENTITY='test@example.invalid',
|
||||
UI_PASSWORD='test-only-panel-password', NPM_PASSWORD='test-only-npm',
|
||||
PIHOLE_PASSWORD='not-used', PIHOLE_URL='', UI_PORT='8098', SSH_PORT='8099')
|
||||
model = json.loads(subprocess.check_output(
|
||||
['docker', 'compose', '--env-file', os.devnull, '-f', 'compose.registry.yaml',
|
||||
'config', '--format', 'json'], env=env))
|
||||
assert set(model['services']) == {'initialize', 'homegate', 'ssh-export', 'ui'}
|
||||
for name, service in model['services'].items():
|
||||
assert 'build' not in service, name
|
||||
assert service['image'] == env['HOMEGATE_IMAGE']
|
||||
assert all(v['type'] == 'volume' for v in service.get('volumes', [])), name
|
||||
if name != 'initialize':
|
||||
assert service['user'] == '1000:1000'
|
||||
assert service['depends_on']['initialize']['condition'] == 'service_completed_successfully'
|
||||
for port in service.get('ports', []):
|
||||
assert port['host_ip'] == env['HOMEGATE_LAN_IP']
|
||||
assert model['services']['initialize']['network_mode'] == 'none'
|
||||
assert not model['services']['initialize'].get('secrets')
|
||||
assert not model['services']['ssh-export'].get('secrets')
|
||||
assert [v['source'] for v in model['services']['ui']['secrets']] == ['ui_password']
|
||||
for name in ('ui', 'ssh-export'):
|
||||
assert all(v['target'] != '/data' for v in model['services'][name]['volumes'])
|
||||
assert set(model['secrets']) == {'ui_password', 'npm_password', 'pihole_password'}
|
||||
assert all('environment' in s and 'file' not in s for s in model['secrets'].values())
|
||||
seed = h.validate_config(yaml.safe_load(model['configs']['initial-config']['content']))
|
||||
assert seed['services'] == [] and seed['ssh_hosts'] == []
|
||||
subprocess.run(['docker', 'compose', '--env-file', '.env.example', '-f', 'compose.yaml',
|
||||
'config', '--quiet'], check=True, env=env)
|
||||
# The one-file deployment cannot accidentally fall back to all host interfaces.
|
||||
del env['HOMEGATE_LAN_IP']
|
||||
missing = subprocess.run(['docker', 'compose', '--env-file', os.devnull,
|
||||
'-f', 'compose.registry.yaml', 'config', '--quiet'],
|
||||
env=env, capture_output=True)
|
||||
assert missing.returncode != 0
|
||||
print('Build and registry Compose models validated; isolation and first-run defaults verified.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
49
ci/image_metadata.py
Normal file
49
ci/image_metadata.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""Validate release identity before registry credentials are used."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
|
||||
VERSION_PATTERN = r'(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[0-9A-Za-z][0-9A-Za-z.-]*)?'
|
||||
COMPONENT = r'[a-z0-9]+(?:(?:[._]|__|-+)[a-z0-9]+)*'
|
||||
|
||||
|
||||
def metadata(server_url, repository, ref, revision, version_file, registry_override=''):
|
||||
server = urlsplit(server_url)
|
||||
if server.scheme not in ('http', 'https') or not server.hostname or server.username or server.query or server.fragment:
|
||||
raise ValueError('Invalid Gitea server URL')
|
||||
registry = (registry_override or server.netloc).lower()
|
||||
parsed = urlsplit('https://' + registry)
|
||||
if (not re.fullmatch(r'[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?(?::[0-9]{1,5})?', registry)
|
||||
or parsed.port is not None and not 1 <= parsed.port <= 65535):
|
||||
raise ValueError('REGISTRY must be a hostname[:port], without scheme or path')
|
||||
if '.' not in registry and ':' not in registry and registry != 'localhost':
|
||||
raise ValueError('Use a registry FQDN or explicit port; a bare name is a Docker Hub namespace')
|
||||
repository = repository.lower()
|
||||
if not re.fullmatch(COMPONENT + '/' + COMPONENT, repository):
|
||||
raise ValueError('Repository must be owner/name with a Docker-compatible lowercase name')
|
||||
if not ref.startswith('refs/tags/v'):
|
||||
raise ValueError('Images are published only for vVERSION tags')
|
||||
version = ref.removeprefix('refs/tags/v')
|
||||
if not re.fullmatch(VERSION_PATTERN, version) or len(version) > 100:
|
||||
raise ValueError('Use a tag such as v0.4.0 or v0.4.0-rc.1')
|
||||
if version != version_file.strip():
|
||||
raise ValueError('Release tag must match VERSION in the checked-out commit')
|
||||
if not re.fullmatch(r'[a-fA-F0-9]{40}|[a-fA-F0-9]{64}', revision):
|
||||
raise ValueError('Invalid commit SHA')
|
||||
return {'registry': registry, 'image': registry + '/' + repository,
|
||||
'version': version, 'revision': revision.lower(),
|
||||
'source': server_url.rstrip('/') + '/' + repository}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
values = metadata(os.environ['CI_SERVER_URL'], os.environ['CI_REPOSITORY'],
|
||||
os.environ['CI_REF'], os.environ['CI_REVISION'],
|
||||
Path('VERSION').read_text(), os.environ.get('REGISTRY', ''))
|
||||
with open(os.environ['GITHUB_OUTPUT'], 'a') as output:
|
||||
for key, value in values.items():
|
||||
if '\n' in value or '\r' in value:
|
||||
raise ValueError('Invalid multiline output')
|
||||
output.write(f'{key}={value}\n')
|
||||
print('Validated release:', values['image'] + ':' + values['version'])
|
||||
56
ci/smoke_image.sh
Executable file
56
ci/smoke_image.sh
Executable file
@@ -0,0 +1,56 @@
|
||||
#!/bin/sh
|
||||
# Disposable CI deployment. No host ports and no access to NPM/Pi-hole.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${1:?Pass a locally built image}"
|
||||
export HOMEGATE_IMAGE=$1
|
||||
export HOMEGATE_LAN_IP=192.168.42.10
|
||||
export HOMEGATE_PROXY_IP=192.168.42.11
|
||||
export HOMEGATE_ALLOWED_NETWORKS='[192.168.42.0/24]'
|
||||
export HOMEGATE_ZONES='[home]'
|
||||
export HOMEGATE_DNS_MODE=manual
|
||||
export UI_ORIGIN=http://192.168.42.10:8098
|
||||
export NPM_URL=http://192.168.42.11:81
|
||||
export NPM_IDENTITY=smoke@example.invalid
|
||||
export PIHOLE_URL=
|
||||
export API_CA_BUNDLE=
|
||||
export UI_PASSWORD=disposable-smoke-password
|
||||
export NPM_PASSWORD=disposable-smoke-npm
|
||||
export PIHOLE_PASSWORD=not-used
|
||||
smoke_dir=$(mktemp -d)
|
||||
project="homegate-smoke-$(python3 -c 'import secrets; print(secrets.token_hex(6))')"
|
||||
cat > "$smoke_dir/override.yaml" <<'YAML'
|
||||
services:
|
||||
ui:
|
||||
ports: !reset []
|
||||
network_mode: none
|
||||
ssh-export:
|
||||
ports: !reset []
|
||||
network_mode: none
|
||||
homegate:
|
||||
network_mode: none
|
||||
YAML
|
||||
compose() {
|
||||
docker compose --env-file /dev/null -p "$project" -f compose.registry.yaml -f "$smoke_dir/override.yaml" "$@"
|
||||
}
|
||||
cleanup() {
|
||||
compose down --volumes --remove-orphans >/dev/null 2>&1 || true
|
||||
rm -rf -- "$smoke_dir"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 1' HUP INT TERM
|
||||
compose up -d --wait --wait-timeout 60 --pull never ui ssh-export
|
||||
compose run --rm --no-deps homegate validate
|
||||
compose exec -T ui python - write < ci/smoke_ui.py
|
||||
compose exec -T ssh-export python -c "from pathlib import Path; import os,urllib.request; assert os.getuid()==1000; assert not Path('/run/secrets/ui_password').exists(); assert not Path('/data').exists(); urllib.request.urlopen('http://127.0.0.1:8080/healthz',timeout=3).read()"
|
||||
ca_check="from pathlib import Path; import hashlib,os,homegate as h; assert os.getuid()==1000; c=h.load_config('/config/services.yaml'); h.Authority(Path('/data'),c['domain_zones']); print(hashlib.sha256(Path('/data/root-ca.crt').read_bytes()).hexdigest())"
|
||||
ca_before=$(compose run --rm --no-deps --entrypoint python homegate -c "$ca_check")
|
||||
# Changing a first-run seed must not replace live settings or the established CA.
|
||||
export HOMEGATE_PROXY_IP=192.168.42.99
|
||||
export HOMEGATE_ZONES='[changed.internal]'
|
||||
compose run --rm --no-deps initialize
|
||||
compose restart ui ssh-export
|
||||
compose exec -T ui python - verify < ci/smoke_ui.py
|
||||
ca_after=$(compose run --rm --no-deps --entrypoint python homegate -c "$ca_check")
|
||||
[ "$ca_before" = "$ca_after" ]
|
||||
printf 'Runtime smoke test passed: bootstrap, permissions, secrets, config and CA persistence.\n'
|
||||
61
ci/smoke_ui.py
Normal file
61
ci/smoke_ui.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""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')
|
||||
Reference in New Issue
Block a user