fix(instances): assign the owner on create and inject the owner's SSH keys
Newly created VMs/LXCs ended up with owner_id = NULL, and a VM created by an admin on behalf of a user received the admin's SSH key instead of the user's. Ownership: save_vm_instance() only set owner_id when inserting a new row. The row practically always existed by then — sync_vm_cache runs every 10s and registers VMs it sees on Proxmox with owner_id = NULL while the clone is still running, whereas the deploy saves the owner minutes later at 95% progress. So every deploy landed in the update branch and silently dropped the owner. Set owner_id there too, but only when it is passed explicitly: callers such as save-before-delete do not know the owner and must not blank it out. SSH keys: the profile key was taken from whoever pressed "Create" rather than from the instance owner, and the LXC path injected no profile key at all. Add build_deploy_ssh_keys(), which merges pasted keys, keys picked from the library (same authorization rules as before) and the owner's profile key, deduplicated. Both the template and LXC deploy endpoints now use it, so the key material is resolved in one place instead of inside the deploy worker. ISO VMs are left alone on purpose: they are blank VMs without cloud-init, so there is nowhere to inject keys. Covered by backend/tests/test_instance_ownership.py, including the sync race.
This commit is contained in:
@@ -465,6 +465,15 @@ def save_vm_instance(
|
||||
existing.template_name = template_name
|
||||
existing.description = description
|
||||
existing.extra_config = extra_config
|
||||
# Владельца проставляем только когда он передан явно: часть вызовов
|
||||
# (например, сохранение конфига перед удалением) владельца не знает,
|
||||
# и None не должен обезличивать инстанс.
|
||||
# Без этой строки владелец, выбранный при создании, терялся: фоновый
|
||||
# sync_vm_cache идёт каждые 10с и заводит запись с owner_id=NULL ещё
|
||||
# во время клонирования, поэтому деплой почти всегда попадает сюда,
|
||||
# а не в ветку создания новой записи.
|
||||
if owner_id is not None:
|
||||
existing.owner_id = owner_id
|
||||
existing.updated_at = func.now()
|
||||
db.commit()
|
||||
db.refresh(existing)
|
||||
@@ -496,7 +505,8 @@ def save_vm_instance(
|
||||
deleted_existing.template_name = template_name
|
||||
deleted_existing.description = description
|
||||
deleted_existing.extra_config = extra_config
|
||||
deleted_existing.owner_id = owner_id
|
||||
if owner_id is not None:
|
||||
deleted_existing.owner_id = owner_id
|
||||
deleted_existing.status = 'unknown'
|
||||
deleted_existing.deleted_at = None # Восстанавливаем запись
|
||||
deleted_existing.updated_at = func.now()
|
||||
|
||||
@@ -463,11 +463,10 @@ async def deploy_lxc_from_template(
|
||||
from ...services.quota_service import check_quota
|
||||
check_quota(db, instance_owner_id, add_cores=req.cores, add_memory_mb=req.memory, add_disk_gb=req.disk)
|
||||
|
||||
if req.ssh_key_ids:
|
||||
from ..ssh_keys import resolve_ssh_keys_for_deploy
|
||||
resolved = resolve_ssh_keys_for_deploy(db, current_user, req.ssh_key_ids, instance_owner_id)
|
||||
existing = (req.ssh_keys or "").strip()
|
||||
req.ssh_keys = (existing + "\n" + resolved).strip() if existing else resolved
|
||||
from ..ssh_keys import build_deploy_ssh_keys
|
||||
req.ssh_keys = build_deploy_ssh_keys(
|
||||
db, current_user, instance_owner_id, req.ssh_key_ids, req.ssh_keys,
|
||||
) or None
|
||||
|
||||
# Create task record
|
||||
task = DeployTask(
|
||||
|
||||
Reference in New Issue
Block a user