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
This commit is contained in:
Christian Manivong committed 2026-11-01 21:45:49 +01:00
1 parent 52d63eb689
commit 0779695349
3 files changed
+54 -3

No files matched your search

+3
View File
@@ -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 - `configure_snmp_agent()` completes: its "configure" step reported a failure
with empty output although `/etc/snmp/snmpd.conf` was written, so the firewall with empty output although `/etc/snmp/snmpd.conf` was written, so the firewall
and start steps never ran (#30). 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 ## [0.8.0] - 2026-10-23
+3 -3
View File
@@ -736,9 +736,9 @@ class ProxmoxVMProvisionMixin:
cloud_init_args[f"ipconfig{i}"] = "ip=dhcp" cloud_init_args[f"ipconfig{i}"] = "ip=dhcp"
if ssh_public_keys: if ssh_public_keys:
# URL-encode SSH keys for Proxmox API # One key per line, URL-encoded without exception: Proxmox's
sshkeys = ";".join(ssh_public_keys) # "urlencoded" format refuses an unescaped "/" or "+".
cloud_init_args["sshkeys"] = quote(sshkeys) cloud_init_args["sshkeys"] = quote("\n".join(ssh_public_keys), safe="")
self._node_api().qemu(vmid).config.post(**cloud_init_args) self._node_api().qemu(vmid).config.post(**cloud_init_args)
+48
View File
@@ -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]) 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]["ide2"] == "local-zfs:cloudinit"
assert cloud_init_call[1]["cicustom"].startswith("user=local:snippets/") 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)