From 07796953496e0dfa5a3faa8f8daf5e7f6dd132e1 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:45:49 +0100 Subject: [PATCH] fix(vm-provision): pass every SSH key, one per line Proxmox's sshkeys holds one OpenSSH key per line, URL-encoded. The keys were joined with ";", so only the first one worked, and quote() kept "/" unescaped, which Proxmox's urlencoded format refuses. Closes #23 --- CHANGELOG.md | 3 ++ napalm_proxmox/vm_provision_mixin.py | 6 ++-- tests/test_vm_provision_mixin.py | 48 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d37d0..96c89c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `configure_snmp_agent()` completes: its "configure" step reported a failure with empty output although `/etc/snmp/snmpd.conf` was written, so the firewall and start steps never ran (#30). +- `create_vm_from_cloud_init(ssh_public_keys=...)` installs every key, one per + line, instead of only the first; a key containing `/` or `+` is no longer + refused by Proxmox (#23). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index 9a2d206..0d7577d 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -736,9 +736,9 @@ class ProxmoxVMProvisionMixin: cloud_init_args[f"ipconfig{i}"] = "ip=dhcp" if ssh_public_keys: - # URL-encode SSH keys for Proxmox API - sshkeys = ";".join(ssh_public_keys) - cloud_init_args["sshkeys"] = quote(sshkeys) + # One key per line, URL-encoded without exception: Proxmox's + # "urlencoded" format refuses an unescaped "/" or "+". + cloud_init_args["sshkeys"] = quote("\n".join(ssh_public_keys), safe="") self._node_api().qemu(vmid).config.post(**cloud_init_args) diff --git a/tests/test_vm_provision_mixin.py b/tests/test_vm_provision_mixin.py index d3d9f0d..06fbe36 100644 --- a/tests/test_vm_provision_mixin.py +++ b/tests/test_vm_provision_mixin.py @@ -1280,3 +1280,51 @@ def test_create_vm_cloudinit_drive_uses_image_storage_not_snippet_storage(): cloud_init_call = next(c for c in mock_vm.config.post.call_args_list if "ide2" in c[1]) assert cloud_init_call[1]["ide2"] == "local-zfs:cloudinit" assert cloud_init_call[1]["cicustom"].startswith("user=local:snippets/") + + +def test_create_vm_passes_every_ssh_key_one_per_line(): + """Proxmox's sshkeys is one key per line, URL-encoded (napalm-proxmox#23). + + Joined with ";", only the first key worked. Base64 keys carry "/" and "+", + which Proxmox's urlencoded format refuses unescaped. + """ + import re + from urllib.parse import unquote + + mixin = ProxmoxVMProvisionMixin() + mixin._node_name = "pve1" + mock_api = MagicMock() + mock_api.cluster.nextid.get.return_value = 110 + mock_api.storage.return_value.get.return_value = {"path": "/var/lib/vz"} + mock_node = MagicMock() + mock_node.storage.get.return_value = [ + {"storage": "local", "type": "dir", "content": "images,snippets", "enabled": 1}, + ] + mixin._api = mock_api + mixin._node_api = MagicMock(return_value=mock_node) + mixin._download_cloud_image = MagicMock(return_value="/var/lib/vz/x.qcow2") + mixin._run_node_command = MagicMock(return_value="") + mock_vm = mock_node.qemu.return_value + mock_vm.config.get.return_value = {"unused0": "local:vm-110-disk-0"} + mock_vm.status.start.post.return_value = "UPID:pve1:136:start" + mock_node.tasks.return_value.status.get.return_value = {"status": "stopped", "exitstatus": "OK"} + keys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI/+k1 alice@example", + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB/+k2 bob@example", + ] + + with patch("time.sleep"): + mixin.create_vm_from_cloud_init( + name="keys-vm", + image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2", + cpu=1, + memory=512, + nics=[{"bridge": "vmbr0"}], + cloud_init_config={}, + ssh_public_keys=keys, + ) + + call = next(c for c in mock_vm.config.post.call_args_list if "sshkeys" in c[1]) + sshkeys = call[1]["sshkeys"] + assert unquote(sshkeys) == "\n".join(keys) + assert re.fullmatch(r"[-%a-zA-Z0-9_.!~*'()]*", sshkeys)