"""Production database-mode registration and authorization HTTP boundary.""" from __future__ import annotations import asyncio import ipaddress import json import sys import time import types from pathlib import Path import pytest from fastapi.testclient import TestClient from starlette.websockets import WebSocketDisconnect try: import libvirt # noqa: F401 except ModuleNotFoundError: stub = types.ModuleType("libvirt") stub.libvirtError = type("libvirtError", (Exception,), {}) sys.modules["libvirt"] = stub from tests.test_database_http import MemoryStore # noqa: E402 from virsh_panel import app as app_module # noqa: E402 from virsh_panel.auth import hash_password # noqa: E402 from virsh_panel.config import Settings # noqa: E402 from virsh_panel.database import DatabasePanelState, DatabaseTemplateStore # noqa: E402 from virsh_panel.libvirt_service import VMError # noqa: E402 from virsh_panel.metrics import host_scope # noqa: E402 from virsh_panel.cloud_templates import template_scope # noqa: E402 from virsh_panel.ssh_keys import SSHKeyError, _name, _public_key # noqa: E402 ORIGIN = "https://panel.example.test" PASSWORD = "correct-horse-battery-staple" CREATE_VM = { "name": "tenant-vm", "vcpu": 2, "memory_mib": 2048, "disk_gib": 20, "network": "bridge:br0", "iso": "AlmaLinux-10.iso", } GUEST_PUBLIC_KEY = ( "ssh-ed25519 " "AAAAC3NzaC1lZDI1NTE5AAAAIF7mFh4aYz3lRzNqjoFQasAQLpm7BdSULhe1iWuvj3y5 " "panel-test" ) CLOUD_VM = { "source": "cloud", "template_id": "alma10-template", "name": "tenant-cloud", "vcpu": 2, "memory_mib": 2048, "disk_gib": 20, "network": "bridge:br0", "public_ip_cidr": "8.8.8.18/24", "gateway": "8.8.8.1", "dns_servers": ["1.1.1.1"], } class _MemorySSHKeys: """Mimic the owner-scoped SQL boundary for production-mode HTTP tests.""" def __init__(self): self.rows = {} self.next_id = 1 def list_for_user(self, user_id): return [row.copy() for owner, row in self.rows.values() if owner == user_id] def create_for_user(self, user_id, name, public_key): name = _name(name) public_key, fingerprint = _public_key(public_key) if any(owner == user_id and row["fingerprint"] == fingerprint for owner, row in self.rows.values()): raise SSHKeyError("此 SSH 公钥已添加到当前账户", 409) key_id = self.next_id self.next_id += 1 row = {"id": key_id, "name": name, "public_key": public_key, "fingerprint": fingerprint, "created_at": 100} self.rows[key_id] = (user_id, row) return row.copy() def get_for_user(self, user_id, key_id): found = self.rows.get(key_id) return found[1].copy() if found is not None and found[0] == user_id else None def delete_for_user(self, user_id, key_id): if self.get_for_user(user_id, key_id) is None: return False del self.rows[key_id] return True class _Manager: profile = { "host": "vmhost.example.test", "port": 22, "fingerprint": "SHA256:configured-host", "key_locked": False, "region": "东京", "storage_pool": "/home/kvm", "iso_pool": "/home/iso", "default_network": "bridge:br0", "ssh_target": "vp-managed", "ssh_config_file": "/fake/config", "ssh_known_hosts_file": "/fake/known_hosts", } def current(self): return self.profile def close(self): pass class _Service: def __init__(self): self.vms = {} self.console_calls = [] self.cloud_specs = [] self.reinstall_specs = [] def create_vm(self, spec): vm = { "uuid": spec["vm_uuid"], "name": spec["name"], "state": "running", "vcpu": spec["vcpu"], "memory_mib": spec["memory_mib"], "disks": [], "autostart": True, } self.vms[vm["uuid"]] = vm return vm def create_cloud_vm(self, spec, volume_name): self.cloud_specs.append((spec.copy(), volume_name)) vm = self.create_vm(spec) vm.update({ "managed": True, "os_source": "cloud_template", "public_ip_cidr": spec["public_ip_cidr"], "credential_generation": f"panel-seed-{vm['uuid']}.iso", }) return vm def reinstall_cloud_vm(self, vm_id, volume_name, *, os_name=None, ssh_public_key="", root_password_hash=None, progress=None): vm = self.get_vm(vm_id) self.reinstall_specs.append((vm_id, volume_name, ssh_public_key, root_password_hash, os_name)) if progress is not None: progress("completed") generation = f"panel-seed-reinstall-{vm_id}.iso" vm["credential_generation"] = generation return {"vm": vm, "credential_generation": generation} def resources(self): return {"networks": ["bridge:br0"], "isos": [], "storage_pool": "/home/kvm"} def list_vms(self): return list(self.vms.values()) def get_vm(self, vm_id): if vm_id not in self.vms: raise VMError("虚拟机不存在", 404) return self.vms[vm_id] def action(self, vm_id, _action): return self.get_vm(vm_id) def delete_vm(self, vm_id, _delete_disks): self.get_vm(vm_id) del self.vms[vm_id] return {"uuid": vm_id, "deleted": True, "deleted_disks": []} def delete_firewall_filter(self, _vm_id): return {"deleted": True, "already_absent": True} def console_port(self, vm_id): self.console_calls.append(vm_id) self.get_vm(vm_id) return 5907 class _NoMetrics: def __init__(self, *_args, **_kwargs): pass def start(self): pass def close(self): pass def _login(client: TestClient, email: str): response = client.post("/api/v1/login", json={ "email": email, "password": PASSWORD, }) assert response.status_code == 200 return {"X-CSRF-Token": response.json()["csrf_token"]} def _register(client: TestClient, address: str, username: str): code = client.post("/api/v1/register/code", json={"email": address}) assert code.status_code == 200 and code.json() == {"sent": True} response = client.post("/api/v1/register", json={ "username": username, "email": address, "password": PASSWORD, "code": "123456", }) assert response.status_code == 201 assert response.json() == {"registered": True, "approved": False} def test_template_rename_is_admin_only_and_ordinary_users_can_list(monkeypatch, tmp_path): store = MemoryStore() service = _Service() monkeypatch.setattr(app_module, "make_components", lambda directory, *_: ( DatabasePanelState(directory, store, store.users), _Manager(), DatabaseTemplateStore(store), store, )) monkeypatch.setattr(app_module, "MetricsCollector", _NoMetrics) settings = Settings( data_dir=tmp_path / "data", static_dir=Path(__file__).resolve().parents[1] / "static", ) app = app_module.create_app(settings, service_factory=lambda _profile: service) token = (settings.data_dir / "setup-token").read_text(encoding="ascii").strip() scope = template_scope(_Manager.profile) template_id = "alma9-12345678-abcdef0123456789" record = { "id": template_id, "name": "AlmaLinux 9", "os_family": "AlmaLinux", "os_version": "9", "sha256": "f" * 64, "volume_name": "panel-template-alma9-12345678-abcdef0123456789.qcow2", } endpoint = f"/api/v1/vm-templates/{template_id}" with TestClient(app, base_url=ORIGIN) as admin: assert admin.patch(endpoint, json={"name": "Rename"}).status_code == 401 configured = admin.post("/api/v1/database/setup", json={ "host": "127.0.0.1", "port": 3306, "database": "virsh_panel", "username": "panel", "password": "database-password", "setup_token": token, }) assert configured.status_code == 200 created = admin.post("/api/v1/setup/admin", json={ "token": token, "username": "initial", "password": PASSWORD, }) assert created.status_code == 200 admin_headers = {"X-CSRF-Token": created.json()["csrf_token"]} templates = DatabaseTemplateStore(store) templates.add(scope, record) templates.add(scope, {**record, "id": "another", "name": "Other template"}) templates.add("b" * 64, {**record, "name": "Different host"}) assert admin.patch(endpoint, json={"name": "Rename"}, headers=admin_headers).status_code == 403 store.users.promote("initial") store.set("test_user", "tenant", json.dumps({ "id": 2, "username": "tenant", "email": "tenant@example.test", "password_hash": hash_password(PASSWORD), "admin": False, "approved": True, "email_verified": True, "created_at": 100, })) with TestClient(app, base_url=ORIGIN) as tenant: tenant_headers = _login(tenant, "tenant@example.test") listed = tenant.get("/api/v1/vm-templates") assert listed.status_code == 200 assert [row["id"] for row in listed.json()["templates"]] == [ template_id, "another", ] assert tenant.patch(endpoint, json={"name": "Hijack"}, headers=tenant_headers).status_code == 403 assert tenant.post("/api/v1/vm-templates/import", json={ "name": "Extra", "url": "https://invalid.example/image.qcow2", "sha256": "a" * 64, }, headers=tenant_headers).status_code == 403 assert admin.patch(endpoint, json={"name": "Rename"}).status_code == 403 assert admin.patch(endpoint, json={"name": "Other TEMPLATE"}, headers=admin_headers).status_code == 409 assert admin.patch(endpoint, json={"name": " "}, headers=admin_headers).status_code == 400 missing = admin.patch("/api/v1/vm-templates/missing", json={"name": "Missing"}, headers=admin_headers) assert missing.status_code == 404 renamed = admin.patch(endpoint, json={"name": " AlmaLinux 9 Gold "}, headers=admin_headers) assert renamed.status_code == 200 assert renamed.json()["template"] == {**record, "name": "AlmaLinux 9 Gold"} assert DatabaseTemplateStore(store).get(scope, template_id) == renamed.json()["template"] assert templates.get("b" * 64, template_id)["name"] == "Different host" def test_registration_smtp_approval_and_per_user_vm_isolation(monkeypatch, tmp_path): store = MemoryStore() service = _Service() sent = [] monkeypatch.setattr(app_module, "make_components", lambda directory, *_: ( DatabasePanelState(directory, store, store.users), _Manager(), DatabaseTemplateStore(store), store, )) monkeypatch.setattr(app_module, "MetricsCollector", _NoMetrics) monkeypatch.setattr( app_module, "send_verification_code", lambda config, email, code: sent.append((config.host, email, code)), ) settings = Settings( data_dir=tmp_path / "data", static_dir=Path(__file__).resolve().parents[1] / "static", ) app = app_module.create_app(settings, service_factory=lambda _profile: service) token = (settings.data_dir / "setup-token").read_text(encoding="ascii").strip() with TestClient(app, base_url=ORIGIN) as admin: configured = admin.post("/api/v1/database/setup", json={ "host": "127.0.0.1", "port": 3306, "database": "virsh_panel", "username": "panel", "password": "database-password", "setup_token": token, }) assert configured.status_code == 200 initial = admin.post("/api/v1/setup/admin", json={ "token": token, "username": "firstuser", "password": PASSWORD, }) assert initial.status_code == 200 admin_headers = {"X-CSRF-Token": initial.json()["csrf_token"]} assert admin.get("/api/v1/session").json()["admin"] is False assert admin.get("/api/v1/smtp/settings").status_code == 403 # The operator promotes the initial account and adds its email in MariaDB. store.users.promote("firstuser") assert admin.get("/api/v1/session").json()["admin"] is True initial_account = store.users.get_by_username("firstuser") assert initial_account["email"] is None with TestClient(app, base_url=ORIGIN) as missing_email: assert missing_email.post("/api/v1/login", json={ "email": "first@example.test", "password": PASSWORD, }).status_code == 401 store.set("test_user", "firstuser", json.dumps({ **initial_account, "email": "first@example.test", })) with TestClient(app, base_url=ORIGIN) as fresh_admin: assert fresh_admin.post("/api/v1/login", json={ "username": "firstuser", "password": PASSWORD, }).status_code == 422 assert fresh_admin.post("/api/v1/login", json={ "email": "firstuser", "password": PASSWORD, }).status_code == 401 assert _login(fresh_admin, "FIRST@EXAMPLE.TEST") assert fresh_admin.get("/api/v1/session").json()["admin"] is True smtp = admin.put("/api/v1/smtp/settings", json={ "host": "smtp.example.test", "port": 587, "tls_mode": "starttls", "from_address": "panel@example.test", "username": "mailer", "password": "smtp-secret-password", }, headers=admin_headers) assert smtp.status_code == 200 assert "smtp-secret-password" not in smtp.text original_sender = app_module.send_verification_code def fail_delivery(_config, _email, _code): raise app_module.SMTPError("模拟 SMTP 发送失败", 502) monkeypatch.setattr(app_module, "send_verification_code", fail_delivery) with TestClient(app, base_url=ORIGIN) as visitor: failed_send = visitor.post("/api/v1/register/code", json={ "email": "delivery-fail@example.test", }) assert failed_send.status_code == 200 assert failed_send.json() == {"sent": True} # A failed background delivery must remove the challenge so the # user is not trapped behind the resend cooldown. assert store.get("test_code", "delivery-fail@example.test") is None monkeypatch.setattr(app_module, "send_verification_code", original_sender) with TestClient(app, base_url=ORIGIN) as visitor: retried = visitor.post("/api/v1/register/code", json={ "email": "delivery-fail@example.test", }) assert retried.status_code == failed_send.status_code assert retried.json() == failed_send.json() assert store.get("test_code", "delivery-fail@example.test") == "123456" sent.clear() with TestClient(app, base_url=ORIGIN) as first: _register(first, "one@example.test", "tenantone") assert sent == [("smtp.example.test", "one@example.test", "123456")] first_headers = _login(first, "ONE@EXAMPLE.TEST") assert first.get("/api/v1/session").json()["approved"] is False assert first.post("/api/v1/vms", json=CREATE_VM, headers=first_headers).status_code == 403 for protected in ( "/api/v1/host", "/api/v1/update/settings", "/api/v1/smtp/settings", "/api/v1/users/pending", ): assert first.get(protected).status_code == 403 pending = admin.get("/api/v1/users/pending") assert pending.status_code == 200 items = pending.json()["items"] assert len(items) == 1 and items[0]["username"] == "tenantone" assert "password_hash" not in items[0] approve = admin.post(f"/api/v1/users/{items[0]['id']}/approve", headers=admin_headers) assert approve.status_code == 200 assert first.get("/api/v1/session").json()["approved"] is True own = first.post("/api/v1/vms", json=CREATE_VM, headers=first_headers) assert own.status_code == 201 own_id = own.json()["uuid"] with TestClient(app, base_url=ORIGIN) as second: _register(second, "two@example.test", "tenanttwo") second_headers = _login(second, "two@example.test") second_id = store.users.get_by_username("tenanttwo")["id"] assert admin.post(f"/api/v1/users/{second_id}/approve", headers=admin_headers).status_code == 200 other = second.post("/api/v1/vms", json={**CREATE_VM, "name": "other-vm"}, headers=second_headers) assert other.status_code == 201 other_id = other.json()["uuid"] assert [vm["uuid"] for vm in first.get("/api/v1/vms").json()["items"]] == [own_id] assert [vm["uuid"] for vm in second.get("/api/v1/vms").json()["items"]] == [other_id] assert len(admin.get("/api/v1/vms").json()["items"]) == 2 assert first.get(f"/api/v1/vms/{other_id}").status_code == 404 assert first.post(f"/api/v1/vms/{other_id}/credential", headers=first_headers).status_code == 404 assert first.get(f"/api/v1/vms/{other_id}/metrics").status_code == 404 assert first.post(f"/api/v1/vms/{other_id}/actions", json={ "action": "reboot", }, headers=first_headers).status_code == 404 assert first.delete(f"/api/v1/vms/{other_id}", headers=first_headers).status_code == 404 assert first.get(f"/api/v1/vms/{own_id}").status_code == 200 assert first.get("/api/v1/host").status_code == 403 assert first.put("/api/v1/update/settings", json={ "repository_url": "https://g.luoli.live/root/virt.git", "branch": "main", "access_token": "forbidden", }, headers=first_headers).status_code == 403 assert first.put("/api/v1/smtp/settings", json={ "host": "evil.example.test", "port": 587, "tls_mode": "starttls", "from_address": "evil@example.test", }, headers=first_headers).status_code == 403 assert first.post(f"/api/v1/users/{second_id}/approve", headers=first_headers).status_code == 403 cookie = first.cookies.get("virsh_panel_session") with pytest.raises(WebSocketDisconnect) as denied: with first.websocket_connect( f"wss://panel.example.test/api/v1/vms/{other_id}/console", headers={ "host": "panel.example.test", "origin": ORIGIN, "cookie": f"virsh_panel_session={cookie}", }, ): pass assert denied.value.code == 4404 assert service.console_calls == [] monkeypatch.setattr(app_module, "CONSOLE_ACCESS_CHECK_SECONDS", 0.05) async def held_console(_websocket, **_kwargs): # If the watchdog fails to close promptly, the relay # closes with 1011 after a bounded fallback delay. await asyncio.sleep(1) raise app_module.ConsoleRelayError("test relay timeout") monkeypatch.setattr(app_module, "relay_console", held_console) own_console = f"wss://panel.example.test/api/v1/vms/{own_id}/console" own_cookie = first.cookies.get("virsh_panel_session") with first.websocket_connect(own_console, headers={ "host": "panel.example.test", "origin": ORIGIN, "cookie": f"virsh_panel_session={own_cookie}", }) as console: assert first.post("/api/v1/logout", headers=first_headers).status_code == 200 with pytest.raises(WebSocketDisconnect) as revoked: console.receive_text() assert revoked.value.code == 4403 first_headers = _login(first, "one@example.test") own_cookie = first.cookies.get("virsh_panel_session") with first.websocket_connect(own_console, headers={ "host": "panel.example.test", "origin": ORIGIN, "cookie": f"virsh_panel_session={own_cookie}", }) as console: store.users.delete_vm_owner(host_scope(_Manager.profile), own_id) with pytest.raises(WebSocketDisconnect) as ownership_revoked: console.receive_text() assert ownership_revoked.value.code == 4403 # A deleted username may later be reused. Its old bearer # cookie must not acquire the replacement account's role. old_account = store.users.get_by_username("tenantone") store.delete("test_user", "tenantone") replacement = {**old_account, "id": old_account["id"] + 1000, "admin": True} store.set("test_user", "tenantone", json.dumps(replacement)) assert first.get("/api/v1/session").json()["authenticated"] is False assert first.get("/api/v1/smtp/settings").status_code == 401 assert first.post("/api/v1/vms", json=CREATE_VM, headers=first_headers).status_code == 401 def test_per_user_ssh_key_library_and_cloud_vm_selection(monkeypatch, tmp_path): store = MemoryStore() key_store = _MemorySSHKeys() service = _Service() def components(directory, *_args): state = DatabasePanelState(directory, store, store.users) state.ssh_keys = key_store return state, _Manager(), DatabaseTemplateStore(store), store monkeypatch.setattr(app_module, "make_components", components) monkeypatch.setattr(app_module, "MetricsCollector", _NoMetrics) settings = Settings( data_dir=tmp_path / "data", static_dir=Path(__file__).resolve().parents[1] / "static", ) app = app_module.create_app(settings, service_factory=lambda _profile: service) token = (settings.data_dir / "setup-token").read_text(encoding="ascii").strip() with TestClient(app, base_url=ORIGIN) as admin: assert admin.get("/api/v1/ssh-keys").status_code == 401 assert admin.post("/api/v1/ssh-keys", json={ "name": "initial", "public_key": GUEST_PUBLIC_KEY, }).status_code == 401 assert admin.delete("/api/v1/ssh-keys/1").status_code == 401 assert admin.post("/api/v1/database/setup", json={ "host": "127.0.0.1", "port": 3306, "database": "virsh_panel", "username": "panel", "password": "database-password", "setup_token": token, }).status_code == 200 initial = admin.post("/api/v1/setup/admin", json={ "token": token, "username": "firstuser", "password": PASSWORD, }) assert initial.status_code == 200 admin_headers = {"X-CSRF-Token": initial.json()["csrf_token"]} store.users.promote("firstuser") DatabaseTemplateStore(store).add(template_scope(_Manager.profile), { "id": "alma10-template", "name": "AlmaLinux 10", "volume_name": "panel-template-alma10.qcow2", }) # Even an administrator has a personal key library, not every # tenant's keys. No private-key content is accepted by the API. assert admin.get("/api/v1/ssh-keys").json() == {"items": []} rejected_private = admin.post("/api/v1/ssh-keys", json={ "name": "wrong", "public_key": "-----BEGIN OPENSSH PRIVATE KEY-----\nsecret", }, headers=admin_headers) assert rejected_private.status_code in (400, 422) store.users.issue_registration_code("one@example.test", "123456") first_account = store.users.register_with_code( "tenantone", "one@example.test", PASSWORD, "123456", ) store.users.issue_registration_code("two@example.test", "123456") second_account = store.users.register_with_code( "tenanttwo", "two@example.test", PASSWORD, "123456", ) with TestClient(app, base_url=ORIGIN) as first, \ TestClient(app, base_url=ORIGIN) as second: first_headers = _login(first, "one@example.test") second_headers = _login(second, "two@example.test") assert first.get("/api/v1/session").json()["approved"] is False assert first.get("/api/v1/ssh-keys").json() == {"items": []} assert first.post("/api/v1/ssh-keys", json={ "name": "laptop", "public_key": GUEST_PUBLIC_KEY, }).status_code == 403 imported = first.post("/api/v1/ssh-keys", json={ "name": "laptop", "public_key": GUEST_PUBLIC_KEY, }, headers=first_headers) assert imported.status_code == 201 first_key = imported.json() assert first_key["name"] == "laptop" assert first_key["public_key"].startswith("ssh-ed25519 ") assert first_key["fingerprint"].startswith("SHA256:") assert isinstance(first_key["id"], int) assert first.post("/api/v1/ssh-keys", json={ "name": "duplicate comment", "public_key": GUEST_PUBLIC_KEY, }, headers=first_headers).status_code == 409 assert first.get("/api/v1/ssh-keys").json()["items"] == [first_key] assert second.get("/api/v1/ssh-keys").json() == {"items": []} assert admin.get("/api/v1/ssh-keys").json() == {"items": []} assert first.post("/api/v1/vms", json={ **CLOUD_VM, "guest_ssh_key_id": first_key["id"], }, headers=first_headers).status_code == 403 # The same public key is allowed in another account, but its # record has a different ID and remains independently owned. second_import = second.post("/api/v1/ssh-keys", json={ "name": "other laptop", "public_key": GUEST_PUBLIC_KEY, }, headers=second_headers) assert second_import.status_code == 201 second_key = second_import.json() assert second_key["id"] != first_key["id"] assert second.get("/api/v1/ssh-keys").json()["items"] == [second_key] assert second.delete(f"/api/v1/ssh-keys/{first_key['id']}", headers=second_headers).status_code == 404 assert first.delete("/api/v1/ssh-keys/-1", headers=first_headers).status_code == 404 assert admin.delete(f"/api/v1/ssh-keys/{first_key['id']}", headers=admin_headers).status_code == 404 assert first.get("/api/v1/ssh-keys").json()["items"] == [first_key] assert admin.post(f"/api/v1/users/{first_account['id']}/approve", headers=admin_headers).status_code == 200 assert admin.post(f"/api/v1/users/{second_account['id']}/approve", headers=admin_headers).status_code == 200 before = len(service.cloud_specs) for client, headers, forbidden_id in ( (first, first_headers, second_key["id"]), (second, second_headers, first_key["id"]), (admin, admin_headers, first_key["id"]), ): forbidden = client.post("/api/v1/vms", json={ **CLOUD_VM, "guest_ssh_key_id": forbidden_id, }, headers=headers) assert forbidden.status_code == 404 missing = first.post("/api/v1/vms", json={ **CLOUD_VM, "guest_ssh_key_id": 999999, }, headers=first_headers) assert missing.status_code == 404 for conflicting in ( {"guest_ssh_key_id": first_key["id"], "guest_ssh_public_key": GUEST_PUBLIC_KEY}, ): response = first.post("/api/v1/vms", json={ **CLOUD_VM, **conflicting, }, headers=first_headers) assert response.status_code == 422 assert len(service.cloud_specs) == before selected = first.post("/api/v1/vms", json={ **CLOUD_VM, "guest_ssh_key_id": first_key["id"], "root_password": "A-strong-guest-password-2026", }, headers=first_headers) assert selected.status_code == 201 spec, _volume = service.cloud_specs[-1] assert spec["guest_ssh_public_key"] == first_key["public_key"] assert "guest_ssh_key_id" not in spec assert spec["root_password_hash"].startswith("$6$") assert selected.json()["auth_mode"] == "ssh_key_password" assert selected.json()["password_available"] assert first.post( f"/api/v1/vms/{selected.json()['uuid']}/credential", headers=first_headers, ).json()["password"] == "A-strong-guest-password-2026" assert second.get(f"/api/v1/vms/{selected.json()['uuid']}").status_code == 404 key_with_generated_password = first.post("/api/v1/vms", json={ **CLOUD_VM, "name": "saved-key-auto-password", "public_ip_cidr": "8.8.8.21/24", "guest_ssh_key_id": first_key["id"], "root_password": "", }, headers=first_headers) assert key_with_generated_password.status_code == 201 key_spec, _key_volume = service.cloud_specs[-1] assert key_spec["guest_ssh_public_key"] == first_key["public_key"] assert key_spec["root_password_hash"].startswith("$6$") assert key_with_generated_password.json()["auth_mode"] == "ssh_key_password" assert key_with_generated_password.json()["password_available"] key_credential = first.post( f"/api/v1/vms/{key_with_generated_password.json()['uuid']}/credential", headers=first_headers, ).json() assert key_credential["auth_mode"] == "ssh_key_password" assert len(key_credential["password"]) == 32 assert any(c.isupper() for c in key_credential["password"]) assert any(c.islower() for c in key_credential["password"]) assert any(c.isdigit() for c in key_credential["password"]) assert any(not c.isalnum() and not c.isspace() for c in key_credential["password"]) assert set(key_credential["password"]) <= set( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789!@#$%*-_=+?" ) from passlib.hash import sha512_crypt assert sha512_crypt.verify( key_credential["password"], key_spec["root_password_hash"], ) assert key_credential["password"] not in key_with_generated_password.text assert key_credential["password"] not in repr(service.cloud_specs) assert second.get( f"/api/v1/vms/{key_with_generated_password.json()['uuid']}" ).status_code == 404 reinstall_url = f"/api/v1/vms/{selected.json()['uuid']}/reinstall" reinstall_body = {"template_id": "alma10-template", "confirmation_name": "tenant-cloud"} for client, headers, key_id in ( (first, first_headers, second_key["id"]), (admin, admin_headers, first_key["id"]), ): assert client.post(reinstall_url, json={ **reinstall_body, "ssh_key_id": key_id, }, headers=headers).status_code == 404 assert second.post(reinstall_url, json={ **reinstall_body, "ssh_key_id": second_key["id"], }, headers=second_headers).status_code == 404 assert first.post(reinstall_url, json={ **reinstall_body, "ssh_key_id": first_key["id"], "ssh_public_key": GUEST_PUBLIC_KEY, }, headers=first_headers).status_code == 422 assert service.reinstall_specs == [] queued = first.post(reinstall_url, json={ **reinstall_body, "ssh_key_id": first_key["id"], "root_password": "A-strong-guest-password-2026", }, headers=first_headers) assert queued.status_code == 202 for _ in range(100): job = first.get(f"/api/v1/reinstall-jobs/{queued.json()['job_id']}", headers=first_headers) assert job.status_code == 200 if job.json()["status"] != "queued" and job.json()["status"] != "running": break time.sleep(0.01) assert job.json()["status"] == "succeeded" assert service.reinstall_specs[-1][2] == first_key["public_key"] assert service.reinstall_specs[-1][3].startswith("$6$") assert "generated_password" not in job.json() assert first.post( f"/api/v1/vms/{selected.json()['uuid']}/credential", headers=first_headers, ).json()["password"] == "A-strong-guest-password-2026" # Existing create paths continue to accept a one-off pasted key # or an initial root password without a saved-key ID. manual = first.post("/api/v1/vms", json={ **CLOUD_VM, "name": "manual-key-vm", "public_ip_cidr": "8.8.8.19/24", "guest_ssh_public_key": GUEST_PUBLIC_KEY, }, headers=first_headers) assert manual.status_code == 201 assert service.cloud_specs[-1][0]["guest_ssh_public_key"] == GUEST_PUBLIC_KEY manual_claim = f"{host_scope(_Manager.profile)}.8.8.8.19" assert store.get("ip_claim", manual_claim) is not None duplicate_manual = first.post("/api/v1/vms", json={ **CLOUD_VM, "name": "duplicate-manual-ip", "public_ip_cidr": "8.8.8.19/24", "guest_ssh_public_key": GUEST_PUBLIC_KEY, }, headers=first_headers) assert duplicate_manual.status_code == 409 assert first.delete(f"/api/v1/vms/{manual.json()['uuid']}", headers=first_headers).status_code == 200 assert store.get("ip_claim", manual_claim) is None password = first.post("/api/v1/vms", json={ **CLOUD_VM, "name": "password-vm", "public_ip_cidr": "8.8.8.20/24", "root_password": "A-strong-guest-password-2026", }, headers=first_headers) assert password.status_code == 201 assert password.json()["auth_mode"] == "password" assert first.delete(f"/api/v1/ssh-keys/{first_key['id']}").status_code == 403 deleted = first.delete(f"/api/v1/ssh-keys/{first_key['id']}", headers=first_headers) assert 200 <= deleted.status_code < 300 assert first.get("/api/v1/ssh-keys").json() == {"items": []} assert second.get("/api/v1/ssh-keys").json()["items"] == [second_key] assert first.post("/api/v1/vms", json={ **CLOUD_VM, "name": "deleted-key-vm", "guest_ssh_key_id": first_key["id"], }, headers=first_headers).status_code == 404 assert first.post(reinstall_url, json={ **reinstall_body, "ssh_key_id": first_key["id"], }, headers=first_headers).status_code == 404 assert first.get(f"/api/v1/vms/{selected.json()['uuid']}").status_code == 200 def test_host_public_pool_allocates_globally_and_preserves_tenant_isolation( monkeypatch, tmp_path, ): store = MemoryStore() service = _Service() manager = _Manager() manager.profile = { **_Manager.profile, "host": "8.8.8.3", "public_pool_cidr": "8.8.8.0/24", "public_gateway": "8.8.8.1", "public_dns_servers": ["1.1.1.1", "8.8.4.4"], "public_reserved_ips": ["8.8.8.2", "8.8.8.5"], } # An existing, panel-visible libvirt guest already occupies one address. service.vms["00000000-0000-4000-8000-000000000004"] = { "uuid": "00000000-0000-4000-8000-000000000004", "name": "existing-vm", "state": "running", "vcpu": 1, "memory_mib": 1024, "disks": [], "autostart": True, "public_ip_cidr": "8.8.8.4/24", } monkeypatch.setattr(app_module, "make_components", lambda directory, *_: ( DatabasePanelState(directory, store, store.users), manager, DatabaseTemplateStore(store), store, )) monkeypatch.setattr(app_module, "MetricsCollector", _NoMetrics) settings = Settings( data_dir=tmp_path / "data", static_dir=Path(__file__).resolve().parents[1] / "static", ) app = app_module.create_app(settings, service_factory=lambda _profile: service) token = (settings.data_dir / "setup-token").read_text(encoding="ascii").strip() automatic_body = { key: value for key, value in CLOUD_VM.items() if key not in {"public_ip_cidr", "gateway", "dns_servers"} } automatic_body["guest_ssh_public_key"] = GUEST_PUBLIC_KEY network = ipaddress.ip_network(manager.profile["public_pool_cidr"]) excluded = {"8.8.8.0", "8.8.8.1", "8.8.8.2", "8.8.8.3", "8.8.8.4", "8.8.8.5", "8.8.8.6", "8.8.8.255"} with TestClient(app, base_url=ORIGIN) as admin: assert admin.post("/api/v1/database/setup", json={ "host": "127.0.0.1", "port": 3306, "database": "virsh_panel", "username": "panel", "password": "database-password", "setup_token": token, }).status_code == 200 initial = admin.post("/api/v1/setup/admin", json={ "token": token, "username": "operator", "password": PASSWORD, }) assert initial.status_code == 200 admin_headers = {"X-CSRF-Token": initial.json()["csrf_token"]} store.users.promote("operator") DatabaseTemplateStore(store).add(template_scope(manager.profile), { "id": "alma10-template", "name": "AlmaLinux 10", "volume_name": "panel-template-alma10.qcow2", }) # A claim from another panel worker remains occupied even when its VM # is not yet visible through libvirt's list API. pool_scope = host_scope(manager.profile) assert store.insert("ip_claim", f"{pool_scope}.8.8.8.6", json.dumps({ "vm_uuid": "00000000-0000-4000-8000-000000000006", })) for username in ("tenantone", "tenanttwo"): email = f"{username}@example.test" store.users.issue_registration_code(email, "123456") account = store.users.register_with_code(username, email, PASSWORD, "123456") assert admin.post(f"/api/v1/users/{account['id']}/approve", headers=admin_headers).status_code == 200 with TestClient(app, base_url=ORIGIN) as first, \ TestClient(app, base_url=ORIGIN) as second: first_headers = _login(first, "tenantone@example.test") second_headers = _login(second, "tenanttwo@example.test") resources = first.get("/api/v1/resources") assert resources.status_code == 200 assert resources.json()["public_pool_enabled"] is True assert resources.json()["public_pool_cidr"] == "8.8.8.0/24" assert first.get("/api/v1/host").status_code == 403 # Explicit client-side networking cannot bypass the host pool. for field, value in ( ("public_ip_cidr", "8.8.8.99/24"), ("gateway", "8.8.8.1"), ("dns_servers", ["1.1.1.1"]), ): rejected = first.post("/api/v1/vms", json={ **automatic_body, field: value, }, headers=first_headers) assert rejected.status_code == 422 assert service.cloud_specs == [] first_created = first.post("/api/v1/vms", json=automatic_body, headers=first_headers) second_created = second.post("/api/v1/vms", json={ **automatic_body, "name": "tenant-two-cloud", }, headers=second_headers) assert first_created.status_code == second_created.status_code == 201 first_ip = service.cloud_specs[0][0]["public_ip_cidr"] second_ip = service.cloud_specs[1][0]["public_ip_cidr"] for ip_cidr in (first_ip, second_ip): interface = ipaddress.ip_interface(ip_cidr) assert interface.network == network assert str(interface.ip) not in excluded assert first_ip != second_ip for spec, _volume in service.cloud_specs: assert spec["gateway"] == "8.8.8.1" assert spec["dns_servers"] == ["1.1.1.1", "8.8.4.4"] first_id = first_created.json()["uuid"] second_id = second_created.json()["uuid"] assert first.get(f"/api/v1/vms/{second_id}").status_code == 404 assert second.get(f"/api/v1/vms/{first_id}").status_code == 404 # Every usable address is occupied in libvirt metadata: no IP may # be reused even after the allocator has exhausted the /24 pool. for address in network.hosts(): ip = str(address) if ip not in excluded and ip not in {first_ip.split("/")[0], second_ip.split("/")[0]}: service.vms[f"used-{ip}"] = {"uuid": f"used-{ip}", "public_ip_cidr": f"{ip}/24"} exhausted = first.post("/api/v1/vms", json={ **automatic_body, "name": "no-ip-left", }, headers=first_headers) assert exhausted.status_code == 409 assert len(service.cloud_specs) == 2 first_claim = f"{pool_scope}.{first_ip.split('/')[0]}" assert store.get("ip_claim", first_claim) is not None deleted = first.delete(f"/api/v1/vms/{first_id}", headers=first_headers) assert deleted.status_code == 200 assert store.get("ip_claim", first_claim) is None replacement = first.post("/api/v1/vms", json={ **automatic_body, "name": "freed-ip-reused", }, headers=first_headers) assert replacement.status_code == 201 assert service.cloud_specs[-1][0]["public_ip_cidr"] == first_ip # With exactly one free address, a clearly rolled-back libvirt # failure must release the temporary claim for the next attempt. del service.vms["used-8.8.8.250"] free_claim = f"{pool_scope}.8.8.8.250" original_create = service.create_cloud_vm def rolled_back(_spec, _volume): raise VMError("模拟创建失败,回滚已完成", 502) monkeypatch.setattr(service, "create_cloud_vm", rolled_back) failed = first.post("/api/v1/vms", json={ **automatic_body, "name": "rolled-back-vm", }, headers=first_headers) assert failed.status_code == 502 assert store.get("ip_claim", free_claim) is None # If libvirt reports incomplete rollback, conservatively retain # the address claim even though this fake exposes no resulting VM. def incomplete_rollback(_spec, _volume): raise VMError("模拟回滚不完整,请人工检查", 502) monkeypatch.setattr(service, "create_cloud_vm", incomplete_rollback) uncertain = first.post("/api/v1/vms", json={ **automatic_body, "name": "uncertain-vm", }, headers=first_headers) assert uncertain.status_code == 502 assert store.get("ip_claim", free_claim) is not None monkeypatch.setattr(service, "create_cloud_vm", original_create) still_exhausted = first.post("/api/v1/vms", json={ **automatic_body, "name": "uncertain-ip-protected", }, headers=first_headers) assert still_exhausted.status_code == 409 @pytest.mark.parametrize(("vm_prefix", "first_ip"), [ (None, "123.123.123.114/28"), (24, "123.123.123.114/24"), ]) def test_provider_28_pool_allocates_only_inside_assigned_block( monkeypatch, tmp_path, vm_prefix, first_ip, ): store = MemoryStore() service = _Service() manager = _Manager() manager.profile = { **_Manager.profile, "host": "123.123.123.113", "public_pool_cidr": "123.123.123.112/28", "public_gateway": "123.123.123.113", "public_dns_servers": ["1.1.1.1"], "public_reserved_ips": ["123.123.123.115"], "public_vm_prefix": vm_prefix, } monkeypatch.setattr(app_module, "make_components", lambda directory, *_: ( DatabasePanelState(directory, store, store.users), manager, DatabaseTemplateStore(store), store, )) monkeypatch.setattr(app_module, "MetricsCollector", _NoMetrics) settings = Settings( data_dir=tmp_path / "data", static_dir=Path(__file__).resolve().parents[1] / "static", ) app = app_module.create_app(settings, service_factory=lambda _profile: service) token = (settings.data_dir / "setup-token").read_text(encoding="ascii").strip() automatic_body = { key: value for key, value in CLOUD_VM.items() if key not in {"public_ip_cidr", "gateway", "dns_servers"} } automatic_body["guest_ssh_public_key"] = GUEST_PUBLIC_KEY with TestClient(app, base_url=ORIGIN) as admin: assert admin.post("/api/v1/database/setup", json={ "host": "127.0.0.1", "port": 3306, "database": "virsh_panel", "username": "panel", "password": "database-password", "setup_token": token, }).status_code == 200 initial = admin.post("/api/v1/setup/admin", json={ "token": token, "username": "operator", "password": PASSWORD, }) assert initial.status_code == 200 headers = {"X-CSRF-Token": initial.json()["csrf_token"]} store.users.promote("operator") DatabaseTemplateStore(store).add(template_scope(manager.profile), { "id": "alma10-template", "name": "AlmaLinux 10", "volume_name": "panel-template-alma10.qcow2", }) created = admin.post("/api/v1/vms", json=automatic_body, headers=headers) assert created.status_code == 201 assert service.cloud_specs[0][0]["public_ip_cidr"] == first_ip assert service.cloud_specs[0][0]["gateway"] == "123.123.123.113" # Exhaust the small provider allocation; never spill into the /24. for address in ipaddress.ip_network("123.123.123.112/28").hosts(): ip = str(address) if ip not in {"123.123.123.113", "123.123.123.114", "123.123.123.115"}: service.vms[f"used-{ip}"] = {"uuid": f"used-{ip}", "public_ip_cidr": f"{ip}/{vm_prefix or 28}"} exhausted = admin.post("/api/v1/vms", json={ **automatic_body, "name": "no-provider-ip-left", }, headers=headers) assert exhausted.status_code == 409 assert len(service.cloud_specs) == 1