1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-10 12:16:49 +00:00

fix(datasource): no SSH keys for an address without a reverse DNS entry

- A booting VM is named by its address's reverse DNS entry. Without one,
  the address itself was taken as the name, so its first number ("10",
  "192") named the VM: a user who made a VM with that name had their keys
  given to every such VM. Such an address now gets no keys; its metadata
  hostname is still the address.
- Keys are written as quoted YAML strings, so a comment with an apostrophe
  or ": " no longer breaks the key or the file.
This commit is contained in:
catborise 2026-10-26 15:49:52 +03:00
parent 5b24f9a6f8
commit 52d00e818b
3 changed files with 34 additions and 17 deletions

View file

@ -1,4 +1,6 @@
import json
import yaml
from unittest.mock import patch
from django.contrib.auth import get_user_model
@ -51,9 +53,8 @@ class DataSourceTestCase(TestCase):
request.META["HTTP_X_FORWARDED_FOR"] = "10.0.0.1, 192.168.1.50"
self.assertEqual(get_client_ip(request), "192.168.1.50")
def test_get_hostname_by_ip_fallback(self):
# Invalid IP will cause gethostbyaddr to fail and return the IP string itself
self.assertEqual(get_hostname_by_ip("256.256.256.256"), "256.256.256.256")
def test_get_hostname_by_ip_without_an_entry(self):
self.assertIsNone(get_hostname_by_ip("256.256.256.256"))
def test_vdi_url_not_found_compute(self):
response = self.client.get(reverse("vdi_url", args=[9999, "vm1"]))
@ -73,7 +74,7 @@ class CloudInitTestCase(TestCase):
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.reverse_dns = patcher.start()
self.addCleanup(patcher.stop)
def userdata(self):
@ -97,3 +98,16 @@ class CloudInitTestCase(TestCase):
def test_no_keys_for_an_unknown_name(self):
self.vm.delete()
self.assertNotIn("ssh", self.userdata())
def test_no_keys_for_an_address_without_a_reverse_dns_entry(self):
# the address's first number is no VM's name: a VM named "10" gets nothing
self.reverse_dns.return_value = None
Instance.objects.create(compute=self.compute, name="127", uuid="11111111-2222-3333-4444-999999999993")
self.assertNotIn("ssh", self.userdata())
metadata = json.loads(self.client.get(reverse("ds_openstack_metadata", args=["latest"])).content)
self.assertEqual(metadata["hostname"], "127.0.0.1")
def test_a_key_comment_stays_one_string(self):
key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOwner it's: mine"
UserSSHKey.objects.filter(user=self.owner).update(keypublic=key)
self.assertEqual(yaml.safe_load(self.userdata())["ssh_authorized_keys"], [key])