1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00

fix(datasource): cloud-init endpoints work without login, keys need a unique name

The OpenStack-style cloud-init endpoints (index, meta_data.json,
user_data) are fetched by booting VMs, which do not log in. Since the
login-required middleware they redirected to the login page, so the
datasource has not worked. They are login_not_required now; they expose
a hostname and the public SSH keys of a VM's owners.

A VM is found by the reverse DNS name of the client address, and VM
names are not unique (only compute and UUID are). user_data merged the
owners of every VM with that name, so a user who named their VM like
another user's VM got their key into that VM at its next cloud-init
run. Keys are now given only when exactly one VM has the name.

The client address is the last X-Forwarded-For entry, which nginx in
the default setup appends; gunicorn listens on 127.0.0.1 only. The bare
except around the reverse lookup is narrowed to OSError.
This commit is contained in:
catborise 2026-10-04 20:46:15 +03:00
parent 99dabbdcec
commit 99eadaab31
3 changed files with 64 additions and 12 deletions

View file

@ -1,9 +1,13 @@
import json
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import RequestFactory, TestCase
from django.urls import reverse
from accounts.models import UserInstance, UserSSHKey
from computes.models import Compute
from instances.models import Instance
from datasource.views import get_client_ip, get_hostname_by_ip
@ -54,3 +58,42 @@ class DataSourceTestCase(TestCase):
def test_vdi_url_not_found_compute(self):
response = self.client.get(reverse("vdi_url", args=[9999, "vm1"]))
self.assertEqual(response.status_code, 404)
class CloudInitTestCase(TestCase):
"""VMs fetch the cloud-init endpoints without logging in; keys go only to
a VM whose name is unique."""
KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOwner owner@example"
def setUp(self):
self.compute = Compute.objects.create(name="ci-a", hostname="a.example", login="", password="", type=4)
self.owner = get_user_model().objects.create_user("ci_owner", password="pw")
UserSSHKey.objects.create(user=self.owner, keyname="k", keypublic=self.KEY)
self.vm = Instance.objects.create(compute=self.compute, name="web1", uuid="11111111-2222-3333-4444-999999999991")
UserInstance.objects.create(instance=self.vm, user=self.owner)
patcher = patch("datasource.views.get_hostname_by_ip", return_value="web1.lan")
patcher.start()
self.addCleanup(patcher.stop)
def userdata(self):
response = self.client.get(reverse("ds_openstack_userdata", args=["latest"]))
self.assertEqual(response.status_code, 200)
return response.content.decode()
def test_endpoints_need_no_login(self):
for name, args in (("ds_openstack_index", []), ("ds_openstack_metadata", ["latest"])):
self.assertEqual(self.client.get(reverse(name, args=args)).status_code, 200, name)
self.assertEqual(json.loads(self.client.get(reverse("ds_openstack_metadata", args=["latest"])).content)["hostname"], "web1.lan")
def test_owner_keys_for_a_unique_name(self):
self.assertIn(self.KEY, self.userdata())
def test_no_keys_when_the_name_is_taken_twice(self):
other = Compute.objects.create(name="ci-b", hostname="b.example", login="", password="", type=4)
Instance.objects.create(compute=other, name="web1", uuid="11111111-2222-3333-4444-999999999992")
self.assertNotIn("ssh", self.userdata())
def test_no_keys_for_an_unknown_name(self):
self.vm.delete()
self.assertNotIn("ssh", self.userdata())